Skip to content

Commit 90a7734

Browse files
authored
fix(cognito-identitypool-alpha): inconvenient IdentityPoolProviderUrl.userPool() (#29025)
### Reason for this change `IdentityPoolProviderUrl.userPool()` requires a string `url` currently. The description is "User Pool Provider Url". It should be ``` `${userPool.userPoolProviderName}:${userPoolClient.userPoolClientId}` ```. `UserPool` has an attribute `userPoolProviderUrl` which description is "User Pool Provider Url", but confusingly, it cannot be specified to `IdentityPoolProviderUrl.userPool()`. The format of the identity provider identifier isn't well documented. See [SetIdentityPoolRoles API reference](https://docs.aws.amazon.com/cognitoidentity/latest/APIReference/API_SetIdentityPoolRoles.html) for example of User Pool's identity provider identifier. ### Description of changes This PR fixes `IdentityPoolProviderUrl.userPool()` to accept `UserPool` and `UserPoolClient` instead of a string `url`. It generates a correct identifier described above. ### Description of how you validated changes Existing integration test generates an identifier as described above. The snapshot won't be changed by this PR. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) BREAKING CHANGE: The argument of `IdentityPoolProviderUrl.userPool()` has been changed from `url: string` to `userPool: UserPool, userPoolClient: UserPoolClient`. If you want to specify custom identifier string, use `IdentityPoolProviderUrl.custom()` instead. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6cd1e1f commit 90a7734

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

packages/@aws-cdk/aws-cognito-identitypool-alpha/README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -329,18 +329,14 @@ new IdentityPool(this, 'myidentitypool', {
329329
});
330330
```
331331

332-
For identity providers that don't have static Urls, a custom Url or User Pool Client Url can be supplied:
332+
For identity providers that don't have static Urls, a custom Url can be supplied:
333333

334334
```ts
335335
import { IdentityPoolProviderUrl } from '@aws-cdk/aws-cognito-identitypool-alpha';
336336

337337
new IdentityPool(this, 'myidentitypool', {
338338
identityPoolName: 'myidentitypool',
339339
roleMappings: [
340-
{
341-
providerUrl: IdentityPoolProviderUrl.userPool('cognito-idp.my-idp-region.amazonaws.com/my-idp-region_abcdefghi:app_client_id'),
342-
useToken: true,
343-
},
344340
{
345341
providerUrl: IdentityPoolProviderUrl.custom('my-custom-provider.com'),
346342
useToken: true,
@@ -354,15 +350,16 @@ This is because by default, the key in the Cloudformation role mapping hash is t
354350
cannot be references. For example:
355351

356352
```ts
357-
import { UserPool } from 'aws-cdk-lib/aws-cognito';
353+
import { UserPool, UserPoolClient } from 'aws-cdk-lib/aws-cognito';
358354
import { IdentityPoolProviderUrl } from '@aws-cdk/aws-cognito-identitypool-alpha';
359355

360-
declare const userPool : UserPool;
356+
declare const userPool: UserPool;
357+
declare const userPoolClient: UserPoolClient;
361358
new IdentityPool(this, 'myidentitypool', {
362359
identityPoolName: 'myidentitypool',
363360
roleMappings: [{
364361
mappingKey: 'cognito',
365-
providerUrl: IdentityPoolProviderUrl.userPool(userPool.userPoolProviderUrl),
362+
providerUrl: IdentityPoolProviderUrl.userPool(userPool, userPoolClient),
366363
useToken: true,
367364
}],
368365
});
@@ -399,4 +396,3 @@ IdentityPool.fromIdentityPoolId(this, 'my-imported-identity-pool',
399396
IdentityPool.fromIdentityPoolArn(this, 'my-imported-identity-pool',
400397
'arn:aws:cognito-identity:us-east-1:123456789012:identitypool/us-east-1:dj2823ryiwuhef937');
401398
```
402-

packages/@aws-cdk/aws-cognito-identitypool-alpha/lib/identitypool.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {
22
CfnIdentityPool,
3+
UserPool,
4+
UserPoolClient,
35
} from 'aws-cdk-lib/aws-cognito';
46
import {
57
IOpenIdConnectProvider,
@@ -155,7 +157,8 @@ export class IdentityPoolProviderUrl {
155157
}
156158

157159
/** User Pool Provider Url */
158-
public static userPool(url: string): IdentityPoolProviderUrl {
160+
public static userPool(userPool: UserPool, userPoolClient: UserPoolClient): IdentityPoolProviderUrl {
161+
const url = `${userPool.userPoolProviderName}:${userPoolClient.userPoolClientId}`;
159162
return new IdentityPoolProviderUrl(IdentityPoolProviderType.USER_POOL, url);
160163
}
161164

packages/@aws-cdk/aws-cognito-identitypool-alpha/test/identitypool.test.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ describe('role mappings', () => {
440440
const providerUrl = Fn.importValue('ProviderUrl');
441441
expect(() => new IdentityPool(stack, 'TestIdentityPoolRoleMappingErrors', {
442442
roleMappings: [{
443-
providerUrl: IdentityPoolProviderUrl.userPool(providerUrl),
443+
providerUrl: IdentityPoolProviderUrl.custom(providerUrl),
444444
useToken: true,
445445
}],
446446
})).toThrowError('mappingKey must be provided when providerUrl.value is a token');
@@ -452,7 +452,7 @@ describe('role mappings', () => {
452452
new IdentityPool(stack, 'TestIdentityPoolRoleMappingToken', {
453453
roleMappings: [{
454454
mappingKey: 'theKey',
455-
providerUrl: IdentityPoolProviderUrl.userPool(providerUrl),
455+
providerUrl: IdentityPoolProviderUrl.custom(providerUrl),
456456
useToken: true,
457457
}],
458458
});
@@ -532,6 +532,8 @@ describe('role mappings', () => {
532532

533533
test('role mapping with rules configuration', () => {
534534
const stack = new Stack();
535+
const pool = new UserPool(stack, 'Pool');
536+
const client = pool.addClient('Client');
535537
const adminRole = new Role(stack, 'adminRole', {
536538
assumedBy: new ServicePrincipal('admin.amazonaws.com'),
537539
});
@@ -557,6 +559,11 @@ describe('role mappings', () => {
557559
});
558560
const idPool = new IdentityPool(stack, 'TestIdentityPoolRoleMappingRules', {
559561
roleMappings: [{
562+
mappingKey: 'cognito',
563+
providerUrl: IdentityPoolProviderUrl.userPool(pool, client),
564+
useToken: true,
565+
},
566+
{
560567
providerUrl: IdentityPoolProviderUrl.AMAZON,
561568
resolveAmbiguousRoles: true,
562569
rules: [
@@ -601,6 +608,16 @@ describe('role mappings', () => {
601608
Ref: 'TestIdentityPoolRoleMappingRulesC8C07BC3',
602609
},
603610
RoleMappings: {
611+
'cognito': {
612+
IdentityProvider: {
613+
'Fn::Join': ['', [
614+
{ 'Fn::GetAtt': ['PoolD3F588B8', 'ProviderName'] },
615+
':',
616+
{ Ref: 'PoolClient8A3E5EB7' },
617+
]],
618+
},
619+
Type: 'Token',
620+
},
604621
'www.amazon.com': {
605622
AmbiguousRoleResolution: 'AuthenticatedRole',
606623
IdentityProvider: 'www.amazon.com',
@@ -696,4 +713,4 @@ describe('role mappings', () => {
696713
},
697714
});
698715
});
699-
});
716+
});

packages/@aws-cdk/aws-cognito-identitypool-alpha/test/integ.identitypool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const idPool = new IdentityPool(stack, 'identitypool', {
6262
roleMappings: [
6363
{
6464
mappingKey: 'theKey',
65-
providerUrl: IdentityPoolProviderUrl.userPool(`${userPool.userPoolProviderName}:${client.userPoolClientId}`),
65+
providerUrl: IdentityPoolProviderUrl.userPool(userPool, client),
6666
useToken: true,
6767
},
6868
],

0 commit comments

Comments
 (0)