@infoxchange/make-it-so 2.11.0-internal-testing-vdt-199-add-auth-token-verify-function-2.7 → 2.11.0-internal-testing-vdt-199-add-auth-token-verify-function-3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cdk-constructs/SiteOidcAuth/auth-check-handler-body.js +11 -5
- package/dist/cdk-constructs/SiteOidcAuth/index.d.ts.map +1 -1
- package/dist/cdk-constructs/SiteOidcAuth/index.js +2 -5
- package/package.json +1 -1
- package/src/cdk-constructs/SiteOidcAuth/auth-check-handler-body.ts +23 -13
- package/src/cdk-constructs/SiteOidcAuth/index.ts +16 -17
|
@@ -9,6 +9,7 @@ const jwtSecret = "__placeholder-for-jwt-secret__";
|
|
|
9
9
|
const authRoutePrefix = "__placeholder-for-auth-route-prefix__";
|
|
10
10
|
// Set to true to enable console logging
|
|
11
11
|
const loggingEnabled = false;
|
|
12
|
+
// Simple logger that can be enabled/disabled via the loggingEnabled variable.
|
|
12
13
|
const log = function () {
|
|
13
14
|
if (!loggingEnabled)
|
|
14
15
|
return;
|
|
@@ -32,6 +33,7 @@ const redirectResponse = {
|
|
|
32
33
|
location: { value: `${authRoutePrefix}/oidc/authorize` },
|
|
33
34
|
},
|
|
34
35
|
};
|
|
36
|
+
// Takes a JWT token to decode and throws an error if invalid
|
|
35
37
|
function jwtDecode(token, key, noVerify) {
|
|
36
38
|
// check segments
|
|
37
39
|
const segments = token.split(".");
|
|
@@ -75,6 +77,7 @@ function _constantTimeEquals(a, b) {
|
|
|
75
77
|
}
|
|
76
78
|
return 0 === xor;
|
|
77
79
|
}
|
|
80
|
+
// Verifies some input matches an expected signature.
|
|
78
81
|
function _verify(input, key, method, type, signature) {
|
|
79
82
|
if (type === "hmac") {
|
|
80
83
|
return _constantTimeEquals(signature, _sign(input, key, method));
|
|
@@ -83,15 +86,18 @@ function _verify(input, key, method, type, signature) {
|
|
|
83
86
|
throw new Error("Algorithm type not recognized");
|
|
84
87
|
}
|
|
85
88
|
}
|
|
89
|
+
// Signs some input with a key and method.
|
|
86
90
|
function _sign(input, key, method) {
|
|
87
91
|
return crypto.createHmac(method, key).update(input).digest("base64url");
|
|
88
92
|
}
|
|
93
|
+
// Very annoying that we have to implement this ourselves but it seems like the v1 runtime does not have atob/btoa or
|
|
94
|
+
// Buffer available.
|
|
89
95
|
function _base64urlDecode(str) {
|
|
90
|
-
str = str.replace(/-/g,
|
|
96
|
+
str = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
91
97
|
while (str.length % 4)
|
|
92
|
-
str +=
|
|
93
|
-
const chars =
|
|
94
|
-
let output =
|
|
98
|
+
str += "=";
|
|
99
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
100
|
+
let output = "";
|
|
95
101
|
let bc = 0, bs = 0, buffer, i = 0;
|
|
96
102
|
for (; i < str.length; i++) {
|
|
97
103
|
buffer = chars.indexOf(str.charAt(i));
|
|
@@ -101,7 +107,7 @@ function _base64urlDecode(str) {
|
|
|
101
107
|
bc += 6;
|
|
102
108
|
if (bc >= 8) {
|
|
103
109
|
bc -= 8;
|
|
104
|
-
output += String.fromCharCode((bs >> bc) &
|
|
110
|
+
output += String.fromCharCode((bs >> bc) & 0xff);
|
|
105
111
|
}
|
|
106
112
|
}
|
|
107
113
|
return output;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/cdk-constructs/SiteOidcAuth/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,UAAU,MAAM,4BAA4B,CAAC;AACpD,OAAO,GAAG,MAAM,aAAa,CAAC;AAE9B,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AAOtC,OAAO,KAAK,EACV,uBAAuB,EACvB,uBAAuB,EACxB,MAAM,2BAA2B,CAAC;AAEnC,KAAK,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AAE,SAAS,EACpB,EAAE,MAA+B,EAAE,GAAE,cAAmB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAuC1D,OAAO,CAAC,eAAe;IAgBvB,OAAO,CAAC,sBAAsB;IA8B9B,OAAO,CAAC,2BAA2B;IAsBnC,OAAO,CAAC,yCAAyC;IAgBjD,OAAO,CAAC,sBAAsB;CAkF/B"}
|
|
@@ -94,7 +94,6 @@ export class SiteOidcAuth extends Construct {
|
|
|
94
94
|
// Get the CloudFront Function Association for auth checking
|
|
95
95
|
// Roughly based off https://github.com/sst/v2/blob/4283d706f251724308b397996ff307929bf3a976/packages/sst/src/constructs/SsrSite.ts#L941
|
|
96
96
|
getFunctionAssociation(scope, jwtSecret, authRoutePrefix) {
|
|
97
|
-
console.log("---- 1");
|
|
98
97
|
const authCheckFunction = new CloudFront.Function(scope, `${this.id}AuthCheckFunction`, {
|
|
99
98
|
code: CloudFront.FunctionCode.fromInline(this.convertToCloudFrontFunctionCompatibleCode(`function handler(event) {
|
|
100
99
|
var request = event.request;
|
|
@@ -104,21 +103,20 @@ export class SiteOidcAuth extends Construct {
|
|
|
104
103
|
// We could specify the JS v2.0 runtime here but for SSR sites SST does the function creation and that currently
|
|
105
104
|
// uses JS v1.0 so no point using v2.0 here as the code has to be compatible with v1.0 anyway.
|
|
106
105
|
});
|
|
107
|
-
console.log("---- 2");
|
|
108
106
|
return {
|
|
109
107
|
function: authCheckFunction,
|
|
110
108
|
eventType: CloudFront.FunctionEventType.VIEWER_REQUEST,
|
|
111
109
|
};
|
|
112
110
|
}
|
|
113
111
|
getAuthCheckHandlerBodyCode(jwtSecret, authRoutePrefix) {
|
|
114
|
-
return fs
|
|
112
|
+
return (fs
|
|
115
113
|
.readFileSync(path.join(import.meta.dirname, "auth-check-handler-body.js"), "utf8")
|
|
116
114
|
.replace("__placeholder-for-jwt-secret__", jwtSecret.secretValue.toString())
|
|
117
115
|
.replace("__placeholder-for-auth-route-prefix__", authRoutePrefix)
|
|
118
116
|
// When typescript builds the make-it-so code including "auth-check-handler-body.ts" it will add "export {}" to
|
|
119
117
|
// the end of the file if it's not already a module. This will cause a syntax error in CloudFront Functions so we
|
|
120
118
|
// remove it here.
|
|
121
|
-
.replace(/export {};\s*$/g, "");
|
|
119
|
+
.replace(/export {};\s*$/g, ""));
|
|
122
120
|
}
|
|
123
121
|
convertToCloudFrontFunctionCompatibleCode(sourceCode, esbuildOptions) {
|
|
124
122
|
// ESBuild doesn't currently support transforming const/let to var, which is required for CloudFront Functions
|
|
@@ -126,7 +124,6 @@ export class SiteOidcAuth extends Construct {
|
|
|
126
124
|
sourceCode = sourceCode
|
|
127
125
|
.replaceAll(/const /g, "var ")
|
|
128
126
|
.replaceAll(/let /g, "var ");
|
|
129
|
-
console.log("---- 3", sourceCode);
|
|
130
127
|
return transformSync(sourceCode, {
|
|
131
128
|
target: "es5",
|
|
132
129
|
...esbuildOptions,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@infoxchange/make-it-so",
|
|
3
|
-
"version": "2.11.0-internal-testing-vdt-199-add-auth-token-verify-function-2
|
|
3
|
+
"version": "2.11.0-internal-testing-vdt-199-add-auth-token-verify-function-3.2",
|
|
4
4
|
"description": "Makes deploying services to IX infra easy",
|
|
5
5
|
"repository": "github:infoxchange/make-it-so",
|
|
6
6
|
"type": "module",
|
|
@@ -15,6 +15,7 @@ const authRoutePrefix = "__placeholder-for-auth-route-prefix__";
|
|
|
15
15
|
// Set to true to enable console logging
|
|
16
16
|
const loggingEnabled = false;
|
|
17
17
|
|
|
18
|
+
// Simple logger that can be enabled/disabled via the loggingEnabled variable.
|
|
18
19
|
const log: typeof console.log = function () {
|
|
19
20
|
if (!loggingEnabled) return;
|
|
20
21
|
|
|
@@ -41,6 +42,7 @@ const redirectResponse = {
|
|
|
41
42
|
},
|
|
42
43
|
};
|
|
43
44
|
|
|
45
|
+
// Takes a JWT token to decode and throws an error if invalid
|
|
44
46
|
function jwtDecode(token: string, key: string, noVerify?: boolean) {
|
|
45
47
|
// check segments
|
|
46
48
|
const segments = token.split(".");
|
|
@@ -97,6 +99,7 @@ function _constantTimeEquals(a: string, b: string) {
|
|
|
97
99
|
return 0 === xor;
|
|
98
100
|
}
|
|
99
101
|
|
|
102
|
+
// Verifies some input matches an expected signature.
|
|
100
103
|
function _verify(
|
|
101
104
|
input: string,
|
|
102
105
|
key: string,
|
|
@@ -111,29 +114,36 @@ function _verify(
|
|
|
111
114
|
}
|
|
112
115
|
}
|
|
113
116
|
|
|
117
|
+
// Signs some input with a key and method.
|
|
114
118
|
function _sign(input: string, key: string, method: string) {
|
|
115
119
|
return crypto.createHmac(method, key).update(input).digest("base64url");
|
|
116
120
|
}
|
|
117
121
|
|
|
122
|
+
// Very annoying that we have to implement this ourselves but it seems like the v1 runtime does not have atob/btoa or
|
|
123
|
+
// Buffer available.
|
|
118
124
|
function _base64urlDecode(str: string) {
|
|
119
|
-
str = str.replace(/-/g,
|
|
120
|
-
while (str.length % 4) str +=
|
|
125
|
+
str = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
126
|
+
while (str.length % 4) str += "=";
|
|
121
127
|
|
|
122
|
-
const chars =
|
|
123
|
-
|
|
128
|
+
const chars =
|
|
129
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
130
|
+
let output = "";
|
|
124
131
|
|
|
125
|
-
let bc = 0,
|
|
132
|
+
let bc = 0,
|
|
133
|
+
bs = 0,
|
|
134
|
+
buffer,
|
|
135
|
+
i = 0;
|
|
126
136
|
for (; i < str.length; i++) {
|
|
127
|
-
|
|
128
|
-
|
|
137
|
+
buffer = chars.indexOf(str.charAt(i));
|
|
138
|
+
if (buffer === -1) continue;
|
|
129
139
|
|
|
130
|
-
|
|
131
|
-
|
|
140
|
+
bs = (bs << 6) | buffer;
|
|
141
|
+
bc += 6;
|
|
132
142
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
143
|
+
if (bc >= 8) {
|
|
144
|
+
bc -= 8;
|
|
145
|
+
output += String.fromCharCode((bs >> bc) & 0xff);
|
|
146
|
+
}
|
|
137
147
|
}
|
|
138
148
|
|
|
139
149
|
return output;
|
|
@@ -151,7 +151,6 @@ export class SiteOidcAuth extends Construct {
|
|
|
151
151
|
jwtSecret: SecretsManager.Secret,
|
|
152
152
|
authRoutePrefix: string,
|
|
153
153
|
): CloudFront.FunctionAssociation {
|
|
154
|
-
console.log("---- 1")
|
|
155
154
|
const authCheckFunction = new CloudFront.Function(
|
|
156
155
|
scope,
|
|
157
156
|
`${this.id}AuthCheckFunction`,
|
|
@@ -170,7 +169,6 @@ export class SiteOidcAuth extends Construct {
|
|
|
170
169
|
// uses JS v1.0 so no point using v2.0 here as the code has to be compatible with v1.0 anyway.
|
|
171
170
|
},
|
|
172
171
|
);
|
|
173
|
-
console.log("---- 2")
|
|
174
172
|
|
|
175
173
|
return {
|
|
176
174
|
function: authCheckFunction,
|
|
@@ -182,20 +180,22 @@ export class SiteOidcAuth extends Construct {
|
|
|
182
180
|
jwtSecret: SecretsManager.Secret,
|
|
183
181
|
authRoutePrefix: string,
|
|
184
182
|
): string {
|
|
185
|
-
return
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
183
|
+
return (
|
|
184
|
+
fs
|
|
185
|
+
.readFileSync(
|
|
186
|
+
path.join(import.meta.dirname, "auth-check-handler-body.js"),
|
|
187
|
+
"utf8",
|
|
188
|
+
)
|
|
189
|
+
.replace(
|
|
190
|
+
"__placeholder-for-jwt-secret__",
|
|
191
|
+
jwtSecret.secretValue.toString(),
|
|
192
|
+
)
|
|
193
|
+
.replace("__placeholder-for-auth-route-prefix__", authRoutePrefix)
|
|
194
|
+
// When typescript builds the make-it-so code including "auth-check-handler-body.ts" it will add "export {}" to
|
|
195
|
+
// the end of the file if it's not already a module. This will cause a syntax error in CloudFront Functions so we
|
|
196
|
+
// remove it here.
|
|
197
|
+
.replace(/export {};\s*$/g, "")
|
|
198
|
+
);
|
|
199
199
|
}
|
|
200
200
|
|
|
201
201
|
private convertToCloudFrontFunctionCompatibleCode(
|
|
@@ -207,7 +207,6 @@ export class SiteOidcAuth extends Construct {
|
|
|
207
207
|
sourceCode = sourceCode
|
|
208
208
|
.replaceAll(/const /g, "var ")
|
|
209
209
|
.replaceAll(/let /g, "var ");
|
|
210
|
-
console.log("---- 3", sourceCode)
|
|
211
210
|
return transformSync(sourceCode, {
|
|
212
211
|
target: "es5",
|
|
213
212
|
...esbuildOptions,
|