Skip to content

Commit f2bba19

Browse files
author
Niranjan Jayakar
committed
More feedback
1 parent ee21d0e commit f2bba19

File tree

4 files changed

+81
-99
lines changed

4 files changed

+81
-99
lines changed

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

+42-26
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aw
3636
- [Emails](#emails)
3737
- [Lambda Triggers](#lambda-triggers)
3838
- [Import](#importing-user-pools)
39-
- [App Clients](#app-clients)
4039
- [Identity Providers](#identity-providers)
40+
- [App Clients](#app-clients)
4141
- [Domains](#domains)
4242

4343
## User Pools
@@ -335,6 +335,36 @@ const otherAwesomePool = UserPool.fromUserPoolArn(stack, 'other-awesome-user-poo
335335
'arn:aws:cognito-idp:eu-west-1:123456789012:userpool/us-east-1_mtRyYQ14D');
336336
```
337337

338+
### Identity Providers
339+
340+
Users that are part of a user pool can sign in either directly through a user pool, or federate through a third-party
341+
identity provider. Once configured, the Cognito backend will take care of integrating with the third-party provider.
342+
Read more about [Adding User Pool Sign-in Through a Third
343+
Party](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-identity-federation.html).
344+
345+
The following third-party identity providers are currentlhy supported in the CDK -
346+
347+
* [Login With Amazon](https://developer.amazon.com/apps-and-games/login-with-amazon)
348+
* [Facebook Login](https://developers.facebook.com/docs/facebook-login/)
349+
350+
The following code configures a user pool to federate with the third party provider, 'Login with Amazon'. The identity
351+
provider needs to be configured with a set of credentials that the Cognito backend can use to federate with the
352+
third-party identity provider.
353+
354+
```ts
355+
const userpool = new UserPool(stack, 'Pool');
356+
357+
const provider = new UserPoolIdentityProviderAmazon(stack, 'Amazon', {
358+
clientId: 'amzn-client-id',
359+
clientSecret: 'amzn-client-secret',
360+
userPool: userpool,
361+
});
362+
```
363+
364+
In order to allow users to sign in with a third-party identity provider, the app client that faces the user should be
365+
configured to use the identity provider. See [App Clients](#app-clients) section to know more about App Clients.
366+
The identity providers should be configured on `identityProviders` property available on the `UserPoolClient` construct.
367+
338368
### App Clients
339369

340370
An app is an entity within a user pool that has permission to call unauthenticated APIs (APIs that do not have an
@@ -418,36 +448,22 @@ pool.addClient('app-client', {
418448
});
419449
```
420450

421-
### Identity Providers
422-
423-
Users that are part of a user pool can sign in either directly through a user pool, or federate through a third-party
424-
identity provider. Once configured, the Cognito backend will take care of integrating with the third-party provider.
425-
Read more about [Adding User Pool Sign-in Through a Third
426-
Party](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-identity-federation.html).
427-
428-
The following third-party identity providers are currentlhy supported in the CDK -
429-
430-
* [Login With Amazon](https://developer.amazon.com/apps-and-games/login-with-amazon)
431-
* [Facebook Login](https://developers.facebook.com/docs/facebook-login/)
432-
433-
The following code configures a user pool to federate with the third party provider, 'Login with Amazon'. The identity
434-
provider needs to be configured with a set of credentials that the Cognito backend can use to federate with the
435-
third-party identity provider.
451+
All identity providers created in the CDK app are automatically registered into the corresponding user pool. All app
452+
clients created in the CDK have all of the identity providers enabled by default. The 'Cognito' identity provider,
453+
that allows users to register and sign in directly with the Cognito user pool, is also enabled by default.
454+
Alternatively, the list of supported identity providers for a client can be explicitly specified -
436455

437456
```ts
438-
const userpool = new UserPool(stack, 'Pool');
439-
440-
const provider = UserPoolIdentityProvider.amazon(stack, 'Amazon', {
441-
clientId: 'amzn-client-id',
442-
clientSecret: 'amzn-client-secret',
443-
userPool: userpool,
457+
const pool = new UserPool(this, 'Pool');
458+
pool.addClient('app-client', {
459+
// ...
460+
supportedIdentityProviders: [
461+
UserPoolClientIdentityProvider.AMAZON,
462+
UserPoolClientIdentityProvider.COGNITO,
463+
]
444464
});
445465
```
446466

447-
In order to allow users to sign in with a third-party identity provider, the app client that faces the user should be
448-
configured to use the identity provider. See [App Clients](#app-clients) section to know more about App Clients.
449-
The identity providers should be configured on `identityProviders` property available on the `UserPoolClient` construct.
450-
451467
### Domains
452468

453469
After setting up an [app client](#app-clients), the address for the user pool's sign-up and sign-in webpages can be

packages/@aws-cdk/aws-cognito/lib/user-pool-client.ts

+28-16
Original file line numberDiff line numberDiff line change
@@ -148,22 +148,38 @@ export class OAuthScope {
148148
/**
149149
* Identity providers supported by the UserPoolClient
150150
*/
151-
export interface SupportedIdentityProviders {
151+
export class UserPoolClientIdentityProvider {
152152
/**
153-
* Whether users can sign in directly as a user of the User Pool.
154-
* @default true
153+
* Allow users to sign in using 'Facebook Login'.
154+
* A `UserPoolIdentityProviderFacebook` must be attached to the user pool.
155155
*/
156-
readonly cognito?: boolean;
156+
public static readonly FACEBOOK = new UserPoolClientIdentityProvider('Facebook');
157+
157158
/**
158-
* Whether users can sign in using 'Facebook Login'.
159-
* @default false
159+
* Allow users to sign in using 'Login With Amazon'.
160+
* A `UserPoolIdentityProviderAmazon` must be attached to the user pool.
160161
*/
161-
readonly facebook?: boolean;
162+
public static readonly AMAZON = new UserPoolClientIdentityProvider('LoginWithAmazon');
163+
162164
/**
163-
* Whether users can sign in using 'Login With Amazon'.
164-
* @default false
165+
* Allow users to sign in directly as a user of the User Pool
166+
*/
167+
public static readonly COGNITO = new UserPoolClientIdentityProvider('COGNITO');
168+
169+
/**
170+
* Specify a provider not yet supported by the CDK.
171+
* @param name name of the identity provider as recognized by CloudFormation property `SupportedIdentityProviders`
165172
*/
166-
readonly amazon?: boolean;
173+
public static custom(name: string) {
174+
return new UserPoolClientIdentityProvider(name);
175+
}
176+
177+
/** The name of the identity provider as recognized by CloudFormation property `SupportedIdentityProviders` */
178+
public readonly name: string;
179+
180+
private constructor(name: string) {
181+
this.name = name;
182+
}
167183
}
168184

169185
/**
@@ -211,7 +227,7 @@ export interface UserPoolClientOptions {
211227
* identity providers are imported, either specify this option explicitly or ensure that the identity providers are
212228
* registered with the user pool using the `UserPool.registerIdentityProvider()` API.
213229
*/
214-
readonly supportedIdentityProviders?: SupportedIdentityProviders;
230+
readonly supportedIdentityProviders?: UserPoolClientIdentityProvider[];
215231
}
216232

217233
/**
@@ -364,11 +380,7 @@ export class UserPoolClient extends Resource implements IUserPoolClient {
364380
providerSet.add('COGNITO');
365381
providers = Array.from(providerSet);
366382
} else {
367-
providers = [];
368-
const idps = props.supportedIdentityProviders;
369-
if (idps.cognito === undefined || idps.cognito === true) { providers.push('COGNITO'); }
370-
if (idps.facebook) { providers.push('Facebook'); }
371-
if (idps.amazon) { providers.push('LoginWithAmazon'); }
383+
providers = props.supportedIdentityProviders.map((p) => p.name);
372384
}
373385
if (providers.length === 0) { return undefined; }
374386
return Array.from(providers);

packages/@aws-cdk/aws-cognito/lib/user-pool-idp.ts

-20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
import { Construct, IResource, Resource } from '@aws-cdk/core';
2-
import {
3-
UserPoolIdentityProviderAmazon,
4-
UserPoolIdentityProviderAmazonProps,
5-
UserPoolIdentityProviderFacebook,
6-
UserPoolIdentityProviderFacebookProps,
7-
} from './user-pool-idps';
82

93
/**
104
* Represents a UserPoolIdentityProvider
@@ -33,19 +27,5 @@ export class UserPoolIdentityProvider {
3327
return new Import(scope, id);
3428
}
3529

36-
/**
37-
* Federate login with 'Login with Amazon'
38-
*/
39-
public static amazon(scope: Construct, id: string, props: UserPoolIdentityProviderAmazonProps) {
40-
return new UserPoolIdentityProviderAmazon(scope, id, props);
41-
}
42-
43-
/**
44-
* Federate login with 'Facebook Login'
45-
*/
46-
public static facebook(scope: Construct, id: string, props: UserPoolIdentityProviderFacebookProps) {
47-
return new UserPoolIdentityProviderFacebook(scope, id, props);
48-
}
49-
5030
private constructor() {}
5131
}

packages/@aws-cdk/aws-cognito/test/user-pool-client.test.ts

+11-37
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ABSENT } from '@aws-cdk/assert';
22
import '@aws-cdk/assert/jest';
33
import { Stack } from '@aws-cdk/core';
4-
import { OAuthScope, UserPool, UserPoolClient, UserPoolIdentityProviderAmazon, UserPoolIdentityProviderFacebook } from '../lib';
4+
import { OAuthScope, UserPool, UserPoolClient, UserPoolClientIdentityProvider, UserPoolIdentityProvider } from '../lib';
55

66
describe('User Pool Client', () => {
77
test('default setup', () => {
@@ -370,16 +370,9 @@ describe('User Pool Client', () => {
370370
// GIVEN
371371
const stack = new Stack();
372372
const pool = new UserPool(stack, 'Pool');
373-
new UserPoolIdentityProviderAmazon(stack, 'amznidp', {
374-
userPool: pool,
375-
clientId: 'amzn-client-id',
376-
clientSecret: 'amzn-client-secret',
377-
});
378-
new UserPoolIdentityProviderFacebook(stack, 'fbidp', {
379-
userPool: pool,
380-
clientId: 'amzn-client-id',
381-
clientSecret: 'amzn-client-secret',
382-
});
373+
374+
const idp = UserPoolIdentityProvider.fromProviderName(stack, 'imported', 'userpool-idp');
375+
pool.registerIdentityProvider(idp);
383376

384377
// WHEN
385378
new UserPoolClient(stack, 'Client', {
@@ -389,50 +382,31 @@ describe('User Pool Client', () => {
389382
// THEN
390383
expect(stack).toHaveResource('AWS::Cognito::UserPoolClient', {
391384
SupportedIdentityProviders: [
392-
{ Ref: 'amznidp99BF1483' },
393-
{ Ref: 'fbidp86F36311' },
385+
'userpool-idp',
394386
'COGNITO',
395387
],
396388
});
397389
});
398390

399-
test('explicit supportedIdentityProviders', () => {
391+
test('supportedIdentityProviders', () => {
400392
// GIVEN
401393
const stack = new Stack();
402394
const pool = new UserPool(stack, 'Pool');
403395

404396
// WHEN
405-
pool.addClient('DefaultExplicit', {
406-
userPoolClientName: 'DefaultExplicit',
407-
supportedIdentityProviders: {},
408-
});
409397
pool.addClient('AllEnabled', {
410398
userPoolClientName: 'AllEnabled',
411-
supportedIdentityProviders: {
412-
amazon: true,
413-
facebook: true,
414-
cognito: true,
415-
},
416-
});
417-
pool.addClient('CognitoDisabled', {
418-
userPoolClientName: 'CognitoDisabled',
419-
supportedIdentityProviders: {
420-
cognito: false,
421-
},
399+
supportedIdentityProviders: [
400+
UserPoolClientIdentityProvider.COGNITO,
401+
UserPoolClientIdentityProvider.FACEBOOK,
402+
UserPoolClientIdentityProvider.AMAZON,
403+
],
422404
});
423405

424406
// THEN
425-
expect(stack).toHaveResource('AWS::Cognito::UserPoolClient', {
426-
ClientName: 'DefaultExplicit',
427-
SupportedIdentityProviders: [ 'COGNITO' ],
428-
});
429407
expect(stack).toHaveResource('AWS::Cognito::UserPoolClient', {
430408
ClientName: 'AllEnabled',
431409
SupportedIdentityProviders: [ 'COGNITO', 'Facebook', 'LoginWithAmazon' ],
432410
});
433-
expect(stack).toHaveResource('AWS::Cognito::UserPoolClient', {
434-
ClientName: 'CognitoDisabled',
435-
SupportedIdentityProviders: ABSENT,
436-
});
437411
});
438412
});

0 commit comments

Comments
 (0)