@ttoss/cloud-auth 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/template.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { CloudFormationTemplate } from '@ttoss/cloudformation';
1
2
  import { PASSWORD_MINIMUM_LENGTH } from './config';
2
3
 
3
4
  const CognitoUserPoolLogicalId = 'CognitoUserPool';
@@ -6,13 +7,55 @@ const CognitoUserPoolClientLogicalId = 'CognitoUserPoolClient';
6
7
 
7
8
  const CognitoIdentityPoolLogicalId = 'CognitoIdentityPool';
8
9
 
9
- export const createAuthTemplate = () => {
10
- const template = {
10
+ type Role =
11
+ | string
12
+ | {
13
+ 'Fn::ImportValue': string;
14
+ };
15
+
16
+ export const createAuthTemplate = ({
17
+ autoVerifiedAttributes = ['email'],
18
+ identityPool = true,
19
+ roles,
20
+ schema,
21
+ }: {
22
+ autoVerifiedAttributes?: Array<'email' | 'phone_number'> | null | false;
23
+ identityPool?: boolean;
24
+ roles?: {
25
+ authenticated?: Role;
26
+ unauthenticated?: Role;
27
+ };
28
+ /**
29
+ * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cognito-userpool-schemaattribute.html
30
+ */
31
+ schema?: {
32
+ AttributeDataType?: 'Boolean' | 'DateTime' | 'Number' | 'String';
33
+ DeveloperOnlyAttribute?: boolean;
34
+ Mutable?: boolean;
35
+ Name?: string;
36
+ NumberAttributeConstraints?: {
37
+ MaxValue?: string;
38
+ MinValue?: string;
39
+ };
40
+ Required?: boolean;
41
+ StringAttributeConstraints?: {
42
+ MaxLength: string;
43
+ MinLength: string;
44
+ };
45
+ }[];
46
+ } = {}) => {
47
+ const AutoVerifiedAttributes =
48
+ Array.isArray(autoVerifiedAttributes) && autoVerifiedAttributes.length > 0
49
+ ? autoVerifiedAttributes
50
+ : undefined;
51
+
52
+ const template: CloudFormationTemplate = {
53
+ AWSTemplateFormatVersion: '2010-09-09',
11
54
  Resources: {
12
55
  [CognitoUserPoolLogicalId]: {
13
56
  Type: 'AWS::Cognito::UserPool',
14
57
  Properties: {
15
- AutoVerifiedAttributes: ['email'],
58
+ AutoVerifiedAttributes,
16
59
  Policies: {
17
60
  PasswordPolicy: {
18
61
  MinimumLength: PASSWORD_MINIMUM_LENGTH,
@@ -23,6 +66,7 @@ export const createAuthTemplate = () => {
23
66
  TemporaryPasswordValidityDays: 30,
24
67
  },
25
68
  },
69
+ Schema: schema,
26
70
  UsernameAttributes: ['email'],
27
71
  UsernameConfiguration: {
28
72
  CaseSensitive: false,
@@ -41,36 +85,6 @@ export const createAuthTemplate = () => {
41
85
  },
42
86
  },
43
87
  },
44
- /**
45
- * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-identitypool.html
46
- */
47
- [CognitoIdentityPoolLogicalId]: {
48
- Type: 'AWS::Cognito::IdentityPool',
49
- Properties: {
50
- AllowUnauthenticatedIdentities: true,
51
- CognitoIdentityProviders: [
52
- {
53
- ClientId: {
54
- Ref: CognitoUserPoolClientLogicalId,
55
- },
56
- ProviderName: {
57
- 'Fn::GetAtt': [CognitoUserPoolLogicalId, 'ProviderName'],
58
- },
59
- },
60
- ],
61
- },
62
- },
63
- /**
64
- * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-identitypoolroleattachment.html
65
- */
66
- // CognitoIdentityPoolRoleAttachment: {
67
- // Type: 'AWS::Cognito::IdentityPoolRoleAttachment',
68
- // Properties: {
69
- // IdentityPoolId: {
70
- // Ref: CognitoIdentityPoolLogicalId,
71
- // },
72
- // },
73
- // },
74
88
  },
75
89
  Outputs: {
76
90
  Region: {
@@ -107,22 +121,69 @@ export const createAuthTemplate = () => {
107
121
  },
108
122
  },
109
123
  },
110
- IdentityPoolId: {
111
- Description: 'You use this value on Amplify Auth `identityPoolId`.',
112
- Value: {
113
- Ref: CognitoIdentityPoolLogicalId,
114
- },
115
- Export: {
116
- Name: {
117
- 'Fn::Join': [
118
- ':',
119
- [{ Ref: 'AWS::StackName' }, 'CognitoIdentityPoolId'],
120
- ],
124
+ },
125
+ };
126
+
127
+ if (identityPool) {
128
+ /**
129
+ * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-identitypool.html
130
+ */
131
+ template.Resources[CognitoIdentityPoolLogicalId] = {
132
+ Type: 'AWS::Cognito::IdentityPool',
133
+ Properties: {
134
+ AllowUnauthenticatedIdentities: true,
135
+ CognitoIdentityProviders: [
136
+ {
137
+ ClientId: {
138
+ Ref: CognitoUserPoolClientLogicalId,
139
+ },
140
+ ProviderName: {
141
+ 'Fn::GetAtt': [CognitoUserPoolLogicalId, 'ProviderName'],
142
+ },
143
+ },
144
+ ],
145
+ },
146
+ };
147
+
148
+ if (roles) {
149
+ /**
150
+ * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-identitypoolroleattachment.html
151
+ */
152
+ template.Resources.CognitoIdentityPoolRoleAttachment = {
153
+ Type: 'AWS::Cognito::IdentityPoolRoleAttachment',
154
+ Properties: {
155
+ IdentityPoolId: {
156
+ Ref: CognitoIdentityPoolLogicalId,
121
157
  },
158
+ Roles: roles,
122
159
  },
160
+ };
161
+ }
162
+
163
+ if (!template.Outputs) {
164
+ template.Outputs = {};
165
+ }
166
+
167
+ template.Outputs.IdentityPoolId = {
168
+ Description: 'You use this value on Amplify Auth `identityPoolId`.',
169
+ Value: {
170
+ Ref: CognitoIdentityPoolLogicalId,
123
171
  },
124
- },
125
- };
172
+ Export: {
173
+ Name: {
174
+ 'Fn::Join': [
175
+ ':',
176
+ [{ Ref: 'AWS::StackName' }, 'CognitoIdentityPoolId'],
177
+ ],
178
+ },
179
+ },
180
+ };
181
+ }
126
182
 
127
183
  return template;
128
184
  };
185
+
186
+ createAuthTemplate.CognitoUserPoolLogicalId = CognitoUserPoolLogicalId;
187
+ createAuthTemplate.CognitoUserPoolClientLogicalId =
188
+ CognitoUserPoolClientLogicalId;
189
+ createAuthTemplate.CognitoIdentityPoolLogicalId = CognitoIdentityPoolLogicalId;
@@ -0,0 +1,73 @@
1
+ import { createAuthTemplate } from '../../src';
2
+
3
+ test('do not add schema if not provided', () => {
4
+ const template = createAuthTemplate();
5
+ expect(template.Resources.CognitoUserPool.Properties.Schema).toBeUndefined();
6
+ });
7
+
8
+ test('add schema if provided', () => {
9
+ const schema = [
10
+ {
11
+ AttributeDataType: 'String' as const,
12
+ DeveloperOnlyAttribute: false,
13
+ Mutable: true,
14
+ Name: 'email',
15
+ Required: true,
16
+ StringAttributeConstraints: {
17
+ MaxLength: '2048',
18
+ MinLength: '0',
19
+ },
20
+ },
21
+ ];
22
+
23
+ const template = createAuthTemplate({ schema });
24
+ expect(template.Resources.CognitoUserPool.Properties.Schema).toEqual(schema);
25
+ });
26
+
27
+ test('should have autoVerifiedAttributes equal email by default', () => {
28
+ const template = createAuthTemplate();
29
+ expect(
30
+ template.Resources.CognitoUserPool.Properties.AutoVerifiedAttributes
31
+ ).toEqual(['email']);
32
+ });
33
+
34
+ test.each([[], null, false])(
35
+ 'should have autoVerifiedAttributes undefined: %p',
36
+ (autoVerifiedAttributes: any) => {
37
+ const template = createAuthTemplate({ autoVerifiedAttributes });
38
+ expect(
39
+ template.Resources.CognitoUserPool.Properties.AutoVerifiedAttributes
40
+ ).toBeUndefined();
41
+ }
42
+ );
43
+
44
+ test.each([true, undefined])(
45
+ 'should have identity pool by default or true: %p',
46
+ (identityPool) => {
47
+ const template = createAuthTemplate({ identityPool });
48
+ expect(template.Resources.CognitoIdentityPool).toBeDefined();
49
+ expect(template.Outputs?.IdentityPoolId).toBeDefined();
50
+ }
51
+ );
52
+
53
+ test('should not have identity pool if false', () => {
54
+ const template = createAuthTemplate({ identityPool: false });
55
+ expect(template.Resources.CognitoIdentityPool).toBeUndefined();
56
+ expect(template.Outputs?.IdentityPoolId).toBeUndefined();
57
+ });
58
+
59
+ test('should have identity pool role attachment with roles', () => {
60
+ const roles = {
61
+ authenticated: 'arn:aws:iam::123456789012:role/authenticated',
62
+ unauthenticated: 'arn:aws:iam::123456789012:role/unauthenticated',
63
+ };
64
+ const template = createAuthTemplate({ roles });
65
+ expect(
66
+ template.Resources.CognitoIdentityPoolRoleAttachment.Properties.Roles
67
+ ).toEqual(roles);
68
+ });
69
+
70
+ test('should not have identity pool role attachment without roles', () => {
71
+ const template = createAuthTemplate();
72
+ expect(template.Resources.CognitoIdentityPoolRoleAttachment).toBeUndefined();
73
+ });