@stackframe/stack-shared 2.8.12 → 2.8.17
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/CHANGELOG.md +22 -0
- package/dist/config/format.js +2 -2
- package/dist/config/schema.d.mts +32 -32
- package/dist/config/schema.d.ts +32 -32
- package/dist/config/schema.js +4 -4
- package/dist/config/schema.js.map +1 -1
- package/dist/crud.js +2 -2
- package/dist/esm/config/format.js +2 -2
- package/dist/esm/config/schema.js +4 -4
- package/dist/esm/config/schema.js.map +1 -1
- package/dist/esm/crud.js +2 -2
- package/dist/esm/helpers/password.js +1 -1
- package/dist/esm/helpers/production-mode.js +2 -2
- package/dist/esm/hooks/use-async-callback.js +1 -1
- package/dist/esm/hooks/use-async-external-store.js +1 -1
- package/dist/esm/hooks/use-hash.js +1 -1
- package/dist/esm/hooks/use-strict-memo.js +1 -1
- package/dist/esm/index.js +4 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/interface/{adminInterface.js → admin-interface.js} +19 -3
- package/dist/esm/interface/admin-interface.js.map +1 -0
- package/dist/esm/interface/{clientInterface.js → client-interface.js} +74 -856
- package/dist/esm/interface/client-interface.js.map +1 -0
- package/dist/esm/interface/crud/contact-channels.js +2 -2
- package/dist/esm/interface/crud/current-user.js +4 -4
- package/dist/esm/interface/crud/email-templates.js +3 -3
- package/dist/esm/interface/crud/email-templates.js.map +1 -1
- package/dist/esm/interface/crud/emails.js +3 -3
- package/dist/esm/interface/crud/internal-api-keys.js +2 -2
- package/dist/esm/interface/crud/notification-preferences.js +20 -0
- package/dist/esm/interface/crud/notification-preferences.js.map +1 -0
- package/dist/esm/interface/crud/oauth.js +2 -2
- package/dist/esm/interface/crud/project-api-keys.js +3 -3
- package/dist/esm/interface/crud/project-permissions.js +3 -3
- package/dist/esm/interface/crud/projects.js +3 -3
- package/dist/esm/interface/crud/sessions.js +3 -3
- package/dist/esm/interface/crud/svix-token.js +2 -2
- package/dist/esm/interface/crud/team-invitation-details.js +3 -3
- package/dist/esm/interface/crud/team-invitation.js +3 -3
- package/dist/esm/interface/crud/team-member-profiles.js +4 -4
- package/dist/esm/interface/crud/team-memberships.js +2 -2
- package/dist/esm/interface/crud/team-permissions.js +3 -3
- package/dist/esm/interface/crud/teams.js +3 -3
- package/dist/esm/interface/crud/users.js +3 -3
- package/dist/esm/interface/{serverInterface.js → server-interface.js} +34 -8
- package/dist/esm/interface/server-interface.js.map +1 -0
- package/dist/esm/interface/webhooks.js +4 -4
- package/dist/esm/known-errors.js +3 -3
- package/dist/esm/schema-fields.js +11 -11
- package/dist/esm/schema-fields.js.map +1 -1
- package/dist/esm/sessions.js +2 -2
- package/dist/esm/utils/api-keys.js +3 -3
- package/dist/esm/utils/arrays.js +1 -1
- package/dist/esm/utils/bytes.js +4 -27
- package/dist/esm/utils/bytes.js.map +1 -1
- package/dist/esm/utils/caches.js +4 -4
- package/dist/esm/utils/crypto.js +3 -3
- package/dist/esm/utils/dates.js +1 -1
- package/dist/esm/utils/env.js +2 -2
- package/dist/esm/utils/errors.js +3 -3
- package/dist/esm/utils/geo.js +1 -1
- package/dist/esm/utils/hashes.js +1 -1
- package/dist/esm/utils/html.js +1 -1
- package/dist/esm/utils/http.js +1 -1
- package/dist/esm/utils/json.js +1 -1
- package/dist/esm/utils/jwt.js +4 -4
- package/dist/esm/utils/maps.js +1 -1
- package/dist/esm/utils/node-http.js +1 -1
- package/dist/esm/utils/objects.js +3 -3
- package/dist/esm/utils/promises.js +5 -5
- package/dist/esm/utils/proxies.js +1 -1
- package/dist/esm/utils/react.js +3 -3
- package/dist/esm/utils/results.js +2 -2
- package/dist/esm/utils/stores.js +4 -4
- package/dist/esm/utils/strings.js +6 -3
- package/dist/esm/utils/strings.js.map +1 -1
- package/dist/esm/utils/strings.nicify.test.js +1 -1
- package/dist/esm/utils/unicode.js +1 -1
- package/dist/esm/utils/urls.js +2 -2
- package/dist/esm/utils/uuids.js +1 -1
- package/dist/helpers/password.js +1 -1
- package/dist/helpers/production-mode.js +2 -2
- package/dist/hooks/use-async-callback.js +1 -1
- package/dist/hooks/use-async-external-store.js +1 -1
- package/dist/hooks/use-hash.js +1 -1
- package/dist/hooks/use-strict-memo.js +1 -1
- package/dist/index.d.mts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +7 -7
- package/dist/index.js.map +1 -1
- package/dist/interface/{adminInterface.d.mts → admin-interface.d.mts} +4 -2
- package/dist/interface/{adminInterface.d.ts → admin-interface.d.ts} +4 -2
- package/dist/interface/{adminInterface.js → admin-interface.js} +23 -7
- package/dist/interface/admin-interface.js.map +1 -0
- package/dist/interface/{clientInterface.d.mts → client-interface.d.mts} +8 -0
- package/dist/interface/{clientInterface.d.ts → client-interface.d.ts} +8 -0
- package/dist/interface/{clientInterface.js → client-interface.js} +87 -861
- package/dist/interface/client-interface.js.map +1 -0
- package/dist/interface/crud/contact-channels.js +2 -2
- package/dist/interface/crud/current-user.js +4 -4
- package/dist/interface/crud/email-templates.d.mts +5 -5
- package/dist/interface/crud/email-templates.d.ts +5 -5
- package/dist/interface/crud/email-templates.js +3 -3
- package/dist/interface/crud/email-templates.js.map +1 -1
- package/dist/interface/crud/emails.js +3 -3
- package/dist/interface/crud/internal-api-keys.js +2 -2
- package/dist/interface/crud/notification-preferences.d.mts +25 -0
- package/dist/interface/crud/notification-preferences.d.ts +25 -0
- package/dist/interface/crud/notification-preferences.js +45 -0
- package/dist/interface/crud/notification-preferences.js.map +1 -0
- package/dist/interface/crud/oauth.js +2 -2
- package/dist/interface/crud/project-api-keys.js +3 -3
- package/dist/interface/crud/project-permissions.js +3 -3
- package/dist/interface/crud/projects.js +3 -3
- package/dist/interface/crud/sessions.js +3 -3
- package/dist/interface/crud/svix-token.js +2 -2
- package/dist/interface/crud/team-invitation-details.js +3 -3
- package/dist/interface/crud/team-invitation.js +3 -3
- package/dist/interface/crud/team-member-profiles.js +4 -4
- package/dist/interface/crud/team-memberships.js +2 -2
- package/dist/interface/crud/team-permissions.js +3 -3
- package/dist/interface/crud/teams.js +3 -3
- package/dist/interface/crud/users.js +3 -3
- package/dist/interface/{serverInterface.d.mts → server-interface.d.mts} +4 -1
- package/dist/interface/{serverInterface.d.ts → server-interface.d.ts} +4 -1
- package/dist/interface/{serverInterface.js → server-interface.js} +38 -12
- package/dist/interface/server-interface.js.map +1 -0
- package/dist/interface/webhooks.js +4 -4
- package/dist/known-errors.js +3 -3
- package/dist/schema-fields.js +11 -11
- package/dist/schema-fields.js.map +1 -1
- package/dist/sessions.js +2 -2
- package/dist/utils/api-keys.js +3 -3
- package/dist/utils/arrays.js +1 -1
- package/dist/utils/bytes.js +4 -27
- package/dist/utils/bytes.js.map +1 -1
- package/dist/utils/caches.js +4 -4
- package/dist/utils/crypto.js +3 -3
- package/dist/utils/dates.js +1 -1
- package/dist/utils/env.js +2 -2
- package/dist/utils/errors.js +3 -3
- package/dist/utils/geo.js +1 -1
- package/dist/utils/hashes.js +1 -1
- package/dist/utils/html.js +1 -1
- package/dist/utils/http.js +1 -1
- package/dist/utils/json.js +1 -1
- package/dist/utils/jwt.js +4 -4
- package/dist/utils/maps.js +1 -1
- package/dist/utils/node-http.js +1 -1
- package/dist/utils/objects.js +3 -3
- package/dist/utils/promises.js +5 -5
- package/dist/utils/proxies.js +1 -1
- package/dist/utils/react.js +3 -3
- package/dist/utils/results.js +2 -2
- package/dist/utils/stores.js +4 -4
- package/dist/utils/strings.js +6 -3
- package/dist/utils/strings.js.map +1 -1
- package/dist/utils/strings.nicify.test.js +1 -1
- package/dist/utils/unicode.js +1 -1
- package/dist/utils/urls.js +2 -2
- package/dist/utils/uuids.js +1 -1
- package/package.json +1 -1
- package/dist/esm/interface/adminInterface.js.map +0 -1
- package/dist/esm/interface/clientInterface.js.map +0 -1
- package/dist/esm/interface/serverInterface.js.map +0 -1
- package/dist/interface/adminInterface.js.map +0 -1
- package/dist/interface/clientInterface.js.map +0 -1
- package/dist/interface/serverInterface.js.map +0 -1
|
@@ -1,848 +1,15 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return input instanceof expected || Object.getPrototypeOf(input)[Symbol.toStringTag] === expected.prototype[Symbol.toStringTag];
|
|
14
|
-
} catch {
|
|
15
|
-
return false;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
var clockSkew = Symbol();
|
|
19
|
-
var clockTolerance = Symbol();
|
|
20
|
-
var customFetch = Symbol();
|
|
21
|
-
var useMtlsAlias = Symbol();
|
|
22
|
-
var encoder = new TextEncoder();
|
|
23
|
-
var decoder = new TextDecoder();
|
|
24
|
-
function buf(input) {
|
|
25
|
-
if (typeof input === "string") {
|
|
26
|
-
return encoder.encode(input);
|
|
27
|
-
}
|
|
28
|
-
return decoder.decode(input);
|
|
29
|
-
}
|
|
30
|
-
var CHUNK_SIZE = 32768;
|
|
31
|
-
function encodeBase64Url(input) {
|
|
32
|
-
if (input instanceof ArrayBuffer) {
|
|
33
|
-
input = new Uint8Array(input);
|
|
34
|
-
}
|
|
35
|
-
const arr = [];
|
|
36
|
-
for (let i = 0; i < input.byteLength; i += CHUNK_SIZE) {
|
|
37
|
-
arr.push(String.fromCharCode.apply(null, input.subarray(i, i + CHUNK_SIZE)));
|
|
38
|
-
}
|
|
39
|
-
return btoa(arr.join("")).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
40
|
-
}
|
|
41
|
-
function decodeBase64Url(input) {
|
|
42
|
-
try {
|
|
43
|
-
const binary = atob(input.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, ""));
|
|
44
|
-
const bytes = new Uint8Array(binary.length);
|
|
45
|
-
for (let i = 0; i < binary.length; i++) {
|
|
46
|
-
bytes[i] = binary.charCodeAt(i);
|
|
47
|
-
}
|
|
48
|
-
return bytes;
|
|
49
|
-
} catch (cause) {
|
|
50
|
-
throw new OPE("The input to be decoded is not correctly encoded.", { cause });
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
function b64u(input) {
|
|
54
|
-
if (typeof input === "string") {
|
|
55
|
-
return decodeBase64Url(input);
|
|
56
|
-
}
|
|
57
|
-
return encodeBase64Url(input);
|
|
58
|
-
}
|
|
59
|
-
var LRU = class {
|
|
60
|
-
constructor(maxSize) {
|
|
61
|
-
this.cache = /* @__PURE__ */ new Map();
|
|
62
|
-
this._cache = /* @__PURE__ */ new Map();
|
|
63
|
-
this.maxSize = maxSize;
|
|
64
|
-
}
|
|
65
|
-
get(key) {
|
|
66
|
-
let v = this.cache.get(key);
|
|
67
|
-
if (v) {
|
|
68
|
-
return v;
|
|
69
|
-
}
|
|
70
|
-
if (v = this._cache.get(key)) {
|
|
71
|
-
this.update(key, v);
|
|
72
|
-
return v;
|
|
73
|
-
}
|
|
74
|
-
return void 0;
|
|
75
|
-
}
|
|
76
|
-
has(key) {
|
|
77
|
-
return this.cache.has(key) || this._cache.has(key);
|
|
78
|
-
}
|
|
79
|
-
set(key, value) {
|
|
80
|
-
if (this.cache.has(key)) {
|
|
81
|
-
this.cache.set(key, value);
|
|
82
|
-
} else {
|
|
83
|
-
this.update(key, value);
|
|
84
|
-
}
|
|
85
|
-
return this;
|
|
86
|
-
}
|
|
87
|
-
delete(key) {
|
|
88
|
-
if (this.cache.has(key)) {
|
|
89
|
-
return this.cache.delete(key);
|
|
90
|
-
}
|
|
91
|
-
if (this._cache.has(key)) {
|
|
92
|
-
return this._cache.delete(key);
|
|
93
|
-
}
|
|
94
|
-
return false;
|
|
95
|
-
}
|
|
96
|
-
update(key, value) {
|
|
97
|
-
this.cache.set(key, value);
|
|
98
|
-
if (this.cache.size >= this.maxSize) {
|
|
99
|
-
this._cache = this.cache;
|
|
100
|
-
this.cache = /* @__PURE__ */ new Map();
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
var UnsupportedOperationError = class extends Error {
|
|
105
|
-
constructor(message) {
|
|
106
|
-
super(message ?? "operation not supported");
|
|
107
|
-
this.name = this.constructor.name;
|
|
108
|
-
Error.captureStackTrace?.(this, this.constructor);
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
var OperationProcessingError = class extends Error {
|
|
112
|
-
constructor(message, options) {
|
|
113
|
-
super(message, options);
|
|
114
|
-
this.name = this.constructor.name;
|
|
115
|
-
Error.captureStackTrace?.(this, this.constructor);
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
var OPE = OperationProcessingError;
|
|
119
|
-
var dpopNonces = new LRU(100);
|
|
120
|
-
function isCryptoKey(key) {
|
|
121
|
-
return key instanceof CryptoKey;
|
|
122
|
-
}
|
|
123
|
-
function isPrivateKey(key) {
|
|
124
|
-
return isCryptoKey(key) && key.type === "private";
|
|
125
|
-
}
|
|
126
|
-
function isPublicKey(key) {
|
|
127
|
-
return isCryptoKey(key) && key.type === "public";
|
|
128
|
-
}
|
|
129
|
-
function processDpopNonce(response) {
|
|
130
|
-
try {
|
|
131
|
-
const nonce = response.headers.get("dpop-nonce");
|
|
132
|
-
if (nonce) {
|
|
133
|
-
dpopNonces.set(new URL(response.url).origin, nonce);
|
|
134
|
-
}
|
|
135
|
-
} catch {
|
|
136
|
-
}
|
|
137
|
-
return response;
|
|
138
|
-
}
|
|
139
|
-
function isJsonObject(input) {
|
|
140
|
-
if (input === null || typeof input !== "object" || Array.isArray(input)) {
|
|
141
|
-
return false;
|
|
142
|
-
}
|
|
143
|
-
return true;
|
|
144
|
-
}
|
|
145
|
-
function prepareHeaders(input) {
|
|
146
|
-
if (looseInstanceOf(input, Headers)) {
|
|
147
|
-
input = Object.fromEntries(input.entries());
|
|
148
|
-
}
|
|
149
|
-
const headers = new Headers(input);
|
|
150
|
-
if (USER_AGENT && !headers.has("user-agent")) {
|
|
151
|
-
headers.set("user-agent", USER_AGENT);
|
|
152
|
-
}
|
|
153
|
-
if (headers.has("authorization")) {
|
|
154
|
-
throw new TypeError('"options.headers" must not include the "authorization" header name');
|
|
155
|
-
}
|
|
156
|
-
if (headers.has("dpop")) {
|
|
157
|
-
throw new TypeError('"options.headers" must not include the "dpop" header name');
|
|
158
|
-
}
|
|
159
|
-
return headers;
|
|
160
|
-
}
|
|
161
|
-
function signal(value) {
|
|
162
|
-
if (typeof value === "function") {
|
|
163
|
-
value = value();
|
|
164
|
-
}
|
|
165
|
-
if (!(value instanceof AbortSignal)) {
|
|
166
|
-
throw new TypeError('"options.signal" must return or be an instance of AbortSignal');
|
|
167
|
-
}
|
|
168
|
-
return value;
|
|
169
|
-
}
|
|
170
|
-
function validateString(input) {
|
|
171
|
-
return typeof input === "string" && input.length !== 0;
|
|
172
|
-
}
|
|
173
|
-
function randomBytes() {
|
|
174
|
-
return b64u(crypto.getRandomValues(new Uint8Array(32)));
|
|
175
|
-
}
|
|
176
|
-
function getKeyAndKid(input) {
|
|
177
|
-
if (input instanceof CryptoKey) {
|
|
178
|
-
return { key: input };
|
|
179
|
-
}
|
|
180
|
-
if (!(input?.key instanceof CryptoKey)) {
|
|
181
|
-
return {};
|
|
182
|
-
}
|
|
183
|
-
if (input.kid !== void 0 && !validateString(input.kid)) {
|
|
184
|
-
throw new TypeError('"kid" must be a non-empty string');
|
|
185
|
-
}
|
|
186
|
-
return { key: input.key, kid: input.kid };
|
|
187
|
-
}
|
|
188
|
-
function formUrlEncode(token) {
|
|
189
|
-
return encodeURIComponent(token).replace(/%20/g, "+");
|
|
190
|
-
}
|
|
191
|
-
function clientSecretBasic(clientId, clientSecret) {
|
|
192
|
-
const username = formUrlEncode(clientId);
|
|
193
|
-
const password = formUrlEncode(clientSecret);
|
|
194
|
-
const credentials = btoa(`${username}:${password}`);
|
|
195
|
-
return `Basic ${credentials}`;
|
|
196
|
-
}
|
|
197
|
-
function psAlg(key) {
|
|
198
|
-
switch (key.algorithm.hash.name) {
|
|
199
|
-
case "SHA-256":
|
|
200
|
-
return "PS256";
|
|
201
|
-
case "SHA-384":
|
|
202
|
-
return "PS384";
|
|
203
|
-
case "SHA-512":
|
|
204
|
-
return "PS512";
|
|
205
|
-
default:
|
|
206
|
-
throw new UnsupportedOperationError("unsupported RsaHashedKeyAlgorithm hash name");
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
function rsAlg(key) {
|
|
210
|
-
switch (key.algorithm.hash.name) {
|
|
211
|
-
case "SHA-256":
|
|
212
|
-
return "RS256";
|
|
213
|
-
case "SHA-384":
|
|
214
|
-
return "RS384";
|
|
215
|
-
case "SHA-512":
|
|
216
|
-
return "RS512";
|
|
217
|
-
default:
|
|
218
|
-
throw new UnsupportedOperationError("unsupported RsaHashedKeyAlgorithm hash name");
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
function esAlg(key) {
|
|
222
|
-
switch (key.algorithm.namedCurve) {
|
|
223
|
-
case "P-256":
|
|
224
|
-
return "ES256";
|
|
225
|
-
case "P-384":
|
|
226
|
-
return "ES384";
|
|
227
|
-
case "P-521":
|
|
228
|
-
return "ES512";
|
|
229
|
-
default:
|
|
230
|
-
throw new UnsupportedOperationError("unsupported EcKeyAlgorithm namedCurve");
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
function keyToJws(key) {
|
|
234
|
-
switch (key.algorithm.name) {
|
|
235
|
-
case "RSA-PSS":
|
|
236
|
-
return psAlg(key);
|
|
237
|
-
case "RSASSA-PKCS1-v1_5":
|
|
238
|
-
return rsAlg(key);
|
|
239
|
-
case "ECDSA":
|
|
240
|
-
return esAlg(key);
|
|
241
|
-
case "Ed25519":
|
|
242
|
-
case "Ed448":
|
|
243
|
-
return "EdDSA";
|
|
244
|
-
default:
|
|
245
|
-
throw new UnsupportedOperationError("unsupported CryptoKey algorithm name");
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
function getClockSkew(client) {
|
|
249
|
-
const skew = client?.[clockSkew];
|
|
250
|
-
return typeof skew === "number" && Number.isFinite(skew) ? skew : 0;
|
|
251
|
-
}
|
|
252
|
-
function getClockTolerance(client) {
|
|
253
|
-
const tolerance = client?.[clockTolerance];
|
|
254
|
-
return typeof tolerance === "number" && Number.isFinite(tolerance) && Math.sign(tolerance) !== -1 ? tolerance : 30;
|
|
255
|
-
}
|
|
256
|
-
function epochTime() {
|
|
257
|
-
return Math.floor(Date.now() / 1e3);
|
|
258
|
-
}
|
|
259
|
-
function clientAssertion(as, client) {
|
|
260
|
-
const now = epochTime() + getClockSkew(client);
|
|
261
|
-
return {
|
|
262
|
-
jti: randomBytes(),
|
|
263
|
-
aud: [as.issuer, as.token_endpoint],
|
|
264
|
-
exp: now + 60,
|
|
265
|
-
iat: now,
|
|
266
|
-
nbf: now,
|
|
267
|
-
iss: client.client_id,
|
|
268
|
-
sub: client.client_id
|
|
269
|
-
};
|
|
270
|
-
}
|
|
271
|
-
async function privateKeyJwt(as, client, key, kid) {
|
|
272
|
-
return jwt({
|
|
273
|
-
alg: keyToJws(key),
|
|
274
|
-
kid
|
|
275
|
-
}, clientAssertion(as, client), key);
|
|
276
|
-
}
|
|
277
|
-
function assertAs(as) {
|
|
278
|
-
if (typeof as !== "object" || as === null) {
|
|
279
|
-
throw new TypeError('"as" must be an object');
|
|
280
|
-
}
|
|
281
|
-
if (!validateString(as.issuer)) {
|
|
282
|
-
throw new TypeError('"as.issuer" property must be a non-empty string');
|
|
283
|
-
}
|
|
284
|
-
return true;
|
|
285
|
-
}
|
|
286
|
-
function assertClient(client) {
|
|
287
|
-
if (typeof client !== "object" || client === null) {
|
|
288
|
-
throw new TypeError('"client" must be an object');
|
|
289
|
-
}
|
|
290
|
-
if (!validateString(client.client_id)) {
|
|
291
|
-
throw new TypeError('"client.client_id" property must be a non-empty string');
|
|
292
|
-
}
|
|
293
|
-
return true;
|
|
294
|
-
}
|
|
295
|
-
function assertClientSecret(clientSecret) {
|
|
296
|
-
if (!validateString(clientSecret)) {
|
|
297
|
-
throw new TypeError('"client.client_secret" property must be a non-empty string');
|
|
298
|
-
}
|
|
299
|
-
return clientSecret;
|
|
300
|
-
}
|
|
301
|
-
function assertNoClientPrivateKey(clientAuthMethod, clientPrivateKey) {
|
|
302
|
-
if (clientPrivateKey !== void 0) {
|
|
303
|
-
throw new TypeError(`"options.clientPrivateKey" property must not be provided when ${clientAuthMethod} client authentication method is used.`);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
function assertNoClientSecret(clientAuthMethod, clientSecret) {
|
|
307
|
-
if (clientSecret !== void 0) {
|
|
308
|
-
throw new TypeError(`"client.client_secret" property must not be provided when ${clientAuthMethod} client authentication method is used.`);
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
async function clientAuthentication(as, client, body, headers, clientPrivateKey) {
|
|
312
|
-
body.delete("client_secret");
|
|
313
|
-
body.delete("client_assertion_type");
|
|
314
|
-
body.delete("client_assertion");
|
|
315
|
-
switch (client.token_endpoint_auth_method) {
|
|
316
|
-
case void 0:
|
|
317
|
-
case "client_secret_basic": {
|
|
318
|
-
assertNoClientPrivateKey("client_secret_basic", clientPrivateKey);
|
|
319
|
-
headers.set("authorization", clientSecretBasic(client.client_id, assertClientSecret(client.client_secret)));
|
|
320
|
-
break;
|
|
321
|
-
}
|
|
322
|
-
case "client_secret_post": {
|
|
323
|
-
assertNoClientPrivateKey("client_secret_post", clientPrivateKey);
|
|
324
|
-
body.set("client_id", client.client_id);
|
|
325
|
-
body.set("client_secret", assertClientSecret(client.client_secret));
|
|
326
|
-
break;
|
|
327
|
-
}
|
|
328
|
-
case "private_key_jwt": {
|
|
329
|
-
assertNoClientSecret("private_key_jwt", client.client_secret);
|
|
330
|
-
if (clientPrivateKey === void 0) {
|
|
331
|
-
throw new TypeError('"options.clientPrivateKey" must be provided when "client.token_endpoint_auth_method" is "private_key_jwt"');
|
|
332
|
-
}
|
|
333
|
-
const { key, kid } = getKeyAndKid(clientPrivateKey);
|
|
334
|
-
if (!isPrivateKey(key)) {
|
|
335
|
-
throw new TypeError('"options.clientPrivateKey.key" must be a private CryptoKey');
|
|
336
|
-
}
|
|
337
|
-
body.set("client_id", client.client_id);
|
|
338
|
-
body.set("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
|
|
339
|
-
body.set("client_assertion", await privateKeyJwt(as, client, key, kid));
|
|
340
|
-
break;
|
|
341
|
-
}
|
|
342
|
-
case "tls_client_auth":
|
|
343
|
-
case "self_signed_tls_client_auth":
|
|
344
|
-
case "none": {
|
|
345
|
-
assertNoClientSecret(client.token_endpoint_auth_method, client.client_secret);
|
|
346
|
-
assertNoClientPrivateKey(client.token_endpoint_auth_method, clientPrivateKey);
|
|
347
|
-
body.set("client_id", client.client_id);
|
|
348
|
-
break;
|
|
349
|
-
}
|
|
350
|
-
default:
|
|
351
|
-
throw new UnsupportedOperationError("unsupported client token_endpoint_auth_method");
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
async function jwt(header, claimsSet, key) {
|
|
355
|
-
if (!key.usages.includes("sign")) {
|
|
356
|
-
throw new TypeError('CryptoKey instances used for signing assertions must include "sign" in their "usages"');
|
|
357
|
-
}
|
|
358
|
-
const input = `${b64u(buf(JSON.stringify(header)))}.${b64u(buf(JSON.stringify(claimsSet)))}`;
|
|
359
|
-
const signature = b64u(await crypto.subtle.sign(keyToSubtle(key), key, buf(input)));
|
|
360
|
-
return `${input}.${signature}`;
|
|
361
|
-
}
|
|
362
|
-
async function dpopProofJwt(headers, options, url, htm, clockSkew2, accessToken) {
|
|
363
|
-
const { privateKey, publicKey, nonce = dpopNonces.get(url.origin) } = options;
|
|
364
|
-
if (!isPrivateKey(privateKey)) {
|
|
365
|
-
throw new TypeError('"DPoP.privateKey" must be a private CryptoKey');
|
|
366
|
-
}
|
|
367
|
-
if (!isPublicKey(publicKey)) {
|
|
368
|
-
throw new TypeError('"DPoP.publicKey" must be a public CryptoKey');
|
|
369
|
-
}
|
|
370
|
-
if (nonce !== void 0 && !validateString(nonce)) {
|
|
371
|
-
throw new TypeError('"DPoP.nonce" must be a non-empty string or undefined');
|
|
372
|
-
}
|
|
373
|
-
if (!publicKey.extractable) {
|
|
374
|
-
throw new TypeError('"DPoP.publicKey.extractable" must be true');
|
|
375
|
-
}
|
|
376
|
-
const now = epochTime() + clockSkew2;
|
|
377
|
-
const proof = await jwt({
|
|
378
|
-
alg: keyToJws(privateKey),
|
|
379
|
-
typ: "dpop+jwt",
|
|
380
|
-
jwk: await publicJwk(publicKey)
|
|
381
|
-
}, {
|
|
382
|
-
iat: now,
|
|
383
|
-
jti: randomBytes(),
|
|
384
|
-
htm,
|
|
385
|
-
nonce,
|
|
386
|
-
htu: `${url.origin}${url.pathname}`,
|
|
387
|
-
ath: accessToken ? b64u(await crypto.subtle.digest("SHA-256", buf(accessToken))) : void 0
|
|
388
|
-
}, privateKey);
|
|
389
|
-
headers.set("dpop", proof);
|
|
390
|
-
}
|
|
391
|
-
var jwkCache;
|
|
392
|
-
async function getSetPublicJwkCache(key) {
|
|
393
|
-
const { kty, e, n, x, y, crv } = await crypto.subtle.exportKey("jwk", key);
|
|
394
|
-
const jwk = { kty, e, n, x, y, crv };
|
|
395
|
-
jwkCache.set(key, jwk);
|
|
396
|
-
return jwk;
|
|
397
|
-
}
|
|
398
|
-
async function publicJwk(key) {
|
|
399
|
-
jwkCache || (jwkCache = /* @__PURE__ */ new WeakMap());
|
|
400
|
-
return jwkCache.get(key) || getSetPublicJwkCache(key);
|
|
401
|
-
}
|
|
402
|
-
function validateEndpoint(value, endpoint, options) {
|
|
403
|
-
if (typeof value !== "string") {
|
|
404
|
-
if (options?.[useMtlsAlias]) {
|
|
405
|
-
throw new TypeError(`"as.mtls_endpoint_aliases.${endpoint}" must be a string`);
|
|
406
|
-
}
|
|
407
|
-
throw new TypeError(`"as.${endpoint}" must be a string`);
|
|
408
|
-
}
|
|
409
|
-
return new URL(value);
|
|
410
|
-
}
|
|
411
|
-
function resolveEndpoint(as, endpoint, options) {
|
|
412
|
-
if (options?.[useMtlsAlias] && as.mtls_endpoint_aliases && endpoint in as.mtls_endpoint_aliases) {
|
|
413
|
-
return validateEndpoint(as.mtls_endpoint_aliases[endpoint], endpoint, options);
|
|
414
|
-
}
|
|
415
|
-
return validateEndpoint(as[endpoint], endpoint);
|
|
416
|
-
}
|
|
417
|
-
function isOAuth2Error(input) {
|
|
418
|
-
const value = input;
|
|
419
|
-
if (typeof value !== "object" || Array.isArray(value) || value === null) {
|
|
420
|
-
return false;
|
|
421
|
-
}
|
|
422
|
-
return value.error !== void 0;
|
|
423
|
-
}
|
|
424
|
-
var skipSubjectCheck = Symbol();
|
|
425
|
-
async function authenticatedRequest(as, client, method, url, body, headers, options) {
|
|
426
|
-
await clientAuthentication(as, client, body, headers, options?.clientPrivateKey);
|
|
427
|
-
headers.set("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
|
|
428
|
-
return (options?.[customFetch] || fetch)(url.href, {
|
|
429
|
-
body,
|
|
430
|
-
headers: Object.fromEntries(headers.entries()),
|
|
431
|
-
method,
|
|
432
|
-
redirect: "manual",
|
|
433
|
-
signal: options?.signal ? signal(options.signal) : null
|
|
434
|
-
}).then(processDpopNonce);
|
|
435
|
-
}
|
|
436
|
-
async function tokenEndpointRequest(as, client, grantType, parameters, options) {
|
|
437
|
-
const url = resolveEndpoint(as, "token_endpoint", options);
|
|
438
|
-
parameters.set("grant_type", grantType);
|
|
439
|
-
const headers = prepareHeaders(options?.headers);
|
|
440
|
-
headers.set("accept", "application/json");
|
|
441
|
-
if (options?.DPoP !== void 0) {
|
|
442
|
-
await dpopProofJwt(headers, options.DPoP, url, "POST", getClockSkew(client));
|
|
443
|
-
}
|
|
444
|
-
return authenticatedRequest(as, client, "POST", url, parameters, headers, options);
|
|
445
|
-
}
|
|
446
|
-
async function refreshTokenGrantRequest(as, client, refreshToken, options) {
|
|
447
|
-
assertAs(as);
|
|
448
|
-
assertClient(client);
|
|
449
|
-
if (!validateString(refreshToken)) {
|
|
450
|
-
throw new TypeError('"refreshToken" must be a non-empty string');
|
|
451
|
-
}
|
|
452
|
-
const parameters = new URLSearchParams(options?.additionalParameters);
|
|
453
|
-
parameters.set("refresh_token", refreshToken);
|
|
454
|
-
return tokenEndpointRequest(as, client, "refresh_token", parameters, options);
|
|
455
|
-
}
|
|
456
|
-
var idTokenClaims = /* @__PURE__ */ new WeakMap();
|
|
457
|
-
async function processGenericAccessTokenResponse(as, client, response, ignoreIdToken = false, ignoreRefreshToken = false) {
|
|
458
|
-
assertAs(as);
|
|
459
|
-
assertClient(client);
|
|
460
|
-
if (!looseInstanceOf(response, Response)) {
|
|
461
|
-
throw new TypeError('"response" must be an instance of Response');
|
|
462
|
-
}
|
|
463
|
-
if (response.status !== 200) {
|
|
464
|
-
let err;
|
|
465
|
-
if (err = await handleOAuthBodyError(response)) {
|
|
466
|
-
return err;
|
|
467
|
-
}
|
|
468
|
-
throw new OPE('"response" is not a conform Token Endpoint response');
|
|
469
|
-
}
|
|
470
|
-
assertReadableResponse(response);
|
|
471
|
-
let json;
|
|
472
|
-
try {
|
|
473
|
-
json = await response.json();
|
|
474
|
-
} catch (cause) {
|
|
475
|
-
throw new OPE('failed to parse "response" body as JSON', { cause });
|
|
476
|
-
}
|
|
477
|
-
if (!isJsonObject(json)) {
|
|
478
|
-
throw new OPE('"response" body must be a top level object');
|
|
479
|
-
}
|
|
480
|
-
if (!validateString(json.access_token)) {
|
|
481
|
-
throw new OPE('"response" body "access_token" property must be a non-empty string');
|
|
482
|
-
}
|
|
483
|
-
if (!validateString(json.token_type)) {
|
|
484
|
-
throw new OPE('"response" body "token_type" property must be a non-empty string');
|
|
485
|
-
}
|
|
486
|
-
json.token_type = json.token_type.toLowerCase();
|
|
487
|
-
if (json.token_type !== "dpop" && json.token_type !== "bearer") {
|
|
488
|
-
throw new UnsupportedOperationError("unsupported `token_type` value");
|
|
489
|
-
}
|
|
490
|
-
if (json.expires_in !== void 0 && (typeof json.expires_in !== "number" || json.expires_in <= 0)) {
|
|
491
|
-
throw new OPE('"response" body "expires_in" property must be a positive number');
|
|
492
|
-
}
|
|
493
|
-
if (!ignoreRefreshToken && json.refresh_token !== void 0 && !validateString(json.refresh_token)) {
|
|
494
|
-
throw new OPE('"response" body "refresh_token" property must be a non-empty string');
|
|
495
|
-
}
|
|
496
|
-
if (json.scope !== void 0 && typeof json.scope !== "string") {
|
|
497
|
-
throw new OPE('"response" body "scope" property must be a string');
|
|
498
|
-
}
|
|
499
|
-
if (!ignoreIdToken) {
|
|
500
|
-
if (json.id_token !== void 0 && !validateString(json.id_token)) {
|
|
501
|
-
throw new OPE('"response" body "id_token" property must be a non-empty string');
|
|
502
|
-
}
|
|
503
|
-
if (json.id_token) {
|
|
504
|
-
const { claims } = await validateJwt(json.id_token, checkSigningAlgorithm.bind(void 0, client.id_token_signed_response_alg, as.id_token_signing_alg_values_supported), noSignatureCheck, getClockSkew(client), getClockTolerance(client)).then(validatePresence.bind(void 0, ["aud", "exp", "iat", "iss", "sub"])).then(validateIssuer.bind(void 0, as.issuer)).then(validateAudience.bind(void 0, client.client_id));
|
|
505
|
-
if (Array.isArray(claims.aud) && claims.aud.length !== 1 && claims.azp !== client.client_id) {
|
|
506
|
-
throw new OPE('unexpected ID Token "azp" (authorized party) claim value');
|
|
507
|
-
}
|
|
508
|
-
if (client.require_auth_time && typeof claims.auth_time !== "number") {
|
|
509
|
-
throw new OPE('unexpected ID Token "auth_time" (authentication time) claim value');
|
|
510
|
-
}
|
|
511
|
-
idTokenClaims.set(json, claims);
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
return json;
|
|
515
|
-
}
|
|
516
|
-
async function processRefreshTokenResponse(as, client, response) {
|
|
517
|
-
return processGenericAccessTokenResponse(as, client, response);
|
|
518
|
-
}
|
|
519
|
-
function validateAudience(expected, result) {
|
|
520
|
-
if (Array.isArray(result.claims.aud)) {
|
|
521
|
-
if (!result.claims.aud.includes(expected)) {
|
|
522
|
-
throw new OPE('unexpected JWT "aud" (audience) claim value');
|
|
523
|
-
}
|
|
524
|
-
} else if (result.claims.aud !== expected) {
|
|
525
|
-
throw new OPE('unexpected JWT "aud" (audience) claim value');
|
|
526
|
-
}
|
|
527
|
-
return result;
|
|
528
|
-
}
|
|
529
|
-
function validateIssuer(expected, result) {
|
|
530
|
-
if (result.claims.iss !== expected) {
|
|
531
|
-
throw new OPE('unexpected JWT "iss" (issuer) claim value');
|
|
532
|
-
}
|
|
533
|
-
return result;
|
|
534
|
-
}
|
|
535
|
-
var branded = /* @__PURE__ */ new WeakSet();
|
|
536
|
-
function brand(searchParams) {
|
|
537
|
-
branded.add(searchParams);
|
|
538
|
-
return searchParams;
|
|
539
|
-
}
|
|
540
|
-
async function authorizationCodeGrantRequest(as, client, callbackParameters, redirectUri, codeVerifier, options) {
|
|
541
|
-
assertAs(as);
|
|
542
|
-
assertClient(client);
|
|
543
|
-
if (!branded.has(callbackParameters)) {
|
|
544
|
-
throw new TypeError('"callbackParameters" must be an instance of URLSearchParams obtained from "validateAuthResponse()", or "validateJwtAuthResponse()');
|
|
545
|
-
}
|
|
546
|
-
if (!validateString(redirectUri)) {
|
|
547
|
-
throw new TypeError('"redirectUri" must be a non-empty string');
|
|
548
|
-
}
|
|
549
|
-
if (!validateString(codeVerifier)) {
|
|
550
|
-
throw new TypeError('"codeVerifier" must be a non-empty string');
|
|
551
|
-
}
|
|
552
|
-
const code = getURLSearchParameter(callbackParameters, "code");
|
|
553
|
-
if (!code) {
|
|
554
|
-
throw new OPE('no authorization code in "callbackParameters"');
|
|
555
|
-
}
|
|
556
|
-
const parameters = new URLSearchParams(options?.additionalParameters);
|
|
557
|
-
parameters.set("redirect_uri", redirectUri);
|
|
558
|
-
parameters.set("code_verifier", codeVerifier);
|
|
559
|
-
parameters.set("code", code);
|
|
560
|
-
return tokenEndpointRequest(as, client, "authorization_code", parameters, options);
|
|
561
|
-
}
|
|
562
|
-
var jwtClaimNames = {
|
|
563
|
-
aud: "audience",
|
|
564
|
-
c_hash: "code hash",
|
|
565
|
-
client_id: "client id",
|
|
566
|
-
exp: "expiration time",
|
|
567
|
-
iat: "issued at",
|
|
568
|
-
iss: "issuer",
|
|
569
|
-
jti: "jwt id",
|
|
570
|
-
nonce: "nonce",
|
|
571
|
-
s_hash: "state hash",
|
|
572
|
-
sub: "subject",
|
|
573
|
-
ath: "access token hash",
|
|
574
|
-
htm: "http method",
|
|
575
|
-
htu: "http uri",
|
|
576
|
-
cnf: "confirmation"
|
|
577
|
-
};
|
|
578
|
-
function validatePresence(required, result) {
|
|
579
|
-
for (const claim of required) {
|
|
580
|
-
if (result.claims[claim] === void 0) {
|
|
581
|
-
throw new OPE(`JWT "${claim}" (${jwtClaimNames[claim]}) claim missing`);
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
return result;
|
|
585
|
-
}
|
|
586
|
-
var expectNoNonce = Symbol();
|
|
587
|
-
var skipAuthTimeCheck = Symbol();
|
|
588
|
-
async function processAuthorizationCodeOAuth2Response(as, client, response) {
|
|
589
|
-
const result = await processGenericAccessTokenResponse(as, client, response, true);
|
|
590
|
-
if (isOAuth2Error(result)) {
|
|
591
|
-
return result;
|
|
592
|
-
}
|
|
593
|
-
if (result.id_token !== void 0) {
|
|
594
|
-
if (typeof result.id_token === "string" && result.id_token.length) {
|
|
595
|
-
throw new OPE("Unexpected ID Token returned, use processAuthorizationCodeOpenIDResponse() for OpenID Connect callback processing");
|
|
596
|
-
}
|
|
597
|
-
delete result.id_token;
|
|
598
|
-
}
|
|
599
|
-
return result;
|
|
600
|
-
}
|
|
601
|
-
function assertReadableResponse(response) {
|
|
602
|
-
if (response.bodyUsed) {
|
|
603
|
-
throw new TypeError('"response" body has been used already');
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
async function handleOAuthBodyError(response) {
|
|
607
|
-
if (response.status > 399 && response.status < 500) {
|
|
608
|
-
assertReadableResponse(response);
|
|
609
|
-
try {
|
|
610
|
-
const json = await response.json();
|
|
611
|
-
if (isJsonObject(json) && typeof json.error === "string" && json.error.length) {
|
|
612
|
-
if (json.error_description !== void 0 && typeof json.error_description !== "string") {
|
|
613
|
-
delete json.error_description;
|
|
614
|
-
}
|
|
615
|
-
if (json.error_uri !== void 0 && typeof json.error_uri !== "string") {
|
|
616
|
-
delete json.error_uri;
|
|
617
|
-
}
|
|
618
|
-
if (json.algs !== void 0 && typeof json.algs !== "string") {
|
|
619
|
-
delete json.algs;
|
|
620
|
-
}
|
|
621
|
-
if (json.scope !== void 0 && typeof json.scope !== "string") {
|
|
622
|
-
delete json.scope;
|
|
623
|
-
}
|
|
624
|
-
return json;
|
|
625
|
-
}
|
|
626
|
-
} catch {
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
return void 0;
|
|
630
|
-
}
|
|
631
|
-
function checkRsaKeyAlgorithm(algorithm) {
|
|
632
|
-
if (typeof algorithm.modulusLength !== "number" || algorithm.modulusLength < 2048) {
|
|
633
|
-
throw new OPE(`${algorithm.name} modulusLength must be at least 2048 bits`);
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
function ecdsaHashName(namedCurve) {
|
|
637
|
-
switch (namedCurve) {
|
|
638
|
-
case "P-256":
|
|
639
|
-
return "SHA-256";
|
|
640
|
-
case "P-384":
|
|
641
|
-
return "SHA-384";
|
|
642
|
-
case "P-521":
|
|
643
|
-
return "SHA-512";
|
|
644
|
-
default:
|
|
645
|
-
throw new UnsupportedOperationError();
|
|
646
|
-
}
|
|
647
|
-
}
|
|
648
|
-
function keyToSubtle(key) {
|
|
649
|
-
switch (key.algorithm.name) {
|
|
650
|
-
case "ECDSA":
|
|
651
|
-
return {
|
|
652
|
-
name: key.algorithm.name,
|
|
653
|
-
hash: ecdsaHashName(key.algorithm.namedCurve)
|
|
654
|
-
};
|
|
655
|
-
case "RSA-PSS": {
|
|
656
|
-
checkRsaKeyAlgorithm(key.algorithm);
|
|
657
|
-
switch (key.algorithm.hash.name) {
|
|
658
|
-
case "SHA-256":
|
|
659
|
-
case "SHA-384":
|
|
660
|
-
case "SHA-512":
|
|
661
|
-
return {
|
|
662
|
-
name: key.algorithm.name,
|
|
663
|
-
saltLength: parseInt(key.algorithm.hash.name.slice(-3), 10) >> 3
|
|
664
|
-
};
|
|
665
|
-
default:
|
|
666
|
-
throw new UnsupportedOperationError();
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
case "RSASSA-PKCS1-v1_5":
|
|
670
|
-
checkRsaKeyAlgorithm(key.algorithm);
|
|
671
|
-
return key.algorithm.name;
|
|
672
|
-
case "Ed448":
|
|
673
|
-
case "Ed25519":
|
|
674
|
-
return key.algorithm.name;
|
|
675
|
-
}
|
|
676
|
-
throw new UnsupportedOperationError();
|
|
677
|
-
}
|
|
678
|
-
var noSignatureCheck = Symbol();
|
|
679
|
-
async function validateJwt(jws, checkAlg, getKey, clockSkew2, clockTolerance2) {
|
|
680
|
-
const { 0: protectedHeader, 1: payload, 2: encodedSignature, length } = jws.split(".");
|
|
681
|
-
if (length === 5) {
|
|
682
|
-
throw new UnsupportedOperationError("JWE structure JWTs are not supported");
|
|
683
|
-
}
|
|
684
|
-
if (length !== 3) {
|
|
685
|
-
throw new OPE("Invalid JWT");
|
|
686
|
-
}
|
|
687
|
-
let header;
|
|
688
|
-
try {
|
|
689
|
-
header = JSON.parse(buf(b64u(protectedHeader)));
|
|
690
|
-
} catch (cause) {
|
|
691
|
-
throw new OPE("failed to parse JWT Header body as base64url encoded JSON", { cause });
|
|
692
|
-
}
|
|
693
|
-
if (!isJsonObject(header)) {
|
|
694
|
-
throw new OPE("JWT Header must be a top level object");
|
|
695
|
-
}
|
|
696
|
-
checkAlg(header);
|
|
697
|
-
if (header.crit !== void 0) {
|
|
698
|
-
throw new OPE('unexpected JWT "crit" header parameter');
|
|
699
|
-
}
|
|
700
|
-
const signature = b64u(encodedSignature);
|
|
701
|
-
let key;
|
|
702
|
-
if (getKey !== noSignatureCheck) {
|
|
703
|
-
key = await getKey(header);
|
|
704
|
-
const input = `${protectedHeader}.${payload}`;
|
|
705
|
-
const verified = await crypto.subtle.verify(keyToSubtle(key), key, signature, buf(input));
|
|
706
|
-
if (!verified) {
|
|
707
|
-
throw new OPE("JWT signature verification failed");
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
let claims;
|
|
711
|
-
try {
|
|
712
|
-
claims = JSON.parse(buf(b64u(payload)));
|
|
713
|
-
} catch (cause) {
|
|
714
|
-
throw new OPE("failed to parse JWT Payload body as base64url encoded JSON", { cause });
|
|
715
|
-
}
|
|
716
|
-
if (!isJsonObject(claims)) {
|
|
717
|
-
throw new OPE("JWT Payload must be a top level object");
|
|
718
|
-
}
|
|
719
|
-
const now = epochTime() + clockSkew2;
|
|
720
|
-
if (claims.exp !== void 0) {
|
|
721
|
-
if (typeof claims.exp !== "number") {
|
|
722
|
-
throw new OPE('unexpected JWT "exp" (expiration time) claim type');
|
|
723
|
-
}
|
|
724
|
-
if (claims.exp <= now - clockTolerance2) {
|
|
725
|
-
throw new OPE('unexpected JWT "exp" (expiration time) claim value, timestamp is <= now()');
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
if (claims.iat !== void 0) {
|
|
729
|
-
if (typeof claims.iat !== "number") {
|
|
730
|
-
throw new OPE('unexpected JWT "iat" (issued at) claim type');
|
|
731
|
-
}
|
|
732
|
-
}
|
|
733
|
-
if (claims.iss !== void 0) {
|
|
734
|
-
if (typeof claims.iss !== "string") {
|
|
735
|
-
throw new OPE('unexpected JWT "iss" (issuer) claim type');
|
|
736
|
-
}
|
|
737
|
-
}
|
|
738
|
-
if (claims.nbf !== void 0) {
|
|
739
|
-
if (typeof claims.nbf !== "number") {
|
|
740
|
-
throw new OPE('unexpected JWT "nbf" (not before) claim type');
|
|
741
|
-
}
|
|
742
|
-
if (claims.nbf > now + clockTolerance2) {
|
|
743
|
-
throw new OPE('unexpected JWT "nbf" (not before) claim value, timestamp is > now()');
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
if (claims.aud !== void 0) {
|
|
747
|
-
if (typeof claims.aud !== "string" && !Array.isArray(claims.aud)) {
|
|
748
|
-
throw new OPE('unexpected JWT "aud" (audience) claim type');
|
|
749
|
-
}
|
|
750
|
-
}
|
|
751
|
-
return { header, claims, signature, key };
|
|
752
|
-
}
|
|
753
|
-
function checkSigningAlgorithm(client, issuer, header) {
|
|
754
|
-
if (client !== void 0) {
|
|
755
|
-
if (header.alg !== client) {
|
|
756
|
-
throw new OPE('unexpected JWT "alg" header parameter');
|
|
757
|
-
}
|
|
758
|
-
return;
|
|
759
|
-
}
|
|
760
|
-
if (Array.isArray(issuer)) {
|
|
761
|
-
if (!issuer.includes(header.alg)) {
|
|
762
|
-
throw new OPE('unexpected JWT "alg" header parameter');
|
|
763
|
-
}
|
|
764
|
-
return;
|
|
765
|
-
}
|
|
766
|
-
if (header.alg !== "RS256") {
|
|
767
|
-
throw new OPE('unexpected JWT "alg" header parameter');
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
function getURLSearchParameter(parameters, name) {
|
|
771
|
-
const { 0: value, length } = parameters.getAll(name);
|
|
772
|
-
if (length > 1) {
|
|
773
|
-
throw new OPE(`"${name}" parameter must be provided only once`);
|
|
774
|
-
}
|
|
775
|
-
return value;
|
|
776
|
-
}
|
|
777
|
-
var skipStateCheck = Symbol();
|
|
778
|
-
var expectNoState = Symbol();
|
|
779
|
-
function validateAuthResponse(as, client, parameters, expectedState) {
|
|
780
|
-
assertAs(as);
|
|
781
|
-
assertClient(client);
|
|
782
|
-
if (parameters instanceof URL) {
|
|
783
|
-
parameters = parameters.searchParams;
|
|
784
|
-
}
|
|
785
|
-
if (!(parameters instanceof URLSearchParams)) {
|
|
786
|
-
throw new TypeError('"parameters" must be an instance of URLSearchParams, or URL');
|
|
787
|
-
}
|
|
788
|
-
if (getURLSearchParameter(parameters, "response")) {
|
|
789
|
-
throw new OPE('"parameters" contains a JARM response, use validateJwtAuthResponse() instead of validateAuthResponse()');
|
|
790
|
-
}
|
|
791
|
-
const iss = getURLSearchParameter(parameters, "iss");
|
|
792
|
-
const state = getURLSearchParameter(parameters, "state");
|
|
793
|
-
if (!iss && as.authorization_response_iss_parameter_supported) {
|
|
794
|
-
throw new OPE('response parameter "iss" (issuer) missing');
|
|
795
|
-
}
|
|
796
|
-
if (iss && iss !== as.issuer) {
|
|
797
|
-
throw new OPE('unexpected "iss" (issuer) response parameter value');
|
|
798
|
-
}
|
|
799
|
-
switch (expectedState) {
|
|
800
|
-
case void 0:
|
|
801
|
-
case expectNoState:
|
|
802
|
-
if (state !== void 0) {
|
|
803
|
-
throw new OPE('unexpected "state" response parameter encountered');
|
|
804
|
-
}
|
|
805
|
-
break;
|
|
806
|
-
case skipStateCheck:
|
|
807
|
-
break;
|
|
808
|
-
default:
|
|
809
|
-
if (!validateString(expectedState)) {
|
|
810
|
-
throw new OPE('"expectedState" must be a non-empty string');
|
|
811
|
-
}
|
|
812
|
-
if (state === void 0) {
|
|
813
|
-
throw new OPE('response parameter "state" missing');
|
|
814
|
-
}
|
|
815
|
-
if (state !== expectedState) {
|
|
816
|
-
throw new OPE('unexpected "state" response parameter value');
|
|
817
|
-
}
|
|
818
|
-
}
|
|
819
|
-
const error = getURLSearchParameter(parameters, "error");
|
|
820
|
-
if (error) {
|
|
821
|
-
return {
|
|
822
|
-
error,
|
|
823
|
-
error_description: getURLSearchParameter(parameters, "error_description"),
|
|
824
|
-
error_uri: getURLSearchParameter(parameters, "error_uri")
|
|
825
|
-
};
|
|
826
|
-
}
|
|
827
|
-
const id_token = getURLSearchParameter(parameters, "id_token");
|
|
828
|
-
const token = getURLSearchParameter(parameters, "token");
|
|
829
|
-
if (id_token !== void 0 || token !== void 0) {
|
|
830
|
-
throw new UnsupportedOperationError("implicit and hybrid flows are not supported");
|
|
831
|
-
}
|
|
832
|
-
return brand(new URLSearchParams(parameters));
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
// src/interface/clientInterface.ts
|
|
836
|
-
import { KnownError, KnownErrors } from "../known-errors";
|
|
837
|
-
import { AccessToken, InternalSession } from "../sessions";
|
|
838
|
-
import { generateSecureRandomString } from "../utils/crypto";
|
|
839
|
-
import { StackAssertionError, throwErr } from "../utils/errors";
|
|
840
|
-
import { globalVar } from "../utils/globals";
|
|
841
|
-
import { HTTP_METHODS } from "../utils/http";
|
|
842
|
-
import { filterUndefined, filterUndefinedOrNull } from "../utils/objects";
|
|
843
|
-
import { wait } from "../utils/promises";
|
|
844
|
-
import { Result } from "../utils/results";
|
|
845
|
-
import { deindent } from "../utils/strings";
|
|
1
|
+
// src/interface/client-interface.ts
|
|
2
|
+
import * as oauth from "oauth4webapi";
|
|
3
|
+
import { KnownError, KnownErrors } from "../known-errors.js";
|
|
4
|
+
import { AccessToken, InternalSession } from "../sessions.js";
|
|
5
|
+
import { generateSecureRandomString } from "../utils/crypto.js";
|
|
6
|
+
import { StackAssertionError, throwErr } from "../utils/errors.js";
|
|
7
|
+
import { globalVar } from "../utils/globals.js";
|
|
8
|
+
import { HTTP_METHODS } from "../utils/http.js";
|
|
9
|
+
import { filterUndefined, filterUndefinedOrNull } from "../utils/objects.js";
|
|
10
|
+
import { wait } from "../utils/promises.js";
|
|
11
|
+
import { Result } from "../utils/results.js";
|
|
12
|
+
import { deindent } from "../utils/strings.js";
|
|
846
13
|
var StackClientInterface = class {
|
|
847
14
|
constructor(options) {
|
|
848
15
|
this.options = options;
|
|
@@ -904,11 +71,11 @@ var StackClientInterface = class {
|
|
|
904
71
|
async _createNetworkError(cause, session, requestType) {
|
|
905
72
|
return new Error(deindent`
|
|
906
73
|
Stack Auth is unable to connect to the server. Please check your internet connection and try again.
|
|
907
|
-
|
|
74
|
+
|
|
908
75
|
If the problem persists, please contact support and provide a screenshot of your entire browser console.
|
|
909
76
|
|
|
910
77
|
${cause}
|
|
911
|
-
|
|
78
|
+
|
|
912
79
|
${JSON.stringify(await this.runNetworkDiagnostics(session, requestType), null, 2)}
|
|
913
80
|
`, { cause });
|
|
914
81
|
}
|
|
@@ -944,7 +111,7 @@ var StackClientInterface = class {
|
|
|
944
111
|
token_endpoint_auth_method: "client_secret_post"
|
|
945
112
|
};
|
|
946
113
|
const rawResponse = await this._networkRetryException(
|
|
947
|
-
async () => await refreshTokenGrantRequest(
|
|
114
|
+
async () => await oauth.refreshTokenGrantRequest(
|
|
948
115
|
as,
|
|
949
116
|
client,
|
|
950
117
|
refreshToken.token
|
|
@@ -962,8 +129,8 @@ var StackClientInterface = class {
|
|
|
962
129
|
const body = await response.data.text();
|
|
963
130
|
throw new Error(`Failed to send refresh token request: ${response.status} ${body}`);
|
|
964
131
|
}
|
|
965
|
-
const result = await processRefreshTokenResponse(as, client, response.data);
|
|
966
|
-
if (isOAuth2Error(result)) {
|
|
132
|
+
const result = await oauth.processRefreshTokenResponse(as, client, response.data);
|
|
133
|
+
if (oauth.isOAuth2Error(result)) {
|
|
967
134
|
throw new StackAssertionError("OAuth error", { result });
|
|
968
135
|
}
|
|
969
136
|
if (!result.access_token) {
|
|
@@ -1503,6 +670,33 @@ var StackClientInterface = class {
|
|
|
1503
670
|
newUser: result.is_new_user
|
|
1504
671
|
});
|
|
1505
672
|
}
|
|
673
|
+
async signInWithMfa(totp, code) {
|
|
674
|
+
const res = await this.sendClientRequestAndCatchKnownError(
|
|
675
|
+
"/auth/mfa/sign-in",
|
|
676
|
+
{
|
|
677
|
+
method: "POST",
|
|
678
|
+
headers: {
|
|
679
|
+
"Content-Type": "application/json"
|
|
680
|
+
},
|
|
681
|
+
body: JSON.stringify({
|
|
682
|
+
type: "totp",
|
|
683
|
+
totp,
|
|
684
|
+
code
|
|
685
|
+
})
|
|
686
|
+
},
|
|
687
|
+
null,
|
|
688
|
+
[KnownErrors.VerificationCodeError]
|
|
689
|
+
);
|
|
690
|
+
if (res.status === "error") {
|
|
691
|
+
return Result.error(res.error);
|
|
692
|
+
}
|
|
693
|
+
const result = await res.data.json();
|
|
694
|
+
return Result.ok({
|
|
695
|
+
accessToken: result.access_token,
|
|
696
|
+
refreshToken: result.refresh_token,
|
|
697
|
+
newUser: result.is_new_user
|
|
698
|
+
});
|
|
699
|
+
}
|
|
1506
700
|
async signInWithPasskey(body) {
|
|
1507
701
|
const res = await this.sendClientRequestAndCatchKnownError(
|
|
1508
702
|
"/auth/passkey/sign-in",
|
|
@@ -1575,20 +769,20 @@ var StackClientInterface = class {
|
|
|
1575
769
|
token_endpoint_auth_method: "client_secret_post"
|
|
1576
770
|
};
|
|
1577
771
|
const params = await this._networkRetryException(
|
|
1578
|
-
async () => validateAuthResponse(as, client, options.oauthParams, options.state)
|
|
772
|
+
async () => oauth.validateAuthResponse(as, client, options.oauthParams, options.state)
|
|
1579
773
|
);
|
|
1580
|
-
if (isOAuth2Error(params)) {
|
|
774
|
+
if (oauth.isOAuth2Error(params)) {
|
|
1581
775
|
throw new StackAssertionError("Error validating outer OAuth response", { params });
|
|
1582
776
|
}
|
|
1583
|
-
const response = await authorizationCodeGrantRequest(
|
|
777
|
+
const response = await oauth.authorizationCodeGrantRequest(
|
|
1584
778
|
as,
|
|
1585
779
|
client,
|
|
1586
780
|
params,
|
|
1587
781
|
options.redirectUri,
|
|
1588
782
|
options.codeVerifier
|
|
1589
783
|
);
|
|
1590
|
-
const result = await processAuthorizationCodeOAuth2Response(as, client, response);
|
|
1591
|
-
if (isOAuth2Error(result)) {
|
|
784
|
+
const result = await oauth.processAuthorizationCodeOAuth2Response(as, client, response);
|
|
785
|
+
if (oauth.isOAuth2Error(result)) {
|
|
1592
786
|
if ("code" in result && result.code === "MULTI_FACTOR_AUTHENTICATION_REQUIRED") {
|
|
1593
787
|
throw new KnownErrors.MultiFactorAuthenticationRequired(result.details.attempt_code);
|
|
1594
788
|
}
|
|
@@ -2036,8 +1230,32 @@ var StackClientInterface = class {
|
|
|
2036
1230
|
}
|
|
2037
1231
|
return await result.data.json();
|
|
2038
1232
|
}
|
|
1233
|
+
async listNotificationCategories(session) {
|
|
1234
|
+
const response = await this.sendClientRequest(
|
|
1235
|
+
`/emails/notification-preference/me`,
|
|
1236
|
+
{},
|
|
1237
|
+
session
|
|
1238
|
+
);
|
|
1239
|
+
const result = await response.json();
|
|
1240
|
+
return result.items;
|
|
1241
|
+
}
|
|
1242
|
+
async setNotificationsEnabled(notificationCategoryId, enabled, session) {
|
|
1243
|
+
await this.sendClientRequest(
|
|
1244
|
+
`/emails/notification-preference/me/${notificationCategoryId}`,
|
|
1245
|
+
{
|
|
1246
|
+
method: "PATCH",
|
|
1247
|
+
headers: {
|
|
1248
|
+
"content-type": "application/json"
|
|
1249
|
+
},
|
|
1250
|
+
body: JSON.stringify({
|
|
1251
|
+
enabled
|
|
1252
|
+
})
|
|
1253
|
+
},
|
|
1254
|
+
session
|
|
1255
|
+
);
|
|
1256
|
+
}
|
|
2039
1257
|
};
|
|
2040
1258
|
export {
|
|
2041
1259
|
StackClientInterface
|
|
2042
1260
|
};
|
|
2043
|
-
//# sourceMappingURL=
|
|
1261
|
+
//# sourceMappingURL=client-interface.js.map
|