{"version":3,"file":"graphqlAuth.mjs","sources":["../../../src/internals/graphqlAuth.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { GraphQLApiError } from '../utils/errors';\nimport { NO_API_KEY, NO_AUTH_TOKEN_HEADER, NO_SIGNED_IN_USER, NO_VALID_AUTH_TOKEN, NO_VALID_CREDENTIALS, } from '../utils/errors/constants';\nexport async function headerBasedAuth(amplify, authMode, apiKey, additionalHeaders = {}) {\n    let headers = {};\n    switch (authMode) {\n        case 'apiKey':\n            if (!apiKey) {\n                throw new GraphQLApiError(NO_API_KEY);\n            }\n            headers = {\n                'X-Api-Key': apiKey,\n            };\n            break;\n        case 'iam': {\n            const session = await amplify.Auth.fetchAuthSession();\n            if (session.credentials === undefined) {\n                throw new GraphQLApiError(NO_VALID_CREDENTIALS);\n            }\n            break;\n        }\n        case 'oidc':\n        case 'userPool': {\n            let token;\n            try {\n                token = (await amplify.Auth.fetchAuthSession()).tokens?.accessToken.toString();\n            }\n            catch (e) {\n                // fetchAuthSession failed\n                throw new GraphQLApiError({\n                    ...NO_SIGNED_IN_USER,\n                    underlyingError: e,\n                });\n            }\n            // `fetchAuthSession()` succeeded but didn't return `tokens`.\n            // This may happen when unauthenticated access is enabled and there is\n            // no user signed in.\n            if (!token) {\n                throw new GraphQLApiError(NO_VALID_AUTH_TOKEN);\n            }\n            headers = {\n                Authorization: token,\n            };\n            break;\n        }\n        case 'lambda':\n            if (typeof additionalHeaders === 'object' &&\n                !additionalHeaders.Authorization) {\n                throw new GraphQLApiError(NO_AUTH_TOKEN_HEADER);\n            }\n            headers = {\n                Authorization: additionalHeaders.Authorization,\n            };\n            break;\n        case 'none':\n            break;\n        default:\n            break;\n    }\n    return headers;\n}\n"],"names":[],"mappings":";;;;;AAAA;AACA;AAGO,eAAe,eAAe,CAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,iBAAiB,GAAG,EAAE,EAAE;AACzF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