@aura-stack/auth 0.4.0 → 0.6.0
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/README.md +36 -1
- package/dist/@types/index.cjs +0 -18
- package/dist/@types/index.d.ts +2 -8
- package/dist/@types/index.js +0 -1
- package/dist/assert-B3iQSYlK.js +3 -0
- package/dist/assert-NJGroSJd.cjs +3 -0
- package/dist/client/index.cjs +1 -0
- package/dist/client/index.d.ts +11 -0
- package/dist/client/index.js +1 -0
- package/dist/crypto-Bz8nIciY.js +1 -0
- package/dist/crypto-CoXA5w_4.cjs +1 -0
- package/dist/env-bq387KyP.cjs +1 -0
- package/dist/env-nvh8QBNz.js +1 -0
- package/dist/errors-CCYPHuBO.cjs +1 -0
- package/dist/errors-DFWHOho6.js +1 -0
- package/dist/index-BkpwQ0l4.d.cts +2279 -0
- package/dist/index-nqLV2t91.d.ts +2279 -0
- package/dist/index.cjs +1 -1839
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -35
- package/dist/index.js +1 -132
- package/dist/logger-C59_CDMk.js +1 -0
- package/dist/logger-UnUhYL2V.cjs +1 -0
- package/dist/oauth/atlassian.cjs +1 -0
- package/dist/oauth/atlassian.d.ts +2 -0
- package/dist/oauth/atlassian.js +1 -0
- package/dist/oauth/bitbucket.cjs +1 -49
- package/dist/oauth/bitbucket.d.ts +2 -8
- package/dist/oauth/bitbucket.js +1 -6
- package/dist/oauth/discord.cjs +1 -57
- package/dist/oauth/discord.d.ts +2 -8
- package/dist/oauth/discord.js +1 -6
- package/dist/oauth/dropbox.cjs +1 -0
- package/dist/oauth/dropbox.d.ts +2 -0
- package/dist/oauth/dropbox.js +1 -0
- package/dist/oauth/figma.cjs +1 -49
- package/dist/oauth/figma.d.ts +2 -8
- package/dist/oauth/figma.js +1 -6
- package/dist/oauth/github.cjs +1 -49
- package/dist/oauth/github.d.ts +2 -8
- package/dist/oauth/github.js +1 -6
- package/dist/oauth/gitlab.cjs +1 -49
- package/dist/oauth/gitlab.d.ts +2 -8
- package/dist/oauth/gitlab.js +1 -6
- package/dist/oauth/index.cjs +1 -483
- package/dist/oauth/index.d.ts +2 -8
- package/dist/oauth/index.js +1 -52
- package/dist/oauth/mailchimp.cjs +1 -49
- package/dist/oauth/mailchimp.d.ts +2 -8
- package/dist/oauth/mailchimp.js +1 -6
- package/dist/oauth/notion.cjs +1 -0
- package/dist/oauth/notion.d.ts +2 -0
- package/dist/oauth/notion.js +1 -0
- package/dist/oauth/pinterest.cjs +1 -49
- package/dist/oauth/pinterest.d.ts +2 -8
- package/dist/oauth/pinterest.js +1 -6
- package/dist/oauth/spotify.cjs +1 -49
- package/dist/oauth/spotify.d.ts +2 -8
- package/dist/oauth/spotify.js +1 -6
- package/dist/oauth/strava.cjs +1 -49
- package/dist/oauth/strava.d.ts +2 -8
- package/dist/oauth/strava.js +1 -6
- package/dist/oauth/twitch.cjs +1 -0
- package/dist/oauth/twitch.d.ts +2 -0
- package/dist/oauth/twitch.js +1 -0
- package/dist/oauth/x.cjs +1 -49
- package/dist/oauth/x.d.ts +2 -8
- package/dist/oauth/x.js +1 -6
- package/dist/oauth-BntNm6aE.cjs +1 -0
- package/dist/oauth-DmHy9VrB.js +1 -0
- package/dist/shared/crypto.cjs +1 -0
- package/dist/shared/crypto.d.ts +47 -0
- package/dist/shared/crypto.js +1 -0
- package/dist/shared/identity.cjs +1 -0
- package/dist/shared/identity.d.ts +2 -0
- package/dist/shared/identity.js +1 -0
- package/dist/shared/index.cjs +1 -0
- package/dist/shared/index.d.ts +5 -0
- package/dist/shared/index.js +1 -0
- package/package.json +39 -12
- package/dist/@types/router.d.cjs +0 -1
- package/dist/@types/router.d.d.ts +0 -12
- package/dist/@types/router.d.js +0 -0
- package/dist/@types/utility.cjs +0 -18
- package/dist/@types/utility.d.ts +0 -6
- package/dist/@types/utility.js +0 -1
- package/dist/actions/callback/access-token.cjs +0 -206
- package/dist/actions/callback/access-token.d.ts +0 -29
- package/dist/actions/callback/access-token.js +0 -9
- package/dist/actions/callback/callback.cjs +0 -649
- package/dist/actions/callback/callback.d.ts +0 -13
- package/dist/actions/callback/callback.js +0 -19
- package/dist/actions/callback/userinfo.cjs +0 -250
- package/dist/actions/callback/userinfo.d.ts +0 -21
- package/dist/actions/callback/userinfo.js +0 -14
- package/dist/actions/csrfToken/csrfToken.cjs +0 -197
- package/dist/actions/csrfToken/csrfToken.d.ts +0 -5
- package/dist/actions/csrfToken/csrfToken.js +0 -14
- package/dist/actions/index.cjs +0 -954
- package/dist/actions/index.d.ts +0 -14
- package/dist/actions/index.js +0 -36
- package/dist/actions/session/session.cjs +0 -136
- package/dist/actions/session/session.d.ts +0 -5
- package/dist/actions/session/session.js +0 -10
- package/dist/actions/signIn/authorization.cjs +0 -322
- package/dist/actions/signIn/authorization.d.ts +0 -53
- package/dist/actions/signIn/authorization.js +0 -18
- package/dist/actions/signIn/signIn.cjs +0 -467
- package/dist/actions/signIn/signIn.d.ts +0 -13
- package/dist/actions/signIn/signIn.js +0 -15
- package/dist/actions/signOut/signOut.cjs +0 -493
- package/dist/actions/signOut/signOut.d.ts +0 -8
- package/dist/actions/signOut/signOut.js +0 -16
- package/dist/assert.cjs +0 -161
- package/dist/assert.d.ts +0 -33
- package/dist/assert.js +0 -26
- package/dist/chunk-4EKY7655.js +0 -123
- package/dist/chunk-4MYWAOLG.js +0 -31
- package/dist/chunk-4YHJ4IEQ.js +0 -25
- package/dist/chunk-54CZPKR4.js +0 -25
- package/dist/chunk-5LZ7TOM3.js +0 -25
- package/dist/chunk-5W4BRQYG.js +0 -201
- package/dist/chunk-6MXFPFR3.js +0 -143
- package/dist/chunk-7QF22LHP.js +0 -67
- package/dist/chunk-ALG3GIV4.js +0 -95
- package/dist/chunk-E6G5YCI6.js +0 -25
- package/dist/chunk-EBAMFRB7.js +0 -34
- package/dist/chunk-EEE7UM5T.js +0 -25
- package/dist/chunk-FRJFWTOY.js +0 -70
- package/dist/chunk-FW4W3REU.js +0 -25
- package/dist/chunk-ICAZ4OVS.js +0 -37
- package/dist/chunk-IPKO6UQN.js +0 -25
- package/dist/chunk-ITQ7352M.js +0 -0
- package/dist/chunk-KJBAQZX2.js +0 -92
- package/dist/chunk-KMMAZFSJ.js +0 -25
- package/dist/chunk-LDU7A2JE.js +0 -25
- package/dist/chunk-NUDITUKX.js +0 -73
- package/dist/chunk-OVHNRULD.js +0 -33
- package/dist/chunk-PG7UYFG5.js +0 -0
- package/dist/chunk-PHFH2MGS.js +0 -36
- package/dist/chunk-QQVSRXGX.js +0 -149
- package/dist/chunk-RRLIF4PQ.js +0 -55
- package/dist/chunk-TM5IPSNF.js +0 -113
- package/dist/chunk-TZB6MUXN.js +0 -78
- package/dist/chunk-VNCNJKS2.js +0 -267
- package/dist/chunk-XGLBNXL4.js +0 -75
- package/dist/chunk-XUP6KKNG.js +0 -106
- package/dist/chunk-ZNCZVF6U.js +0 -14
- package/dist/cookie.cjs +0 -246
- package/dist/cookie.d.ts +0 -85
- package/dist/cookie.js +0 -29
- package/dist/env.cjs +0 -56
- package/dist/env.d.ts +0 -7
- package/dist/env.js +0 -6
- package/dist/errors.cjs +0 -85
- package/dist/errors.d.ts +0 -50
- package/dist/errors.js +0 -18
- package/dist/headers.cjs +0 -61
- package/dist/headers.d.ts +0 -33
- package/dist/headers.js +0 -12
- package/dist/index-CSyIJmCM.d.ts +0 -1007
- package/dist/jose.cjs +0 -128
- package/dist/jose.d.ts +0 -25
- package/dist/jose.js +0 -12
- package/dist/logger.cjs +0 -292
- package/dist/logger.d.ts +0 -8
- package/dist/logger.js +0 -8
- package/dist/request.cjs +0 -38
- package/dist/request.d.ts +0 -13
- package/dist/request.js +0 -6
- package/dist/schemas.cjs +0 -125
- package/dist/schemas.d.ts +0 -149
- package/dist/schemas.js +0 -24
- package/dist/secure.cjs +0 -170
- package/dist/secure.d.ts +0 -35
- package/dist/secure.js +0 -19
- package/dist/utils.cjs +0 -223
- package/dist/utils.d.ts +0 -24
- package/dist/utils.js +0 -29
package/dist/actions/index.cjs
DELETED
|
@@ -1,954 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/actions/index.ts
|
|
31
|
-
var actions_exports = {};
|
|
32
|
-
__export(actions_exports, {
|
|
33
|
-
callbackAction: () => callbackAction,
|
|
34
|
-
csrfTokenAction: () => csrfTokenAction,
|
|
35
|
-
sessionAction: () => sessionAction,
|
|
36
|
-
signInAction: () => signInAction,
|
|
37
|
-
signOutAction: () => signOutAction
|
|
38
|
-
});
|
|
39
|
-
module.exports = __toCommonJS(actions_exports);
|
|
40
|
-
|
|
41
|
-
// src/actions/signIn/signIn.ts
|
|
42
|
-
var import_zod2 = require("zod");
|
|
43
|
-
var import_router2 = require("@aura-stack/router");
|
|
44
|
-
|
|
45
|
-
// src/headers.ts
|
|
46
|
-
var cacheControl = {
|
|
47
|
-
"Cache-Control": "no-store",
|
|
48
|
-
Pragma: "no-cache",
|
|
49
|
-
Expires: "0",
|
|
50
|
-
Vary: "Cookie"
|
|
51
|
-
};
|
|
52
|
-
var contentSecurityPolicy = {
|
|
53
|
-
"Content-Security-Policy": [
|
|
54
|
-
"default-src 'none'",
|
|
55
|
-
"script-src 'self'",
|
|
56
|
-
"frame-src 'none'",
|
|
57
|
-
"object-src 'none'",
|
|
58
|
-
"frame-ancestors 'none'",
|
|
59
|
-
"base-uri 'none'"
|
|
60
|
-
].join("; ")
|
|
61
|
-
};
|
|
62
|
-
var secureHeaders = {
|
|
63
|
-
"X-Content-Type-Options": "nosniff",
|
|
64
|
-
"X-Frame-Options": "DENY",
|
|
65
|
-
"Referrer-Policy": "strict-origin-when-cross-origin"
|
|
66
|
-
};
|
|
67
|
-
var secureApiHeaders = {
|
|
68
|
-
...cacheControl,
|
|
69
|
-
...contentSecurityPolicy,
|
|
70
|
-
...secureHeaders
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
// src/secure.ts
|
|
74
|
-
var import_crypto2 = __toESM(require("crypto"), 1);
|
|
75
|
-
|
|
76
|
-
// src/utils.ts
|
|
77
|
-
var import_router = require("@aura-stack/router");
|
|
78
|
-
|
|
79
|
-
// src/errors.ts
|
|
80
|
-
var OAuthProtocolError = class extends Error {
|
|
81
|
-
type = "OAUTH_PROTOCOL_ERROR";
|
|
82
|
-
error;
|
|
83
|
-
errorURI;
|
|
84
|
-
constructor(error, description, errorURI, options2) {
|
|
85
|
-
super(description, options2);
|
|
86
|
-
this.error = error;
|
|
87
|
-
this.errorURI = errorURI;
|
|
88
|
-
this.name = new.target.name;
|
|
89
|
-
Error.captureStackTrace(this, new.target);
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
var AuthInternalError = class extends Error {
|
|
93
|
-
type = "AUTH_INTERNAL_ERROR";
|
|
94
|
-
code;
|
|
95
|
-
constructor(code, message, options2) {
|
|
96
|
-
super(message, options2);
|
|
97
|
-
this.code = code;
|
|
98
|
-
this.name = new.target.name;
|
|
99
|
-
Error.captureStackTrace(this, new.target);
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
var AuthSecurityError = class extends Error {
|
|
103
|
-
type = "AUTH_SECURITY_ERROR";
|
|
104
|
-
code;
|
|
105
|
-
constructor(code, message, options2) {
|
|
106
|
-
super(message, options2);
|
|
107
|
-
this.code = code;
|
|
108
|
-
this.name = new.target.name;
|
|
109
|
-
Error.captureStackTrace(this, new.target);
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
var isNativeError = (error) => {
|
|
113
|
-
return error instanceof Error;
|
|
114
|
-
};
|
|
115
|
-
var isOAuthProtocolError = (error) => {
|
|
116
|
-
return error instanceof OAuthProtocolError;
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
// src/utils.ts
|
|
120
|
-
var toSnakeCase = (str) => {
|
|
121
|
-
return str.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2").toLowerCase().replace(/^_+/, "");
|
|
122
|
-
};
|
|
123
|
-
var toUpperCase = (str) => {
|
|
124
|
-
return str.toUpperCase();
|
|
125
|
-
};
|
|
126
|
-
var toCastCase = (obj, type = "snake") => {
|
|
127
|
-
return Object.entries(obj).reduce((previous, [key, value]) => {
|
|
128
|
-
const newKey = type === "snake" ? toSnakeCase(key) : toUpperCase(key);
|
|
129
|
-
return { ...previous, [newKey]: value };
|
|
130
|
-
}, {});
|
|
131
|
-
};
|
|
132
|
-
var equals = (a, b) => {
|
|
133
|
-
if (a === null || b === null || a === void 0 || b === void 0) return false;
|
|
134
|
-
return a === b;
|
|
135
|
-
};
|
|
136
|
-
var getBaseURL = (request) => {
|
|
137
|
-
const url = new URL(request.url);
|
|
138
|
-
return `${url.origin}${url.pathname}`;
|
|
139
|
-
};
|
|
140
|
-
var toISOString = (date) => {
|
|
141
|
-
return new Date(date).toISOString();
|
|
142
|
-
};
|
|
143
|
-
var extractPath = (url) => {
|
|
144
|
-
const pathRegex = /^https?:\/\/[a-zA-Z0-9_\-\.]+(:\d+)?(\/.*)$/;
|
|
145
|
-
const match = url.match(pathRegex);
|
|
146
|
-
return match && match[2] ? match[2] : "/";
|
|
147
|
-
};
|
|
148
|
-
var getErrorName = (error) => {
|
|
149
|
-
if (error instanceof Error) {
|
|
150
|
-
return error.name;
|
|
151
|
-
}
|
|
152
|
-
return typeof error === "string" ? error : "UnknownError";
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
// src/assert.ts
|
|
156
|
-
var import_crypto = require("crypto");
|
|
157
|
-
var unsafeChars = [
|
|
158
|
-
"<",
|
|
159
|
-
">",
|
|
160
|
-
'"',
|
|
161
|
-
"`",
|
|
162
|
-
" ",
|
|
163
|
-
"\r",
|
|
164
|
-
"\n",
|
|
165
|
-
" ",
|
|
166
|
-
"\\",
|
|
167
|
-
"%2F",
|
|
168
|
-
"%5C",
|
|
169
|
-
"%2f",
|
|
170
|
-
"%5c",
|
|
171
|
-
"\r\n",
|
|
172
|
-
"%0A",
|
|
173
|
-
"%0D",
|
|
174
|
-
"%0a",
|
|
175
|
-
"%0d",
|
|
176
|
-
"..",
|
|
177
|
-
"//",
|
|
178
|
-
"///",
|
|
179
|
-
"...",
|
|
180
|
-
"%20",
|
|
181
|
-
"\0"
|
|
182
|
-
];
|
|
183
|
-
var isValidURL = (value) => {
|
|
184
|
-
if (!new RegExp(/^https?:\/\/[^/]/).test(value)) {
|
|
185
|
-
return false;
|
|
186
|
-
}
|
|
187
|
-
const match = value.match(/^(https?:\/\/)(.*)$/);
|
|
188
|
-
if (!match) return false;
|
|
189
|
-
const rest = match[2];
|
|
190
|
-
for (const char of unsafeChars) {
|
|
191
|
-
if (rest.includes(char)) return false;
|
|
192
|
-
}
|
|
193
|
-
const regex = /^https?:\/\/(?:[a-zA-Z0-9._-]+|localhost|\[[0-9a-fA-F:]+\])(?::\d{1,5})?(?:\/[a-zA-Z0-9._~!$&'()?#*+,;=:@-]*)*\/?$/;
|
|
194
|
-
return regex.test(match[0]);
|
|
195
|
-
};
|
|
196
|
-
var isJWTPayloadWithToken = (payload) => {
|
|
197
|
-
return typeof payload === "object" && payload !== null && "token" in payload && typeof payload?.token === "string";
|
|
198
|
-
};
|
|
199
|
-
var isRelativeURL = (value) => {
|
|
200
|
-
if (value.length > 100) return false;
|
|
201
|
-
for (const char of unsafeChars) {
|
|
202
|
-
if (value.includes(char)) return false;
|
|
203
|
-
}
|
|
204
|
-
const regex = /^\/[a-zA-Z0-9\-_\/.?&=#]*\/?$/;
|
|
205
|
-
return regex.test(value);
|
|
206
|
-
};
|
|
207
|
-
var isSameOrigin = (origin, expected) => {
|
|
208
|
-
const originURL = new URL(origin);
|
|
209
|
-
const expectedURL = new URL(expected);
|
|
210
|
-
return equals(originURL.origin, expectedURL.origin);
|
|
211
|
-
};
|
|
212
|
-
var patternToRegex = (pattern) => {
|
|
213
|
-
try {
|
|
214
|
-
if (pattern.length > 2048) return null;
|
|
215
|
-
pattern = pattern.replace(/\\/g, "");
|
|
216
|
-
const match = pattern.match(/^(https?):\/\/([a-zA-Z0-9.*-]{1,253})(?::(\d{1,5}|\*))?(?:\/.*)?$/);
|
|
217
|
-
if (!match) return null;
|
|
218
|
-
const [, protocol, host, port] = match;
|
|
219
|
-
const hasWildcard = host.includes("*");
|
|
220
|
-
if (hasWildcard && !host.startsWith("*.")) return null;
|
|
221
|
-
if (hasWildcard && host.slice(2).includes("*")) return null;
|
|
222
|
-
const domain = hasWildcard ? host.slice(2) : host;
|
|
223
|
-
const escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
224
|
-
const hostRegex = hasWildcard ? `[^.]+\\.${escapedDomain}` : escapedDomain;
|
|
225
|
-
const portRegex = port === "*" ? ":\\d{1,5}" : port ? `:${port}` : "";
|
|
226
|
-
return new RegExp(`^${protocol}:\\/\\/${hostRegex}${portRegex}$`);
|
|
227
|
-
} catch {
|
|
228
|
-
return null;
|
|
229
|
-
}
|
|
230
|
-
};
|
|
231
|
-
var isTrustedOrigin = (url, trustedOrigins) => {
|
|
232
|
-
if (!isValidURL(url) || trustedOrigins.length === 0) return false;
|
|
233
|
-
try {
|
|
234
|
-
const urlOrigin = new URL(url).origin;
|
|
235
|
-
for (const pattern of trustedOrigins) {
|
|
236
|
-
const regex = patternToRegex(pattern);
|
|
237
|
-
if (regex?.test(urlOrigin)) return true;
|
|
238
|
-
try {
|
|
239
|
-
if (isValidURL(pattern) && equals(new URL(pattern).origin, urlOrigin)) return true;
|
|
240
|
-
} catch {
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
} catch {
|
|
244
|
-
}
|
|
245
|
-
return false;
|
|
246
|
-
};
|
|
247
|
-
var safeEquals = (a, b) => {
|
|
248
|
-
const bufferA = Buffer.from(a);
|
|
249
|
-
const bufferB = Buffer.from(b);
|
|
250
|
-
if (bufferA.length !== bufferB.length) {
|
|
251
|
-
return false;
|
|
252
|
-
}
|
|
253
|
-
return (0, import_crypto.timingSafeEqual)(bufferA, bufferB);
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
// src/env.ts
|
|
257
|
-
var import_meta = {};
|
|
258
|
-
var env = new Proxy({}, {
|
|
259
|
-
get(_, prop) {
|
|
260
|
-
if (typeof prop !== "string") return void 0;
|
|
261
|
-
const hasProperty = (process2) => {
|
|
262
|
-
return process2 && Object.prototype.hasOwnProperty.call(process2, prop);
|
|
263
|
-
};
|
|
264
|
-
try {
|
|
265
|
-
if (typeof process !== "undefined" && hasProperty(process.env)) {
|
|
266
|
-
return process.env[prop];
|
|
267
|
-
}
|
|
268
|
-
if (typeof import_meta !== "undefined" && hasProperty(import_meta.env)) {
|
|
269
|
-
return import_meta.env[prop];
|
|
270
|
-
}
|
|
271
|
-
if (typeof Deno !== "undefined" && Deno.env?.get) {
|
|
272
|
-
return Deno.env.get(prop);
|
|
273
|
-
}
|
|
274
|
-
if (typeof Bun !== "undefined" && hasProperty(Bun.env)) {
|
|
275
|
-
return Bun.env[prop];
|
|
276
|
-
}
|
|
277
|
-
const globalValue = globalThis[prop];
|
|
278
|
-
return typeof globalValue === "string" ? globalValue : void 0;
|
|
279
|
-
} catch {
|
|
280
|
-
return void 0;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
// src/jose.ts
|
|
286
|
-
var import_jose = require("@aura-stack/jose");
|
|
287
|
-
var jwtVerificationOptions = {
|
|
288
|
-
algorithms: ["HS256"],
|
|
289
|
-
typ: "JWT"
|
|
290
|
-
};
|
|
291
|
-
|
|
292
|
-
// src/secure.ts
|
|
293
|
-
var generateSecure = (length = 32) => {
|
|
294
|
-
return import_crypto2.default.randomBytes(length).toString("base64url");
|
|
295
|
-
};
|
|
296
|
-
var createHash = (data, base = "hex") => {
|
|
297
|
-
return import_crypto2.default.createHash("sha256").update(data).digest().toString(base);
|
|
298
|
-
};
|
|
299
|
-
var createPKCE = async (verifier) => {
|
|
300
|
-
const byteLength = verifier ? void 0 : Math.floor(Math.random() * (96 - 32 + 1) + 32);
|
|
301
|
-
const codeVerifier = verifier ?? generateSecure(byteLength ?? 64);
|
|
302
|
-
if (codeVerifier.length < 43 || codeVerifier.length > 128) {
|
|
303
|
-
throw new AuthSecurityError("PKCE_VERIFIER_INVALID", "The code verifier must be between 43 and 128 characters in length.");
|
|
304
|
-
}
|
|
305
|
-
const codeChallenge = createHash(codeVerifier, "base64url");
|
|
306
|
-
return { codeVerifier, codeChallenge, method: "S256" };
|
|
307
|
-
};
|
|
308
|
-
var createCSRF = async (jose, csrfCookie) => {
|
|
309
|
-
try {
|
|
310
|
-
const token = generateSecure(32);
|
|
311
|
-
if (csrfCookie) {
|
|
312
|
-
await jose.verifyJWS(csrfCookie, jwtVerificationOptions);
|
|
313
|
-
return csrfCookie;
|
|
314
|
-
}
|
|
315
|
-
return jose.signJWS({ token });
|
|
316
|
-
} catch {
|
|
317
|
-
const token = generateSecure(32);
|
|
318
|
-
return jose.signJWS({ token });
|
|
319
|
-
}
|
|
320
|
-
};
|
|
321
|
-
var verifyCSRF = async (jose, cookie, header) => {
|
|
322
|
-
try {
|
|
323
|
-
const cookiePayload = await jose.verifyJWS(cookie, jwtVerificationOptions);
|
|
324
|
-
const headerPayload = await jose.verifyJWS(header, jwtVerificationOptions);
|
|
325
|
-
if (!isJWTPayloadWithToken(cookiePayload)) {
|
|
326
|
-
throw new AuthSecurityError("CSRF_TOKEN_INVALID", "Cookie payload missing token field.");
|
|
327
|
-
}
|
|
328
|
-
if (!isJWTPayloadWithToken(headerPayload)) {
|
|
329
|
-
throw new AuthSecurityError("CSRF_TOKEN_INVALID", "Header payload missing token field.");
|
|
330
|
-
}
|
|
331
|
-
if (!equals(cookiePayload.token.length, headerPayload.token.length)) {
|
|
332
|
-
throw new AuthSecurityError("CSRF_TOKEN_INVALID", "The CSRF tokens do not match.");
|
|
333
|
-
}
|
|
334
|
-
if (!safeEquals(cookiePayload.token, headerPayload.token)) {
|
|
335
|
-
throw new AuthSecurityError("CSRF_TOKEN_INVALID", "The CSRF tokens do not match.");
|
|
336
|
-
}
|
|
337
|
-
return true;
|
|
338
|
-
} catch {
|
|
339
|
-
throw new AuthSecurityError("CSRF_TOKEN_INVALID", "The CSRF tokens do not match.");
|
|
340
|
-
}
|
|
341
|
-
};
|
|
342
|
-
|
|
343
|
-
// src/schemas.ts
|
|
344
|
-
var import_zod = require("zod");
|
|
345
|
-
var OAuthProviderCredentialsSchema = (0, import_zod.object)({
|
|
346
|
-
id: (0, import_zod.string)(),
|
|
347
|
-
name: (0, import_zod.string)(),
|
|
348
|
-
authorizeURL: (0, import_zod.string)().url(),
|
|
349
|
-
accessToken: (0, import_zod.string)().url(),
|
|
350
|
-
scope: (0, import_zod.string)(),
|
|
351
|
-
userInfo: (0, import_zod.string)().url(),
|
|
352
|
-
responseType: (0, import_zod.enum)(["code", "token", "id_token"]),
|
|
353
|
-
clientId: (0, import_zod.string)(),
|
|
354
|
-
clientSecret: (0, import_zod.string)(),
|
|
355
|
-
profile: import_zod.z.function().optional()
|
|
356
|
-
});
|
|
357
|
-
var OAuthProviderConfigSchema = (0, import_zod.object)({
|
|
358
|
-
authorizeURL: (0, import_zod.string)().url(),
|
|
359
|
-
accessToken: (0, import_zod.string)().url(),
|
|
360
|
-
scope: (0, import_zod.string)().optional(),
|
|
361
|
-
userInfo: (0, import_zod.string)().url(),
|
|
362
|
-
responseType: (0, import_zod.enum)(["code", "token", "id_token"]),
|
|
363
|
-
clientId: (0, import_zod.string)(),
|
|
364
|
-
clientSecret: (0, import_zod.string)()
|
|
365
|
-
});
|
|
366
|
-
var OAuthAuthorization = OAuthProviderConfigSchema.extend({
|
|
367
|
-
redirectURI: (0, import_zod.string)(),
|
|
368
|
-
state: (0, import_zod.string)(),
|
|
369
|
-
codeChallenge: (0, import_zod.string)(),
|
|
370
|
-
codeChallengeMethod: (0, import_zod.enum)(["plain", "S256"])
|
|
371
|
-
});
|
|
372
|
-
var OAuthAuthorizationResponse = (0, import_zod.object)({
|
|
373
|
-
state: (0, import_zod.string)({ message: "Missing state parameter in the OAuth authorization response." }),
|
|
374
|
-
code: (0, import_zod.string)({ message: "Missing code parameter in the OAuth authorization response." })
|
|
375
|
-
});
|
|
376
|
-
var OAuthAuthorizationErrorResponse = (0, import_zod.object)({
|
|
377
|
-
error: (0, import_zod.enum)([
|
|
378
|
-
"invalid_request",
|
|
379
|
-
"unauthorized_client",
|
|
380
|
-
"access_denied",
|
|
381
|
-
"unsupported_response_type",
|
|
382
|
-
"invalid_scope",
|
|
383
|
-
"server_error",
|
|
384
|
-
"temporarily_unavailable"
|
|
385
|
-
]),
|
|
386
|
-
error_description: (0, import_zod.string)().optional(),
|
|
387
|
-
error_uri: (0, import_zod.string)().optional(),
|
|
388
|
-
state: (0, import_zod.string)()
|
|
389
|
-
});
|
|
390
|
-
var OAuthAccessToken = OAuthProviderConfigSchema.extend({
|
|
391
|
-
redirectURI: (0, import_zod.string)(),
|
|
392
|
-
code: (0, import_zod.string)(),
|
|
393
|
-
codeVerifier: (0, import_zod.string)().min(43).max(128)
|
|
394
|
-
});
|
|
395
|
-
var OAuthAccessTokenResponse = (0, import_zod.object)({
|
|
396
|
-
access_token: (0, import_zod.string)(),
|
|
397
|
-
token_type: (0, import_zod.string)().optional(),
|
|
398
|
-
expires_in: (0, import_zod.number)().optional(),
|
|
399
|
-
refresh_token: (0, import_zod.string)().optional(),
|
|
400
|
-
scope: (0, import_zod.string)().optional().or((0, import_zod.null)())
|
|
401
|
-
});
|
|
402
|
-
var OAuthAccessTokenErrorResponse = (0, import_zod.object)({
|
|
403
|
-
error: (0, import_zod.enum)([
|
|
404
|
-
"invalid_request",
|
|
405
|
-
"invalid_client",
|
|
406
|
-
"invalid_grant",
|
|
407
|
-
"unauthorized_client",
|
|
408
|
-
"unsupported_grant_type",
|
|
409
|
-
"invalid_scope"
|
|
410
|
-
]),
|
|
411
|
-
error_description: (0, import_zod.string)().optional(),
|
|
412
|
-
error_uri: (0, import_zod.string)().optional()
|
|
413
|
-
});
|
|
414
|
-
var OAuthErrorResponse = (0, import_zod.object)({
|
|
415
|
-
error: (0, import_zod.string)(),
|
|
416
|
-
error_description: (0, import_zod.string)().optional()
|
|
417
|
-
});
|
|
418
|
-
var OAuthEnvSchema = (0, import_zod.object)({
|
|
419
|
-
clientId: import_zod.z.string().min(1, "OAuth Client ID is required in the environment variables."),
|
|
420
|
-
clientSecret: import_zod.z.string().min(1, "OAuth Client Secret is required in the environment variables.")
|
|
421
|
-
});
|
|
422
|
-
|
|
423
|
-
// src/actions/signIn/authorization.ts
|
|
424
|
-
var createAuthorizationURL = (oauthConfig, redirectURI, state, codeChallenge, codeChallengeMethod, logger) => {
|
|
425
|
-
const parsed = OAuthAuthorization.safeParse({ ...oauthConfig, redirectURI, state, codeChallenge, codeChallengeMethod });
|
|
426
|
-
if (!parsed.success) {
|
|
427
|
-
logger?.log("INVALID_OAUTH_CONFIGURATION", {
|
|
428
|
-
structuredData: {
|
|
429
|
-
scope: oauthConfig.scope,
|
|
430
|
-
redirect_uri: redirectURI,
|
|
431
|
-
has_state: Boolean(state),
|
|
432
|
-
has_code_challenge: Boolean(codeChallenge),
|
|
433
|
-
code_challenge_method: codeChallengeMethod
|
|
434
|
-
}
|
|
435
|
-
});
|
|
436
|
-
throw new AuthInternalError("INVALID_OAUTH_CONFIGURATION", "The OAuth provider configuration is invalid.");
|
|
437
|
-
}
|
|
438
|
-
const { authorizeURL, ...options2 } = parsed.data;
|
|
439
|
-
const { userInfo, accessToken, clientSecret, ...required } = options2;
|
|
440
|
-
const searchParams = new URLSearchParams(toCastCase(required));
|
|
441
|
-
return `${authorizeURL}?${searchParams}`;
|
|
442
|
-
};
|
|
443
|
-
var getTrustedOrigins = async (request, trustedOrigins) => {
|
|
444
|
-
if (!trustedOrigins) return [];
|
|
445
|
-
const raw = typeof trustedOrigins === "function" ? await trustedOrigins(request) : trustedOrigins;
|
|
446
|
-
return Array.isArray(raw) ? raw : typeof raw === "string" ? [raw] : [];
|
|
447
|
-
};
|
|
448
|
-
var getOriginURL = async (request, context) => {
|
|
449
|
-
const headers = request.headers;
|
|
450
|
-
let origin = new URL(request.url).origin;
|
|
451
|
-
const trustedOrigins = await getTrustedOrigins(request, context?.trustedOrigins);
|
|
452
|
-
trustedOrigins.push(origin);
|
|
453
|
-
if (context?.trustedProxyHeaders) {
|
|
454
|
-
const protocol = headers.get("Forwarded")?.match(/proto=([^;]+)/i)?.[1] ?? headers.get("X-Forwarded-Proto") ?? "http";
|
|
455
|
-
const host = headers.get("Host") ?? headers.get("Forwarded")?.match(/host=([^;]+)/i)?.[1] ?? headers.get("X-Forwarded-Host") ?? null;
|
|
456
|
-
origin = `${protocol}://${host}`;
|
|
457
|
-
}
|
|
458
|
-
if (!isTrustedOrigin(origin, trustedOrigins)) {
|
|
459
|
-
context?.logger?.log("UNTRUSTED_ORIGIN", { structuredData: { origin } });
|
|
460
|
-
throw new AuthInternalError("UNTRUSTED_ORIGIN", "The constructed origin URL is not trusted.");
|
|
461
|
-
}
|
|
462
|
-
return origin;
|
|
463
|
-
};
|
|
464
|
-
var createRedirectURI = async (request, oauth, context) => {
|
|
465
|
-
const origin = await getOriginURL(request, context);
|
|
466
|
-
return `${origin}${context.basePath}/callback/${oauth}`;
|
|
467
|
-
};
|
|
468
|
-
var createRedirectTo = async (request, redirectTo, context) => {
|
|
469
|
-
try {
|
|
470
|
-
const headers = request.headers;
|
|
471
|
-
const requestOrigin = await getOriginURL(request, context);
|
|
472
|
-
const origins = await getTrustedOrigins(request, context?.trustedOrigins);
|
|
473
|
-
const validateURL = (url) => {
|
|
474
|
-
if (!isRelativeURL(url) && !isValidURL(url)) return "/";
|
|
475
|
-
if (isRelativeURL(url)) return url;
|
|
476
|
-
if (origins.length > 0) {
|
|
477
|
-
if (isTrustedOrigin(url, origins)) {
|
|
478
|
-
const urlOrigin = new URL(url).origin;
|
|
479
|
-
for (const pattern of origins) {
|
|
480
|
-
const regex = patternToRegex(pattern);
|
|
481
|
-
if (regex?.test(urlOrigin)) {
|
|
482
|
-
return isSameOrigin(url, request.url) ? extractPath(url) : url;
|
|
483
|
-
}
|
|
484
|
-
if (isValidURL(pattern) && equals(new URL(pattern).origin, urlOrigin)) return url;
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
context?.logger?.log("OPEN_REDIRECT_ATTACK");
|
|
488
|
-
return "/";
|
|
489
|
-
}
|
|
490
|
-
if (isSameOrigin(url, requestOrigin)) {
|
|
491
|
-
return extractPath(url);
|
|
492
|
-
}
|
|
493
|
-
context?.logger?.log("OPEN_REDIRECT_ATTACK");
|
|
494
|
-
return "/";
|
|
495
|
-
};
|
|
496
|
-
return validateURL(redirectTo ?? headers.get("Referer") ?? headers.get("Origin") ?? "/");
|
|
497
|
-
} catch (error) {
|
|
498
|
-
context?.logger?.log("POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED");
|
|
499
|
-
return "/";
|
|
500
|
-
}
|
|
501
|
-
};
|
|
502
|
-
|
|
503
|
-
// src/actions/signIn/signIn.ts
|
|
504
|
-
var signInConfig = (oauth) => {
|
|
505
|
-
return (0, import_router2.createEndpointConfig)("/signIn/:oauth", {
|
|
506
|
-
schemas: {
|
|
507
|
-
params: import_zod2.z.object({
|
|
508
|
-
oauth: import_zod2.z.enum(
|
|
509
|
-
Object.keys(oauth),
|
|
510
|
-
"The OAuth provider is not supported or invalid."
|
|
511
|
-
)
|
|
512
|
-
}),
|
|
513
|
-
searchParams: import_zod2.z.object({
|
|
514
|
-
redirectTo: import_zod2.z.string().optional()
|
|
515
|
-
})
|
|
516
|
-
}
|
|
517
|
-
});
|
|
518
|
-
};
|
|
519
|
-
var signInAction = (oauth) => {
|
|
520
|
-
return (0, import_router2.createEndpoint)(
|
|
521
|
-
"GET",
|
|
522
|
-
"/signIn/:oauth",
|
|
523
|
-
async (ctx) => {
|
|
524
|
-
const {
|
|
525
|
-
request,
|
|
526
|
-
params: { oauth: oauth2 },
|
|
527
|
-
searchParams: { redirectTo },
|
|
528
|
-
context
|
|
529
|
-
} = ctx;
|
|
530
|
-
const { oauth: providers, cookies, logger } = context;
|
|
531
|
-
const state = generateSecure();
|
|
532
|
-
const redirectURI = await createRedirectURI(request, oauth2, context);
|
|
533
|
-
const redirectToValue = await createRedirectTo(request, redirectTo, context);
|
|
534
|
-
const { codeVerifier, codeChallenge, method } = await createPKCE();
|
|
535
|
-
const authorization = createAuthorizationURL(providers[oauth2], redirectURI, state, codeChallenge, method, logger);
|
|
536
|
-
logger?.log("SIGN_IN_INITIATED", {
|
|
537
|
-
structuredData: { oauth_provider: oauth2, code_challenge_method: method }
|
|
538
|
-
});
|
|
539
|
-
const headers = new import_router2.HeadersBuilder(cacheControl).setHeader("Location", authorization).setCookie(cookies.state.name, state, cookies.state.attributes).setCookie(cookies.redirectURI.name, redirectURI, cookies.redirectURI.attributes).setCookie(cookies.redirectTo.name, redirectToValue, cookies.redirectTo.attributes).setCookie(cookies.codeVerifier.name, codeVerifier, cookies.codeVerifier.attributes).toHeaders();
|
|
540
|
-
return Response.json(
|
|
541
|
-
{ oauth: oauth2 },
|
|
542
|
-
{
|
|
543
|
-
status: 302,
|
|
544
|
-
headers
|
|
545
|
-
}
|
|
546
|
-
);
|
|
547
|
-
},
|
|
548
|
-
signInConfig(oauth)
|
|
549
|
-
);
|
|
550
|
-
};
|
|
551
|
-
|
|
552
|
-
// src/actions/callback/callback.ts
|
|
553
|
-
var import_zod3 = require("zod");
|
|
554
|
-
var import_router3 = require("@aura-stack/router");
|
|
555
|
-
|
|
556
|
-
// src/request.ts
|
|
557
|
-
var fetchAsync = async (url, options2 = {}, timeout = 5e3) => {
|
|
558
|
-
const controller = new AbortController();
|
|
559
|
-
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
560
|
-
const response = await fetch(url, {
|
|
561
|
-
...options2,
|
|
562
|
-
signal: controller.signal
|
|
563
|
-
}).finally(() => clearTimeout(timeoutId));
|
|
564
|
-
return response;
|
|
565
|
-
};
|
|
566
|
-
|
|
567
|
-
// src/actions/callback/userinfo.ts
|
|
568
|
-
var getDefaultUserInfo = (profile) => {
|
|
569
|
-
const sub = generateSecure(16);
|
|
570
|
-
return {
|
|
571
|
-
sub: profile?.id ?? profile?.sub ?? sub,
|
|
572
|
-
email: profile?.email,
|
|
573
|
-
name: profile?.name ?? profile?.username ?? profile?.nickname,
|
|
574
|
-
image: profile?.image ?? profile?.picture
|
|
575
|
-
};
|
|
576
|
-
};
|
|
577
|
-
var getUserInfo = async (oauthConfig, accessToken, logger) => {
|
|
578
|
-
const userinfoEndpoint = oauthConfig.userInfo;
|
|
579
|
-
try {
|
|
580
|
-
logger?.log("OAUTH_USERINFO_REQUEST_INITIATED", {
|
|
581
|
-
structuredData: {
|
|
582
|
-
endpoint: userinfoEndpoint
|
|
583
|
-
}
|
|
584
|
-
});
|
|
585
|
-
const response = await fetchAsync(userinfoEndpoint, {
|
|
586
|
-
method: "GET",
|
|
587
|
-
headers: {
|
|
588
|
-
Accept: "application/json",
|
|
589
|
-
Authorization: `Bearer ${accessToken}`
|
|
590
|
-
}
|
|
591
|
-
});
|
|
592
|
-
if (!response.ok) {
|
|
593
|
-
logger?.log("OAUTH_USERINFO_INVALID_RESPONSE");
|
|
594
|
-
throw new OAuthProtocolError("INVALID_REQUEST", "Invalid userinfo response format");
|
|
595
|
-
}
|
|
596
|
-
const json = await response.json();
|
|
597
|
-
const { success, data } = OAuthErrorResponse.safeParse(json);
|
|
598
|
-
if (success) {
|
|
599
|
-
logger?.log("OAUTH_USERINFO_ERROR", {
|
|
600
|
-
message: "Error response received from OAuth userinfo endpoint",
|
|
601
|
-
structuredData: {
|
|
602
|
-
error: data.error,
|
|
603
|
-
error_description: data.error_description ?? ""
|
|
604
|
-
}
|
|
605
|
-
});
|
|
606
|
-
throw new OAuthProtocolError("INVALID_REQUEST", "An error was received from the OAuth userinfo endpoint.");
|
|
607
|
-
}
|
|
608
|
-
logger?.log("OAUTH_USERINFO_SUCCESS");
|
|
609
|
-
return oauthConfig?.profile ? oauthConfig.profile(json) : getDefaultUserInfo(json);
|
|
610
|
-
} catch (error) {
|
|
611
|
-
if (isOAuthProtocolError(error)) {
|
|
612
|
-
throw error;
|
|
613
|
-
}
|
|
614
|
-
logger?.log("OAUTH_USERINFO_REQUEST_FAILED");
|
|
615
|
-
if (isNativeError(error)) {
|
|
616
|
-
throw new OAuthProtocolError("SERVER_ERROR", "Failed to fetch user information from OAuth provider", "", {
|
|
617
|
-
cause: error
|
|
618
|
-
});
|
|
619
|
-
}
|
|
620
|
-
throw new OAuthProtocolError("SERVER_ERROR", "Failed to fetch user information", "", { cause: error });
|
|
621
|
-
}
|
|
622
|
-
};
|
|
623
|
-
|
|
624
|
-
// src/actions/callback/access-token.ts
|
|
625
|
-
var createAccessToken = async (oauthConfig, redirectURI, code, codeVerifier, logger) => {
|
|
626
|
-
const parsed = OAuthAccessToken.safeParse({ ...oauthConfig, redirectURI, code, codeVerifier });
|
|
627
|
-
if (!parsed.success) {
|
|
628
|
-
logger?.log("INVALID_OAUTH_CONFIGURATION");
|
|
629
|
-
throw new AuthInternalError("INVALID_OAUTH_CONFIGURATION", "The OAuth provider configuration is invalid.");
|
|
630
|
-
}
|
|
631
|
-
const { accessToken, clientId, clientSecret, code: codeParsed, redirectURI: redirectParsed } = parsed.data;
|
|
632
|
-
try {
|
|
633
|
-
logger?.log("OAUTH_ACCESS_TOKEN_REQUEST_INITIATED", {
|
|
634
|
-
structuredData: {
|
|
635
|
-
has_client_id: Boolean(clientId),
|
|
636
|
-
redirect_uri: redirectParsed,
|
|
637
|
-
grant_type: "authorization_code"
|
|
638
|
-
}
|
|
639
|
-
});
|
|
640
|
-
const response = await fetchAsync(accessToken, {
|
|
641
|
-
method: "POST",
|
|
642
|
-
headers: {
|
|
643
|
-
Accept: "application/json",
|
|
644
|
-
"Content-Type": "application/x-www-form-urlencoded"
|
|
645
|
-
},
|
|
646
|
-
body: new URLSearchParams({
|
|
647
|
-
client_id: clientId,
|
|
648
|
-
client_secret: clientSecret,
|
|
649
|
-
code: codeParsed,
|
|
650
|
-
redirect_uri: redirectParsed,
|
|
651
|
-
grant_type: "authorization_code",
|
|
652
|
-
code_verifier: codeVerifier
|
|
653
|
-
}).toString()
|
|
654
|
-
});
|
|
655
|
-
if (!response.ok) {
|
|
656
|
-
logger?.log("INVALID_OAUTH_ACCESS_TOKEN_RESPONSE");
|
|
657
|
-
throw new OAuthProtocolError("invalid_request", "Invalid access token response");
|
|
658
|
-
}
|
|
659
|
-
const json = await response.json();
|
|
660
|
-
const token = OAuthAccessTokenResponse.safeParse(json);
|
|
661
|
-
if (!token.success) {
|
|
662
|
-
const { success, data } = OAuthAccessTokenErrorResponse.safeParse(json);
|
|
663
|
-
if (!success) {
|
|
664
|
-
logger?.log("INVALID_OAUTH_ACCESS_TOKEN_RESPONSE");
|
|
665
|
-
throw new OAuthProtocolError("invalid_request", "Invalid access token response format");
|
|
666
|
-
}
|
|
667
|
-
logger?.log("OAUTH_ACCESS_TOKEN_ERROR", {
|
|
668
|
-
structuredData: {
|
|
669
|
-
error: data.error,
|
|
670
|
-
error_description: data.error_description ?? ""
|
|
671
|
-
}
|
|
672
|
-
});
|
|
673
|
-
throw new OAuthProtocolError("INVALID_ACCESS_TOKEN", "Failed to retrieve access token");
|
|
674
|
-
}
|
|
675
|
-
logger?.log("OAUTH_ACCESS_TOKEN_SUCCESS");
|
|
676
|
-
return token.data;
|
|
677
|
-
} catch (error) {
|
|
678
|
-
logger?.log("OAUTH_ACCESS_TOKEN_REQUEST_FAILED");
|
|
679
|
-
if (error instanceof Error) {
|
|
680
|
-
throw new OAuthProtocolError("server_error", "Failed to communicate with OAuth provider", "", { cause: error });
|
|
681
|
-
}
|
|
682
|
-
throw error;
|
|
683
|
-
}
|
|
684
|
-
};
|
|
685
|
-
|
|
686
|
-
// src/cookie.ts
|
|
687
|
-
var import_cookie = require("@aura-stack/router/cookie");
|
|
688
|
-
var defaultCookieOptions = {
|
|
689
|
-
httpOnly: true,
|
|
690
|
-
sameSite: "lax",
|
|
691
|
-
path: "/",
|
|
692
|
-
maxAge: 60 * 60 * 24 * 15
|
|
693
|
-
};
|
|
694
|
-
var oauthCookieOptions = {
|
|
695
|
-
httpOnly: true,
|
|
696
|
-
maxAge: 5 * 60,
|
|
697
|
-
sameSite: "lax",
|
|
698
|
-
expires: new Date(Date.now() + 5 * 60 * 1e3)
|
|
699
|
-
};
|
|
700
|
-
var setCookie = (cookieName, value, options2) => {
|
|
701
|
-
return (0, import_cookie.serialize)(cookieName, value, options2);
|
|
702
|
-
};
|
|
703
|
-
var expiredCookieAttributes = {
|
|
704
|
-
...defaultCookieOptions,
|
|
705
|
-
expires: /* @__PURE__ */ new Date(0),
|
|
706
|
-
maxAge: 0,
|
|
707
|
-
secure: true
|
|
708
|
-
};
|
|
709
|
-
var getCookie = (request, cookieName) => {
|
|
710
|
-
const cookies = request.headers.get("Cookie");
|
|
711
|
-
if (!cookies) {
|
|
712
|
-
throw new AuthInternalError("COOKIE_NOT_FOUND", "No cookies found. There is no active session");
|
|
713
|
-
}
|
|
714
|
-
const value = (0, import_cookie.parse)(cookies)[cookieName];
|
|
715
|
-
if (!value) {
|
|
716
|
-
throw new AuthInternalError("COOKIE_NOT_FOUND", `Cookie "${cookieName}" not found. There is no active session`);
|
|
717
|
-
}
|
|
718
|
-
return value;
|
|
719
|
-
};
|
|
720
|
-
var createSessionCookie = async (jose, session) => {
|
|
721
|
-
try {
|
|
722
|
-
const encoded = await jose.encodeJWT(session);
|
|
723
|
-
return encoded;
|
|
724
|
-
} catch (error) {
|
|
725
|
-
throw new AuthInternalError("INVALID_JWT_TOKEN", "Failed to create session cookie", { cause: error });
|
|
726
|
-
}
|
|
727
|
-
};
|
|
728
|
-
|
|
729
|
-
// src/actions/callback/callback.ts
|
|
730
|
-
var callbackConfig = (oauth) => {
|
|
731
|
-
return (0, import_router3.createEndpointConfig)("/callback/:oauth", {
|
|
732
|
-
schemas: {
|
|
733
|
-
params: import_zod3.z.object({
|
|
734
|
-
oauth: import_zod3.z.enum(
|
|
735
|
-
Object.keys(oauth),
|
|
736
|
-
"The OAuth provider is not supported or invalid."
|
|
737
|
-
)
|
|
738
|
-
}),
|
|
739
|
-
searchParams: import_zod3.z.object({
|
|
740
|
-
code: import_zod3.z.string("Missing code parameter in the OAuth authorization response."),
|
|
741
|
-
state: import_zod3.z.string("Missing state parameter in the OAuth authorization response.")
|
|
742
|
-
})
|
|
743
|
-
},
|
|
744
|
-
middlewares: [
|
|
745
|
-
(ctx) => {
|
|
746
|
-
const {
|
|
747
|
-
searchParams,
|
|
748
|
-
context: { logger }
|
|
749
|
-
} = ctx;
|
|
750
|
-
const response = OAuthAuthorizationErrorResponse.safeParse(searchParams);
|
|
751
|
-
if (response.success) {
|
|
752
|
-
const { error, error_description } = response.data;
|
|
753
|
-
const criticalAuthErrors = ["access_denied", "server_error"];
|
|
754
|
-
const severity = criticalAuthErrors.includes(error.toLowerCase()) ? "critical" : "warning";
|
|
755
|
-
logger?.log("OAUTH_AUTHORIZATION_ERROR", {
|
|
756
|
-
severity,
|
|
757
|
-
structuredData: {
|
|
758
|
-
error,
|
|
759
|
-
error_description: error_description ?? ""
|
|
760
|
-
}
|
|
761
|
-
});
|
|
762
|
-
throw new OAuthProtocolError(error, error_description || "OAuth Authorization Error");
|
|
763
|
-
}
|
|
764
|
-
return ctx;
|
|
765
|
-
}
|
|
766
|
-
]
|
|
767
|
-
});
|
|
768
|
-
};
|
|
769
|
-
var callbackAction = (oauth) => {
|
|
770
|
-
return (0, import_router3.createEndpoint)(
|
|
771
|
-
"GET",
|
|
772
|
-
"/callback/:oauth",
|
|
773
|
-
async (ctx) => {
|
|
774
|
-
const {
|
|
775
|
-
request,
|
|
776
|
-
params: { oauth: oauth2 },
|
|
777
|
-
searchParams: { code, state },
|
|
778
|
-
context
|
|
779
|
-
} = ctx;
|
|
780
|
-
const { oauth: providers, cookies, jose, logger, trustedOrigins } = context;
|
|
781
|
-
const oauthConfig = providers[oauth2];
|
|
782
|
-
const cookieState = getCookie(request, cookies.state.name);
|
|
783
|
-
const codeVerifier = getCookie(request, cookies.codeVerifier.name);
|
|
784
|
-
const cookieRedirectTo = getCookie(request, cookies.redirectTo.name);
|
|
785
|
-
const cookieRedirectURI = getCookie(request, cookies.redirectURI.name);
|
|
786
|
-
if (!safeEquals(cookieState, state)) {
|
|
787
|
-
logger?.log("MISMATCHING_STATE", {
|
|
788
|
-
structuredData: {
|
|
789
|
-
oauth_provider: oauth2
|
|
790
|
-
}
|
|
791
|
-
});
|
|
792
|
-
throw new AuthSecurityError(
|
|
793
|
-
"MISMATCHING_STATE",
|
|
794
|
-
"The provided state passed in the OAuth response does not match the stored state."
|
|
795
|
-
);
|
|
796
|
-
}
|
|
797
|
-
const accessToken = await createAccessToken(oauthConfig, cookieRedirectURI, code, codeVerifier, logger);
|
|
798
|
-
const origins = await getTrustedOrigins(request, trustedOrigins);
|
|
799
|
-
const requestOrigin = await getOriginURL(request, context);
|
|
800
|
-
if (!isRelativeURL(cookieRedirectTo)) {
|
|
801
|
-
const isValid = origins.length > 0 ? isTrustedOrigin(cookieRedirectTo, origins) : isSameOrigin(cookieRedirectTo, requestOrigin);
|
|
802
|
-
if (!isValid) {
|
|
803
|
-
logger?.log("POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED", {
|
|
804
|
-
structuredData: {
|
|
805
|
-
redirect_path: cookieRedirectTo,
|
|
806
|
-
provider: oauth2,
|
|
807
|
-
has_trusted_origins: origins.length > 0,
|
|
808
|
-
request_origin: requestOrigin
|
|
809
|
-
}
|
|
810
|
-
});
|
|
811
|
-
throw new AuthSecurityError(
|
|
812
|
-
"POTENTIAL_OPEN_REDIRECT_ATTACK_DETECTED",
|
|
813
|
-
"Invalid redirect path. Potential open redirect attack detected."
|
|
814
|
-
);
|
|
815
|
-
}
|
|
816
|
-
}
|
|
817
|
-
const userInfo = await getUserInfo(oauthConfig, accessToken.access_token, logger);
|
|
818
|
-
const sessionCookie = await createSessionCookie(jose, userInfo);
|
|
819
|
-
const csrfToken = await createCSRF(jose);
|
|
820
|
-
logger?.log("OAUTH_CALLBACK_SUCCESS", {
|
|
821
|
-
structuredData: {
|
|
822
|
-
provider: oauth2
|
|
823
|
-
}
|
|
824
|
-
});
|
|
825
|
-
const headers = new import_router3.HeadersBuilder(cacheControl).setHeader("Location", cookieRedirectTo).setCookie(cookies.sessionToken.name, sessionCookie, cookies.sessionToken.attributes).setCookie(cookies.csrfToken.name, csrfToken, cookies.csrfToken.attributes).setCookie(cookies.state.name, "", expiredCookieAttributes).setCookie(cookies.redirectURI.name, "", expiredCookieAttributes).setCookie(cookies.redirectTo.name, "", expiredCookieAttributes).setCookie(cookies.codeVerifier.name, "", expiredCookieAttributes).toHeaders();
|
|
826
|
-
return Response.json({ oauth: oauth2 }, { status: 302, headers });
|
|
827
|
-
},
|
|
828
|
-
callbackConfig(oauth)
|
|
829
|
-
);
|
|
830
|
-
};
|
|
831
|
-
|
|
832
|
-
// src/actions/session/session.ts
|
|
833
|
-
var import_router4 = require("@aura-stack/router");
|
|
834
|
-
var sessionAction = (0, import_router4.createEndpoint)("GET", "/session", async (ctx) => {
|
|
835
|
-
const {
|
|
836
|
-
request,
|
|
837
|
-
context: { jose, cookies, logger }
|
|
838
|
-
} = ctx;
|
|
839
|
-
try {
|
|
840
|
-
const session = getCookie(request, cookies.sessionToken.name);
|
|
841
|
-
const decoded = await jose.decodeJWT(session);
|
|
842
|
-
logger?.log("AUTH_SESSION_VALID");
|
|
843
|
-
const { exp, iat, jti, nbf, ...user } = decoded;
|
|
844
|
-
const headers = new Headers(secureApiHeaders);
|
|
845
|
-
return Response.json({ user, expires: toISOString(exp * 1e3) }, { headers });
|
|
846
|
-
} catch (error) {
|
|
847
|
-
logger?.log("AUTH_SESSION_INVALID", { structuredData: { error_type: getErrorName(error) } });
|
|
848
|
-
const headers = new import_router4.HeadersBuilder(secureApiHeaders).setCookie(cookies.sessionToken.name, "", expiredCookieAttributes).toHeaders();
|
|
849
|
-
return Response.json({ authenticated: false, message: "Unauthorized" }, { status: 401, headers });
|
|
850
|
-
}
|
|
851
|
-
});
|
|
852
|
-
|
|
853
|
-
// src/actions/signOut/signOut.ts
|
|
854
|
-
var import_zod4 = require("zod");
|
|
855
|
-
var import_router5 = require("@aura-stack/router");
|
|
856
|
-
var config = (0, import_router5.createEndpointConfig)({
|
|
857
|
-
schemas: {
|
|
858
|
-
searchParams: import_zod4.z.object({
|
|
859
|
-
token_type_hint: import_zod4.z.literal("session_token"),
|
|
860
|
-
redirectTo: import_zod4.z.string().optional()
|
|
861
|
-
})
|
|
862
|
-
}
|
|
863
|
-
});
|
|
864
|
-
var signOutAction = (0, import_router5.createEndpoint)(
|
|
865
|
-
"POST",
|
|
866
|
-
"/signOut",
|
|
867
|
-
async (ctx) => {
|
|
868
|
-
const {
|
|
869
|
-
request,
|
|
870
|
-
headers,
|
|
871
|
-
searchParams: { redirectTo },
|
|
872
|
-
context
|
|
873
|
-
} = ctx;
|
|
874
|
-
const { jose, cookies, logger } = context;
|
|
875
|
-
const session = headers.getCookie(cookies.sessionToken.name);
|
|
876
|
-
const csrfToken = headers.getCookie(cookies.csrfToken.name);
|
|
877
|
-
const header = headers.getHeader("X-CSRF-Token");
|
|
878
|
-
logger?.log("SIGN_OUT_ATTEMPT", {
|
|
879
|
-
structuredData: {
|
|
880
|
-
has_session: Boolean(session),
|
|
881
|
-
has_csrf_token: Boolean(csrfToken),
|
|
882
|
-
has_csrf_header: Boolean(header)
|
|
883
|
-
}
|
|
884
|
-
});
|
|
885
|
-
if (!session) {
|
|
886
|
-
logger?.log("SESSION_TOKEN_MISSING");
|
|
887
|
-
throw new AuthSecurityError("SESSION_TOKEN_MISSING", "The sessionToken is missing.");
|
|
888
|
-
}
|
|
889
|
-
if (!csrfToken) {
|
|
890
|
-
logger?.log("CSRF_TOKEN_MISSING");
|
|
891
|
-
throw new AuthSecurityError("CSRF_TOKEN_MISSING", "The CSRF token is missing.");
|
|
892
|
-
}
|
|
893
|
-
if (!header) {
|
|
894
|
-
logger?.log("CSRF_HEADER_MISSING");
|
|
895
|
-
throw new AuthSecurityError("CSRF_HEADER_MISSING", "The CSRF header is missing.");
|
|
896
|
-
}
|
|
897
|
-
try {
|
|
898
|
-
await verifyCSRF(jose, csrfToken, header);
|
|
899
|
-
} catch (error) {
|
|
900
|
-
logger?.log("CSRF_TOKEN_INVALID", { structuredData: { error_type: getErrorName(error) } });
|
|
901
|
-
throw new AuthSecurityError("CSRF_TOKEN_INVALID", "CSRF token verification failed");
|
|
902
|
-
}
|
|
903
|
-
logger?.log("SIGN_OUT_CSRF_VERIFIED");
|
|
904
|
-
try {
|
|
905
|
-
await jose.decodeJWT(session);
|
|
906
|
-
logger?.log("SIGN_OUT_SUCCESS");
|
|
907
|
-
} catch (error) {
|
|
908
|
-
logger?.log("INVALID_JWT_TOKEN", { structuredData: { error_type: getErrorName(error) } });
|
|
909
|
-
}
|
|
910
|
-
const baseURL = getBaseURL(request);
|
|
911
|
-
const location = await createRedirectTo(
|
|
912
|
-
new Request(baseURL, {
|
|
913
|
-
headers: headers.toHeaders()
|
|
914
|
-
}),
|
|
915
|
-
redirectTo,
|
|
916
|
-
context
|
|
917
|
-
);
|
|
918
|
-
logger?.log("SIGN_OUT_REDIRECT", { structuredData: { location } });
|
|
919
|
-
const headersList = new import_router5.HeadersBuilder(secureApiHeaders).setHeader("Location", location).setCookie(cookies.csrfToken.name, "", expiredCookieAttributes).setCookie(cookies.sessionToken.name, "", expiredCookieAttributes).toHeaders();
|
|
920
|
-
return Response.json({ message: "Signed out successfully" }, { status: import_router5.statusCode.ACCEPTED, headers: headersList });
|
|
921
|
-
},
|
|
922
|
-
config
|
|
923
|
-
);
|
|
924
|
-
|
|
925
|
-
// src/actions/csrfToken/csrfToken.ts
|
|
926
|
-
var import_router6 = require("@aura-stack/router");
|
|
927
|
-
var getCSRFToken = (request, cookieName) => {
|
|
928
|
-
try {
|
|
929
|
-
return getCookie(request, cookieName);
|
|
930
|
-
} catch {
|
|
931
|
-
return void 0;
|
|
932
|
-
}
|
|
933
|
-
};
|
|
934
|
-
var csrfTokenAction = (0, import_router6.createEndpoint)("GET", "/csrfToken", async (ctx) => {
|
|
935
|
-
const {
|
|
936
|
-
request,
|
|
937
|
-
context: { jose, cookies, logger }
|
|
938
|
-
} = ctx;
|
|
939
|
-
const token = getCSRFToken(request, cookies.csrfToken.name);
|
|
940
|
-
logger?.log("CSRF_TOKEN_REQUESTED", { structuredData: { has_token: Boolean(token) } });
|
|
941
|
-
const csrfToken = await createCSRF(jose, token);
|
|
942
|
-
logger?.log("CSRF_TOKEN_ISSUED", { structuredData: { issued: Boolean(csrfToken) } });
|
|
943
|
-
const headers = new Headers(secureApiHeaders);
|
|
944
|
-
headers.append("Set-Cookie", setCookie(cookies.csrfToken.name, csrfToken, cookies.csrfToken.attributes));
|
|
945
|
-
return Response.json({ csrfToken }, { headers });
|
|
946
|
-
});
|
|
947
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
948
|
-
0 && (module.exports = {
|
|
949
|
-
callbackAction,
|
|
950
|
-
csrfTokenAction,
|
|
951
|
-
sessionAction,
|
|
952
|
-
signInAction,
|
|
953
|
-
signOutAction
|
|
954
|
-
});
|