Skip to content

Commit

Permalink
split & rename
Browse files Browse the repository at this point in the history
  • Loading branch information
Niranjan Jayakar committed May 22, 2020
1 parent 05a9e53 commit 00ce0d5
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 173 deletions.
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-cognito/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export * from './user-pool';
export * from './user-pool-attr';
export * from './user-pool-client';
export * from './user-pool-domain';
export * from './user-pool-idp';
export * from './user-pool-idp';
export * from './user-pool-idps';
131 changes: 11 additions & 120 deletions packages/@aws-cdk/aws-cognito/lib/user-pool-idp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Construct, IResource, Resource } from '@aws-cdk/core';
import { CfnUserPoolIdentityProvider } from './cognito.generated';
import { IUserPool } from './user-pool';
import { Construct, IResource } from '@aws-cdk/core';
import {
UserPoolIdentityProviderAmazon,
UserPoolIdentityProviderAmazonProps,
UserPoolIdentityProviderFacebook,
UserPoolIdentityProviderFacebookProps,
} from './user-pool-idps';

/**
* Represents a UserPoolIdentityProvider
Expand All @@ -13,119 +17,6 @@ export interface IUserPoolIdentityProvider extends IResource {
readonly providerName: string;
}

/**
* Properties to initialize UserPoolFacebookIdentityProvider
*/
export interface UserPoolFacebookIdentityProviderProps {
/**
* The user pool to which this construct provides identities.
*/
readonly userPool: IUserPool;

/**
* The client id recognized by Facebook APIs.
*/
readonly clientId: string;
/**
* The client secret to be accompanied with clientUd for Facebook to authenticate the client.
* @see https://developers.facebook.com/docs/facebook-login/security#appsecret
*/
readonly clientSecret: string;
/**
* The list of facebook permissions to obtain for getting access to the Facebook profile.
* @see https://developers.facebook.com/docs/facebook-login/permissions
* @default [ public_profile ]
*/
readonly scopes?: string[];
/**
* The Facebook API version to use
* @default - to the oldest version supported by Facebook
*/
readonly apiVersion?: string;
}

/**
* Represents a identity provider that integrates with 'Facebook Login'
* @resource AWS::Cognito::UserPoolIdentityProvider
*/
export class UserPoolFacebookIdentityProvider extends Resource implements IUserPoolIdentityProvider {
public readonly providerName: string;

constructor(scope: Construct, id: string, props: UserPoolFacebookIdentityProviderProps) {
super(scope, id);

const scopes = props.scopes ?? [ 'public_profile' ];

const resource = new CfnUserPoolIdentityProvider(this, 'Resource', {
userPoolId: props.userPool.userPoolId,
providerName: 'Facebook', // must be 'Facebook' when the type is 'Facebook'
providerType: 'Facebook',
providerDetails: {
client_id: props.clientId,
client_secret: props.clientSecret,
authorize_scopes: scopes.join(','),
api_version: props.apiVersion,
},
});

this.providerName = super.getResourceNameAttribute(resource.ref);
}
}

/**
* Properties to initialize UserPoolAmazonIdentityProvider
*/
export interface UserPoolAmazonIdentityProviderProps {
/**
* The user pool to which this construct provides identities.
*/
readonly userPool: IUserPool;

/**
* The client id recognized by 'Login with Amazon' APIs.
* @see https://developer.amazon.com/docs/login-with-amazon/security-profile.html#client-identifier
*/
readonly clientId: string;
/**
* The client secret to be accompanied with clientId for 'Login with Amazon' APIs to authenticate the client.
* @see https://developer.amazon.com/docs/login-with-amazon/security-profile.html#client-identifier
*/
readonly clientSecret: string;
/**
* The types of user profile data to obtain for the Amazon profile.
* @see https://developer.amazon.com/docs/login-with-amazon/customer-profile.html
* @default [ profile ]
*/
readonly scopes?: string[];
}

/**
* Represents a identity provider that integrates with 'Login with Amazon'
* @resource AWS::Cognito::UserPoolIdentityProvider
*/
export class UserPoolAmazonIdentityProvider extends Resource implements IUserPoolIdentityProvider {
public readonly providerName: string;

constructor(scope: Construct, id: string, props: UserPoolAmazonIdentityProviderProps) {
super(scope, id);

const scopes = props.scopes ?? [ 'profile' ];

const resource = new CfnUserPoolIdentityProvider(this, 'Resource', {
userPoolId: props.userPool.userPoolId,
providerName: 'LoginWithAmazon', // must be 'LoginWithAmazon' when the type is 'LoginWithAmazon'
providerType: 'LoginWithAmazon',
providerDetails: {
client_id: props.clientId,
client_secret: props.clientSecret,
authorize_scopes: scopes.join(' '),
},
});

this.providerName = super.getResourceNameAttribute(resource.ref);
}
}

/**
* Options to integrate with the various social identity providers.
*/
Expand All @@ -134,16 +25,16 @@ export class UserPoolIdentityProvider {
* Federate with 'Facebook Login'
* @see https://developers.facebook.com/docs/facebook-login/
*/
public static facebook(scope: Construct, id: string, options: UserPoolFacebookIdentityProviderProps) {
return new UserPoolFacebookIdentityProvider(scope, id, options);
public static facebook(scope: Construct, id: string, options: UserPoolIdentityProviderFacebookProps) {
return new UserPoolIdentityProviderFacebook(scope, id, options);
}

/**
* Federate with 'Login with Amazon'
* @see https://developer.amazon.com/apps-and-games/login-with-amazon
*/
public static amazon(scope: Construct, id: string, options: UserPoolAmazonIdentityProviderProps) {
return new UserPoolAmazonIdentityProvider(scope, id, options);
public static amazon(scope: Construct, id: string, options: UserPoolIdentityProviderAmazonProps) {
return new UserPoolIdentityProviderAmazon(scope, id, options);
}

private constructor() {}
Expand Down
58 changes: 58 additions & 0 deletions packages/@aws-cdk/aws-cognito/lib/user-pool-idps/amazon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Construct, Resource } from '@aws-cdk/core';
import { CfnUserPoolIdentityProvider } from '../cognito.generated';
import { IUserPool } from '../user-pool';
import { IUserPoolIdentityProvider } from '../user-pool-idp';

/**
* Properties to initialize UserPoolAmazonIdentityProvider
*/
export interface UserPoolIdentityProviderAmazonProps {
/**
* The user pool to which this construct provides identities.
*/
readonly userPool: IUserPool;

/**
* The client id recognized by 'Login with Amazon' APIs.
* @see https://developer.amazon.com/docs/login-with-amazon/security-profile.html#client-identifier
*/
readonly clientId: string;
/**
* The client secret to be accompanied with clientId for 'Login with Amazon' APIs to authenticate the client.
* @see https://developer.amazon.com/docs/login-with-amazon/security-profile.html#client-identifier
*/
readonly clientSecret: string;
/**
* The types of user profile data to obtain for the Amazon profile.
* @see https://developer.amazon.com/docs/login-with-amazon/customer-profile.html
* @default [ profile ]
*/
readonly scopes?: string[];
}

/**
* Represents a identity provider that integrates with 'Login with Amazon'
* @resource AWS::Cognito::UserPoolIdentityProvider
*/
export class UserPoolIdentityProviderAmazon extends Resource implements IUserPoolIdentityProvider {
public readonly providerName: string;

constructor(scope: Construct, id: string, props: UserPoolIdentityProviderAmazonProps) {
super(scope, id);

const scopes = props.scopes ?? [ 'profile' ];

const resource = new CfnUserPoolIdentityProvider(this, 'Resource', {
userPoolId: props.userPool.userPoolId,
providerName: 'LoginWithAmazon', // must be 'LoginWithAmazon' when the type is 'LoginWithAmazon'
providerType: 'LoginWithAmazon',
providerDetails: {
client_id: props.clientId,
client_secret: props.clientSecret,
authorize_scopes: scopes.join(' '),
},
});

this.providerName = super.getResourceNameAttribute(resource.ref);
}
}
63 changes: 63 additions & 0 deletions packages/@aws-cdk/aws-cognito/lib/user-pool-idps/facebook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Construct, Resource } from '@aws-cdk/core';
import { CfnUserPoolIdentityProvider } from '../cognito.generated';
import { IUserPool } from '../user-pool';
import { IUserPoolIdentityProvider } from '../user-pool-idp';

/**
* Properties to initialize UserPoolFacebookIdentityProvider
*/
export interface UserPoolIdentityProviderFacebookProps {
/**
* The user pool to which this construct provides identities.
*/
readonly userPool: IUserPool;

/**
* The client id recognized by Facebook APIs.
*/
readonly clientId: string;
/**
* The client secret to be accompanied with clientUd for Facebook to authenticate the client.
* @see https://developers.facebook.com/docs/facebook-login/security#appsecret
*/
readonly clientSecret: string;
/**
* The list of facebook permissions to obtain for getting access to the Facebook profile.
* @see https://developers.facebook.com/docs/facebook-login/permissions
* @default [ public_profile ]
*/
readonly scopes?: string[];
/**
* The Facebook API version to use
* @default - to the oldest version supported by Facebook
*/
readonly apiVersion?: string;
}

/**
* Represents a identity provider that integrates with 'Facebook Login'
* @resource AWS::Cognito::UserPoolIdentityProvider
*/
export class UserPoolIdentityProviderFacebook extends Resource implements IUserPoolIdentityProvider {
public readonly providerName: string;

constructor(scope: Construct, id: string, props: UserPoolIdentityProviderFacebookProps) {
super(scope, id);

const scopes = props.scopes ?? [ 'public_profile' ];

const resource = new CfnUserPoolIdentityProvider(this, 'Resource', {
userPoolId: props.userPool.userPoolId,
providerName: 'Facebook', // must be 'Facebook' when the type is 'Facebook'
providerType: 'Facebook',
providerDetails: {
client_id: props.clientId,
client_secret: props.clientSecret,
authorize_scopes: scopes.join(','),
api_version: props.apiVersion,
},
});

this.providerName = super.getResourceNameAttribute(resource.ref);
}
}
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-cognito/lib/user-pool-idps/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './amazon';
export * from './facebook';
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-cognito/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@
"attribute-tag:@aws-cdk/aws-cognito.UserPoolClient.userPoolClientName",
"resource-attribute:@aws-cdk/aws-cognito.UserPoolClient.userPoolClientClientSecret",
"props-physical-name:@aws-cdk/aws-cognito.UserPoolDomainProps",
"props-physical-name:@aws-cdk/aws-cognito.UserPoolFacebookIdentityProviderProps",
"props-physical-name:@aws-cdk/aws-cognito.UserPoolAmazonIdentityProviderProps"
"props-physical-name:@aws-cdk/aws-cognito.UserPoolIdentityProviderFacebookProps",
"props-physical-name:@aws-cdk/aws-cognito.UserPoolIdentityProviderAmazonProps"
]
},
"stability": "experimental",
Expand Down
54 changes: 54 additions & 0 deletions packages/@aws-cdk/aws-cognito/test/user-pool-idps/amazon.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import '@aws-cdk/assert/jest';
import { Stack } from '@aws-cdk/core';
import { UserPool, UserPoolIdentityProvider } from '../../lib';

describe('UserPoolIdentityProvider', () => {
describe('amazon', () => {
test('defaults', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'userpool');

// WHEN
UserPoolIdentityProvider.amazon(stack, 'userpoolidp', {
userPool: pool,
clientId: 'amzn-client-id',
clientSecret: 'amzn-client-secret',
});

expect(stack).toHaveResource('AWS::Cognito::UserPoolIdentityProvider', {
ProviderName: 'LoginWithAmazon',
ProviderType: 'LoginWithAmazon',
ProviderDetails: {
client_id: 'amzn-client-id',
client_secret: 'amzn-client-secret',
authorize_scopes: 'profile',
},
});
});

test('scopes', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'userpool');

// WHEN
UserPoolIdentityProvider.amazon(stack, 'userpoolidp', {
userPool: pool,
clientId: 'amzn-client-id',
clientSecret: 'amzn-client-secret',
scopes: [ 'scope1', 'scope2' ],
});

expect(stack).toHaveResource('AWS::Cognito::UserPoolIdentityProvider', {
ProviderName: 'LoginWithAmazon',
ProviderType: 'LoginWithAmazon',
ProviderDetails: {
client_id: 'amzn-client-id',
client_secret: 'amzn-client-secret',
authorize_scopes: 'scope1 scope2',
},
});
});
});
});
Loading

0 comments on commit 00ce0d5

Please sign in to comment.