Skip to content

Commit

Permalink
Merge branch 'main' into signInWithRedirect-error
Browse files Browse the repository at this point in the history
  • Loading branch information
israx committed Nov 30, 2023
2 parents 52e8fdd + 3340588 commit 5f7cb9b
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 148 deletions.
60 changes: 28 additions & 32 deletions packages/aws-amplify/__tests__/initSingleton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,41 +91,37 @@ describe('initSingleton (DefaultAmplify)', () => {

Amplify.configure(mockLegacyConfig);

expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
{
Auth: {
Cognito: {
allowGuestAccess: true,
identityPoolId: 'aws_cognito_identity_pool_id',
loginWith: {
email: false,
phone: false,
username: true,
},
mfa: {
smsEnabled: true,
status: 'off',
totpEnabled: false,
},
passwordFormat: {
minLength: 8,
requireLowercase: false,
requireNumbers: false,
requireSpecialCharacters: false,
requireUppercase: false,
},
userAttributes: [
{
phone_number: {
required: true,
},
},
],
userPoolClientId: 'aws_user_pools_web_client_id',
userPoolId: 'aws_user_pools_id',
const resourcesConfig: ResourcesConfig = {
Auth: {
Cognito: {
allowGuestAccess: true,
identityPoolId: 'aws_cognito_identity_pool_id',
loginWith: {
email: false,
phone: false,
username: true,
},
mfa: {
smsEnabled: true,
status: 'off',
totpEnabled: false,
},
passwordFormat: {
minLength: 8,
requireLowercase: false,
requireNumbers: false,
requireSpecialCharacters: false,
requireUppercase: false,
},
userAttributes: { phone_number: { required: true } },
userPoolClientId: 'aws_user_pools_web_client_id',
userPoolId: 'aws_user_pools_id',
},
},
};

expect(mockAmplifySingletonConfigure).toHaveBeenCalledWith(
resourcesConfig,
expect.anything()
);
});
Expand Down
142 changes: 56 additions & 86 deletions packages/core/__tests__/parseAWSExports.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { parseAWSExports } from '../src/parseAWSExports';
import { ResourcesConfig } from '../src/singleton/types';

// TODO: Add API category tests
describe('Parser', () => {
describe('parseAWSExports', () => {
const appId = 'app-id';
const bucket = 'bucket';
const identityPoolId = 'identity-pool-id';
Expand Down Expand Up @@ -75,62 +76,8 @@ describe('Parser', () => {
const oAuthResponseType = 'code';

it('should parse valid aws-exports.js', () => {
expect(
parseAWSExports({
aws_cognito_identity_pool_id: identityPoolId,
aws_cognito_sign_up_verification_method: signUpVerificationMethod,
aws_cognito_username_attributes: ['PHONE_NUMBER'],
aws_cognito_signup_attributes: ['PHONE_NUMBER'],
aws_cognito_mfa_configuration: 'OFF',
aws_cognito_mfa_types: ['SMS', 'TOTP'],
aws_cognito_password_protection_settings: {
passwordPolicyMinLength: 8,
passwordPolicyCharacters: [
'REQUIRES_SYMBOLS',
'REQUIRES_UPPERCASE',
'REQUIRES_NUMBERS',
],
},
oauth: {
domain: oAuthDomain,
scope: oAuthScopes,
redirectSignIn: oAuthSigninUrl,
redirectSignOut: oAuthSignoutUrl,
responseType: oAuthResponseType,
},
aws_cognito_verification_mechanisms: ['EMAIL'],
aws_cognito_social_providers: ['GOOGLE', 'APPLE', 'FACEBOOK', 'AMAZON'],
aws_mandatory_sign_in: 'enable',
aws_mobile_analytics_app_id: appId,
aws_mobile_analytics_app_region: region,
aws_user_files_s3_bucket: bucket,
aws_user_files_s3_bucket_region: region,
aws_user_pools_id: userPoolId,
aws_user_pools_web_client_id: userPoolClientId,
geo: {
amazon_location_service: amazonLocationService,
},
aws_cloud_logic_custom: [restEndpoint1, restEndpoint2],
aws_appsync_graphqlEndpoint: appsyncEndpoint,
aws_appsync_apiKey: apiKey,
aws_appsync_region: region,
aws_appsync_authenticationType: 'AMAZON_COGNITO_USER_POOLS',
Notifications: {
InAppMessaging: {
AWSPinpoint: {
appId: appId,
region: region,
},
},
},
})
).toStrictEqual({
Analytics: {
Pinpoint: {
appId,
region,
},
},
const expected: ResourcesConfig = {
Analytics: { Pinpoint: { appId, region } },
Auth: {
Cognito: {
identityPoolId,
Expand All @@ -148,11 +95,7 @@ describe('Parser', () => {
phone: true,
username: false,
},
mfa: {
smsEnabled: true,
status: 'off',
totpEnabled: true,
},
mfa: { smsEnabled: true, status: 'off', totpEnabled: true },
passwordFormat: {
minLength: 8,
requireLowercase: false,
Expand All @@ -161,18 +104,10 @@ describe('Parser', () => {
requireUppercase: true,
},
signUpVerificationMethod,
userAttributes: [
{
email: {
required: true,
},
},
{
phone_number: {
required: true,
},
},
],
userAttributes: {
email: { required: true },
phone_number: { required: true },
},
userPoolId,
userPoolClientId,
},
Expand All @@ -189,10 +124,7 @@ describe('Parser', () => {
},
API: {
REST: {
api1: {
endpoint: 'https://api1.com',
region: 'us-east-1',
},
api1: { endpoint: 'https://api1.com', region: 'us-east-1' },
api2: {
endpoint: 'https://api2.com',
region: 'us-west-2',
Expand All @@ -207,14 +139,52 @@ describe('Parser', () => {
},
},
Notifications: {
InAppMessaging: {
Pinpoint: {
appId,
region,
},
},
InAppMessaging: { Pinpoint: { appId, region } },
},
});
};
expect(
parseAWSExports({
aws_cognito_identity_pool_id: identityPoolId,
aws_cognito_sign_up_verification_method: signUpVerificationMethod,
aws_cognito_username_attributes: ['PHONE_NUMBER'],
aws_cognito_signup_attributes: ['PHONE_NUMBER'],
aws_cognito_mfa_configuration: 'OFF',
aws_cognito_mfa_types: ['SMS', 'TOTP'],
aws_cognito_password_protection_settings: {
passwordPolicyMinLength: 8,
passwordPolicyCharacters: [
'REQUIRES_SYMBOLS',
'REQUIRES_UPPERCASE',
'REQUIRES_NUMBERS',
],
},
oauth: {
domain: oAuthDomain,
scope: oAuthScopes,
redirectSignIn: oAuthSigninUrl,
redirectSignOut: oAuthSignoutUrl,
responseType: oAuthResponseType,
},
aws_cognito_verification_mechanisms: ['EMAIL'],
aws_cognito_social_providers: ['GOOGLE', 'APPLE', 'FACEBOOK', 'AMAZON'],
aws_mandatory_sign_in: 'enable',
aws_mobile_analytics_app_id: appId,
aws_mobile_analytics_app_region: region,
aws_user_files_s3_bucket: bucket,
aws_user_files_s3_bucket_region: region,
aws_user_pools_id: userPoolId,
aws_user_pools_web_client_id: userPoolClientId,
geo: { amazon_location_service: amazonLocationService },
aws_cloud_logic_custom: [restEndpoint1, restEndpoint2],
aws_appsync_graphqlEndpoint: appsyncEndpoint,
aws_appsync_apiKey: apiKey,
aws_appsync_region: region,
aws_appsync_authenticationType: 'AMAZON_COGNITO_USER_POOLS',
Notifications: {
InAppMessaging: { AWSPinpoint: { appId, region } },
},
})
).toStrictEqual(expected);
});

it('should fallback to IAM auth mode if Appsync auth type is invalid', () => {
Expand Down Expand Up @@ -267,7 +237,7 @@ describe('Parser', () => {
mfa: undefined,
passwordFormat: undefined,
signUpVerificationMethod: undefined,
userAttributes: [],
userAttributes: {},
userPoolClientId: undefined,
userPoolId: userPoolId,
},
Expand Down
36 changes: 14 additions & 22 deletions packages/core/__tests__/singleton/Singleton.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AuthClass as Auth } from '../../src/singleton/Auth';
import { decodeJWT } from '../../src/singleton/Auth/utils';
import { CredentialsAndIdentityId } from '../../src/singleton/Auth/types';
import { TextEncoder, TextDecoder } from 'util';
import { fetchAuthSession } from '../../src';
import { fetchAuthSession, ResourcesConfig } from '../../src';
Object.assign(global, { TextDecoder, TextEncoder });
type ArgumentTypes<F extends Function> = F extends (...args: infer A) => any
? A
Expand All @@ -16,7 +16,12 @@ const MOCK_AUTH_CONFIG = {
},
},
};
const modelIntrospection = {

type ModelIntrospection = NonNullable<
NonNullable<ResourcesConfig['API']>['GraphQL']
>['modelIntrospection'];

const modelIntrospection: ModelIntrospection = {
version: 1,
models: {
Todo: {
Expand Down Expand Up @@ -107,38 +112,23 @@ describe('Amplify.configure() and Amplify.getConfig()', () => {

Amplify.configure(mockLegacyConfig);
const result = Amplify.getConfig();

expect(result).toEqual({
const expected: ResourcesConfig = {
Auth: {
Cognito: {
allowGuestAccess: true,
identityPoolId: 'aws_cognito_identity_pool_id',
userPoolClientId: 'aws_user_pools_web_client_id',
userPoolId: 'aws_user_pools_id',
loginWith: {
email: false,
phone: false,
username: true,
},
mfa: {
smsEnabled: true,
status: 'off',
totpEnabled: false,
},
loginWith: { email: false, phone: false, username: true },
mfa: { smsEnabled: true, status: 'off', totpEnabled: false },
passwordFormat: {
minLength: 8,
requireLowercase: false,
requireNumbers: false,
requireSpecialCharacters: false,
requireUppercase: false,
},
userAttributes: [
{
phone_number: {
required: true,
},
},
],
userAttributes: { phone_number: { required: true } },
},
},
API: {
Expand All @@ -150,7 +140,9 @@ describe('Amplify.configure() and Amplify.getConfig()', () => {
modelIntrospection,
},
},
});
};

expect(result).toEqual(expected);
});

it('should take the v6 shaped config object for configuring and return it from getConfig()', () => {
Expand Down
22 changes: 14 additions & 8 deletions packages/core/src/parseAWSExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// SPDX-License-Identifier: Apache-2.0
import { ConsoleLogger } from './Logger';
import {
OAuthConfig,
AuthConfigUserAttributes,
LegacyUserAttributeKey,
OAuthConfig,
OAuthProvider,
} from './singleton/Auth/types';
import { ResourcesConfig } from './singleton/types';
Expand Down Expand Up @@ -161,17 +162,22 @@ export const parseAWSExports = (
) ?? false,
}
: undefined;
const mergedUserAttributes = Array.from(
const mergedUserAttributes: LegacyUserAttributeKey[] = Array.from(
new Set([
...(aws_cognito_verification_mechanisms ?? []),
...(aws_cognito_signup_attributes ?? []),
])
);
const userAttributesConfig = mergedUserAttributes.map((s: string) => ({
[s.toLowerCase()]: {
required: true, // All user attributes generated by the CLI will be required
},
})) as unknown as AuthConfigUserAttributes;

const userAttributes: AuthConfigUserAttributes = mergedUserAttributes.reduce(
(attributes: AuthConfigUserAttributes, key: LegacyUserAttributeKey) => ({
...attributes,
// All user attributes generated by the CLI are required
[key.toLowerCase()]: { required: true },
}),
{}
);

const loginWithEmailEnabled =
aws_cognito_username_attributes?.includes('EMAIL') ?? false;
const loginWithPhoneEnabled =
Expand All @@ -182,7 +188,7 @@ export const parseAWSExports = (
identityPoolId: aws_cognito_identity_pool_id,
allowGuestAccess: aws_mandatory_sign_in !== 'enable',
signUpVerificationMethod: aws_cognito_sign_up_verification_method,
userAttributes: userAttributesConfig,
userAttributes,
userPoolClientId: aws_user_pools_web_client_id,
userPoolId: aws_user_pools_id,
mfa: mfaConfig,
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/singleton/Auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ export type AuthStandardAttributeKey =
| 'zoneinfo'
| AuthVerifiableAttributeKey;

// legacy config user attribute keys are uppercase
export type LegacyUserAttributeKey = Uppercase<AuthStandardAttributeKey>;

export type AuthVerifiableAttributeKey = 'email' | 'phone_number';

export type AuthConfigUserAttributes = Partial<
Expand Down

0 comments on commit 5f7cb9b

Please sign in to comment.