@ttoss/cloud-auth 0.11.0 → 0.12.1

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @ttoss/cloud-auth
2
2
 
3
- It's a library for creating AWS Cognito resources. It creates an user pool, an identity pool and a client application.
3
+ It's a library for creating AWS Cognito resources. It creates an user pool, identity pool, a client application, and others resources.
4
4
 
5
5
  ## Installation
6
6
 
@@ -10,7 +10,7 @@ pnpm add @ttoss/cloud-auth
10
10
 
11
11
  ## Quickstart
12
12
 
13
- Create a `clouformation.ts` file in your project and export the template:
13
+ Create a `cloudformation.ts` file in your project and export the template:
14
14
 
15
15
  ```typescript src/cloudformation.ts
16
16
  import { createAuthTemplate } from '@ttoss/cloud-auth';
@@ -89,3 +89,54 @@ const template = createAuthTemplate({
89
89
  },
90
90
  });
91
91
  ```
92
+
93
+ #### Using attributes for access control
94
+
95
+ When you enable the identity pool, it maps the following [principal tags to handle access control](https://docs.aws.amazon.com/cognito/latest/developerguide/attributes-for-access-control.html) by default:
96
+
97
+ ```yml
98
+ PrincipalTags:
99
+ appClientId: 'aud'
100
+ userId: 'sub'
101
+ ```
102
+
103
+ This way you can use the `appClientId` and `userId` tags in your IAM policies by [controlling access for IAM principals](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_iam-tags.html#access_iam-tags_control-principals). For example:
104
+
105
+ ```json
106
+ {
107
+ "Version": "2012-10-17",
108
+ "Statement": [
109
+ {
110
+ "Effect": "Allow",
111
+ "Action": "s3:GetObject*",
112
+ "Resource": "arn:aws:s3:::*-${aws:PrincipalTag/userId}/*"
113
+ }
114
+ ]
115
+ }
116
+ ```
117
+
118
+ You can change the default tags by passing the `principalTags` property and [other tokens](https://docs.aws.amazon.com/cognito/latest/developerguide/role-based-access-control.html#token-claims-for-role-based-access-control):
119
+
120
+ ```typescript
121
+ const template = createAuthTemplate({
122
+ identityPool: {
123
+ enabled: true,
124
+ principalTags: {
125
+ appId: 'aud',
126
+ username: 'sub',
127
+ name: 'name',
128
+ },
129
+ },
130
+ });
131
+ ```
132
+
133
+ If you want to disable the principal tags, you can pass the `principalTags` property with `false` value:
134
+
135
+ ```typescript
136
+ const template = createAuthTemplate({
137
+ identityPool: {
138
+ enabled: true,
139
+ principalTags: false,
140
+ },
141
+ });
142
+ ```
package/dist/esm/index.js CHANGED
@@ -14,6 +14,10 @@ var DenyStatement = {
14
14
  Action: ["*"],
15
15
  Resource: ["*"]
16
16
  };
17
+ var defaultPrincipalTags = {
18
+ appClientId: "aud",
19
+ userId: "sub"
20
+ };
17
21
  var createAuthTemplate = ({
18
22
  autoVerifiedAttributes = ["email"],
19
23
  identityPool,
@@ -244,6 +248,30 @@ var createAuthTemplate = ({
244
248
  } else {
245
249
  template.Resources.CognitoIdentityPoolRoleAttachment.Properties.Roles.unauthenticated = identityPool.unauthenticatedRoleArn;
246
250
  }
251
+ if (identityPool.principalTags || identityPool.principalTags === void 0) {
252
+ const PrincipalTags = (() => {
253
+ if (typeof identityPool.principalTags === "boolean") {
254
+ return defaultPrincipalTags;
255
+ }
256
+ if (identityPool.principalTags === void 0) {
257
+ return defaultPrincipalTags;
258
+ }
259
+ return identityPool.principalTags;
260
+ })();
261
+ template.Resources.CognitoIdentityPoolPrincipalTag = {
262
+ Type: "AWS::Cognito::IdentityPoolPrincipalTag",
263
+ Properties: {
264
+ IdentityPoolId: {
265
+ Ref: CognitoIdentityPoolLogicalId
266
+ },
267
+ IdentityProviderName: {
268
+ "Fn::GetAtt": [CognitoUserPoolLogicalId, "ProviderName"]
269
+ },
270
+ PrincipalTags,
271
+ UseDefaults: false
272
+ }
273
+ };
274
+ }
247
275
  if (!template.Outputs) {
248
276
  template.Outputs = {};
249
277
  }
package/dist/index.d.mts CHANGED
@@ -13,6 +13,7 @@ declare const createAuthTemplate: {
13
13
  authenticatedPolicies?: Policy[] | undefined;
14
14
  unauthenticatedRoleArn?: string | undefined;
15
15
  unauthenticatedPolicies?: Policy[] | undefined;
16
+ principalTags?: boolean | Record<string, string> | undefined;
16
17
  } | undefined;
17
18
  /**
18
19
  * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cognito-userpool-schemaattribute.html
package/dist/index.d.ts CHANGED
@@ -13,6 +13,7 @@ declare const createAuthTemplate: {
13
13
  authenticatedPolicies?: Policy[] | undefined;
14
14
  unauthenticatedRoleArn?: string | undefined;
15
15
  unauthenticatedPolicies?: Policy[] | undefined;
16
+ principalTags?: boolean | Record<string, string> | undefined;
16
17
  } | undefined;
17
18
  /**
18
19
  * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cognito-userpool-schemaattribute.html
package/dist/index.js CHANGED
@@ -46,6 +46,10 @@ var DenyStatement = {
46
46
  Action: ["*"],
47
47
  Resource: ["*"]
48
48
  };
49
+ var defaultPrincipalTags = {
50
+ appClientId: "aud",
51
+ userId: "sub"
52
+ };
49
53
  var createAuthTemplate = ({
50
54
  autoVerifiedAttributes = ["email"],
51
55
  identityPool,
@@ -276,6 +280,30 @@ var createAuthTemplate = ({
276
280
  } else {
277
281
  template.Resources.CognitoIdentityPoolRoleAttachment.Properties.Roles.unauthenticated = identityPool.unauthenticatedRoleArn;
278
282
  }
283
+ if (identityPool.principalTags || identityPool.principalTags === void 0) {
284
+ const PrincipalTags = (() => {
285
+ if (typeof identityPool.principalTags === "boolean") {
286
+ return defaultPrincipalTags;
287
+ }
288
+ if (identityPool.principalTags === void 0) {
289
+ return defaultPrincipalTags;
290
+ }
291
+ return identityPool.principalTags;
292
+ })();
293
+ template.Resources.CognitoIdentityPoolPrincipalTag = {
294
+ Type: "AWS::Cognito::IdentityPoolPrincipalTag",
295
+ Properties: {
296
+ IdentityPoolId: {
297
+ Ref: CognitoIdentityPoolLogicalId
298
+ },
299
+ IdentityProviderName: {
300
+ "Fn::GetAtt": [CognitoUserPoolLogicalId, "ProviderName"]
301
+ },
302
+ PrincipalTags,
303
+ UseDefaults: false
304
+ }
305
+ };
306
+ }
279
307
  if (!template.Outputs) {
280
308
  template.Outputs = {};
281
309
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/cloud-auth",
3
- "version": "0.11.0",
3
+ "version": "0.12.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/ttoss/ttoss.git",
@@ -15,7 +15,7 @@
15
15
  "sideEffects": false,
16
16
  "typings": "./dist/index.d.ts",
17
17
  "dependencies": {
18
- "@ttoss/cloudformation": "^0.8.7"
18
+ "@ttoss/cloudformation": "^0.9.0"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@types/jest": "^29.5.11",
package/src/template.ts CHANGED
@@ -19,6 +19,11 @@ export const DenyStatement = {
19
19
  Resource: ['*'],
20
20
  };
21
21
 
22
+ export const defaultPrincipalTags = {
23
+ appClientId: 'aud',
24
+ userId: 'sub',
25
+ };
26
+
22
27
  export const createAuthTemplate = ({
23
28
  autoVerifiedAttributes = ['email'],
24
29
  identityPool,
@@ -34,6 +39,7 @@ export const createAuthTemplate = ({
34
39
  authenticatedPolicies?: Policy[];
35
40
  unauthenticatedRoleArn?: string;
36
41
  unauthenticatedPolicies?: Policy[];
42
+ principalTags?: Record<string, string> | boolean;
37
43
  };
38
44
  /**
39
45
  * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cognito-userpool-schemaattribute.html
@@ -310,6 +316,40 @@ export const createAuthTemplate = ({
310
316
  identityPool.unauthenticatedRoleArn;
311
317
  }
312
318
 
319
+ /**
320
+ * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-identitypoolprincipaltag.html
321
+ */
322
+ if (
323
+ identityPool.principalTags ||
324
+ identityPool.principalTags === undefined
325
+ ) {
326
+ const PrincipalTags = (() => {
327
+ if (typeof identityPool.principalTags === 'boolean') {
328
+ return defaultPrincipalTags;
329
+ }
330
+
331
+ if (identityPool.principalTags === undefined) {
332
+ return defaultPrincipalTags;
333
+ }
334
+
335
+ return identityPool.principalTags;
336
+ })();
337
+
338
+ template.Resources.CognitoIdentityPoolPrincipalTag = {
339
+ Type: 'AWS::Cognito::IdentityPoolPrincipalTag',
340
+ Properties: {
341
+ IdentityPoolId: {
342
+ Ref: CognitoIdentityPoolLogicalId,
343
+ },
344
+ IdentityProviderName: {
345
+ 'Fn::GetAtt': [CognitoUserPoolLogicalId, 'ProviderName'],
346
+ },
347
+ PrincipalTags,
348
+ UseDefaults: false,
349
+ },
350
+ };
351
+ }
352
+
313
353
  if (!template.Outputs) {
314
354
  template.Outputs = {};
315
355
  }