-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
162 lines (148 loc) · 4.71 KB
/
index.ts
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
// eslint-disable-next-line @typescript-eslint/no-var-requires
const OktaJwtVerifier = require("@okta/jwt-verifier");
class AuthPolicy {
awsAccountId: string;
principalId: string;
version = "2012-10-17";
pathRegex = new RegExp("^[/.a-zA-Z0-9-*]+$");
allowMethods: Method[] = [];
denyMethods: Method[] = [];
apiOptions: ApiOptions;
constructor(principal: string, awsAccountId: string, apiOptions: ApiOptions) {
this.principalId = principal;
this.awsAccountId = awsAccountId;
this.apiOptions = apiOptions;
}
addMethod(effect: Effect, verb: HttpVerb, resource: string, conditions: Condition[]) {
if (!this.pathRegex.test(resource)) {
throw new Error(`Invalid resource path: ${resource}. Path should match ${this.pathRegex}`);
}
let cleanedResource = resource;
if (resource.startsWith("/")) {
cleanedResource = resource.substring(1, resource.length);
}
const resourceArn =
"arn:aws:execute-api:" +
this.apiOptions.region +
":" +
this.awsAccountId +
":" +
this.apiOptions.restApiId +
"/" +
this.apiOptions.stage +
"/" +
verb +
"/" +
cleanedResource;
if (effect === "Allow") {
this.allowMethods.push({
resourceArn,
conditions,
});
} else if (effect === "Deny") {
this.denyMethods.push({
resourceArn,
conditions,
});
}
}
allowAllMethods() {
this.addMethod("Allow", "*", "*", null);
}
denyAllMethods() {
this.addMethod("Deny", "*", "*", null);
}
allowMethod(verb: HttpVerb, resource: string) {
this.addMethod("Allow", verb, resource, null);
}
allowMethodWithConditions(verb: HttpVerb, resource: string, conditions: Condition[]) {
this.addMethod("Allow", verb, resource, conditions);
}
denyMethod(verb: HttpVerb, resource: string) {
this.addMethod("Deny", verb, resource, null);
}
denyMethodWithConditions(verb: HttpVerb, resource: string, conditions: Condition[]) {
this.addMethod("Deny", verb, resource, conditions);
}
getEmptyStatement(effect: Effect): PolicyStatement {
return {
Action: "execute-api:Invoke",
Effect: effect,
Resource: [],
};
}
getStatementsForEffect(effect: Effect, methods: Method[]): PolicyStatement[] {
const statements: PolicyStatement[] = [];
if (methods && methods.length > 0) {
const statement = this.getEmptyStatement(effect);
for (const method of methods) {
if (method.conditions === null || method.conditions.length === 0) {
statement.Resource.push(method.resourceArn);
} else {
const conditionalStatement = this.getEmptyStatement(effect);
conditionalStatement.Resource.push(method.resourceArn);
conditionalStatement.Condition = method.conditions;
statements.push(conditionalStatement);
}
}
if (statement.Resource.length > 0) {
statements.push(statement);
}
}
return statements;
}
build(claims?: any): Policy {
if (
(!this.allowMethods || this.allowMethods.length == 0) &&
(!this.denyMethods || this.denyMethods.length == 0)
) {
throw new Error("No statements defined for the policy");
}
return {
principalId: this.principalId,
policyDocument: {
Version: this.version,
Statement: [
...this.getStatementsForEffect("Allow", this.allowMethods),
...this.getStatementsForEffect("Deny", this.denyMethods),
],
},
context: {
principalId: this.principalId,
claims: {
sub: claims && claims.sub ? claims.sub : {},
},
},
};
}
}
export const handler = async (event: any = {}, context: any = {}): Promise<any> => {
const token = event.headers.authorization;
const jwtVerifier = new OktaJwtVerifier({
clientId: process.env.OKTA_CLIENTID,
issuer: process.env.OKTA_ISSUER,
});
const tmp = event.routeArn.split(":");
const apiGatewayArnTmp = tmp[5].split("/");
const awsAccountId = tmp[4];
const apiOptions: ApiOptions = {
region: tmp[3],
restApiId: apiGatewayArnTmp[0],
stage: apiGatewayArnTmp[1],
};
try {
const { claims } = await jwtVerifier.verifyAccessToken(token, process.env.OKTA_AUDIENCE);
// const method = apiGatewayArnTmp[2];
// let resource = "/";
// if (apiGatewayArnTmp[3]) {
// resource += apiGatewayArnTmp.slice(3, apiGatewayArnTmp.length).join("/");
// }
const authPolicy = new AuthPolicy(claims.sub, awsAccountId, apiOptions);
authPolicy.allowAllMethods();
return authPolicy.build(claims);
} catch (err) {
const authPolicy = new AuthPolicy(null, null, apiOptions);
authPolicy.denyAllMethods();
return authPolicy.build();
}
};