@stxapp/stx-typescript 0.9.0-alpha.1 → 0.10.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/CHANGELOG.md +69 -16
- package/README.md +27 -24
- package/dist/{chunk-33AQCUGM.cjs → chunk-42OCIF3G.cjs} +988 -225
- package/dist/chunk-42OCIF3G.cjs.map +1 -0
- package/dist/{chunk-4CSBA2YV.js → chunk-FTZCBXT7.js} +929 -166
- package/dist/chunk-FTZCBXT7.js.map +1 -0
- package/dist/{client-CNkaTuhG.d.ts → client-DJr0UUvB.d.cts} +671 -148
- package/dist/{client-CNkaTuhG.d.cts → client-DJr0UUvB.d.ts} +671 -148
- package/dist/index.cjs +20 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +23 -5
- package/dist/oauth.cjs +244 -107
- package/dist/oauth.cjs.map +1 -1
- package/dist/oauth.d.cts +115 -15
- package/dist/oauth.d.ts +115 -15
- package/dist/oauth.js +206 -69
- package/dist/oauth.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-33AQCUGM.cjs.map +0 -1
- package/dist/chunk-4CSBA2YV.js.map +0 -1
package/dist/oauth.js
CHANGED
|
@@ -4,10 +4,14 @@ import {
|
|
|
4
4
|
STXAuthenticationException,
|
|
5
5
|
STXConfigException,
|
|
6
6
|
STXForbiddenException,
|
|
7
|
+
STXIPNotAllowedException,
|
|
7
8
|
STXInsufficientScopeException,
|
|
8
9
|
STXInvalidTokenException,
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
STXNotAvailableException,
|
|
11
|
+
STXTermsRequiredException,
|
|
12
|
+
baseUrlFor,
|
|
13
|
+
isEdgeBlock
|
|
14
|
+
} from "./chunk-FTZCBXT7.js";
|
|
11
15
|
|
|
12
16
|
// src/oauth/endpoints.ts
|
|
13
17
|
var DEFAULT_ENDPOINTS = {
|
|
@@ -51,6 +55,11 @@ var STXOAuthException = class extends STXAuthenticationException {
|
|
|
51
55
|
* verified.
|
|
52
56
|
*/
|
|
53
57
|
errorReason;
|
|
58
|
+
/**
|
|
59
|
+
* On an `invalid_scope` from the authorize callback, the scope the sign-in
|
|
60
|
+
* asked for (from the pending sign-in), so the cause can be named.
|
|
61
|
+
*/
|
|
62
|
+
requestedScope;
|
|
54
63
|
constructor(error, errorDescription, status, endpoint, retryAfter, errorReason) {
|
|
55
64
|
super(`OAuth ${endpoint} ${status} ${error}${errorDescription ? `: ${errorDescription}` : ""}`, {
|
|
56
65
|
statusCode: status,
|
|
@@ -67,8 +76,10 @@ var STXAccessDeniedException = class extends STXOAuthException {
|
|
|
67
76
|
};
|
|
68
77
|
var STXAccountPendingException = class extends STXOAuthException {
|
|
69
78
|
};
|
|
79
|
+
var STXTermsNotAcceptedException = class extends STXOAuthException {
|
|
80
|
+
};
|
|
70
81
|
function callbackError(error, errorDescription, endpoint) {
|
|
71
|
-
const Class = error === "account_pending" ? STXAccountPendingException : error === "access_denied" ? STXAccessDeniedException : STXOAuthException;
|
|
82
|
+
const Class = error === "account_pending" ? STXAccountPendingException : error === "terms_required" ? STXTermsNotAcceptedException : error === "access_denied" ? STXAccessDeniedException : STXOAuthException;
|
|
72
83
|
return new Class(error, errorDescription, 400, endpoint);
|
|
73
84
|
}
|
|
74
85
|
var STXGrantRevokedException = class extends STXAuthenticationException {
|
|
@@ -77,6 +88,61 @@ var STXGrantRevokedException = class extends STXAuthenticationException {
|
|
|
77
88
|
}
|
|
78
89
|
};
|
|
79
90
|
|
|
91
|
+
// src/oauth/metadata.ts
|
|
92
|
+
async function discoverMetadata(baseUrl, opts = {}) {
|
|
93
|
+
const doc = await fetchWellKnown(baseUrl, WELL_KNOWN.authorizationServer, opts);
|
|
94
|
+
const meta = doc;
|
|
95
|
+
if (typeof meta.issuer !== "string" || typeof meta.authorization_endpoint !== "string" || typeof meta.token_endpoint !== "string") {
|
|
96
|
+
throw httpError(
|
|
97
|
+
200,
|
|
98
|
+
JSON.stringify(doc),
|
|
99
|
+
WELL_KNOWN.authorizationServer + " (missing issuer/authorization_endpoint/token_endpoint)"
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
return meta;
|
|
103
|
+
}
|
|
104
|
+
async function discoverProtectedResource(baseUrl, opts = {}) {
|
|
105
|
+
const doc = await fetchWellKnown(baseUrl, WELL_KNOWN.protectedResource, opts);
|
|
106
|
+
const meta = doc;
|
|
107
|
+
if (typeof meta.resource !== "string" || !Array.isArray(meta.authorization_servers)) {
|
|
108
|
+
throw httpError(
|
|
109
|
+
200,
|
|
110
|
+
JSON.stringify(doc),
|
|
111
|
+
WELL_KNOWN.protectedResource + " (missing resource/authorization_servers)"
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
return meta;
|
|
115
|
+
}
|
|
116
|
+
async function fetchWellKnown(baseUrl, path, opts) {
|
|
117
|
+
const cfg = resolveOAuthConfig({ baseUrl, fetch: opts.fetch });
|
|
118
|
+
const res = await cfg.fetch(joinUrl(cfg.baseUrl, path), {
|
|
119
|
+
method: "GET",
|
|
120
|
+
headers: { Accept: "application/json" }
|
|
121
|
+
});
|
|
122
|
+
const text = await res.text();
|
|
123
|
+
if (res.status === 404 || isEdgeBlock(res.status, text)) {
|
|
124
|
+
throw new STXNotAvailableException("oauth", new URL(cfg.baseUrl).host, {
|
|
125
|
+
statusCode: res.status,
|
|
126
|
+
body: text,
|
|
127
|
+
method: "GET",
|
|
128
|
+
path
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
if (!res.ok) throw httpError(res.status, text, path);
|
|
132
|
+
try {
|
|
133
|
+
return JSON.parse(text);
|
|
134
|
+
} catch {
|
|
135
|
+
throw httpError(res.status, text, path);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
function httpError(status, text, path) {
|
|
139
|
+
return new STXAPIException(`HTTP ${status} from ${path}: ${text.slice(0, 500)}`, {
|
|
140
|
+
statusCode: status,
|
|
141
|
+
body: text,
|
|
142
|
+
path
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
80
146
|
// src/oauth/pkce.ts
|
|
81
147
|
function base64url(bytes) {
|
|
82
148
|
let bin = "";
|
|
@@ -122,11 +188,11 @@ async function registerClient(request, opts = {}) {
|
|
|
122
188
|
try {
|
|
123
189
|
doc = JSON.parse(text);
|
|
124
190
|
} catch {
|
|
125
|
-
throw
|
|
191
|
+
throw httpError2(res.status, text, path);
|
|
126
192
|
}
|
|
127
193
|
const reg = doc;
|
|
128
194
|
if (typeof reg.client_id !== "string" || reg.client_id === "") {
|
|
129
|
-
throw
|
|
195
|
+
throw httpError2(res.status, text, path + " (missing client_id)");
|
|
130
196
|
}
|
|
131
197
|
return reg;
|
|
132
198
|
}
|
|
@@ -142,7 +208,7 @@ async function registerClient(request, opts = {}) {
|
|
|
142
208
|
err.error_reason
|
|
143
209
|
);
|
|
144
210
|
}
|
|
145
|
-
throw
|
|
211
|
+
throw httpError2(res.status, text, path);
|
|
146
212
|
}
|
|
147
213
|
function rfcError(text) {
|
|
148
214
|
try {
|
|
@@ -157,7 +223,7 @@ function rfcError(text) {
|
|
|
157
223
|
return null;
|
|
158
224
|
}
|
|
159
225
|
}
|
|
160
|
-
function
|
|
226
|
+
function httpError2(status, text, path) {
|
|
161
227
|
return new STXAPIException(`HTTP ${status} from ${path}: ${text.slice(0, 500)}`, {
|
|
162
228
|
statusCode: status,
|
|
163
229
|
body: text,
|
|
@@ -178,9 +244,7 @@ var MemberScopes = {
|
|
|
178
244
|
/** Deposit and withdrawal history (read only). */
|
|
179
245
|
TRANSFERS_READ: "transfers.read",
|
|
180
246
|
/** Place and cancel orders; arm `cancel_on_disconnect`. */
|
|
181
|
-
ORDERS_WRITE: "orders.write"
|
|
182
|
-
/** Accept the terms and conditions. */
|
|
183
|
-
TERMS_WRITE: "terms.write"
|
|
247
|
+
ORDERS_WRITE: "orders.write"
|
|
184
248
|
};
|
|
185
249
|
var AppScopes = {
|
|
186
250
|
/** The market catalog (`GET /api/v1/markets`) and the market data channels. */
|
|
@@ -199,9 +263,9 @@ var LEGACY_SCOPES = {
|
|
|
199
263
|
portfolio: MemberScopes.PORTFOLIO_READ,
|
|
200
264
|
history: MemberScopes.ORDERS_READ,
|
|
201
265
|
transfers: MemberScopes.TRANSFERS_READ,
|
|
202
|
-
trade: MemberScopes.ORDERS_WRITE
|
|
203
|
-
onboarding: MemberScopes.TERMS_WRITE
|
|
266
|
+
trade: MemberScopes.ORDERS_WRITE
|
|
204
267
|
};
|
|
268
|
+
var RETIRED_SCOPES = /* @__PURE__ */ new Set(["terms.write", "onboarding"]);
|
|
205
269
|
function canonicalScope(scope) {
|
|
206
270
|
return LEGACY_SCOPES[scope] ?? scope;
|
|
207
271
|
}
|
|
@@ -209,7 +273,7 @@ function parseScopes(scope) {
|
|
|
209
273
|
if (!scope) return [];
|
|
210
274
|
const out = [];
|
|
211
275
|
for (const raw of scope.split(/\s+/)) {
|
|
212
|
-
if (!raw) continue;
|
|
276
|
+
if (!raw || RETIRED_SCOPES.has(raw)) continue;
|
|
213
277
|
const s = canonicalScope(raw);
|
|
214
278
|
if (!out.includes(s)) out.push(s);
|
|
215
279
|
}
|
|
@@ -233,6 +297,8 @@ var OAuthClient = class _OAuthClient {
|
|
|
233
297
|
redirectUri;
|
|
234
298
|
scope;
|
|
235
299
|
endpoints;
|
|
300
|
+
/** The discovery document this client was built from, if any. */
|
|
301
|
+
metadata;
|
|
236
302
|
fetch;
|
|
237
303
|
/**
|
|
238
304
|
* In-flight refreshes per token store and member, shared by every session
|
|
@@ -251,6 +317,7 @@ var OAuthClient = class _OAuthClient {
|
|
|
251
317
|
this.clientSecret = opts.clientSecret || void 0;
|
|
252
318
|
this.redirectUri = opts.redirectUri;
|
|
253
319
|
this.scope = normalizeScope(opts.scope);
|
|
320
|
+
this.metadata = opts.metadata;
|
|
254
321
|
this.endpoints = {
|
|
255
322
|
authorize: opts.endpoints?.authorize ?? DEFAULT_ENDPOINTS.authorize,
|
|
256
323
|
token: opts.endpoints?.token ?? DEFAULT_ENDPOINTS.token,
|
|
@@ -266,6 +333,7 @@ var OAuthClient = class _OAuthClient {
|
|
|
266
333
|
static fromMetadata(metadata, opts) {
|
|
267
334
|
return new _OAuthClient({
|
|
268
335
|
...opts,
|
|
336
|
+
metadata,
|
|
269
337
|
baseUrl: opts.baseUrl ?? metadata.issuer,
|
|
270
338
|
endpoints: {
|
|
271
339
|
authorize: metadata.authorization_endpoint,
|
|
@@ -275,6 +343,27 @@ var OAuthClient = class _OAuthClient {
|
|
|
275
343
|
}
|
|
276
344
|
});
|
|
277
345
|
}
|
|
346
|
+
/**
|
|
347
|
+
* Fetch the exchange's discovery document and build a client from it. Throws
|
|
348
|
+
* `STXNotAvailableException` (feature `oauth`) on an exchange without OAuth,
|
|
349
|
+
* so an app can tell "not here yet" from a misconfiguration at start-up.
|
|
350
|
+
*
|
|
351
|
+
* const oauth = await OAuthClient.discover({
|
|
352
|
+
* baseUrl: "https://demo.stxapp.io",
|
|
353
|
+
* clientId: process.env.STX_CLIENT_ID!,
|
|
354
|
+
* clientSecret: process.env.STX_CLIENT_SECRET,
|
|
355
|
+
* redirectUri: "https://your.app/stx/callback",
|
|
356
|
+
* });
|
|
357
|
+
*/
|
|
358
|
+
static async discover(opts) {
|
|
359
|
+
const discovery = opts.fetch ? { fetch: opts.fetch } : {};
|
|
360
|
+
const metadata = await discoverMetadata(opts.baseUrl, discovery);
|
|
361
|
+
return _OAuthClient.fromMetadata(metadata, { ...opts, baseUrl: opts.baseUrl ?? metadata.issuer });
|
|
362
|
+
}
|
|
363
|
+
/** The exchange host, e.g. `"demo.stxapp.io"`. */
|
|
364
|
+
get host() {
|
|
365
|
+
return new URL(this.baseUrl).host;
|
|
366
|
+
}
|
|
278
367
|
/** True for a public client (no secret): PKCE-only, no app tokens. */
|
|
279
368
|
get isPublic() {
|
|
280
369
|
return this.clientSecret === void 0;
|
|
@@ -326,7 +415,7 @@ var OAuthClient = class _OAuthClient {
|
|
|
326
415
|
}
|
|
327
416
|
/**
|
|
328
417
|
* Finish a sign-in from the callback's query. Checks for an `error` from
|
|
329
|
-
* the exchange (`access_denied`, `account_pending`), consumes the
|
|
418
|
+
* the exchange (`access_denied`, `account_pending`, `terms_required`), consumes the
|
|
330
419
|
* `state` (single use; unknown or expired throws `invalid_state`), exchanges
|
|
331
420
|
* the code with the stored PKCE verifier and, when `store` is given, saves
|
|
332
421
|
* the tokens under `memberKey` (a string, or a function of the `data` passed
|
|
@@ -338,7 +427,38 @@ var OAuthClient = class _OAuthClient {
|
|
|
338
427
|
* for a used, expired or mismatched code).
|
|
339
428
|
*/
|
|
340
429
|
async completeAuthorization(pending, callback, opts = {}) {
|
|
341
|
-
|
|
430
|
+
let read;
|
|
431
|
+
try {
|
|
432
|
+
read = await readCallback(pending, callback, this.endpoints.authorize);
|
|
433
|
+
} catch (err) {
|
|
434
|
+
if (err instanceof STXOAuthException && err.error === "invalid_scope") {
|
|
435
|
+
throw this.unofferedScope(err.requestedScope ?? this.scope, err) ?? err;
|
|
436
|
+
}
|
|
437
|
+
throw err;
|
|
438
|
+
}
|
|
439
|
+
return this.redeemAuthorization(read, opts);
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* `STXNotAvailableException` for the first scope in `requested` that the
|
|
443
|
+
* discovery document does not list, or undefined when every scope is listed
|
|
444
|
+
* or there is no document to check against.
|
|
445
|
+
*/
|
|
446
|
+
unofferedScope(requested, cause) {
|
|
447
|
+
const offered = this.metadata?.scopes_supported;
|
|
448
|
+
if (!offered || !requested) return void 0;
|
|
449
|
+
const missing = parseScopes(requested).find((s) => !offered.includes(s));
|
|
450
|
+
if (!missing) return void 0;
|
|
451
|
+
return new STXNotAvailableException(`scope:${missing}`, this.host, { statusCode: 400, cause });
|
|
452
|
+
}
|
|
453
|
+
/** `STXNotAvailableException` (feature `oauth`) for an OAuth endpoint the exchange does not have. */
|
|
454
|
+
oauthMissing(status, text, endpoint) {
|
|
455
|
+
if (status !== 404 && !isEdgeBlock(status, text)) return void 0;
|
|
456
|
+
return new STXNotAvailableException("oauth", this.host, {
|
|
457
|
+
statusCode: status,
|
|
458
|
+
body: text,
|
|
459
|
+
method: "POST",
|
|
460
|
+
path: pathOf(endpoint)
|
|
461
|
+
});
|
|
342
462
|
}
|
|
343
463
|
/**
|
|
344
464
|
* The second half of `completeAuthorization()`: exchange a callback already
|
|
@@ -404,6 +524,9 @@ var OAuthClient = class _OAuthClient {
|
|
|
404
524
|
* refresh (`invalid_grant`) means the grant was revoked, including STX's
|
|
405
525
|
* reuse detection tripping on a replayed/retired token, and surfaces as
|
|
406
526
|
* `STXGrantRevokedException`.
|
|
527
|
+
*
|
|
528
|
+
* While the member has not accepted the current STX terms the refresh still
|
|
529
|
+
* succeeds, and the result carries `termsRequired: true` and `termsUrl`.
|
|
407
530
|
*/
|
|
408
531
|
refreshToken(refreshToken) {
|
|
409
532
|
return this.postToken(
|
|
@@ -465,7 +588,7 @@ var OAuthClient = class _OAuthClient {
|
|
|
465
588
|
});
|
|
466
589
|
if (!res.ok) {
|
|
467
590
|
const text = await res.text().catch(() => "");
|
|
468
|
-
throw this.oauthError(res.status, text, this.endpoints.revoke);
|
|
591
|
+
throw this.oauthMissing(res.status, text, this.endpoints.revoke) ?? this.oauthError(res.status, text, this.endpoints.revoke);
|
|
469
592
|
}
|
|
470
593
|
}
|
|
471
594
|
/**
|
|
@@ -501,7 +624,9 @@ var OAuthClient = class _OAuthClient {
|
|
|
501
624
|
body: new URLSearchParams({ token }).toString()
|
|
502
625
|
});
|
|
503
626
|
const text = await res.text();
|
|
504
|
-
if (!res.ok)
|
|
627
|
+
if (!res.ok) {
|
|
628
|
+
throw this.oauthMissing(res.status, text, this.endpoints.introspect) ?? this.oauthError(res.status, text, this.endpoints.introspect);
|
|
629
|
+
}
|
|
505
630
|
return parseJsonBody(text, res.status, this.endpoints.introspect);
|
|
506
631
|
}
|
|
507
632
|
// ---- Adapter factories --------------------------------------------------
|
|
@@ -589,7 +714,16 @@ var OAuthClient = class _OAuthClient {
|
|
|
589
714
|
});
|
|
590
715
|
const text = await res.text();
|
|
591
716
|
if (!res.ok) {
|
|
717
|
+
const missing = this.oauthMissing(res.status, text, this.endpoints.token);
|
|
718
|
+
if (missing) throw missing;
|
|
592
719
|
const err = rfcError(text);
|
|
720
|
+
if (err?.error === "invalid_scope") {
|
|
721
|
+
const unoffered = this.unofferedScope(
|
|
722
|
+
body.get("scope") ?? void 0,
|
|
723
|
+
this.oauthError(res.status, text, this.endpoints.token)
|
|
724
|
+
);
|
|
725
|
+
if (unoffered) throw unoffered;
|
|
726
|
+
}
|
|
593
727
|
if (err?.error === "invalid_grant" && err.error_reason === "account_pending") {
|
|
594
728
|
throw new STXAccountPendingException(
|
|
595
729
|
err.error,
|
|
@@ -643,6 +777,10 @@ function parseTokenResponse(json) {
|
|
|
643
777
|
}
|
|
644
778
|
if (typeof obj.scope === "string") set.scope = obj.scope;
|
|
645
779
|
if (typeof obj.id_token === "string") set.idToken = obj.id_token;
|
|
780
|
+
if (obj.terms_required === true) {
|
|
781
|
+
set.termsRequired = true;
|
|
782
|
+
if (typeof obj.terms_url === "string") set.termsUrl = obj.terms_url;
|
|
783
|
+
}
|
|
646
784
|
return set;
|
|
647
785
|
}
|
|
648
786
|
function tokenSetToStored(set) {
|
|
@@ -650,9 +788,14 @@ function tokenSetToStored(set) {
|
|
|
650
788
|
accessToken: set.accessToken,
|
|
651
789
|
refreshToken: set.refreshToken,
|
|
652
790
|
expiresAt: set.expiresAt,
|
|
653
|
-
scope: set.scope
|
|
791
|
+
scope: set.scope,
|
|
792
|
+
...termsState(set)
|
|
654
793
|
};
|
|
655
794
|
}
|
|
795
|
+
function termsState(set) {
|
|
796
|
+
if (!set.termsRequired) return {};
|
|
797
|
+
return { termsRequired: true, ...set.termsUrl ? { termsUrl: set.termsUrl } : {} };
|
|
798
|
+
}
|
|
656
799
|
var AppTokenProvider = class {
|
|
657
800
|
constructor(client, scope, opts = {}) {
|
|
658
801
|
this.client = client;
|
|
@@ -774,6 +917,8 @@ var OAuthSessionAuth = class {
|
|
|
774
917
|
* revoked grant, deletes the store entry and throws
|
|
775
918
|
* `STXGrantRevokedException`. While the member's account is still being
|
|
776
919
|
* verified, keeps the store entry and throws `STXAccountPendingException`.
|
|
920
|
+
* A refresh that says the member has terms to accept is a success: the pair
|
|
921
|
+
* is stored with `termsRequired` and `termsUrl`, and `onTermsRequired` runs.
|
|
777
922
|
* A transient failure resolves null.
|
|
778
923
|
*/
|
|
779
924
|
refresh(refreshToken) {
|
|
@@ -789,10 +934,12 @@ var OAuthSessionAuth = class {
|
|
|
789
934
|
accessToken: set.accessToken,
|
|
790
935
|
refreshToken: set.refreshToken ?? refreshToken,
|
|
791
936
|
expiresAt: set.expiresAt,
|
|
792
|
-
scope: set.scope ?? current?.scope
|
|
937
|
+
scope: set.scope ?? current?.scope,
|
|
938
|
+
...termsState(set)
|
|
793
939
|
};
|
|
794
940
|
await this.store.set(this.memberKey, stored);
|
|
795
941
|
await this.opts.onRefresh?.(stored);
|
|
942
|
+
if (stored.termsUrl) await this.opts.onTermsRequired?.({ termsUrl: stored.termsUrl });
|
|
796
943
|
return stored;
|
|
797
944
|
} catch (err) {
|
|
798
945
|
if (err instanceof STXGrantRevokedException) {
|
|
@@ -863,7 +1010,11 @@ async function readCallback(pending, callback, authorizeEndpoint = DEFAULT_ENDPO
|
|
|
863
1010
|
where
|
|
864
1011
|
);
|
|
865
1012
|
}
|
|
866
|
-
if (error)
|
|
1013
|
+
if (error) {
|
|
1014
|
+
const err = callbackError(error, q.get("error_description") ?? void 0, where);
|
|
1015
|
+
err.requestedScope = entry.scope;
|
|
1016
|
+
throw err;
|
|
1017
|
+
}
|
|
867
1018
|
return { code, pending: entry };
|
|
868
1019
|
}
|
|
869
1020
|
function callbackParams(input) {
|
|
@@ -885,6 +1036,18 @@ function pathOf(endpoint) {
|
|
|
885
1036
|
}
|
|
886
1037
|
}
|
|
887
1038
|
|
|
1039
|
+
// src/oauth/terms.ts
|
|
1040
|
+
function termsUrlWithReturn(termsUrl, returnTo) {
|
|
1041
|
+
const hashAt = termsUrl.indexOf("#");
|
|
1042
|
+
const base = hashAt === -1 ? termsUrl : termsUrl.slice(0, hashAt);
|
|
1043
|
+
const hash = hashAt === -1 ? "" : termsUrl.slice(hashAt);
|
|
1044
|
+
const queryAt = base.indexOf("?");
|
|
1045
|
+
const path = queryAt === -1 ? base : base.slice(0, queryAt);
|
|
1046
|
+
const params = new URLSearchParams(queryAt === -1 ? "" : base.slice(queryAt + 1));
|
|
1047
|
+
params.set("return_to", returnTo);
|
|
1048
|
+
return `${path}?${params.toString()}${hash}`;
|
|
1049
|
+
}
|
|
1050
|
+
|
|
888
1051
|
// src/oauth/connect.ts
|
|
889
1052
|
function createConnect(opts) {
|
|
890
1053
|
if (!opts.issuer) throw new STXConfigException("issuer is required.");
|
|
@@ -900,7 +1063,7 @@ function createConnect(opts) {
|
|
|
900
1063
|
let client;
|
|
901
1064
|
let keys;
|
|
902
1065
|
const configuration = () => {
|
|
903
|
-
config ??= fetchJson(fetchImpl, joinUrl(issuer, "/.well-known/openid-configuration")).then((doc) => {
|
|
1066
|
+
config ??= fetchJson(fetchImpl, joinUrl(issuer, "/.well-known/openid-configuration"), "connect").then((doc) => {
|
|
904
1067
|
const c = doc;
|
|
905
1068
|
if (typeof c.issuer !== "string" || typeof c.authorization_endpoint !== "string" || typeof c.token_endpoint !== "string" || typeof c.jwks_uri !== "string") {
|
|
906
1069
|
throw new STXOAuthException(
|
|
@@ -1032,6 +1195,14 @@ function createConnect(opts) {
|
|
|
1032
1195
|
headers: { Accept: "application/json", Authorization: `Bearer ${accessToken}` }
|
|
1033
1196
|
});
|
|
1034
1197
|
const text = await res.text();
|
|
1198
|
+
if (res.status === 404 || isEdgeBlock(res.status, text)) {
|
|
1199
|
+
throw new STXNotAvailableException("connect", new URL(endpoint).host, {
|
|
1200
|
+
statusCode: res.status,
|
|
1201
|
+
body: text,
|
|
1202
|
+
method: "GET",
|
|
1203
|
+
path: where
|
|
1204
|
+
});
|
|
1205
|
+
}
|
|
1035
1206
|
let body;
|
|
1036
1207
|
try {
|
|
1037
1208
|
body = JSON.parse(text);
|
|
@@ -1088,9 +1259,17 @@ function withOpenId(scopes) {
|
|
|
1088
1259
|
set.delete("openid");
|
|
1089
1260
|
return formatScopes(["openid", ...set]);
|
|
1090
1261
|
}
|
|
1091
|
-
async function fetchJson(fetchImpl, url) {
|
|
1262
|
+
async function fetchJson(fetchImpl, url, feature) {
|
|
1092
1263
|
const res = await fetchImpl(url, { method: "GET", headers: { Accept: "application/json" } });
|
|
1093
1264
|
const text = await res.text();
|
|
1265
|
+
if (feature && (res.status === 404 || isEdgeBlock(res.status, text))) {
|
|
1266
|
+
throw new STXNotAvailableException(feature, new URL(url).host, {
|
|
1267
|
+
statusCode: res.status,
|
|
1268
|
+
body: text,
|
|
1269
|
+
method: "GET",
|
|
1270
|
+
path: new URL(url).pathname
|
|
1271
|
+
});
|
|
1272
|
+
}
|
|
1094
1273
|
if (!res.ok) {
|
|
1095
1274
|
throw new STXOAuthException("invalid_configuration", `GET ${url} returned ${res.status}.`, res.status, url);
|
|
1096
1275
|
}
|
|
@@ -1173,53 +1352,6 @@ function constantTimeEqual(a, b) {
|
|
|
1173
1352
|
return diff === 0;
|
|
1174
1353
|
}
|
|
1175
1354
|
|
|
1176
|
-
// src/oauth/metadata.ts
|
|
1177
|
-
async function discoverMetadata(baseUrl, opts = {}) {
|
|
1178
|
-
const doc = await fetchWellKnown(baseUrl, WELL_KNOWN.authorizationServer, opts);
|
|
1179
|
-
const meta = doc;
|
|
1180
|
-
if (typeof meta.issuer !== "string" || typeof meta.authorization_endpoint !== "string" || typeof meta.token_endpoint !== "string") {
|
|
1181
|
-
throw httpError2(
|
|
1182
|
-
200,
|
|
1183
|
-
JSON.stringify(doc),
|
|
1184
|
-
WELL_KNOWN.authorizationServer + " (missing issuer/authorization_endpoint/token_endpoint)"
|
|
1185
|
-
);
|
|
1186
|
-
}
|
|
1187
|
-
return meta;
|
|
1188
|
-
}
|
|
1189
|
-
async function discoverProtectedResource(baseUrl, opts = {}) {
|
|
1190
|
-
const doc = await fetchWellKnown(baseUrl, WELL_KNOWN.protectedResource, opts);
|
|
1191
|
-
const meta = doc;
|
|
1192
|
-
if (typeof meta.resource !== "string" || !Array.isArray(meta.authorization_servers)) {
|
|
1193
|
-
throw httpError2(
|
|
1194
|
-
200,
|
|
1195
|
-
JSON.stringify(doc),
|
|
1196
|
-
WELL_KNOWN.protectedResource + " (missing resource/authorization_servers)"
|
|
1197
|
-
);
|
|
1198
|
-
}
|
|
1199
|
-
return meta;
|
|
1200
|
-
}
|
|
1201
|
-
async function fetchWellKnown(baseUrl, path, opts) {
|
|
1202
|
-
const cfg = resolveOAuthConfig({ baseUrl, fetch: opts.fetch });
|
|
1203
|
-
const res = await cfg.fetch(joinUrl(cfg.baseUrl, path), {
|
|
1204
|
-
method: "GET",
|
|
1205
|
-
headers: { Accept: "application/json" }
|
|
1206
|
-
});
|
|
1207
|
-
const text = await res.text();
|
|
1208
|
-
if (!res.ok) throw httpError2(res.status, text, path);
|
|
1209
|
-
try {
|
|
1210
|
-
return JSON.parse(text);
|
|
1211
|
-
} catch {
|
|
1212
|
-
throw httpError2(res.status, text, path);
|
|
1213
|
-
}
|
|
1214
|
-
}
|
|
1215
|
-
function httpError2(status, text, path) {
|
|
1216
|
-
return new STXAPIException(`HTTP ${status} from ${path}: ${text.slice(0, 500)}`, {
|
|
1217
|
-
statusCode: status,
|
|
1218
|
-
body: text,
|
|
1219
|
-
path
|
|
1220
|
-
});
|
|
1221
|
-
}
|
|
1222
|
-
|
|
1223
1355
|
// src/oauth/tokenStore.ts
|
|
1224
1356
|
var MemoryTokenStore = class {
|
|
1225
1357
|
map = /* @__PURE__ */ new Map();
|
|
@@ -1276,9 +1408,13 @@ export {
|
|
|
1276
1408
|
STXAuthenticationException,
|
|
1277
1409
|
STXForbiddenException,
|
|
1278
1410
|
STXGrantRevokedException,
|
|
1411
|
+
STXIPNotAllowedException,
|
|
1279
1412
|
STXInsufficientScopeException,
|
|
1280
1413
|
STXInvalidTokenException,
|
|
1414
|
+
STXNotAvailableException,
|
|
1281
1415
|
STXOAuthException,
|
|
1416
|
+
STXTermsNotAcceptedException,
|
|
1417
|
+
STXTermsRequiredException,
|
|
1282
1418
|
Scopes,
|
|
1283
1419
|
WELL_KNOWN,
|
|
1284
1420
|
base64url,
|
|
@@ -1299,6 +1435,7 @@ export {
|
|
|
1299
1435
|
readCallback,
|
|
1300
1436
|
registerClient,
|
|
1301
1437
|
rfcError,
|
|
1438
|
+
termsUrlWithReturn,
|
|
1302
1439
|
tokenSetToStored
|
|
1303
1440
|
};
|
|
1304
1441
|
//# sourceMappingURL=oauth.js.map
|