Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: relax config for AmplifyOutputs #13234

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions packages/core/__tests__/amplify_outputs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for this <3

"$schema": "adipisicing cillum",
"version": "1",
"auth": {
"aws_region": "non proident exercitation anim fugiat",
"user_pool_id": "sit velit dolor magna est",
"user_pool_client_id": "voluptate",
"identity_pool_id": "Lorem",
"oauth": {
"identity_providers": ["FACEBOOK", "SIGN_IN_WITH_APPLE", "GOOGLE"],
"domain": "proident dolore do mollit ad",
"scopes": ["incididunt proident"],
"redirect_sign_in_uri": ["Duis", "ipsum velit in dolore"],
"redirect_sign_out_uri": [
"Excepteur pariatur cillum officia",
"incididunt in Ut Excepteur commodo"
],
"response_type": "token"
},
"standard_required_attributes": [
"address",
"locale",
"family_name",
"sub",
"email"
],
"username_attributes": ["phone_number", "email"],
"user_verification_types": ["email", "email"],
"unauthenticated_identities_enabled": true,
"mfa_configuration": "OPTIONAL",
"mfa_methods": ["TOTP", "TOTP", "SMS", "TOTP", "TOTP"]
},
"data": {
"aws_region": "regasd",
"url": "dolore dolor do cillum nulla",
"api_key": "non",
"default_authorization_type": "API_KEY",
"authorization_types": []
},
"geo": {
"aws_region": "tempor",
"search_indices": {
"items": [
"commodo Lorem",
"reprehenderit consequat",
"amet",
"aliquip deserunt",
"ea dolor in proident"
],
"default": "exercitation fugiat ut dolor sed"
},
"geofence_collections": {
"items": [
"fugiat ea irure dolor",
"Ut",
"culpa ut enim exercitation",
"labore",
"ex pariatur est ullamco"
],
"default": "ullamco incididunt aliquip"
}
},
"custom": {
"occaecat_4_": -51806024,
"dolorc": 87599986
},
"notifications": {
"aws_region": "labore nisi ad",
"amazon_pinpoint_app_id": "in dolor veniam reprehenderit",
"channels": ["EMAIL"]
}
}
107 changes: 102 additions & 5 deletions packages/core/__tests__/parseAmplifyOutputs.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,106 @@
import { AmplifyOutputs, parseAmplifyOutputs } from '../src/libraryUtils';

import amplifyOutputs from './amplify_outputs.json';
describe('parseAmplifyOutputs tests', () => {
describe('auth tests', () => {

it('should parse from amplify-outputs.json', async () => {
const result = parseAmplifyOutputs(amplifyOutputs);

expect(result).toEqual({
"API": {
"GraphQL": {
"apiKey": "non",
"defaultAuthMode": "apiKey",
"endpoint": "dolore dolor do cillum nulla",
"modelIntrospection": undefined,
"region": "regasd",
},
},
"Auth": {
"Cognito": {
"allowGuestAccess": true,
"identityPoolId": "Lorem",
"loginWith": {
"email": true,
"oauth": {
"domain": "proident dolore do mollit ad",
"providers": [
"Facebook",
"Apple",
"Google",
],
"redirectSignIn": [
"Duis",
"ipsum velit in dolore",
],
"redirectSignOut": [
"Excepteur pariatur cillum officia",
"incididunt in Ut Excepteur commodo",
],
"responseType": "token",
"scopes": [
"incididunt proident",
],
},
"phone": true,
},
"mfa": {
"smsEnabled": true,
"status": "optional",
"totpEnabled": true,
},
"userAttributes": {
"address": {
"required": true,
},
"email": {
"required": true,
},
"family_name": {
"required": true,
},
"locale": {
"required": true,
},
"sub": {
"required": true,
},
},
"userPoolClientId": "voluptate",
"userPoolId": "sit velit dolor magna est",
},
},
"Geo": {
"LocationService": {
"geofenceCollections": {
"default": "ullamco incididunt aliquip",
"items": [
"fugiat ea irure dolor",
"Ut",
"culpa ut enim exercitation",
"labore",
"ex pariatur est ullamco",
],
},
"maps": undefined,
"region": "tempor",
"searchIndices": {
"default": "exercitation fugiat ut dolor sed",
"items": [
"commodo Lorem",
"reprehenderit consequat",
"amet",
"aliquip deserunt",
"ea dolor in proident",
],
},
},
},
});
});

it('should parse auth happy path (all enabled)', () => {
const amplifyOutputs: AmplifyOutputs = {
const amplifyOutputs = {
'version': '1',
'auth': {
'user_pool_id': 'us-east-1:',
Expand All @@ -26,8 +123,8 @@ describe('parseAmplifyOutputs tests', () => {
'require_numbers': true
},
'standard_required_attributes': ['email'],
'username_attributes': ['EMAIL'],
'user_verification_mechanisms': ['EMAIL'],
'username_attributes': ['email'],
'user_verification_types': ['email'],
'unauthenticated_identities_enabled': true,
'mfa_configuration': 'OPTIONAL',
'mfa_methods': ['SMS']
Expand Down Expand Up @@ -224,7 +321,7 @@ describe('parseAmplifyOutputs tests', () => {
'version': '1',
'notifications': {
aws_region: 'us-west-2',
pinpoint_app_id: 'appid123',
amazon_pinpoint_app_id: 'appid123',
channels: ['APNS', 'EMAIL', 'FCM', 'IN_APP_MESSAGING', 'SMS'],
}
};
Expand Down
37 changes: 11 additions & 26 deletions packages/core/src/parseAmplifyOutputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ import { NotificationsConfig } from './singleton/Notifications/types';
import {
AmplifyOutputs,
AmplifyOutputsAnalyticsProperties,
AmplifyOutputsAuthMFAConfiguration,
AmplifyOutputsAuthProperties,
AmplifyOutputsDataProperties,
AmplifyOutputsGeoProperties,
AmplifyOutputsNotificationsProperties,
AmplifyOutputsOAuthIdentityProvider,
AmplifyOutputsStorageProperties,
AuthType,
} from './singleton/AmplifyOutputs/types';
import {
AnalyticsConfig,
Expand Down Expand Up @@ -130,34 +127,27 @@ function parseAuth(
domain: oauth.domain,
redirectSignIn: oauth.redirect_sign_in_uri,
redirectSignOut: oauth.redirect_sign_out_uri,
responseType: oauth.response_type,
responseType: oauth.response_type === 'token' ? 'token' : 'code',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: What might this be other than 'token'? Just wondering if we can just do

responseType: oauth.response_type ?? 'code',

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to show both options to make it more readable

scopes: oauth.scopes,
providers: getOAuthProviders(oauth.identity_providers),
},
};
}

if (username_attributes?.includes('EMAIL')) {
if (username_attributes?.includes('email')) {
authConfig.Cognito.loginWith = {
...authConfig.Cognito.loginWith,
email: true,
};
}

if (username_attributes?.includes('PHONE_NUMBER')) {
if (username_attributes?.includes('phone_number')) {
authConfig.Cognito.loginWith = {
...authConfig.Cognito.loginWith,
phone: true,
};
}

if (username_attributes?.includes('USERNAME')) {
authConfig.Cognito.loginWith = {
...authConfig.Cognito.loginWith,
username: true,
};
}

if (standard_required_attributes) {
authConfig.Cognito.userAttributes = standard_required_attributes.reduce(
(acc, curr) => ({ ...acc, [curr]: { required: true } }),
Expand Down Expand Up @@ -240,7 +230,7 @@ function parseNotifications(
return undefined;
}

const { aws_region, channels, pinpoint_app_id } =
const { aws_region, channels, amazon_pinpoint_app_id } =
amplifyOutputsNotificationsProperties;

const hasInAppMessaging = channels.includes('IN_APP_MESSAGING');
Expand All @@ -257,7 +247,7 @@ function parseNotifications(
if (hasInAppMessaging) {
notificationsConfig.InAppMessaging = {
Pinpoint: {
appId: pinpoint_app_id,
appId: amazon_pinpoint_app_id,
region: aws_region,
},
};
Expand All @@ -266,7 +256,7 @@ function parseNotifications(
if (hasPushNotification) {
notificationsConfig.PushNotification = {
Pinpoint: {
appId: pinpoint_app_id,
appId: amazon_pinpoint_app_id,
region: aws_region,
},
};
Expand Down Expand Up @@ -309,36 +299,31 @@ export function parseAmplifyOutputs(
return resourcesConfig;
}

const authModeNames: Record<AuthType, GraphQLAuthMode> = {
const authModeNames: Record<string, GraphQLAuthMode> = {
AMAZON_COGNITO_USER_POOLS: 'userPool',
API_KEY: 'apiKey',
AWS_IAM: 'iam',
AWS_LAMBDA: 'lambda',
OPENID_CONNECT: 'oidc',
};

function getGraphQLAuthMode(authType: AuthType): GraphQLAuthMode {
function getGraphQLAuthMode(authType: string): GraphQLAuthMode {
return authModeNames[authType];
}

const providerNames: Record<
AmplifyOutputsOAuthIdentityProvider,
OAuthProvider
> = {
const providerNames: Record<string, OAuthProvider> = {
GOOGLE: 'Google',
LOGIN_WITH_AMAZON: 'Amazon',
FACEBOOK: 'Facebook',
SIGN_IN_WITH_APPLE: 'Apple',
};

function getOAuthProviders(
providers: AmplifyOutputsOAuthIdentityProvider[] = [],
): OAuthProvider[] {
function getOAuthProviders(providers: string[] = []): OAuthProvider[] {
return providers.map(provider => providerNames[provider]);
}

function getMfaStatus(
mfaConfiguration: AmplifyOutputsAuthMFAConfiguration,
mfaConfiguration: string,
): CognitoUserPoolConfigMfaStatus {
if (mfaConfiguration === 'OPTIONAL') return 'optional';
if (mfaConfiguration === 'REQUIRED') return 'on';
Expand Down
Loading
Loading