@aws-amplify/datastore
Version:
AppSyncLocal support for aws-amplify
1 lines • 8.59 kB
Source Map (JSON)
{"version":3,"file":"multiAuthStrategy.mjs","sources":["../../../src/authModeStrategies/multiAuthStrategy.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { fetchAuthSession } from '@aws-amplify/core';\nimport { ModelAttributeAuthAllow, ModelAttributeAuthProvider, } from '../types';\nfunction getProviderFromRule(rule) {\n // private with no provider means userPools\n if (rule.allow === 'private' && !rule.provider) {\n return ModelAttributeAuthProvider.USER_POOLS;\n }\n // public with no provider means apiKey\n if (rule.allow === 'public' && !rule.provider) {\n return ModelAttributeAuthProvider.API_KEY;\n }\n return rule.provider;\n}\nfunction sortAuthRulesWithPriority(rules) {\n const allowSortPriority = [\n ModelAttributeAuthAllow.CUSTOM,\n ModelAttributeAuthAllow.OWNER,\n ModelAttributeAuthAllow.GROUPS,\n ModelAttributeAuthAllow.PRIVATE,\n ModelAttributeAuthAllow.PUBLIC,\n ];\n const providerSortPriority = [\n ModelAttributeAuthProvider.FUNCTION,\n ModelAttributeAuthProvider.USER_POOLS,\n ModelAttributeAuthProvider.OIDC,\n ModelAttributeAuthProvider.IAM,\n ModelAttributeAuthProvider.API_KEY,\n ];\n return [...rules].sort((a, b) => {\n if (a.allow === b.allow) {\n return (providerSortPriority.indexOf(getProviderFromRule(a)) -\n providerSortPriority.indexOf(getProviderFromRule(b)));\n }\n return (allowSortPriority.indexOf(a.allow) - allowSortPriority.indexOf(b.allow));\n });\n}\nfunction getAuthRules({ rules, currentUser, }) {\n // Using Set to ensure uniqueness\n const authModes = new Set();\n rules.forEach(rule => {\n switch (rule.allow) {\n case ModelAttributeAuthAllow.CUSTOM:\n // custom with no provider -> function\n if (!rule.provider ||\n rule.provider === ModelAttributeAuthProvider.FUNCTION) {\n authModes.add('lambda');\n }\n break;\n case ModelAttributeAuthAllow.GROUPS:\n case ModelAttributeAuthAllow.OWNER: {\n // We shouldn't attempt User Pool or OIDC if there isn't an authenticated user\n if (currentUser) {\n if (rule.provider === ModelAttributeAuthProvider.USER_POOLS) {\n authModes.add('userPool');\n }\n else if (rule.provider === ModelAttributeAuthProvider.OIDC) {\n authModes.add('oidc');\n }\n }\n break;\n }\n case ModelAttributeAuthAllow.PRIVATE: {\n // We shouldn't attempt private if there isn't an authenticated user\n if (currentUser) {\n // private with no provider means userPools\n if (!rule.provider ||\n rule.provider === ModelAttributeAuthProvider.USER_POOLS) {\n authModes.add('userPool');\n }\n else if (rule.provider === ModelAttributeAuthProvider.IAM) {\n authModes.add('iam');\n }\n }\n break;\n }\n case ModelAttributeAuthAllow.PUBLIC: {\n if (rule.provider === ModelAttributeAuthProvider.IAM) {\n authModes.add('iam');\n }\n else if (!rule.provider ||\n rule.provider === ModelAttributeAuthProvider.API_KEY) {\n // public with no provider means apiKey\n authModes.add('apiKey');\n }\n break;\n }\n default:\n break;\n }\n });\n return Array.from(authModes);\n}\n/**\n * Returns an array of auth modes to try based on the schema, model, and\n * authenticated user (or lack thereof). Rules are sourced from `getAuthRules`\n * and returned in the order they ought to be attempted.\n *\n * @see sortAuthRulesWithPriority\n * @see getAuthRules\n *\n * @param param0 The `{schema, modelName}` to inspect.\n * @returns A sorted array of auth modes to attempt.\n */\nexport const multiAuthStrategy = () => async ({ schema, modelName }) => {\n let currentUser;\n try {\n const authSession = await fetchAuthSession();\n if (authSession.tokens.accessToken) {\n // the user is authenticated\n currentUser = authSession;\n }\n }\n catch (e) {\n // No current user\n }\n const { attributes } = schema.namespaces.user.models[modelName];\n if (attributes) {\n const authAttribute = attributes.find(attr => attr.type === 'auth');\n if (authAttribute?.properties?.rules) {\n const sortedRules = sortAuthRulesWithPriority(authAttribute.properties.rules);\n return getAuthRules({ currentUser, rules: sortedRules });\n }\n }\n return [];\n};\n"],"names":[],"mappings":";;;AAAA;AACA;AAGA,SAAS,mBAAmB,CAAC,IAAI,EAAE;AACnC;AACA,IAAI,IAAI,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