Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cognito): support SecretValue for UserPoolIdentityProviderOidc #30255

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions packages/aws-cdk-lib/aws-cognito/lib/user-pool-idps/oidc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Construct } from 'constructs';
import { UserPoolIdentityProviderProps } from './base';
import { UserPoolIdentityProviderBase } from './private/user-pool-idp-base';
import { Names, Token } from '../../../core';
import { Names, SecretValue, Token } from '../../../core';
import { CfnUserPoolIdentityProvider } from '../cognito.generated';

/**
Expand All @@ -14,9 +14,17 @@ export interface UserPoolIdentityProviderOidcProps extends UserPoolIdentityProvi
readonly clientId: string;

/**
* The client secret
* The client secret as a plain text string. Exactly one of clientSecret or clientSecretValue has to be provided.
* @default none
* @deprecated use clientSecretValue instead
*/
readonly clientSecret: string;
readonly clientSecret?: string;

/**
* The client secret read from a @SecretValue. Exactly one of clientSecret or clientSecretValue has to be provided.
* @default none
*/
readonly clientSecretValue?: SecretValue;

/**
* Issuer URL
Expand Down Expand Up @@ -109,13 +117,19 @@ export class UserPoolIdentityProviderOidc extends UserPoolIdentityProviderBase {

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

//at least one of the properties must be configured
if ((!props.clientSecret && !props.clientSecretValue) ||
(props.clientSecret && props.clientSecretValue)) {
throw new Error('Exactly one of "clientSecret" or "clientSecretValue" must be configured.');
}

const resource = new CfnUserPoolIdentityProvider(this, 'Resource', {
userPoolId: props.userPool.userPoolId,
providerName: this.getProviderName(props.name),
providerType: 'OIDC',
providerDetails: {
client_id: props.clientId,
client_secret: props.clientSecret,
client_secret: props.clientSecretValue ? props.clientSecretValue.unsafeUnwrap() : props.clientSecret,
authorize_scopes: scopes.join(' '),
attributes_request_method: props.attributeRequestMethod ?? OidcAttributeRequestMethod.GET,
oidc_issuer: props.issuerUrl,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Template } from '../../../assertions';
import { Stack } from '../../../core';
import { SecretValue, Stack } from '../../../core';
import { ProviderAttribute, UserPool, UserPoolIdentityProviderOidc } from '../../lib';

describe('UserPoolIdentityProvider', () => {
Expand Down Expand Up @@ -226,5 +226,35 @@ describe('UserPoolIdentityProvider', () => {
ProviderName: 'oidcoidcoidcoidccoidcoidcoidcxyz',
});
});

test('throws with invalid param combination when clientSecret and clientSecretValue are passed', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'userpool');

// THEN
expect(() => new UserPoolIdentityProviderOidc(stack, 'userpoolidp', {
userPool: pool,
name: 'xy',
clientId: 'client-id',
clientSecret: 'client-secret',
clientSecretValue: SecretValue.unsafePlainText('client-secret'),
issuerUrl: 'https://my-issuer-url.com',
})).toThrow(/Exactly one of "clientSecret" or "clientSecretValue" must be configured./);
});

test('throws with invalid param combination when neither clientSecret nor clientSecretValue are passed', () => {
// GIVEN
const stack = new Stack();
const pool = new UserPool(stack, 'userpool');

// THEN
expect(() => new UserPoolIdentityProviderOidc(stack, 'userpoolidp', {
userPool: pool,
name: 'xy',
clientId: 'client-id',
issuerUrl: 'https://my-issuer-url.com',
})).toThrow(/Exactly one of "clientSecret" or "clientSecretValue" must be configured./);
});
});
});
Loading