UNPKG

@aws-amplify/auth

Version:
52 lines (50 loc) 2.44 kB
'use strict'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.refreshAuthTokensWithoutDedupe = exports.refreshAuthTokens = void 0; const utils_1 = require("@aws-amplify/core/internals/utils"); const parsers_1 = require("../../../foundation/parsers"); const types_1 = require("../utils/types"); const AuthError_1 = require("../../../errors/AuthError"); const factories_1 = require("../factories"); const cognitoIdentityProvider_1 = require("../../../foundation/factories/serviceClients/cognitoIdentityProvider"); const refreshAuthTokensFunction = async ({ tokens, authConfig, username, }) => { (0, utils_1.assertTokenProviderConfig)(authConfig?.Cognito); const { userPoolId, userPoolClientId, userPoolEndpoint } = authConfig.Cognito; const region = (0, parsers_1.getRegionFromUserPoolId)(userPoolId); (0, types_1.assertAuthTokensWithRefreshToken)(tokens); const getTokensFromRefreshToken = (0, cognitoIdentityProvider_1.createGetTokensFromRefreshTokenClient)({ endpointResolver: (0, factories_1.createCognitoUserPoolEndpointResolver)({ endpointOverride: userPoolEndpoint, }), }); const { AuthenticationResult } = await getTokensFromRefreshToken({ region }, { ClientId: userPoolClientId, RefreshToken: tokens.refreshToken, DeviceKey: tokens.deviceMetadata?.deviceKey, }); const accessToken = (0, utils_1.decodeJWT)(AuthenticationResult?.AccessToken ?? ''); const idToken = AuthenticationResult?.IdToken ? (0, utils_1.decodeJWT)(AuthenticationResult.IdToken) : undefined; const { iat } = accessToken.payload; // This should never happen. If it does, it's a bug from the service. if (!iat) { throw new AuthError_1.AuthError({ name: 'iatNotFoundException', message: 'iat not found in access token', }); } const clockDrift = iat * 1000 - new Date().getTime(); return { accessToken, idToken, clockDrift, refreshToken: AuthenticationResult?.RefreshToken ?? tokens.refreshToken, username, }; }; exports.refreshAuthTokens = (0, utils_1.deDupeAsyncFunction)(refreshAuthTokensFunction); exports.refreshAuthTokensWithoutDedupe = refreshAuthTokensFunction; //# sourceMappingURL=refreshAuthTokens.js.map