Skip to content

Commit

Permalink
unique id
Browse files Browse the repository at this point in the history
  • Loading branch information
jogold committed May 31, 2022
1 parent 4bf430a commit 10771c1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
37 changes: 23 additions & 14 deletions packages/@aws-cdk/aws-cognito/lib/user-pool-idps/oidc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Token } from '@aws-cdk/core';
import { Names, Token } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { CfnUserPoolIdentityProvider } from '../cognito.generated';
import { UserPoolIdentityProviderProps } from './base';
Expand Down Expand Up @@ -26,7 +26,7 @@ export interface UserPoolIdentityProviderOidcProps extends UserPoolIdentityProvi
/**
* The name of the provider
*
* @default - the ID of the construct
* @default - the unique ID of the construct
*/
readonly name?: string;

Expand Down Expand Up @@ -107,11 +107,15 @@ export class UserPoolIdentityProviderOidc extends UserPoolIdentityProviderBase {
constructor(scope: Construct, id: string, props: UserPoolIdentityProviderOidcProps) {
super(scope, id, props);

if (props.name && !Token.isUnresolved(props.name) && (props.name.length < 3 || props.name.length > 32)) {
throw new Error(`Expected provider name to be between 3 and 32 characters, received ${props.name} (${props.name.length} characters)`);
}

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

const resource = new CfnUserPoolIdentityProvider(this, 'Resource', {
userPoolId: props.userPool.userPoolId,
providerName: getProviderName(this.node.id, props.name),
providerName: this.getProviderName(props.name),
providerType: 'OIDC',
providerDetails: {
client_id: props.clientId,
Expand All @@ -130,19 +134,24 @@ export class UserPoolIdentityProviderOidc extends UserPoolIdentityProviderBase {

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

function getProviderName(id: string, name?: string): string {
if (name) {
if (!Token.isUnresolved(name) && (name.length < 3 || name.length > 32)) {
throw new Error(`Expected provider name to be between 3 and 32 characters, received ${name} (${name.length} characters)`);
private getProviderName(name?: string): string {
if (name) {
if (!Token.isUnresolved(name) && (name.length < 3 || name.length > 32)) {
throw new Error(`Expected provider name to be between 3 and 32 characters, received ${name} (${name.length} characters)`);
}
return name;
}
return name;
}

if (id.length < 3 || id.length > 32) {
throw new Error(`Provider name defaults to construct's id (${id}) which is not between 3 and 32 characters. Please specify a valid name with \`name\`.`);
}
const uniqueId = Names.uniqueId(this);

return id;
if (uniqueId.length < 3) {
return `${uniqueId}oidc`;
}

if (uniqueId.length > 32) {
return uniqueId.substring(0, 16) + uniqueId.substring(uniqueId.length - 16);
}
return uniqueId;
}
}
30 changes: 27 additions & 3 deletions packages/@aws-cdk/aws-cognito/test/user-pool-idps/oidc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,42 @@ describe('UserPoolIdentityProvider', () => {
})).toThrow(/Expected provider name to be between 3 and 32 characters/);
});

test('throws when default name is invalid', () => {
test('generates a valid name when unique id is too short', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'userpool');

// WHEN
new UserPoolIdentityProviderOidc(stack, 'xy', {
userPool: pool,
clientId: 'client-id',
clientSecret: 'client-secret',
issuerUrl: 'https://my-issuer-url.com',
});

// THEN
expect(() => new UserPoolIdentityProviderOidc(stack, 'xy', {
Template.fromStack(stack).hasResourceProperties('AWS::Cognito::UserPoolIdentityProvider', {
ProviderName: 'xyoidc',
});
});

test('generates a valid name when unique id is too long', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'userpool');

// WHEN
new UserPoolIdentityProviderOidc(stack, `${'oidc'.repeat(10)}xyz`, {
userPool: pool,
clientId: 'client-id',
clientSecret: 'client-secret',
issuerUrl: 'https://my-issuer-url.com',
})).toThrow(/Provider name defaults to construct's id \(xy\) which is not between 3 and 32 characters/);
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Cognito::UserPoolIdentityProvider', {
ProviderName: 'oidcoidcoidcoidccoidcoidcoidcxyz',
});
});
});
});

0 comments on commit 10771c1

Please sign in to comment.