-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathindex.ts
More file actions
160 lines (145 loc) · 5.72 KB
/
index.ts
File metadata and controls
160 lines (145 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { CfnOutput, CfnResource, CustomResource, Duration, RemovalPolicy, Stack } from 'aws-cdk-lib';
import { ICertificate } from 'aws-cdk-lib/aws-certificatemanager';
import { CfnManagedLoginBranding, ManagedLoginVersion, UserPool, UserPoolClient } from 'aws-cdk-lib/aws-cognito';
import { Code, Runtime, SingletonFunction } from 'aws-cdk-lib/aws-lambda';
import { CnameRecord, IHostedZone } from 'aws-cdk-lib/aws-route53';
import { AwsCustomResource, AwsCustomResourcePolicy, PhysicalResourceId } from 'aws-cdk-lib/custom-resources';
import { Construct } from 'constructs';
import { readFileSync } from 'fs';
import { join } from 'path';
export interface AuthProps {
readonly hostedZone?: IHostedZone;
readonly sharedCertificate?: ICertificate;
}
export class Auth extends Construct {
readonly userPool: UserPool;
readonly client: UserPoolClient;
readonly domainName: string;
private callbackUrlCount = 0;
constructor(scope: Construct, id: string, props: AuthProps) {
super(scope, id);
const { hostedZone } = props;
const subDomain = 'auth';
let domainPrefix = '';
if (!hostedZone) {
// When we do not use a custom domain, we must make domainPrefix unique in the AWS region.
// To avoid a collision, we generate a random string with CFn custom resource.
const generator = new SingletonFunction(this, 'RandomStringGenerator', {
runtime: Runtime.NODEJS_22_X,
handler: 'index.handler',
timeout: Duration.seconds(5),
lambdaPurpose: 'RandomStringGenerator',
uuid: '11e9c903-f11a-4989-833c-985dddef5eb2',
code: Code.fromInline(readFileSync(join(__dirname, 'prefix-generator.js')).toString()),
});
const domainPrefixResource = new CustomResource(this, 'DomainPrefix', {
serviceToken: generator.functionArn,
resourceType: 'Custom::RandomString',
properties: { prefix: 'webapp-', length: 10 },
serviceTimeout: Duration.seconds(10),
});
domainPrefix = domainPrefixResource.getAttString('generated');
}
this.domainName = hostedZone
? `${subDomain}.${hostedZone.zoneName}`
: `${domainPrefix}.auth.${Stack.of(this).region}.amazoncognito.com`;
const userPool = new UserPool(this, 'UserPool', {
passwordPolicy: {
requireUppercase: true,
requireSymbols: true,
requireDigits: true,
minLength: 8,
},
selfSignUpEnabled: true,
signInAliases: {
username: false,
email: true,
},
removalPolicy: RemovalPolicy.DESTROY,
});
const client = userPool.addClient(`Client`, {
idTokenValidity: Duration.days(1),
authFlows: {
userPassword: true,
userSrp: true,
},
oAuth: {
flows: {
authorizationCodeGrant: true,
},
callbackUrls: ['http://localhost/dummy'],
logoutUrls: ['http://localhost/dummy'],
},
});
this.client = client;
this.userPool = userPool;
const domain = userPool.addDomain('CognitoDomain', {
...(hostedZone && props.sharedCertificate
? {
customDomain: {
domainName: this.domainName,
certificate: props.sharedCertificate,
},
}
: {
cognitoDomain: {
domainPrefix,
},
}),
managedLoginVersion: ManagedLoginVersion.NEWER_MANAGED_LOGIN,
});
if (hostedZone) {
new CnameRecord(this, 'CognitoDomainRecord', {
zone: hostedZone,
recordName: subDomain,
domainName: domain.cloudFrontEndpoint,
});
}
new CfnManagedLoginBranding(this, 'Branding', {
userPoolId: this.userPool.userPoolId,
clientId: client.userPoolClientId,
useCognitoProvidedValues: true,
});
new CfnOutput(this, 'UserPoolId', { value: userPool.userPoolId });
new CfnOutput(this, 'UserPoolClientId', { value: client.userPoolClientId });
}
public addAllowedCallbackUrls(callbackUrl: string, logoutUrl: string) {
const resource = this.client.node.defaultChild;
if (!CfnResource.isCfnResource(resource)) {
throw new Error('Expected CfnResource');
}
resource.addPropertyOverride(`CallbackURLs.${this.callbackUrlCount}`, callbackUrl);
resource.addPropertyOverride(`LogoutURLs.${this.callbackUrlCount}`, logoutUrl);
this.callbackUrlCount += 1;
}
public updateAllowedCallbackUrls(callbackUrls: string[], logoutUrls: string[]) {
// Lambda depends on userPoolClientId but userPoolClient depends on the CloudFront domain name (callback URL) which depends on Lambda (fURL).
// To avoid the circular dependency, we update the callback URL after a userPoolClientId is created.
// We only use this when custom domain is not used.
new AwsCustomResource(this, 'UpdateCallbackUrls', {
onUpdate: {
service: '@aws-sdk/client-cognito-identity-provider',
action: 'updateUserPoolClient',
parameters: {
ClientId: this.client.userPoolClientId,
UserPoolId: this.userPool.userPoolId,
AllowedOAuthFlows: ['code'],
AllowedOAuthFlowsUserPoolClient: true,
AllowedOAuthScopes: ['profile', 'phone', 'email', 'openid', 'aws.cognito.signin.user.admin'],
ExplicitAuthFlows: ['ALLOW_USER_PASSWORD_AUTH', 'ALLOW_USER_SRP_AUTH', 'ALLOW_REFRESH_TOKEN_AUTH'],
CallbackURLs: callbackUrls,
LogoutURLs: logoutUrls,
SupportedIdentityProviders: ['COGNITO'],
TokenValidityUnits: {
IdToken: 'minutes',
},
IdTokenValidity: 1440,
},
physicalResourceId: PhysicalResourceId.of(this.userPool.userPoolId),
},
policy: AwsCustomResourcePolicy.fromSdkCalls({
resources: [this.userPool.userPoolArn],
}),
});
}
}