@aws-amplify/core
Version:
Core category of aws-amplify
56 lines (53 loc) • 2.18 kB
JavaScript
import { assert, AuthConfigurationErrorCode } from './errorHelpers.mjs';
import { base64Decoder } from '../../../utils/convert/base64/base64Decoder.mjs';
import '../../../types/errors.mjs';
import '../../../errors/errorHelpers.mjs';
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
function assertTokenProviderConfig(cognitoConfig) {
let assertionValid = true; // assume valid until otherwise proveed
if (!cognitoConfig) {
assertionValid = false;
}
else {
assertionValid =
!!cognitoConfig.userPoolId && !!cognitoConfig.userPoolClientId;
}
return assert(assertionValid, AuthConfigurationErrorCode.AuthUserPoolException);
}
function assertOAuthConfig(cognitoConfig) {
const validOAuthConfig = !!cognitoConfig?.loginWith?.oauth?.domain &&
!!cognitoConfig?.loginWith?.oauth?.redirectSignOut &&
!!cognitoConfig?.loginWith?.oauth?.redirectSignIn &&
!!cognitoConfig?.loginWith?.oauth?.responseType;
return assert(validOAuthConfig, AuthConfigurationErrorCode.OAuthNotConfigureException);
}
function assertIdentityPoolIdConfig(cognitoConfig) {
const validConfig = !!cognitoConfig?.identityPoolId;
return assert(validConfig, AuthConfigurationErrorCode.InvalidIdentityPoolIdException);
}
function decodeJWT(token) {
const tokenParts = token.split('.');
if (tokenParts.length !== 3) {
throw new Error('Invalid token');
}
try {
const base64WithUrlSafe = tokenParts[1];
const base64 = base64WithUrlSafe.replace(/-/g, '+').replace(/_/g, '/');
const jsonStr = decodeURIComponent(base64Decoder
.convert(base64)
.split('')
.map(char => `%${`00${char.charCodeAt(0).toString(16)}`.slice(-2)}`)
.join(''));
const payload = JSON.parse(jsonStr);
return {
toString: () => token,
payload,
};
}
catch (err) {
throw new Error('Invalid token payload');
}
}
export { assertIdentityPoolIdConfig, assertOAuthConfig, assertTokenProviderConfig, decodeJWT };
//# sourceMappingURL=index.mjs.map