UNPKG

@aws-amplify/auth

Version:
60 lines (57 loc) 2.98 kB
import { deDupeAsyncFunction, assertTokenProviderConfig, decodeJWT } from '@aws-amplify/core/internals/utils'; import { getRegionFromUserPoolId } from '../../../foundation/parsers/regionParsers.mjs'; import { assertAuthTokensWithRefreshToken } from './types.mjs'; import { AuthError } from '../../../errors/AuthError.mjs'; import { createCognitoUserPoolEndpointResolver } from '../factories/createCognitoUserPoolEndpointResolver.mjs'; import '@aws-amplify/core'; import '@aws-amplify/core/internals/aws-client-utils/composers'; import '@aws-amplify/core/internals/aws-client-utils'; import '../../../foundation/factories/serviceClients/cognitoIdentityProvider/shared/handler/cognitoUserPoolTransferHandler.mjs'; import '../../../foundation/factories/serviceClients/cognitoIdentityProvider/constants.mjs'; import { createGetTokensFromRefreshTokenClient } from '../../../foundation/factories/serviceClients/cognitoIdentityProvider/createGetTokensFromRefreshTokenClient.mjs'; import '../../../common/AuthErrorStrings.mjs'; import '../../../errors/types/validation.mjs'; import '../types/errors.mjs'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 const refreshAuthTokensFunction = async ({ tokens, authConfig, username, clientMetadata, }) => { assertTokenProviderConfig(authConfig?.Cognito); const { userPoolId, userPoolClientId, userPoolEndpoint } = authConfig.Cognito; const region = getRegionFromUserPoolId(userPoolId); assertAuthTokensWithRefreshToken(tokens); const getTokensFromRefreshToken = createGetTokensFromRefreshTokenClient({ endpointResolver: createCognitoUserPoolEndpointResolver({ endpointOverride: userPoolEndpoint, }), }); const { AuthenticationResult } = await getTokensFromRefreshToken({ region }, { ClientId: userPoolClientId, RefreshToken: tokens.refreshToken, DeviceKey: tokens.deviceMetadata?.deviceKey, ClientMetadata: clientMetadata, }); const accessToken = decodeJWT(AuthenticationResult?.AccessToken ?? ''); const idToken = AuthenticationResult?.IdToken ? 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({ 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, }; }; const refreshAuthTokens = deDupeAsyncFunction(refreshAuthTokensFunction); const refreshAuthTokensWithoutDedupe = refreshAuthTokensFunction; export { refreshAuthTokens, refreshAuthTokensWithoutDedupe }; //# sourceMappingURL=refreshAuthTokens.mjs.map