-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.ts
More file actions
132 lines (122 loc) · 4.56 KB
/
index.ts
File metadata and controls
132 lines (122 loc) · 4.56 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
import { Duration, RemovalPolicy, Stack, StackProps } from "aws-cdk-lib";
import {
AccountRecovery,
CfnUserPoolUser,
Mfa,
UserPool,
UserPoolClient,
} from "aws-cdk-lib/aws-cognito";
import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
import { EmailIdentity, Identity } from "aws-cdk-lib/aws-ses";
import { CfnWebACL, CfnWebACLAssociation } from "aws-cdk-lib/aws-wafv2";
import { Construct } from "constructs";
import * as path from "path";
import { CommonNodejsFunction } from "../../common/constructs/lambda";
import { createManagedRules } from "../../common/utilities";
export default class Auth extends Stack {
public readonly identity: EmailIdentity;
public readonly userPool: UserPool;
public readonly userPoolClient: UserPoolClient;
public readonly regionalWebAclArn: string;
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const identity = new EmailIdentity(this, "identity", {
identity: Identity.email(process.env.EMAIL!),
});
const postConfirmationLambda = new CommonNodejsFunction(this, "postConfirmationLambda", {
entry: path.join(__dirname, "confirm.ts"),
initialPolicy: [
new PolicyStatement({
effect: Effect.ALLOW,
actions: ["ses:GetAccount", "ses:VerifyEmailIdentity"],
resources: ["*"],
}),
],
timeout: Duration.minutes(2),
});
const userPool = new UserPool(this, "userPool", {
removalPolicy: RemovalPolicy.DESTROY,
selfSignUpEnabled: true,
signInAliases: { email: true },
passwordPolicy: {
minLength: 8,
requireLowercase: true,
requireDigits: true,
requireSymbols: true,
requireUppercase: true,
tempPasswordValidity: Duration.days(1),
},
mfa: Mfa.OPTIONAL,
mfaSecondFactor: {
otp: true,
sms: false,
},
accountRecovery: AccountRecovery.EMAIL_ONLY,
// advancedSecurityMode: cognito.AdvancedSecurityMode.ENFORCED,
lambdaTriggers: {
postConfirmation: postConfirmationLambda,
},
});
const userPoolClient = new UserPoolClient(this, "userPoolClient", {
userPool,
preventUserExistenceErrors: true,
authFlows: {
userPassword: true,
userSrp: true,
},
});
new CfnUserPoolUser(this, "userPoolUser", {
userPoolId: userPool.userPoolId,
username: identity.emailIdentityName,
});
const regionalWebAcl = new CfnWebACL(this, "regionalWebAcl", {
defaultAction: { allow: {} },
scope: "REGIONAL",
visibilityConfig: {
metricName: "regionalWebAcl",
sampledRequestsEnabled: true,
cloudWatchMetricsEnabled: true,
},
rules: [
{
name: "ipRateLimitingRule",
priority: 0,
statement: {
rateBasedStatement: {
limit: 3000,
aggregateKeyType: "IP",
},
},
action: {
block: {},
},
visibilityConfig: {
sampledRequestsEnabled: true,
cloudWatchMetricsEnabled: true,
metricName: "ipRateLimitingRule",
},
},
...createManagedRules("regional", 1, [
{
name: "AWSManagedRulesCommonRuleSet",
},
{
name: "AWSManagedRulesKnownBadInputsRuleSet",
},
{
name: "AWSManagedRulesAmazonIpReputationList",
},
]),
],
});
const regionalWebAclArn = regionalWebAcl.attrArn;
new CfnWebACLAssociation(this, "userPoolWebAclAssociation", {
resourceArn: userPool.userPoolArn,
webAclArn: regionalWebAclArn,
});
this.identity = identity;
this.userPool = userPool;
this.userPoolClient = userPoolClient;
this.regionalWebAclArn = regionalWebAclArn;
}
}