-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathauthorizations.ts
More file actions
321 lines (276 loc) · 10.5 KB
/
Copy pathauthorizations.ts
File metadata and controls
321 lines (276 loc) · 10.5 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import { LogLevel } from '@cloudcomponents/cdk-lambda-at-edge-pattern';
import { Duration, aws_cloudfront, aws_cognito } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { AuthFlow, RedirectPaths } from './auth-flow';
import { RetrieveUserPoolClientSecret } from './retrieve-user-pool-client-secret';
import { SecretGenerator } from './secret-generator';
import { UserPoolClientRedirects } from './user-pool-client-redirects';
import { UserPoolDomain } from './user-pool-domain';
export interface UserPoolClientCallbackUrls {
/**
* A list of allowed redirect (callback) URLs for the identity providers.
*/
readonly callbackUrls: string[];
/**
* A list of allowed logout URLs for the identity providers.
*/
readonly logoutUrls: string[];
}
export interface IAuthorization {
readonly redirectPaths: RedirectPaths;
readonly signOutUrlPath: string;
updateUserPoolClientCallbacks(redirects: UserPoolClientCallbackUrls): void;
createDefaultBehavior(
origin: aws_cloudfront.IOrigin,
options?: aws_cloudfront.AddBehaviorOptions,
): aws_cloudfront.BehaviorOptions;
createAdditionalBehaviors(
origin: aws_cloudfront.IOrigin,
options?: aws_cloudfront.AddBehaviorOptions,
): Record<string, aws_cloudfront.BehaviorOptions>;
}
export interface AuthorizationProps {
readonly userPool: aws_cognito.IUserPool;
readonly redirectPaths?: RedirectPaths;
readonly signOutUrl?: string;
readonly customHeaders?: aws_cloudfront.ResponseCustomHeader[];
readonly securityHeadersBehavior?: aws_cloudfront.ResponseSecurityHeadersBehavior;
readonly logLevel?: LogLevel;
readonly oauthScopes?: aws_cognito.OAuthScope[];
readonly cookieSettings?: Record<string, string>;
readonly identityProviders?: aws_cognito.UserPoolClientIdentityProvider[];
readonly httpHeaders?: Record<string, string> | undefined;
}
export abstract class Authorization extends Construct {
public readonly redirectPaths: RedirectPaths;
public readonly signOutUrlPath: string;
public readonly authFlow: AuthFlow;
public readonly userPoolClient: aws_cognito.IUserPoolClient;
protected readonly userPool: aws_cognito.IUserPool;
protected readonly oauthScopes: aws_cognito.OAuthScope[];
protected readonly cookieSettings: Record<string, string> | undefined;
protected readonly nonceSigningSecret: string;
protected readonly cognitoAuthDomain: string;
protected readonly identityProviders: aws_cognito.UserPoolClientIdentityProvider[];
protected readonly responseHeaderPolicy: aws_cloudfront.IResponseHeadersPolicy;
protected readonly httpHeaders: Record<string, string> | undefined;
constructor(scope: Construct, id: string, props: AuthorizationProps) {
super(scope, id);
this.userPool = props.userPool;
this.redirectPaths = props.redirectPaths ?? {
signIn: '/parseauth',
authRefresh: '/refreshauth',
signOut: '/',
};
this.signOutUrlPath = props.signOutUrl ?? '/signout';
this.responseHeaderPolicy = new aws_cloudfront.ResponseHeadersPolicy(this, 'ResponseHeadersPolicy', {
securityHeadersBehavior: props.securityHeadersBehavior ?? {
contentSecurityPolicy: {
contentSecurityPolicy:
"default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; object-src 'none'; connect-src 'self'",
override: true,
},
contentTypeOptions: { override: true },
frameOptions: {
frameOption: aws_cloudfront.HeadersFrameOption.DENY,
override: true,
},
referrerPolicy: {
referrerPolicy: aws_cloudfront.HeadersReferrerPolicy.SAME_ORIGIN,
override: true,
},
strictTransportSecurity: {
accessControlMaxAge: Duration.seconds(31536000),
includeSubdomains: true,
preload: true,
override: true,
},
xssProtection: { protection: true, modeBlock: true, override: true },
},
customHeadersBehavior: {
customHeaders: props.customHeaders ?? [
{
header: 'Cache-Control',
value: 'no-cache',
override: true,
},
],
},
});
this.oauthScopes = props.oauthScopes ?? [
aws_cognito.OAuthScope.PHONE,
aws_cognito.OAuthScope.EMAIL,
aws_cognito.OAuthScope.PROFILE,
aws_cognito.OAuthScope.OPENID,
aws_cognito.OAuthScope.COGNITO_ADMIN,
];
this.cookieSettings = props.cookieSettings;
this.identityProviders = props.identityProviders ?? [aws_cognito.UserPoolClientIdentityProvider.COGNITO];
this.userPoolClient = this.createUserPoolClient();
this.nonceSigningSecret = this.generateNonceSigningSecret();
this.cognitoAuthDomain = this.retrieveCognitoAuthDomain();
this.httpHeaders = props.httpHeaders;
this.authFlow = this.createAuthFlow(props.logLevel ?? LogLevel.WARN);
}
protected abstract createUserPoolClient(): aws_cognito.IUserPoolClient;
protected abstract createAuthFlow(logLevel: LogLevel): AuthFlow;
public updateUserPoolClientCallbacks(redirects: UserPoolClientCallbackUrls): void {
const { callbackUrls, logoutUrls } = redirects;
new UserPoolClientRedirects(this, 'UserPoolClientRedirects', {
userPool: this.userPool,
userPoolClient: this.userPoolClient,
oauthScopes: this.oauthScopes,
callbackUrls,
logoutUrls,
identityProviders: this.identityProviders,
});
}
public createDefaultBehavior(
origin: aws_cloudfront.IOrigin,
options?: aws_cloudfront.AddBehaviorOptions,
): aws_cloudfront.BehaviorOptions {
return {
origin,
compress: true,
originRequestPolicy: aws_cloudfront.OriginRequestPolicy.ALL_VIEWER,
viewerProtocolPolicy: aws_cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
edgeLambdas: [this.authFlow.checkAuth],
responseHeadersPolicy: this.responseHeaderPolicy,
...options,
};
}
public createAdditionalBehaviors(
origin: aws_cloudfront.IOrigin,
options?: aws_cloudfront.AddBehaviorOptions,
): Record<string, aws_cloudfront.BehaviorOptions> {
return {
[this.redirectPaths.signIn]: {
origin,
compress: true,
originRequestPolicy: aws_cloudfront.OriginRequestPolicy.ALL_VIEWER,
viewerProtocolPolicy: aws_cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
edgeLambdas: [this.authFlow.parseAuth],
...options,
},
[this.redirectPaths.authRefresh]: {
origin,
compress: true,
viewerProtocolPolicy: aws_cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
edgeLambdas: [this.authFlow.refreshAuth],
...options,
},
[this.signOutUrlPath]: {
origin,
compress: true,
originRequestPolicy: aws_cloudfront.OriginRequestPolicy.ALL_VIEWER,
viewerProtocolPolicy: aws_cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS,
edgeLambdas: [this.authFlow.signOut],
...options,
},
};
}
private generateNonceSigningSecret(): string {
const { secret } = new SecretGenerator(this, 'SecretGenerator');
return secret;
}
private retrieveCognitoAuthDomain(): string {
const userPoolDomain = new UserPoolDomain(this, 'UserPoolDomain', {
userPool: this.userPool,
});
return userPoolDomain.cognitoAuthDomain;
}
}
export interface ISpaAuthorization extends IAuthorization {
readonly mode: Mode.SPA;
}
export type SpaAuthorizationProps = AuthorizationProps;
export class SpaAuthorization extends Authorization implements ISpaAuthorization {
public readonly mode = Mode.SPA;
constructor(scope: Construct, id: string, props: SpaAuthorizationProps) {
super(scope, id, props);
}
protected createUserPoolClient(): aws_cognito.IUserPoolClient {
return this.userPool.addClient('UserPoolClient', {
generateSecret: false,
oAuth: {
flows: {
authorizationCodeGrant: true,
},
scopes: this.oauthScopes,
},
supportedIdentityProviders: this.identityProviders,
preventUserExistenceErrors: true,
});
}
protected createAuthFlow(logLevel: LogLevel): AuthFlow {
return new AuthFlow(this, 'AuthFlow', {
logLevel,
userPool: this.userPool,
userPoolClient: this.userPoolClient,
oauthScopes: this.oauthScopes,
redirectPaths: this.redirectPaths,
nonceSigningSecret: this.nonceSigningSecret,
cognitoAuthDomain: this.cognitoAuthDomain,
cookieSettings: this.cookieSettings ?? {
idToken: 'Path=/; Secure; SameSite=Lax',
accessToken: 'Path=/; Secure; SameSite=Lax',
refreshToken: 'Path=/; Secure; SameSite=Lax',
nonce: 'Path=/; Secure; HttpOnly; SameSite=Lax',
},
});
}
}
export interface IStaticSiteAuthorization extends IAuthorization {
readonly mode: Mode.STATIC_SITE;
}
export type StaticSiteAuthorizationProps = AuthorizationProps;
export class StaticSiteAuthorization extends Authorization implements IStaticSiteAuthorization {
public readonly mode = Mode.STATIC_SITE;
constructor(scope: Construct, id: string, props: StaticSiteAuthorizationProps) {
super(scope, id, props);
}
protected createUserPoolClient(): aws_cognito.IUserPoolClient {
return this.userPool.addClient('UserPoolClient', {
generateSecret: true,
oAuth: {
flows: {
authorizationCodeGrant: true,
},
scopes: this.oauthScopes,
},
supportedIdentityProviders: this.identityProviders,
preventUserExistenceErrors: true,
});
}
protected createAuthFlow(logLevel: LogLevel): AuthFlow {
const clientSecret = this.retrieveUserPoolClientSecret();
return new AuthFlow(this, 'AuthFlow', {
logLevel,
userPool: this.userPool,
userPoolClient: this.userPoolClient,
oauthScopes: this.oauthScopes,
redirectPaths: this.redirectPaths,
nonceSigningSecret: this.nonceSigningSecret,
cognitoAuthDomain: this.cognitoAuthDomain,
clientSecret,
cookieSettings: this.cookieSettings ?? {
idToken: 'Path=/; Secure; HttpOnly; SameSite=Lax',
accessToken: 'Path=/; Secure; HttpOnly; SameSite=Lax',
refreshToken: 'Path=/; Secure; HttpOnly; SameSite=Lax',
nonce: 'Path=/; Secure; HttpOnly; SameSite=Lax',
},
httpHeaders: this.httpHeaders,
});
}
private retrieveUserPoolClientSecret(): string {
const { clientSecret } = new RetrieveUserPoolClientSecret(this, 'RetrieveUserPoolClientSecret', {
userPool: this.userPool,
userPoolClient: this.userPoolClient,
});
return clientSecret;
}
}
export enum Mode {
SPA = 'SPA',
STATIC_SITE = 'STATIC_SITE',
}