@teammy/server 0.2.23 → 0.2.25
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/LICENSE +21 -0
- package/README.md +11 -7
- package/dist/main.js +21235 -13704
- package/dist/native-pi-entry.js +815 -19
- package/dist/project-pi-entry.js +818 -22
- package/dist/runtime/en/shared/system.md +9 -0
- package/dist/runtime/en/shared/tools.json +22 -0
- package/dist/runtime/en/workflows/directory-analysis/prompt.md +11 -0
- package/dist/runtime/en/workflows/file-summary/prompt.md +7 -0
- package/dist/runtime/en/workflows/session-handoff/prompt.md +31 -0
- package/dist/runtime/en/workflows/session-overview/prompt.md +26 -0
- package/dist/runtime/en/workflows/task-bootstrap/prompt.md +84 -0
- package/dist/runtime/shared/system.md +1 -1
- package/dist/runtime/workflows/session-handoff/prompt.md +1 -1
- package/dist/runtime/workflows/session-overview/prompt.md +1 -1
- package/dist/web/THIRD-PARTY-LICENSES.md +2025 -0
- package/dist/web/assets/Assistant-C-GBv5St.js +1 -0
- package/dist/web/assets/Computers-B-FvcufX.js +14 -0
- package/dist/web/assets/{Computers-BtcIsnDD.css → Computers-BJKv9oJv.css} +1 -1
- package/dist/web/assets/Conversation-Dzr18Rmq.js +1 -0
- package/dist/web/assets/Dashboard-5ksfc6l9.js +1 -0
- package/dist/web/assets/Files-DqrIDBJI.js +1 -0
- package/dist/web/assets/Help-JsdkpQ8l.js +2413 -0
- package/dist/web/assets/MessageComposer-DOJ1PYXg.js +1 -0
- package/dist/web/assets/MessageComposer-DqcbXBIG.css +1 -0
- package/dist/web/assets/ResourceLibrary--aVsAt6m.js +6 -0
- package/dist/web/assets/TerminalPane-jJa53g04.js +22 -0
- package/dist/web/assets/WorkspaceTree-CuwIu3vj.js +1 -0
- package/dist/web/assets/file-links-BnaVOi2G.js +1 -0
- package/dist/web/assets/index-B9gF6reY.css +1 -0
- package/dist/web/assets/index-Ck_8bv0N.js +40 -0
- package/dist/web/assets/project-links-CodATITq.js +105 -0
- package/dist/web/assets/workflow-DR-M8YZx.svg +111 -0
- package/dist/web/downloads/teammy-inbox-skill.zip +0 -0
- package/dist/web/downloads/teammy-skill.zip +0 -0
- package/dist/web/index.html +4 -4
- package/dist/web/notification-worker.js +2 -10
- package/node_modules/i18next/LICENSE +21 -0
- package/node_modules/i18next/README.md +77 -0
- package/node_modules/i18next/dist/cjs/i18next.js +2495 -0
- package/node_modules/i18next/dist/esm/i18next.js +2505 -0
- package/node_modules/i18next/dist/esm/package.json +1 -0
- package/node_modules/i18next/dist/umd/i18next.js +2501 -0
- package/node_modules/i18next/dist/umd/i18next.min.js +1 -0
- package/node_modules/i18next/i18next.js +2501 -0
- package/node_modules/i18next/i18next.min.js +1 -0
- package/node_modules/i18next/index.d.mts +71 -0
- package/node_modules/i18next/index.d.ts +739 -0
- package/node_modules/i18next/package.json +125 -0
- package/node_modules/i18next/typescript/helpers.d.ts +73 -0
- package/node_modules/i18next/typescript/options.d.ts +871 -0
- package/node_modules/i18next/typescript/t.d.ts +1449 -0
- package/node_modules/jose/LICENSE.md +21 -0
- package/node_modules/jose/README.md +150 -0
- package/node_modules/jose/dist/types/index.d.ts +55 -0
- package/node_modules/jose/dist/types/jwe/compact/decrypt.d.ts +25 -0
- package/node_modules/jose/dist/types/jwe/compact/encrypt.d.ts +40 -0
- package/node_modules/jose/dist/types/jwe/flattened/decrypt.d.ts +25 -0
- package/node_modules/jose/dist/types/jwe/flattened/encrypt.d.ts +46 -0
- package/node_modules/jose/dist/types/jwe/general/decrypt.d.ts +29 -0
- package/node_modules/jose/dist/types/jwe/general/encrypt.d.ts +51 -0
- package/node_modules/jose/dist/types/jwk/embedded.d.ts +10 -0
- package/node_modules/jose/dist/types/jwk/thumbprint.d.ts +15 -0
- package/node_modules/jose/dist/types/jwks/local.d.ts +17 -0
- package/node_modules/jose/dist/types/jwks/remote.d.ts +94 -0
- package/node_modules/jose/dist/types/jws/compact/sign.d.ts +21 -0
- package/node_modules/jose/dist/types/jws/compact/verify.d.ts +25 -0
- package/node_modules/jose/dist/types/jws/flattened/sign.d.ts +23 -0
- package/node_modules/jose/dist/types/jws/flattened/verify.d.ts +26 -0
- package/node_modules/jose/dist/types/jws/general/sign.d.ts +41 -0
- package/node_modules/jose/dist/types/jws/general/verify.d.ts +31 -0
- package/node_modules/jose/dist/types/jwt/decrypt.d.ts +38 -0
- package/node_modules/jose/dist/types/jwt/encrypt.d.ts +57 -0
- package/node_modules/jose/dist/types/jwt/sign.d.ts +21 -0
- package/node_modules/jose/dist/types/jwt/unsecured.d.ts +21 -0
- package/node_modules/jose/dist/types/jwt/verify.d.ts +36 -0
- package/node_modules/jose/dist/types/key/export.d.ts +19 -0
- package/node_modules/jose/dist/types/key/generate_key_pair.d.ts +33 -0
- package/node_modules/jose/dist/types/key/generate_secret.d.ts +30 -0
- package/node_modules/jose/dist/types/key/import.d.ts +67 -0
- package/node_modules/jose/dist/types/types.d.ts +802 -0
- package/node_modules/jose/dist/types/util/base64url.d.ts +16 -0
- package/node_modules/jose/dist/types/util/decode_jwt.d.ts +9 -0
- package/node_modules/jose/dist/types/util/decode_protected_header.d.ts +10 -0
- package/node_modules/jose/dist/types/util/errors.d.ts +182 -0
- package/node_modules/jose/dist/webapi/index.js +70 -0
- package/node_modules/jose/dist/webapi/jwe/compact/decrypt.js +7 -0
- package/node_modules/jose/dist/webapi/jwe/compact/encrypt.js +26 -0
- package/node_modules/jose/dist/webapi/jwe/flattened/decrypt.js +15 -0
- package/node_modules/jose/dist/webapi/jwe/flattened/encrypt.js +35 -0
- package/node_modules/jose/dist/webapi/jwe/general/decrypt.js +41 -0
- package/node_modules/jose/dist/webapi/jwe/general/encrypt.js +105 -0
- package/node_modules/jose/dist/webapi/jwk/embedded.js +30 -0
- package/node_modules/jose/dist/webapi/jwk/thumbprint.js +55 -0
- package/node_modules/jose/dist/webapi/jwks/local.js +60 -0
- package/node_modules/jose/dist/webapi/jwks/remote.js +103 -0
- package/node_modules/jose/dist/webapi/jws/compact/sign.js +20 -0
- package/node_modules/jose/dist/webapi/jws/compact/verify.js +8 -0
- package/node_modules/jose/dist/webapi/jws/flattened/sign.js +23 -0
- package/node_modules/jose/dist/webapi/jws/flattened/verify.js +12 -0
- package/node_modules/jose/dist/webapi/jws/general/sign.js +58 -0
- package/node_modules/jose/dist/webapi/jws/general/verify.js +42 -0
- package/node_modules/jose/dist/webapi/jwt/decrypt.js +13 -0
- package/node_modules/jose/dist/webapi/jwt/encrypt.js +42 -0
- package/node_modules/jose/dist/webapi/jwt/sign.js +19 -0
- package/node_modules/jose/dist/webapi/jwt/unsecured.js +34 -0
- package/node_modules/jose/dist/webapi/jwt/verify.js +13 -0
- package/node_modules/jose/dist/webapi/key/export.js +35 -0
- package/node_modules/jose/dist/webapi/key/generate_key_pair.js +35 -0
- package/node_modules/jose/dist/webapi/key/generate_secret.js +36 -0
- package/node_modules/jose/dist/webapi/key/import.js +53 -0
- package/node_modules/jose/dist/webapi/lib/asn1.js +138 -0
- package/node_modules/jose/dist/webapi/lib/buffer_utils.js +75 -0
- package/node_modules/jose/dist/webapi/lib/content_encryption.js +90 -0
- package/node_modules/jose/dist/webapi/lib/deflate.js +41 -0
- package/node_modules/jose/dist/webapi/lib/jwe_algorithms.js +116 -0
- package/node_modules/jose/dist/webapi/lib/jwe_decrypt.js +190 -0
- package/node_modules/jose/dist/webapi/lib/jwe_encrypt.js +75 -0
- package/node_modules/jose/dist/webapi/lib/jws_algorithms.js +74 -0
- package/node_modules/jose/dist/webapi/lib/jws_sign.js +46 -0
- package/node_modules/jose/dist/webapi/lib/jws_verify.js +89 -0
- package/node_modules/jose/dist/webapi/lib/jwt_claims_set.js +150 -0
- package/node_modules/jose/dist/webapi/lib/key.js +157 -0
- package/node_modules/jose/dist/webapi/lib/key_algorithm.js +15 -0
- package/node_modules/jose/dist/webapi/lib/key_descriptor.js +9 -0
- package/node_modules/jose/dist/webapi/lib/key_management.js +195 -0
- package/node_modules/jose/dist/webapi/lib/validate.js +122 -0
- package/node_modules/jose/dist/webapi/util/base64url.js +16 -0
- package/node_modules/jose/dist/webapi/util/decode_jwt.js +33 -0
- package/node_modules/jose/dist/webapi/util/decode_protected_header.js +19 -0
- package/node_modules/jose/dist/webapi/util/errors.js +109 -0
- package/node_modules/jose/package.json +134 -0
- package/node_modules/oauth4webapi/LICENSE.md +21 -0
- package/node_modules/oauth4webapi/README.md +104 -0
- package/node_modules/oauth4webapi/build/index.d.ts +3046 -0
- package/node_modules/oauth4webapi/build/index.js +2530 -0
- package/node_modules/oauth4webapi/build/index.js.map +1 -0
- package/node_modules/oauth4webapi/package.json +112 -0
- package/node_modules/openid-client/LICENSE.md +21 -0
- package/node_modules/openid-client/README.md +258 -0
- package/node_modules/openid-client/build/index.d.ts +2478 -0
- package/node_modules/openid-client/build/index.js +1347 -0
- package/node_modules/openid-client/build/index.js.map +1 -0
- package/node_modules/openid-client/build/passport.d.ts +258 -0
- package/node_modules/openid-client/build/passport.js +264 -0
- package/node_modules/openid-client/build/passport.js.map +1 -0
- package/node_modules/openid-client/package.json +130 -0
- package/package.json +5 -1
- package/dist/web/assets/Assistant-Dks48Y5w.js +0 -1
- package/dist/web/assets/Computers-CaCzZeCc.js +0 -13
- package/dist/web/assets/Conversation-kAMzKADw.js +0 -1
- package/dist/web/assets/Dashboard-ComNJNOg.js +0 -1
- package/dist/web/assets/Dropdown-DGFI68Vd.js +0 -55
- package/dist/web/assets/Files-Blsq_C-B.js +0 -1
- package/dist/web/assets/Help-BM23uS-t.js +0 -1079
- package/dist/web/assets/Markdown-CXnZDm5J.js +0 -29
- package/dist/web/assets/MessageComposer-BoOgfyPA.js +0 -1
- package/dist/web/assets/MessageComposer-hKEluJsT.css +0 -1
- package/dist/web/assets/ResourceLibrary-C12_47kE.js +0 -6
- package/dist/web/assets/TerminalPane-BTRM-36z.js +0 -22
- package/dist/web/assets/WorkspaceTree-Co7XG2RG.js +0 -1
- package/dist/web/assets/file-links-CoVqmrXg.js +0 -1
- package/dist/web/assets/index-CeKzFsxV.css +0 -1
- package/dist/web/assets/index-mhJ33-bH.js +0 -10
- package/dist/web/assets/task-links-BE36-8mc.js +0 -1
- /package/dist/web/assets/{Dropdown-ktgqMqAD.css → project-links-ktgqMqAD.css} +0 -0
|
@@ -0,0 +1,1347 @@
|
|
|
1
|
+
import * as oauth from 'oauth4webapi';
|
|
2
|
+
import { compactDecrypt } from 'jose/jwe/compact/decrypt';
|
|
3
|
+
import { JOSEError } from 'jose/errors';
|
|
4
|
+
let headers;
|
|
5
|
+
let USER_AGENT;
|
|
6
|
+
if (typeof navigator === 'undefined' || !navigator.userAgent?.startsWith?.('Mozilla/5.0 ')) {
|
|
7
|
+
const NAME = 'openid-client';
|
|
8
|
+
const VERSION = 'v6.9.1';
|
|
9
|
+
USER_AGENT = `${NAME}/${VERSION}`;
|
|
10
|
+
headers = { 'user-agent': USER_AGENT };
|
|
11
|
+
}
|
|
12
|
+
const int = (config) => {
|
|
13
|
+
return props.get(config);
|
|
14
|
+
};
|
|
15
|
+
let props;
|
|
16
|
+
export { AuthorizationResponseError, ResponseBodyError, WWWAuthenticateChallengeError, } from 'oauth4webapi';
|
|
17
|
+
export function ClientSecretPost(clientSecret) {
|
|
18
|
+
if (clientSecret !== undefined) {
|
|
19
|
+
return oauth.ClientSecretPost(clientSecret);
|
|
20
|
+
}
|
|
21
|
+
const cache = new WeakMap();
|
|
22
|
+
return (as, client, body, headers) => {
|
|
23
|
+
let auth;
|
|
24
|
+
if (!(auth = cache.get(client))) {
|
|
25
|
+
assertString(client.client_secret, '"metadata.client_secret"');
|
|
26
|
+
auth = oauth.ClientSecretPost(client.client_secret);
|
|
27
|
+
cache.set(client, auth);
|
|
28
|
+
}
|
|
29
|
+
return auth(as, client, body, headers);
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function assertString(input, it) {
|
|
33
|
+
if (typeof input !== 'string') {
|
|
34
|
+
throw CodedTypeError(`${it} must be a string`, ERR_INVALID_ARG_TYPE);
|
|
35
|
+
}
|
|
36
|
+
if (input.length === 0) {
|
|
37
|
+
throw CodedTypeError(`${it} must not be empty`, ERR_INVALID_ARG_VALUE);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export function ClientSecretBasic(clientSecret) {
|
|
41
|
+
if (clientSecret !== undefined) {
|
|
42
|
+
return oauth.ClientSecretBasic(clientSecret);
|
|
43
|
+
}
|
|
44
|
+
const cache = new WeakMap();
|
|
45
|
+
return (as, client, body, headers) => {
|
|
46
|
+
let auth;
|
|
47
|
+
if (!(auth = cache.get(client))) {
|
|
48
|
+
assertString(client.client_secret, '"metadata.client_secret"');
|
|
49
|
+
auth = oauth.ClientSecretBasic(client.client_secret);
|
|
50
|
+
cache.set(client, auth);
|
|
51
|
+
}
|
|
52
|
+
return auth(as, client, body, headers);
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export function ClientSecretJwt(clientSecret, options) {
|
|
56
|
+
if (clientSecret !== undefined) {
|
|
57
|
+
return oauth.ClientSecretJwt(clientSecret, options);
|
|
58
|
+
}
|
|
59
|
+
const cache = new WeakMap();
|
|
60
|
+
return (as, client, body, headers) => {
|
|
61
|
+
let auth;
|
|
62
|
+
if (!(auth = cache.get(client))) {
|
|
63
|
+
assertString(client.client_secret, '"metadata.client_secret"');
|
|
64
|
+
auth = oauth.ClientSecretJwt(client.client_secret, options);
|
|
65
|
+
cache.set(client, auth);
|
|
66
|
+
}
|
|
67
|
+
return auth(as, client, body, headers);
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
export function None() {
|
|
71
|
+
return oauth.None();
|
|
72
|
+
}
|
|
73
|
+
export function PrivateKeyJwt(clientPrivateKey, options) {
|
|
74
|
+
return oauth.PrivateKeyJwt(clientPrivateKey, options);
|
|
75
|
+
}
|
|
76
|
+
export function TlsClientAuth() {
|
|
77
|
+
return oauth.TlsClientAuth();
|
|
78
|
+
}
|
|
79
|
+
export const skipStateCheck = oauth.skipStateCheck;
|
|
80
|
+
export const skipSubjectCheck = oauth.skipSubjectCheck;
|
|
81
|
+
export const customFetch = oauth.customFetch;
|
|
82
|
+
export const modifyAssertion = oauth.modifyAssertion;
|
|
83
|
+
export const clockSkew = oauth.clockSkew;
|
|
84
|
+
export const clockTolerance = oauth.clockTolerance;
|
|
85
|
+
const ERR_INVALID_ARG_VALUE = 'ERR_INVALID_ARG_VALUE';
|
|
86
|
+
const ERR_INVALID_ARG_TYPE = 'ERR_INVALID_ARG_TYPE';
|
|
87
|
+
function CodedTypeError(message, code, cause) {
|
|
88
|
+
const err = new TypeError(message, { cause });
|
|
89
|
+
Object.assign(err, { code });
|
|
90
|
+
return err;
|
|
91
|
+
}
|
|
92
|
+
export function calculatePKCECodeChallenge(codeVerifier) {
|
|
93
|
+
return oauth.calculatePKCECodeChallenge(codeVerifier);
|
|
94
|
+
}
|
|
95
|
+
export function randomPKCECodeVerifier() {
|
|
96
|
+
return oauth.generateRandomCodeVerifier();
|
|
97
|
+
}
|
|
98
|
+
export function randomNonce() {
|
|
99
|
+
return oauth.generateRandomNonce();
|
|
100
|
+
}
|
|
101
|
+
export function randomState() {
|
|
102
|
+
return oauth.generateRandomState();
|
|
103
|
+
}
|
|
104
|
+
export class ClientError extends Error {
|
|
105
|
+
code;
|
|
106
|
+
constructor(message, options) {
|
|
107
|
+
super(message, options);
|
|
108
|
+
this.name = this.constructor.name;
|
|
109
|
+
this.code = options?.code;
|
|
110
|
+
Error.captureStackTrace?.(this, this.constructor);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const decoder = new TextDecoder();
|
|
114
|
+
function e(msg, cause, code) {
|
|
115
|
+
return new ClientError(msg, { cause, code });
|
|
116
|
+
}
|
|
117
|
+
function errorHandler(err) {
|
|
118
|
+
if (err instanceof TypeError ||
|
|
119
|
+
err instanceof ClientError ||
|
|
120
|
+
err instanceof oauth.ResponseBodyError ||
|
|
121
|
+
err instanceof oauth.AuthorizationResponseError ||
|
|
122
|
+
err instanceof oauth.WWWAuthenticateChallengeError) {
|
|
123
|
+
throw err;
|
|
124
|
+
}
|
|
125
|
+
if (err instanceof oauth.OperationProcessingError) {
|
|
126
|
+
switch (err.code) {
|
|
127
|
+
case oauth.HTTP_REQUEST_FORBIDDEN:
|
|
128
|
+
throw e('only requests to HTTPS are allowed', err, err.code);
|
|
129
|
+
case oauth.REQUEST_PROTOCOL_FORBIDDEN:
|
|
130
|
+
throw e('only requests to HTTP or HTTPS are allowed', err, err.code);
|
|
131
|
+
case oauth.RESPONSE_IS_NOT_CONFORM:
|
|
132
|
+
throw e('unexpected HTTP response status code', err.cause, err.code);
|
|
133
|
+
case oauth.RESPONSE_IS_NOT_JSON:
|
|
134
|
+
throw e('unexpected response content-type', err.cause, err.code);
|
|
135
|
+
case oauth.PARSE_ERROR:
|
|
136
|
+
throw e('parsing error occured', err, err.code);
|
|
137
|
+
case oauth.INVALID_RESPONSE:
|
|
138
|
+
throw e('invalid response encountered', err, err.code);
|
|
139
|
+
case oauth.JWT_CLAIM_COMPARISON:
|
|
140
|
+
throw e('unexpected JWT claim value encountered', err, err.code);
|
|
141
|
+
case oauth.JSON_ATTRIBUTE_COMPARISON:
|
|
142
|
+
throw e('unexpected JSON attribute value encountered', err, err.code);
|
|
143
|
+
case oauth.JWT_TIMESTAMP_CHECK:
|
|
144
|
+
throw e('JWT timestamp claim value failed validation', err, err.code);
|
|
145
|
+
default:
|
|
146
|
+
throw e(err.message, err, err.code);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (err instanceof oauth.UnsupportedOperationError) {
|
|
150
|
+
throw e('unsupported operation', err, err.code);
|
|
151
|
+
}
|
|
152
|
+
if (err instanceof DOMException) {
|
|
153
|
+
switch (err.name) {
|
|
154
|
+
case 'OperationError':
|
|
155
|
+
throw e('runtime operation error', err, oauth.UNSUPPORTED_OPERATION);
|
|
156
|
+
case 'NotSupportedError':
|
|
157
|
+
throw e('runtime unsupported operation', err, oauth.UNSUPPORTED_OPERATION);
|
|
158
|
+
case 'TimeoutError':
|
|
159
|
+
throw e('operation timed out', err, 'OAUTH_TIMEOUT');
|
|
160
|
+
case 'AbortError':
|
|
161
|
+
throw e('operation aborted', err, 'OAUTH_ABORT');
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
throw new ClientError('something went wrong', { cause: err });
|
|
165
|
+
}
|
|
166
|
+
export function randomDPoPKeyPair(alg, options) {
|
|
167
|
+
return oauth
|
|
168
|
+
.generateKeyPair(alg ?? 'ES256', {
|
|
169
|
+
extractable: options?.extractable,
|
|
170
|
+
})
|
|
171
|
+
.catch(errorHandler);
|
|
172
|
+
}
|
|
173
|
+
function handleEntraId(server, as, options) {
|
|
174
|
+
if (server.origin === 'https://login.microsoftonline.com' &&
|
|
175
|
+
(!options?.algorithm || options.algorithm === 'oidc')) {
|
|
176
|
+
as[kEntraId] = true;
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
function handleB2Clogin(server, options) {
|
|
182
|
+
if (server.hostname.endsWith('.b2clogin.com') &&
|
|
183
|
+
(!options?.algorithm || options.algorithm === 'oidc')) {
|
|
184
|
+
return true;
|
|
185
|
+
}
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
export async function dynamicClientRegistration(server, metadata, clientAuthentication, options) {
|
|
189
|
+
let as;
|
|
190
|
+
if (options?.flag === retry) {
|
|
191
|
+
as = options.as;
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
as = await performDiscovery(server, options);
|
|
195
|
+
}
|
|
196
|
+
const clockSkew = metadata[oauth.clockSkew] ?? 0;
|
|
197
|
+
const clockTolerance = metadata[oauth.clockTolerance] ?? 30;
|
|
198
|
+
metadata = structuredClone(metadata);
|
|
199
|
+
metadata[oauth.clockSkew] = clockSkew;
|
|
200
|
+
metadata[oauth.clockTolerance] = clockTolerance;
|
|
201
|
+
const timeout = options?.timeout ?? 30;
|
|
202
|
+
const signal = AbortSignal.timeout(timeout * 1000);
|
|
203
|
+
let registered;
|
|
204
|
+
try {
|
|
205
|
+
registered = await oauth
|
|
206
|
+
.dynamicClientRegistrationRequest(as, metadata, {
|
|
207
|
+
initialAccessToken: options?.initialAccessToken,
|
|
208
|
+
DPoP: options?.DPoP,
|
|
209
|
+
headers: new Headers(headers),
|
|
210
|
+
[oauth.customFetch]: options?.[customFetch],
|
|
211
|
+
[oauth.allowInsecureRequests]: options?.execute?.includes(allowInsecureRequests),
|
|
212
|
+
signal,
|
|
213
|
+
})
|
|
214
|
+
.then(oauth.processDynamicClientRegistrationResponse);
|
|
215
|
+
}
|
|
216
|
+
catch (err) {
|
|
217
|
+
if (retryable(err, options)) {
|
|
218
|
+
return dynamicClientRegistration(server, metadata, clientAuthentication, {
|
|
219
|
+
...options,
|
|
220
|
+
flag: retry,
|
|
221
|
+
as,
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
errorHandler(err);
|
|
225
|
+
}
|
|
226
|
+
registered[oauth.clockSkew] = clockSkew;
|
|
227
|
+
registered[oauth.clockTolerance] = clockTolerance;
|
|
228
|
+
const instance = new Configuration(as, registered.client_id, registered, clientAuthentication);
|
|
229
|
+
let internals = int(instance);
|
|
230
|
+
if (options?.[customFetch]) {
|
|
231
|
+
internals.fetch = options[customFetch];
|
|
232
|
+
}
|
|
233
|
+
if (options?.timeout !== undefined) {
|
|
234
|
+
internals.timeout = options.timeout;
|
|
235
|
+
}
|
|
236
|
+
if (options?.execute) {
|
|
237
|
+
for (const extension of options.execute) {
|
|
238
|
+
extension(instance);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return instance;
|
|
242
|
+
}
|
|
243
|
+
export async function discovery(server, clientId, metadata, clientAuthentication, options) {
|
|
244
|
+
const as = await performDiscovery(server, options);
|
|
245
|
+
const instance = new Configuration(as, clientId, metadata, clientAuthentication);
|
|
246
|
+
let internals = int(instance);
|
|
247
|
+
if (options?.[customFetch]) {
|
|
248
|
+
internals.fetch = options[customFetch];
|
|
249
|
+
}
|
|
250
|
+
if (options?.timeout !== undefined) {
|
|
251
|
+
internals.timeout = options.timeout;
|
|
252
|
+
}
|
|
253
|
+
if (options?.execute) {
|
|
254
|
+
for (const extension of options.execute) {
|
|
255
|
+
extension(instance);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return instance;
|
|
259
|
+
}
|
|
260
|
+
async function performDiscovery(server, options) {
|
|
261
|
+
if (!(server instanceof URL)) {
|
|
262
|
+
throw CodedTypeError('"server" must be an instance of URL', ERR_INVALID_ARG_TYPE);
|
|
263
|
+
}
|
|
264
|
+
const resolve = !server.href.includes('/.well-known/');
|
|
265
|
+
const timeout = options?.timeout ?? 30;
|
|
266
|
+
const signal = AbortSignal.timeout(timeout * 1000);
|
|
267
|
+
const as = await (resolve
|
|
268
|
+
? oauth.discoveryRequest(server, {
|
|
269
|
+
algorithm: options?.algorithm,
|
|
270
|
+
[oauth.customFetch]: options?.[customFetch],
|
|
271
|
+
[oauth.allowInsecureRequests]: options?.execute?.includes(allowInsecureRequests),
|
|
272
|
+
signal,
|
|
273
|
+
headers: new Headers(headers),
|
|
274
|
+
})
|
|
275
|
+
: (options?.[customFetch] || fetch)((() => {
|
|
276
|
+
oauth.checkProtocol(server, options?.execute?.includes(allowInsecureRequests) ? false : true);
|
|
277
|
+
return server.href;
|
|
278
|
+
})(), {
|
|
279
|
+
headers: Object.fromEntries(new Headers({ accept: 'application/json', ...headers }).entries()),
|
|
280
|
+
body: undefined,
|
|
281
|
+
method: 'GET',
|
|
282
|
+
redirect: 'manual',
|
|
283
|
+
signal,
|
|
284
|
+
}))
|
|
285
|
+
.then((response) => oauth.processDiscoveryResponse(oauth._nodiscoverycheck, response))
|
|
286
|
+
.catch(errorHandler);
|
|
287
|
+
if (resolve && new URL(as.issuer).href !== server.href) {
|
|
288
|
+
handleEntraId(server, as, options) ||
|
|
289
|
+
handleB2Clogin(server, options) ||
|
|
290
|
+
(() => {
|
|
291
|
+
throw new ClientError('discovered metadata issuer does not match the expected issuer', {
|
|
292
|
+
code: oauth.JSON_ATTRIBUTE_COMPARISON,
|
|
293
|
+
cause: {
|
|
294
|
+
expected: server.href,
|
|
295
|
+
body: as,
|
|
296
|
+
attribute: 'issuer',
|
|
297
|
+
},
|
|
298
|
+
});
|
|
299
|
+
})();
|
|
300
|
+
}
|
|
301
|
+
return as;
|
|
302
|
+
}
|
|
303
|
+
function isRsaOaep(input) {
|
|
304
|
+
return input.name === 'RSA-OAEP';
|
|
305
|
+
}
|
|
306
|
+
function isEcdh(input) {
|
|
307
|
+
return input.name === 'ECDH';
|
|
308
|
+
}
|
|
309
|
+
const ecdhEs = 'ECDH-ES';
|
|
310
|
+
const ecdhEsA128Kw = 'ECDH-ES+A128KW';
|
|
311
|
+
const ecdhEsA192Kw = 'ECDH-ES+A192KW';
|
|
312
|
+
const ecdhEsA256Kw = 'ECDH-ES+A256KW';
|
|
313
|
+
function checkEcdhAlg(algs, alg, pk) {
|
|
314
|
+
switch (alg) {
|
|
315
|
+
case undefined:
|
|
316
|
+
algs.add(ecdhEs);
|
|
317
|
+
algs.add(ecdhEsA128Kw);
|
|
318
|
+
algs.add(ecdhEsA192Kw);
|
|
319
|
+
algs.add(ecdhEsA256Kw);
|
|
320
|
+
break;
|
|
321
|
+
case ecdhEs:
|
|
322
|
+
case ecdhEsA128Kw:
|
|
323
|
+
case ecdhEsA192Kw:
|
|
324
|
+
case ecdhEsA256Kw:
|
|
325
|
+
algs.add(alg);
|
|
326
|
+
break;
|
|
327
|
+
default:
|
|
328
|
+
throw CodedTypeError('invalid key alg', ERR_INVALID_ARG_VALUE, { pk });
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
export function enableDecryptingResponses(config, contentEncryptionAlgorithms = [
|
|
332
|
+
'A128GCM',
|
|
333
|
+
'A192GCM',
|
|
334
|
+
'A256GCM',
|
|
335
|
+
'A128CBC-HS256',
|
|
336
|
+
'A192CBC-HS384',
|
|
337
|
+
'A256CBC-HS512',
|
|
338
|
+
], ...keys) {
|
|
339
|
+
if (int(config).decrypt !== undefined) {
|
|
340
|
+
throw new TypeError('enableDecryptingResponses can only be called on a given Configuration instance once');
|
|
341
|
+
}
|
|
342
|
+
if (keys.length === 0) {
|
|
343
|
+
throw CodedTypeError('no keys were provided', ERR_INVALID_ARG_VALUE);
|
|
344
|
+
}
|
|
345
|
+
const algs = new Set();
|
|
346
|
+
const normalized = [];
|
|
347
|
+
for (const pk of keys) {
|
|
348
|
+
let key;
|
|
349
|
+
if ('key' in pk) {
|
|
350
|
+
key = { key: pk.key };
|
|
351
|
+
if (typeof pk.alg === 'string')
|
|
352
|
+
key.alg = pk.alg;
|
|
353
|
+
if (typeof pk.kid === 'string')
|
|
354
|
+
key.kid = pk.kid;
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
key = { key: pk };
|
|
358
|
+
}
|
|
359
|
+
if (key.key.type !== 'private') {
|
|
360
|
+
throw CodedTypeError('only private keys must be provided', ERR_INVALID_ARG_VALUE);
|
|
361
|
+
}
|
|
362
|
+
if (isRsaOaep(key.key.algorithm)) {
|
|
363
|
+
switch (key.key.algorithm.hash.name) {
|
|
364
|
+
case 'SHA-1':
|
|
365
|
+
case 'SHA-256':
|
|
366
|
+
case 'SHA-384':
|
|
367
|
+
case 'SHA-512': {
|
|
368
|
+
let alg = 'RSA-OAEP';
|
|
369
|
+
let sha;
|
|
370
|
+
if ((sha = parseInt(key.key.algorithm.hash.name.slice(-3), 10))) {
|
|
371
|
+
alg = `${alg}-${sha}`;
|
|
372
|
+
}
|
|
373
|
+
key.alg ||= alg;
|
|
374
|
+
if (alg !== key.alg)
|
|
375
|
+
throw CodedTypeError('invalid key alg', ERR_INVALID_ARG_VALUE, {
|
|
376
|
+
pk,
|
|
377
|
+
});
|
|
378
|
+
algs.add(key.alg);
|
|
379
|
+
break;
|
|
380
|
+
}
|
|
381
|
+
default:
|
|
382
|
+
throw CodedTypeError('only SHA-512, SHA-384, SHA-256, and SHA-1 RSA-OAEP keys are supported', ERR_INVALID_ARG_VALUE);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
else if (isEcdh(key.key.algorithm)) {
|
|
386
|
+
if (key.key.algorithm.namedCurve !== 'P-256') {
|
|
387
|
+
throw CodedTypeError('Only P-256 ECDH keys are supported', ERR_INVALID_ARG_VALUE);
|
|
388
|
+
}
|
|
389
|
+
checkEcdhAlg(algs, key.alg, pk);
|
|
390
|
+
}
|
|
391
|
+
else if (key.key.algorithm.name === 'X25519') {
|
|
392
|
+
checkEcdhAlg(algs, key.alg, pk);
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
throw CodedTypeError('only RSA-OAEP, ECDH, or X25519 keys are supported', ERR_INVALID_ARG_VALUE);
|
|
396
|
+
}
|
|
397
|
+
normalized.push(key);
|
|
398
|
+
}
|
|
399
|
+
int(config).decrypt = async (jwe) => decrypt(normalized, jwe, contentEncryptionAlgorithms, [...algs]).catch(errorHandler);
|
|
400
|
+
}
|
|
401
|
+
let decryptedResponses;
|
|
402
|
+
export function getDecryptedResponses(result) {
|
|
403
|
+
return decryptedResponses?.get(result);
|
|
404
|
+
}
|
|
405
|
+
function setDecryptedResponse(result, signed) {
|
|
406
|
+
if (signed !== undefined) {
|
|
407
|
+
decryptedResponses ||= new WeakMap();
|
|
408
|
+
decryptedResponses.set(result, signed);
|
|
409
|
+
}
|
|
410
|
+
return result;
|
|
411
|
+
}
|
|
412
|
+
function captureDecryptedResponse(decrypt) {
|
|
413
|
+
let signed;
|
|
414
|
+
return {
|
|
415
|
+
decrypt: decrypt && (async (jwe) => (signed = await decrypt(jwe))),
|
|
416
|
+
get: () => signed,
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
function checkCryptoKey(key, alg, epk) {
|
|
420
|
+
if (alg.startsWith('RSA-OAEP')) {
|
|
421
|
+
return key.algorithm.name === 'RSA-OAEP';
|
|
422
|
+
}
|
|
423
|
+
if (alg.startsWith('ECDH-ES')) {
|
|
424
|
+
if (key.algorithm.name !== 'ECDH' && key.algorithm.name !== 'X25519') {
|
|
425
|
+
return false;
|
|
426
|
+
}
|
|
427
|
+
if (key.algorithm.name === 'ECDH') {
|
|
428
|
+
return epk?.crv === key.algorithm.namedCurve;
|
|
429
|
+
}
|
|
430
|
+
if (key.algorithm.name === 'X25519') {
|
|
431
|
+
return epk?.crv === 'X25519';
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
return false;
|
|
435
|
+
}
|
|
436
|
+
function selectCryptoKeyForDecryption(keys, alg, kid, epk) {
|
|
437
|
+
const { 0: key, length } = keys.filter((key) => {
|
|
438
|
+
if (kid !== undefined && kid !== key.kid) {
|
|
439
|
+
return false;
|
|
440
|
+
}
|
|
441
|
+
if (key.alg && alg !== key.alg) {
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
return checkCryptoKey(key.key, alg, epk);
|
|
445
|
+
});
|
|
446
|
+
if (!key) {
|
|
447
|
+
throw e('no applicable decryption key selected', undefined, 'OAUTH_DECRYPTION_FAILED');
|
|
448
|
+
}
|
|
449
|
+
if (length !== 1) {
|
|
450
|
+
throw e('multiple applicable decryption keys selected', undefined, 'OAUTH_DECRYPTION_FAILED');
|
|
451
|
+
}
|
|
452
|
+
return key.key;
|
|
453
|
+
}
|
|
454
|
+
async function decrypt(keys, jwe, contentEncryptionAlgorithms, keyManagementAlgorithms) {
|
|
455
|
+
return decoder.decode((await compactDecrypt(jwe, (header) => {
|
|
456
|
+
const { kid, alg, epk } = header;
|
|
457
|
+
return selectCryptoKeyForDecryption(keys, alg, kid, epk);
|
|
458
|
+
}, { keyManagementAlgorithms, contentEncryptionAlgorithms }).catch((err) => {
|
|
459
|
+
if (err instanceof JOSEError) {
|
|
460
|
+
throw e('decryption failed', err, 'OAUTH_DECRYPTION_FAILED');
|
|
461
|
+
}
|
|
462
|
+
errorHandler(err);
|
|
463
|
+
})).plaintext);
|
|
464
|
+
}
|
|
465
|
+
function getServerHelpers(metadata) {
|
|
466
|
+
return {
|
|
467
|
+
supportsPKCE: {
|
|
468
|
+
__proto__: null,
|
|
469
|
+
value(method = 'S256') {
|
|
470
|
+
return (metadata.code_challenge_methods_supported?.includes(method) === true);
|
|
471
|
+
},
|
|
472
|
+
},
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
function addServerHelpers(metadata) {
|
|
476
|
+
Object.defineProperties(metadata, getServerHelpers(metadata));
|
|
477
|
+
}
|
|
478
|
+
const kEntraId = Symbol();
|
|
479
|
+
export class Configuration {
|
|
480
|
+
constructor(server, clientId, metadata, clientAuthentication) {
|
|
481
|
+
if (typeof clientId !== 'string' || !clientId.length) {
|
|
482
|
+
throw CodedTypeError('"clientId" must be a non-empty string', ERR_INVALID_ARG_TYPE);
|
|
483
|
+
}
|
|
484
|
+
if (typeof metadata === 'string') {
|
|
485
|
+
metadata = { client_secret: metadata };
|
|
486
|
+
}
|
|
487
|
+
if (metadata?.client_id !== undefined && clientId !== metadata.client_id) {
|
|
488
|
+
throw CodedTypeError('"clientId" and "metadata.client_id" must be the same', ERR_INVALID_ARG_VALUE);
|
|
489
|
+
}
|
|
490
|
+
const client = {
|
|
491
|
+
...structuredClone(metadata),
|
|
492
|
+
client_id: clientId,
|
|
493
|
+
};
|
|
494
|
+
client[oauth.clockSkew] = metadata?.[oauth.clockSkew] ?? 0;
|
|
495
|
+
client[oauth.clockTolerance] = metadata?.[oauth.clockTolerance] ?? 30;
|
|
496
|
+
let auth;
|
|
497
|
+
if (clientAuthentication) {
|
|
498
|
+
auth = clientAuthentication;
|
|
499
|
+
}
|
|
500
|
+
else {
|
|
501
|
+
if (typeof client.client_secret === 'string' &&
|
|
502
|
+
client.client_secret.length) {
|
|
503
|
+
auth = ClientSecretPost(client.client_secret);
|
|
504
|
+
}
|
|
505
|
+
else {
|
|
506
|
+
auth = None();
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
let c = Object.freeze(client);
|
|
510
|
+
const clone = structuredClone(server);
|
|
511
|
+
if (kEntraId in server) {
|
|
512
|
+
clone[oauth._expectedIssuer] = ({ claims: { tid } }) => server.issuer.replace('{tenantid}', tid);
|
|
513
|
+
}
|
|
514
|
+
let as = Object.freeze(clone);
|
|
515
|
+
props ||= new WeakMap();
|
|
516
|
+
props.set(this, {
|
|
517
|
+
__proto__: null,
|
|
518
|
+
as,
|
|
519
|
+
c,
|
|
520
|
+
auth,
|
|
521
|
+
tlsOnly: true,
|
|
522
|
+
timeout: 30,
|
|
523
|
+
jwksCache: {},
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
serverMetadata() {
|
|
527
|
+
const metadata = structuredClone(int(this).as);
|
|
528
|
+
addServerHelpers(metadata);
|
|
529
|
+
return metadata;
|
|
530
|
+
}
|
|
531
|
+
clientMetadata() {
|
|
532
|
+
const metadata = structuredClone(int(this).c);
|
|
533
|
+
return metadata;
|
|
534
|
+
}
|
|
535
|
+
get timeout() {
|
|
536
|
+
return int(this).timeout;
|
|
537
|
+
}
|
|
538
|
+
set timeout(value) {
|
|
539
|
+
int(this).timeout = value;
|
|
540
|
+
}
|
|
541
|
+
get [customFetch]() {
|
|
542
|
+
return int(this).fetch;
|
|
543
|
+
}
|
|
544
|
+
set [customFetch](value) {
|
|
545
|
+
int(this).fetch = value;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
Object.freeze(Configuration.prototype);
|
|
549
|
+
function getHelpers(response) {
|
|
550
|
+
let exp = undefined;
|
|
551
|
+
if (response.expires_in !== undefined) {
|
|
552
|
+
exp = Date.now() + response.expires_in * 1000;
|
|
553
|
+
}
|
|
554
|
+
return {
|
|
555
|
+
expiresIn: {
|
|
556
|
+
__proto__: null,
|
|
557
|
+
value() {
|
|
558
|
+
if (exp) {
|
|
559
|
+
const now = Date.now();
|
|
560
|
+
if (exp > now) {
|
|
561
|
+
return Math.floor((exp - now) / 1000);
|
|
562
|
+
}
|
|
563
|
+
return 0;
|
|
564
|
+
}
|
|
565
|
+
return undefined;
|
|
566
|
+
},
|
|
567
|
+
},
|
|
568
|
+
claims: {
|
|
569
|
+
__proto__: null,
|
|
570
|
+
value() {
|
|
571
|
+
return oauth.getValidatedIdTokenClaims(response);
|
|
572
|
+
},
|
|
573
|
+
},
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
function addHelpers(response) {
|
|
577
|
+
Object.defineProperties(response, getHelpers(response));
|
|
578
|
+
}
|
|
579
|
+
export function getDPoPHandle(config, keyPair, options) {
|
|
580
|
+
checkConfig(config);
|
|
581
|
+
return oauth.DPoP(int(config).c, keyPair, options);
|
|
582
|
+
}
|
|
583
|
+
async function handleRetryAfter(response, currentInterval, signal, throwIfInvalid = false) {
|
|
584
|
+
const retryAfter = response.headers.get('retry-after')?.trim();
|
|
585
|
+
if (retryAfter === undefined)
|
|
586
|
+
return;
|
|
587
|
+
let delaySeconds;
|
|
588
|
+
if (/^\d+$/.test(retryAfter)) {
|
|
589
|
+
delaySeconds = parseInt(retryAfter, 10);
|
|
590
|
+
}
|
|
591
|
+
else {
|
|
592
|
+
const retryDate = new Date(retryAfter);
|
|
593
|
+
if (Number.isFinite(retryDate.getTime())) {
|
|
594
|
+
const now = new Date();
|
|
595
|
+
const delayMs = retryDate.getTime() - now.getTime();
|
|
596
|
+
if (delayMs > 0) {
|
|
597
|
+
delaySeconds = Math.ceil(delayMs / 1000);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
if (throwIfInvalid && !Number.isFinite(delaySeconds)) {
|
|
602
|
+
throw new oauth.OperationProcessingError('invalid Retry-After header value', { cause: response });
|
|
603
|
+
}
|
|
604
|
+
if (delaySeconds > currentInterval) {
|
|
605
|
+
await wait(delaySeconds - currentInterval, signal);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
function wait(duration, signal) {
|
|
609
|
+
return new Promise((resolve, reject) => {
|
|
610
|
+
const waitStep = (remaining) => {
|
|
611
|
+
try {
|
|
612
|
+
signal.throwIfAborted();
|
|
613
|
+
}
|
|
614
|
+
catch (err) {
|
|
615
|
+
reject(err);
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
if (remaining <= 0) {
|
|
619
|
+
resolve();
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
const currentWait = Math.min(remaining, 5);
|
|
623
|
+
setTimeout(() => waitStep(remaining - currentWait), currentWait * 1000);
|
|
624
|
+
};
|
|
625
|
+
waitStep(duration);
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
function pollRequestSignal(pollingSignal, timeout) {
|
|
629
|
+
const timeoutSignal = signal(timeout);
|
|
630
|
+
if (!timeoutSignal) {
|
|
631
|
+
return {
|
|
632
|
+
signal: pollingSignal,
|
|
633
|
+
cleanup() { },
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
const controller = new AbortController();
|
|
637
|
+
const abort = (event) => {
|
|
638
|
+
const source = event.target;
|
|
639
|
+
controller.abort(source.reason);
|
|
640
|
+
};
|
|
641
|
+
if (pollingSignal.aborted) {
|
|
642
|
+
controller.abort(pollingSignal.reason);
|
|
643
|
+
}
|
|
644
|
+
else if (timeoutSignal.aborted) {
|
|
645
|
+
controller.abort(timeoutSignal.reason);
|
|
646
|
+
}
|
|
647
|
+
else {
|
|
648
|
+
pollingSignal.addEventListener('abort', abort, { once: true });
|
|
649
|
+
timeoutSignal.addEventListener('abort', abort, { once: true });
|
|
650
|
+
}
|
|
651
|
+
return {
|
|
652
|
+
signal: controller.signal,
|
|
653
|
+
cleanup() {
|
|
654
|
+
pollingSignal.removeEventListener('abort', abort);
|
|
655
|
+
timeoutSignal.removeEventListener('abort', abort);
|
|
656
|
+
},
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
async function pollGrant(config, authorizationResponse, grantCode, request, processResponse, parameters, options) {
|
|
660
|
+
const requestParameters = new URLSearchParams(parameters);
|
|
661
|
+
let interval = authorizationResponse.interval ?? 5;
|
|
662
|
+
const pollingSignal = options?.signal ??
|
|
663
|
+
AbortSignal.timeout(authorizationResponse.expires_in * 1000);
|
|
664
|
+
for (;;) {
|
|
665
|
+
try {
|
|
666
|
+
await wait(interval, pollingSignal);
|
|
667
|
+
}
|
|
668
|
+
catch (error) {
|
|
669
|
+
errorHandler(error);
|
|
670
|
+
}
|
|
671
|
+
const { as, c, auth, fetch, tlsOnly, nonRepudiation, timeout, decrypt } = int(config);
|
|
672
|
+
const requestSignal = pollRequestSignal(pollingSignal, timeout);
|
|
673
|
+
try {
|
|
674
|
+
const response = await request(as, c, auth, grantCode, {
|
|
675
|
+
[oauth.customFetch]: fetch,
|
|
676
|
+
[oauth.allowInsecureRequests]: !tlsOnly,
|
|
677
|
+
additionalParameters: requestParameters,
|
|
678
|
+
DPoP: options?.DPoP,
|
|
679
|
+
headers: new Headers(headers),
|
|
680
|
+
signal: requestSignal.signal,
|
|
681
|
+
}).catch(errorHandler);
|
|
682
|
+
if (response.status === 503 && response.headers.has('retry-after')) {
|
|
683
|
+
await response.body?.cancel();
|
|
684
|
+
await handleRetryAfter(response, interval, pollingSignal, true);
|
|
685
|
+
options = { ...options, flag: undefined };
|
|
686
|
+
continue;
|
|
687
|
+
}
|
|
688
|
+
let result;
|
|
689
|
+
const signed = captureDecryptedResponse(decrypt);
|
|
690
|
+
try {
|
|
691
|
+
result = await processResponse(as, c, response, {
|
|
692
|
+
[oauth.jweDecrypt]: signed.decrypt,
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
catch (error) {
|
|
696
|
+
if (retryable(error, options)) {
|
|
697
|
+
options = { ...options, flag: retry };
|
|
698
|
+
continue;
|
|
699
|
+
}
|
|
700
|
+
if (error instanceof oauth.ResponseBodyError &&
|
|
701
|
+
(error.error === 'slow_down' ||
|
|
702
|
+
error.error === 'authorization_pending')) {
|
|
703
|
+
if (error.error === 'slow_down')
|
|
704
|
+
interval += 5;
|
|
705
|
+
await handleRetryAfter(error.response, interval, pollingSignal);
|
|
706
|
+
options = { ...options, flag: undefined };
|
|
707
|
+
continue;
|
|
708
|
+
}
|
|
709
|
+
errorHandler(error);
|
|
710
|
+
}
|
|
711
|
+
result.id_token && (await nonRepudiation?.(response));
|
|
712
|
+
addHelpers(result);
|
|
713
|
+
return setDecryptedResponse(result, signed.get());
|
|
714
|
+
}
|
|
715
|
+
finally {
|
|
716
|
+
requestSignal.cleanup();
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
export async function pollDeviceAuthorizationGrant(config, deviceAuthorizationResponse, parameters, options) {
|
|
721
|
+
checkConfig(config);
|
|
722
|
+
return pollGrant(config, deviceAuthorizationResponse, deviceAuthorizationResponse.device_code, oauth.deviceCodeGrantRequest, oauth.processDeviceCodeResponse, parameters, options);
|
|
723
|
+
}
|
|
724
|
+
export async function initiateDeviceAuthorization(config, parameters) {
|
|
725
|
+
checkConfig(config);
|
|
726
|
+
const { as, c, auth, fetch, tlsOnly, timeout } = int(config);
|
|
727
|
+
return oauth
|
|
728
|
+
.deviceAuthorizationRequest(as, c, auth, parameters, {
|
|
729
|
+
[oauth.customFetch]: fetch,
|
|
730
|
+
[oauth.allowInsecureRequests]: !tlsOnly,
|
|
731
|
+
headers: new Headers(headers),
|
|
732
|
+
signal: signal(timeout),
|
|
733
|
+
})
|
|
734
|
+
.then((response) => oauth.processDeviceAuthorizationResponse(as, c, response))
|
|
735
|
+
.catch(errorHandler);
|
|
736
|
+
}
|
|
737
|
+
export async function initiateBackchannelAuthentication(config, parameters) {
|
|
738
|
+
checkConfig(config);
|
|
739
|
+
const { as, c, auth, fetch, tlsOnly, timeout } = int(config);
|
|
740
|
+
return oauth
|
|
741
|
+
.backchannelAuthenticationRequest(as, c, auth, parameters, {
|
|
742
|
+
[oauth.customFetch]: fetch,
|
|
743
|
+
[oauth.allowInsecureRequests]: !tlsOnly,
|
|
744
|
+
headers: new Headers(headers),
|
|
745
|
+
signal: signal(timeout),
|
|
746
|
+
})
|
|
747
|
+
.then((response) => oauth.processBackchannelAuthenticationResponse(as, c, response))
|
|
748
|
+
.catch(errorHandler);
|
|
749
|
+
}
|
|
750
|
+
export async function pollBackchannelAuthenticationGrant(config, backchannelAuthenticationResponse, parameters, options) {
|
|
751
|
+
checkConfig(config);
|
|
752
|
+
return pollGrant(config, backchannelAuthenticationResponse, backchannelAuthenticationResponse.auth_req_id, oauth.backchannelAuthenticationGrantRequest, oauth.processBackchannelAuthenticationGrantResponse, parameters, options);
|
|
753
|
+
}
|
|
754
|
+
export function allowInsecureRequests(config) {
|
|
755
|
+
int(config).tlsOnly = false;
|
|
756
|
+
}
|
|
757
|
+
export function setJwksCache(config, jwksCache) {
|
|
758
|
+
int(config).jwksCache = structuredClone(jwksCache);
|
|
759
|
+
}
|
|
760
|
+
export function getJwksCache(config) {
|
|
761
|
+
const cache = int(config).jwksCache;
|
|
762
|
+
if (cache.uat) {
|
|
763
|
+
return cache;
|
|
764
|
+
}
|
|
765
|
+
return undefined;
|
|
766
|
+
}
|
|
767
|
+
export function enableNonRepudiationChecks(config) {
|
|
768
|
+
checkConfig(config);
|
|
769
|
+
int(config).nonRepudiation = (response) => {
|
|
770
|
+
const { as, fetch, tlsOnly, timeout, jwksCache } = int(config);
|
|
771
|
+
return oauth
|
|
772
|
+
.validateApplicationLevelSignature(as, response, {
|
|
773
|
+
[oauth.customFetch]: fetch,
|
|
774
|
+
[oauth.allowInsecureRequests]: !tlsOnly,
|
|
775
|
+
headers: new Headers(headers),
|
|
776
|
+
signal: signal(timeout),
|
|
777
|
+
[oauth.jwksCache]: jwksCache,
|
|
778
|
+
})
|
|
779
|
+
.catch(errorHandler);
|
|
780
|
+
};
|
|
781
|
+
}
|
|
782
|
+
export function useJwtResponseMode(config) {
|
|
783
|
+
checkConfig(config);
|
|
784
|
+
const { hybrid, implicit } = int(config);
|
|
785
|
+
if (hybrid || implicit) {
|
|
786
|
+
throw e('JARM cannot be combined with a hybrid or implicit response types', undefined, oauth.UNSUPPORTED_OPERATION);
|
|
787
|
+
}
|
|
788
|
+
int(config).jarm = (authorizationResponse, expectedState) => validateJARMResponse(config, authorizationResponse, expectedState);
|
|
789
|
+
}
|
|
790
|
+
export function enableDetachedSignatureResponseChecks(config) {
|
|
791
|
+
if (!int(config).hybrid) {
|
|
792
|
+
throw e('"code id_token" response type must be configured to be used first', undefined, oauth.UNSUPPORTED_OPERATION);
|
|
793
|
+
}
|
|
794
|
+
int(config).hybrid = (authorizationResponse, expectedNonce, expectedState, maxAge) => validateCodeIdTokenResponse(config, authorizationResponse, expectedNonce, expectedState, maxAge, true);
|
|
795
|
+
}
|
|
796
|
+
export async function implicitAuthentication(config, currentUrl, expectedNonce, checks) {
|
|
797
|
+
checkConfig(config);
|
|
798
|
+
if (!(currentUrl instanceof URL) &&
|
|
799
|
+
!webInstanceOf(currentUrl, 'Request')) {
|
|
800
|
+
throw CodedTypeError('"currentUrl" must be an instance of URL, or Request', ERR_INVALID_ARG_TYPE);
|
|
801
|
+
}
|
|
802
|
+
if (typeof expectedNonce !== 'string') {
|
|
803
|
+
throw CodedTypeError('"expectedNonce" must be a string', ERR_INVALID_ARG_TYPE);
|
|
804
|
+
}
|
|
805
|
+
const { as, c, fetch, tlsOnly, timeout, decrypt, implicit, jwksCache } = int(config);
|
|
806
|
+
if (!implicit) {
|
|
807
|
+
throw new TypeError('implicitAuthentication() cannot be used by clients using flows other than response_type=id_token');
|
|
808
|
+
}
|
|
809
|
+
let params;
|
|
810
|
+
if (!(currentUrl instanceof URL)) {
|
|
811
|
+
const request = currentUrl;
|
|
812
|
+
switch (request.method) {
|
|
813
|
+
case 'GET':
|
|
814
|
+
params = new URLSearchParams(new URL(request.url).hash.slice(1));
|
|
815
|
+
break;
|
|
816
|
+
case 'POST':
|
|
817
|
+
params = new URLSearchParams(await oauth.formPostResponse(request));
|
|
818
|
+
break;
|
|
819
|
+
default:
|
|
820
|
+
throw CodedTypeError('unexpected Request HTTP method', ERR_INVALID_ARG_VALUE);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
else {
|
|
824
|
+
params = new URLSearchParams(currentUrl.hash.slice(1));
|
|
825
|
+
}
|
|
826
|
+
try {
|
|
827
|
+
{
|
|
828
|
+
const decoy = new URLSearchParams(params);
|
|
829
|
+
decoy.delete('id_token');
|
|
830
|
+
oauth.validateAuthResponse({
|
|
831
|
+
...as,
|
|
832
|
+
authorization_response_iss_parameter_supported: undefined,
|
|
833
|
+
}, c, decoy, checks?.expectedState);
|
|
834
|
+
}
|
|
835
|
+
{
|
|
836
|
+
const decoy = new Response(JSON.stringify({
|
|
837
|
+
access_token: 'decoy',
|
|
838
|
+
token_type: 'bearer',
|
|
839
|
+
id_token: params.get('id_token'),
|
|
840
|
+
}), {
|
|
841
|
+
headers: new Headers({ 'content-type': 'application/json' }),
|
|
842
|
+
});
|
|
843
|
+
const signed = captureDecryptedResponse(decrypt);
|
|
844
|
+
const ref = await oauth.processAuthorizationCodeResponse(as, c, decoy, {
|
|
845
|
+
expectedNonce,
|
|
846
|
+
maxAge: checks?.maxAge,
|
|
847
|
+
[oauth.jweDecrypt]: signed.decrypt,
|
|
848
|
+
});
|
|
849
|
+
await oauth.validateApplicationLevelSignature(as, decoy, {
|
|
850
|
+
[oauth.customFetch]: fetch,
|
|
851
|
+
[oauth.allowInsecureRequests]: !tlsOnly,
|
|
852
|
+
headers: new Headers(headers),
|
|
853
|
+
signal: signal(timeout),
|
|
854
|
+
[oauth.jwksCache]: jwksCache,
|
|
855
|
+
});
|
|
856
|
+
return setDecryptedResponse(oauth.getValidatedIdTokenClaims(ref), signed.get());
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
catch (err) {
|
|
860
|
+
errorHandler(err);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
export function useCodeIdTokenResponseType(config) {
|
|
864
|
+
checkConfig(config);
|
|
865
|
+
const { jarm, implicit } = int(config);
|
|
866
|
+
if (jarm || implicit) {
|
|
867
|
+
throw e('"code id_token" response type cannot be combined with JARM or implicit response type', undefined, oauth.UNSUPPORTED_OPERATION);
|
|
868
|
+
}
|
|
869
|
+
int(config).hybrid = (authorizationResponse, expectedNonce, expectedState, maxAge) => validateCodeIdTokenResponse(config, authorizationResponse, expectedNonce, expectedState, maxAge, false);
|
|
870
|
+
}
|
|
871
|
+
export function useIdTokenResponseType(config) {
|
|
872
|
+
checkConfig(config);
|
|
873
|
+
const { jarm, hybrid } = int(config);
|
|
874
|
+
if (jarm || hybrid) {
|
|
875
|
+
throw e('"id_token" response type cannot be combined with JARM or hybrid response type', undefined, oauth.UNSUPPORTED_OPERATION);
|
|
876
|
+
}
|
|
877
|
+
int(config).implicit = true;
|
|
878
|
+
}
|
|
879
|
+
function stripParams(url) {
|
|
880
|
+
url = new URL(url);
|
|
881
|
+
url.search = '';
|
|
882
|
+
url.hash = '';
|
|
883
|
+
return url.href;
|
|
884
|
+
}
|
|
885
|
+
function webInstanceOf(input, toStringTag) {
|
|
886
|
+
try {
|
|
887
|
+
return Object.getPrototypeOf(input)[Symbol.toStringTag] === toStringTag;
|
|
888
|
+
}
|
|
889
|
+
catch {
|
|
890
|
+
return false;
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
export async function authorizationCodeGrant(config, currentUrl, checks, tokenEndpointParameters, options) {
|
|
894
|
+
checkConfig(config);
|
|
895
|
+
if (options?.flag !== retry &&
|
|
896
|
+
!(currentUrl instanceof URL) &&
|
|
897
|
+
!webInstanceOf(currentUrl, 'Request')) {
|
|
898
|
+
throw CodedTypeError('"currentUrl" must be an instance of URL, or Request', ERR_INVALID_ARG_TYPE);
|
|
899
|
+
}
|
|
900
|
+
let authResponse;
|
|
901
|
+
let redirectUri;
|
|
902
|
+
const { as, c, auth, fetch, tlsOnly, jarm, hybrid, nonRepudiation, timeout, decrypt, implicit } = int(config);
|
|
903
|
+
if (options?.flag === retry) {
|
|
904
|
+
authResponse = options.authResponse;
|
|
905
|
+
redirectUri = options.redirectUri;
|
|
906
|
+
}
|
|
907
|
+
else {
|
|
908
|
+
if (!(currentUrl instanceof URL)) {
|
|
909
|
+
const request = currentUrl;
|
|
910
|
+
currentUrl = new URL(currentUrl.url);
|
|
911
|
+
switch (request.method) {
|
|
912
|
+
case 'GET':
|
|
913
|
+
break;
|
|
914
|
+
case 'POST':
|
|
915
|
+
const params = new URLSearchParams(await oauth.formPostResponse(request));
|
|
916
|
+
if (hybrid) {
|
|
917
|
+
currentUrl.hash = params.toString();
|
|
918
|
+
}
|
|
919
|
+
else {
|
|
920
|
+
for (const [k, v] of params.entries()) {
|
|
921
|
+
currentUrl.searchParams.append(k, v);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
break;
|
|
925
|
+
default:
|
|
926
|
+
throw CodedTypeError('unexpected Request HTTP method', ERR_INVALID_ARG_VALUE);
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
redirectUri = stripParams(currentUrl);
|
|
930
|
+
switch (true) {
|
|
931
|
+
case !!jarm:
|
|
932
|
+
authResponse = await jarm(currentUrl, checks?.expectedState);
|
|
933
|
+
break;
|
|
934
|
+
case !!hybrid:
|
|
935
|
+
authResponse = await hybrid(currentUrl, checks?.expectedNonce, checks?.expectedState, checks?.maxAge);
|
|
936
|
+
break;
|
|
937
|
+
case !!implicit:
|
|
938
|
+
throw new TypeError('authorizationCodeGrant() cannot be used by response_type=id_token clients');
|
|
939
|
+
default:
|
|
940
|
+
try {
|
|
941
|
+
authResponse = oauth.validateAuthResponse(as, c, currentUrl.searchParams, checks?.expectedState);
|
|
942
|
+
}
|
|
943
|
+
catch (err) {
|
|
944
|
+
errorHandler(err);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
const response = await oauth
|
|
949
|
+
.authorizationCodeGrantRequest(as, c, auth, authResponse, redirectUri, checks?.pkceCodeVerifier || oauth.nopkce, {
|
|
950
|
+
additionalParameters: tokenEndpointParameters,
|
|
951
|
+
[oauth.customFetch]: fetch,
|
|
952
|
+
[oauth.allowInsecureRequests]: !tlsOnly,
|
|
953
|
+
DPoP: options?.DPoP,
|
|
954
|
+
headers: new Headers(headers),
|
|
955
|
+
signal: signal(timeout),
|
|
956
|
+
})
|
|
957
|
+
.catch(errorHandler);
|
|
958
|
+
const signed = captureDecryptedResponse(decrypt);
|
|
959
|
+
const p = oauth.processAuthorizationCodeResponse(as, c, response, {
|
|
960
|
+
expectedNonce: checks?.expectedNonce,
|
|
961
|
+
maxAge: checks?.maxAge,
|
|
962
|
+
requireIdToken: checks?.idTokenExpected,
|
|
963
|
+
[oauth.jweDecrypt]: signed.decrypt,
|
|
964
|
+
});
|
|
965
|
+
let result;
|
|
966
|
+
try {
|
|
967
|
+
result = await p;
|
|
968
|
+
}
|
|
969
|
+
catch (err) {
|
|
970
|
+
if (retryable(err, options)) {
|
|
971
|
+
return authorizationCodeGrant(config, undefined, checks, tokenEndpointParameters, {
|
|
972
|
+
...options,
|
|
973
|
+
flag: retry,
|
|
974
|
+
authResponse: authResponse,
|
|
975
|
+
redirectUri: redirectUri,
|
|
976
|
+
});
|
|
977
|
+
}
|
|
978
|
+
errorHandler(err);
|
|
979
|
+
}
|
|
980
|
+
result.id_token && (await nonRepudiation?.(response));
|
|
981
|
+
addHelpers(result);
|
|
982
|
+
const authorization = decryptedResponses?.get(authResponse);
|
|
983
|
+
const tokenEndpoint = signed.get();
|
|
984
|
+
let jwts;
|
|
985
|
+
if (tokenEndpoint || authorization) {
|
|
986
|
+
const authorizationEndpoint = authorization?.id_token?.authorization_endpoint;
|
|
987
|
+
jwts = Object.freeze({
|
|
988
|
+
...(authorization?.authorization_response && {
|
|
989
|
+
authorization_response: authorization.authorization_response,
|
|
990
|
+
}),
|
|
991
|
+
...((tokenEndpoint || authorizationEndpoint) && {
|
|
992
|
+
id_token: Object.freeze({
|
|
993
|
+
...(tokenEndpoint && { token_endpoint: tokenEndpoint }),
|
|
994
|
+
...(authorizationEndpoint && {
|
|
995
|
+
authorization_endpoint: authorizationEndpoint,
|
|
996
|
+
}),
|
|
997
|
+
}),
|
|
998
|
+
}),
|
|
999
|
+
});
|
|
1000
|
+
}
|
|
1001
|
+
return setDecryptedResponse(result, jwts);
|
|
1002
|
+
}
|
|
1003
|
+
async function validateJARMResponse(config, authorizationResponse, expectedState) {
|
|
1004
|
+
const { as, c, fetch, tlsOnly, timeout, decrypt, jwksCache } = int(config);
|
|
1005
|
+
const signed = captureDecryptedResponse(decrypt);
|
|
1006
|
+
const result = await oauth
|
|
1007
|
+
.validateJwtAuthResponse(as, c, authorizationResponse, expectedState, {
|
|
1008
|
+
[oauth.customFetch]: fetch,
|
|
1009
|
+
[oauth.allowInsecureRequests]: !tlsOnly,
|
|
1010
|
+
headers: new Headers(headers),
|
|
1011
|
+
signal: signal(timeout),
|
|
1012
|
+
[oauth.jweDecrypt]: signed.decrypt,
|
|
1013
|
+
[oauth.jwksCache]: jwksCache,
|
|
1014
|
+
})
|
|
1015
|
+
.catch(errorHandler);
|
|
1016
|
+
const jwt = signed.get();
|
|
1017
|
+
if (jwt) {
|
|
1018
|
+
setDecryptedResponse(result, Object.freeze({ authorization_response: jwt }));
|
|
1019
|
+
}
|
|
1020
|
+
return result;
|
|
1021
|
+
}
|
|
1022
|
+
async function validateCodeIdTokenResponse(config, authorizationResponse, expectedNonce, expectedState, maxAge, fapi) {
|
|
1023
|
+
if (typeof expectedNonce !== 'string') {
|
|
1024
|
+
throw CodedTypeError('"expectedNonce" must be a string', ERR_INVALID_ARG_TYPE);
|
|
1025
|
+
}
|
|
1026
|
+
if (expectedState !== undefined && typeof expectedState !== 'string') {
|
|
1027
|
+
throw CodedTypeError('"expectedState" must be a string', ERR_INVALID_ARG_TYPE);
|
|
1028
|
+
}
|
|
1029
|
+
const { as, c, fetch, tlsOnly, timeout, decrypt, jwksCache } = int(config);
|
|
1030
|
+
const signed = captureDecryptedResponse(decrypt);
|
|
1031
|
+
const result = await (fapi
|
|
1032
|
+
? oauth.validateDetachedSignatureResponse
|
|
1033
|
+
: oauth.validateCodeIdTokenResponse)(as, c, authorizationResponse, expectedNonce, expectedState, maxAge, {
|
|
1034
|
+
[oauth.customFetch]: fetch,
|
|
1035
|
+
[oauth.allowInsecureRequests]: !tlsOnly,
|
|
1036
|
+
headers: new Headers(headers),
|
|
1037
|
+
signal: signal(timeout),
|
|
1038
|
+
[oauth.jweDecrypt]: signed.decrypt,
|
|
1039
|
+
[oauth.jwksCache]: jwksCache,
|
|
1040
|
+
}).catch(errorHandler);
|
|
1041
|
+
const jwt = signed.get();
|
|
1042
|
+
if (jwt) {
|
|
1043
|
+
setDecryptedResponse(result, Object.freeze({
|
|
1044
|
+
id_token: Object.freeze({ authorization_endpoint: jwt }),
|
|
1045
|
+
}));
|
|
1046
|
+
}
|
|
1047
|
+
return result;
|
|
1048
|
+
}
|
|
1049
|
+
export async function refreshTokenGrant(config, refreshToken, parameters, options) {
|
|
1050
|
+
checkConfig(config);
|
|
1051
|
+
parameters = new URLSearchParams(parameters);
|
|
1052
|
+
const { as, c, auth, fetch, tlsOnly, nonRepudiation, timeout, decrypt } = int(config);
|
|
1053
|
+
const response = await oauth
|
|
1054
|
+
.refreshTokenGrantRequest(as, c, auth, refreshToken, {
|
|
1055
|
+
[oauth.customFetch]: fetch,
|
|
1056
|
+
[oauth.allowInsecureRequests]: !tlsOnly,
|
|
1057
|
+
additionalParameters: parameters,
|
|
1058
|
+
DPoP: options?.DPoP,
|
|
1059
|
+
headers: new Headers(headers),
|
|
1060
|
+
signal: signal(timeout),
|
|
1061
|
+
})
|
|
1062
|
+
.catch(errorHandler);
|
|
1063
|
+
const signed = captureDecryptedResponse(decrypt);
|
|
1064
|
+
const p = oauth.processRefreshTokenResponse(as, c, response, {
|
|
1065
|
+
[oauth.jweDecrypt]: signed.decrypt,
|
|
1066
|
+
});
|
|
1067
|
+
let result;
|
|
1068
|
+
try {
|
|
1069
|
+
result = await p;
|
|
1070
|
+
}
|
|
1071
|
+
catch (err) {
|
|
1072
|
+
if (retryable(err, options)) {
|
|
1073
|
+
return refreshTokenGrant(config, refreshToken, parameters, {
|
|
1074
|
+
...options,
|
|
1075
|
+
flag: retry,
|
|
1076
|
+
});
|
|
1077
|
+
}
|
|
1078
|
+
errorHandler(err);
|
|
1079
|
+
}
|
|
1080
|
+
result.id_token && (await nonRepudiation?.(response));
|
|
1081
|
+
addHelpers(result);
|
|
1082
|
+
return setDecryptedResponse(result, signed.get());
|
|
1083
|
+
}
|
|
1084
|
+
export async function clientCredentialsGrant(config, parameters, options) {
|
|
1085
|
+
checkConfig(config);
|
|
1086
|
+
parameters = new URLSearchParams(parameters);
|
|
1087
|
+
const { as, c, auth, fetch, tlsOnly, timeout } = int(config);
|
|
1088
|
+
const response = await oauth
|
|
1089
|
+
.clientCredentialsGrantRequest(as, c, auth, parameters, {
|
|
1090
|
+
[oauth.customFetch]: fetch,
|
|
1091
|
+
[oauth.allowInsecureRequests]: !tlsOnly,
|
|
1092
|
+
DPoP: options?.DPoP,
|
|
1093
|
+
headers: new Headers(headers),
|
|
1094
|
+
signal: signal(timeout),
|
|
1095
|
+
})
|
|
1096
|
+
.catch(errorHandler);
|
|
1097
|
+
const p = oauth.processClientCredentialsResponse(as, c, response);
|
|
1098
|
+
let result;
|
|
1099
|
+
try {
|
|
1100
|
+
result = await p;
|
|
1101
|
+
}
|
|
1102
|
+
catch (err) {
|
|
1103
|
+
if (retryable(err, options)) {
|
|
1104
|
+
return clientCredentialsGrant(config, parameters, {
|
|
1105
|
+
...options,
|
|
1106
|
+
flag: retry,
|
|
1107
|
+
});
|
|
1108
|
+
}
|
|
1109
|
+
errorHandler(err);
|
|
1110
|
+
}
|
|
1111
|
+
addHelpers(result);
|
|
1112
|
+
return result;
|
|
1113
|
+
}
|
|
1114
|
+
export function buildAuthorizationUrl(config, parameters) {
|
|
1115
|
+
checkConfig(config);
|
|
1116
|
+
const { as, c, tlsOnly, hybrid, jarm, implicit } = int(config);
|
|
1117
|
+
const authorizationEndpoint = oauth.resolveEndpoint(as, 'authorization_endpoint', false, tlsOnly);
|
|
1118
|
+
parameters = new URLSearchParams(parameters);
|
|
1119
|
+
if (!parameters.has('client_id')) {
|
|
1120
|
+
parameters.set('client_id', c.client_id);
|
|
1121
|
+
}
|
|
1122
|
+
if (!parameters.has('request_uri') && !parameters.has('request')) {
|
|
1123
|
+
if (!parameters.has('response_type')) {
|
|
1124
|
+
parameters.set('response_type', hybrid ? 'code id_token' : implicit ? 'id_token' : 'code');
|
|
1125
|
+
}
|
|
1126
|
+
if (implicit && !parameters.has('nonce')) {
|
|
1127
|
+
throw CodedTypeError('response_type=id_token clients must provide a nonce parameter in their authorization request parameters', ERR_INVALID_ARG_VALUE);
|
|
1128
|
+
}
|
|
1129
|
+
if (jarm) {
|
|
1130
|
+
parameters.set('response_mode', 'jwt');
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
for (const [k, v] of parameters.entries()) {
|
|
1134
|
+
authorizationEndpoint.searchParams.append(k, v);
|
|
1135
|
+
}
|
|
1136
|
+
return authorizationEndpoint;
|
|
1137
|
+
}
|
|
1138
|
+
export async function buildAuthorizationUrlWithJAR(config, parameters, signingKey, options) {
|
|
1139
|
+
checkConfig(config);
|
|
1140
|
+
const authorizationEndpoint = buildAuthorizationUrl(config, parameters);
|
|
1141
|
+
parameters = authorizationEndpoint.searchParams;
|
|
1142
|
+
if (!signingKey) {
|
|
1143
|
+
throw CodedTypeError('"signingKey" must be provided', ERR_INVALID_ARG_VALUE);
|
|
1144
|
+
}
|
|
1145
|
+
const { as, c } = int(config);
|
|
1146
|
+
const request = await oauth
|
|
1147
|
+
.issueRequestObject(as, c, parameters, signingKey, options)
|
|
1148
|
+
.catch(errorHandler);
|
|
1149
|
+
return buildAuthorizationUrl(config, { request });
|
|
1150
|
+
}
|
|
1151
|
+
export async function buildAuthorizationUrlWithPAR(config, parameters, options) {
|
|
1152
|
+
checkConfig(config);
|
|
1153
|
+
const authorizationEndpoint = buildAuthorizationUrl(config, parameters);
|
|
1154
|
+
const { as, c, auth, fetch, tlsOnly, timeout } = int(config);
|
|
1155
|
+
const response = await oauth
|
|
1156
|
+
.pushedAuthorizationRequest(as, c, auth, authorizationEndpoint.searchParams, {
|
|
1157
|
+
[oauth.customFetch]: fetch,
|
|
1158
|
+
[oauth.allowInsecureRequests]: !tlsOnly,
|
|
1159
|
+
DPoP: options?.DPoP,
|
|
1160
|
+
headers: new Headers(headers),
|
|
1161
|
+
signal: signal(timeout),
|
|
1162
|
+
})
|
|
1163
|
+
.catch(errorHandler);
|
|
1164
|
+
const p = oauth.processPushedAuthorizationResponse(as, c, response);
|
|
1165
|
+
let result;
|
|
1166
|
+
try {
|
|
1167
|
+
result = await p;
|
|
1168
|
+
}
|
|
1169
|
+
catch (err) {
|
|
1170
|
+
if (retryable(err, options)) {
|
|
1171
|
+
return buildAuthorizationUrlWithPAR(config, parameters, {
|
|
1172
|
+
...options,
|
|
1173
|
+
flag: retry,
|
|
1174
|
+
});
|
|
1175
|
+
}
|
|
1176
|
+
errorHandler(err);
|
|
1177
|
+
}
|
|
1178
|
+
return buildAuthorizationUrl(config, { request_uri: result.request_uri });
|
|
1179
|
+
}
|
|
1180
|
+
export function buildEndSessionUrl(config, parameters) {
|
|
1181
|
+
checkConfig(config);
|
|
1182
|
+
const { as, c, tlsOnly } = int(config);
|
|
1183
|
+
const endSessionEndpoint = oauth.resolveEndpoint(as, 'end_session_endpoint', false, tlsOnly);
|
|
1184
|
+
parameters = new URLSearchParams(parameters);
|
|
1185
|
+
if (!parameters.has('client_id')) {
|
|
1186
|
+
parameters.set('client_id', c.client_id);
|
|
1187
|
+
}
|
|
1188
|
+
for (const [k, v] of parameters.entries()) {
|
|
1189
|
+
endSessionEndpoint.searchParams.append(k, v);
|
|
1190
|
+
}
|
|
1191
|
+
return endSessionEndpoint;
|
|
1192
|
+
}
|
|
1193
|
+
function checkConfig(input) {
|
|
1194
|
+
if (!(input instanceof Configuration)) {
|
|
1195
|
+
throw CodedTypeError('"config" must be an instance of Configuration', ERR_INVALID_ARG_TYPE);
|
|
1196
|
+
}
|
|
1197
|
+
if (Object.getPrototypeOf(input) !== Configuration.prototype) {
|
|
1198
|
+
throw CodedTypeError('subclassing Configuration is not allowed', ERR_INVALID_ARG_VALUE);
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
function signal(timeout) {
|
|
1202
|
+
return timeout ? AbortSignal.timeout(timeout * 1000) : undefined;
|
|
1203
|
+
}
|
|
1204
|
+
export async function fetchUserInfo(config, accessToken, expectedSubject, options) {
|
|
1205
|
+
checkConfig(config);
|
|
1206
|
+
const { as, c, fetch, tlsOnly, nonRepudiation, timeout, decrypt } = int(config);
|
|
1207
|
+
const response = await oauth
|
|
1208
|
+
.userInfoRequest(as, c, accessToken, {
|
|
1209
|
+
[oauth.customFetch]: fetch,
|
|
1210
|
+
[oauth.allowInsecureRequests]: !tlsOnly,
|
|
1211
|
+
DPoP: options?.DPoP,
|
|
1212
|
+
headers: new Headers(headers),
|
|
1213
|
+
signal: signal(timeout),
|
|
1214
|
+
})
|
|
1215
|
+
.catch(errorHandler);
|
|
1216
|
+
const signed = captureDecryptedResponse(decrypt);
|
|
1217
|
+
let exec = oauth.processUserInfoResponse(as, c, expectedSubject, response, {
|
|
1218
|
+
[oauth.jweDecrypt]: signed.decrypt,
|
|
1219
|
+
});
|
|
1220
|
+
let result;
|
|
1221
|
+
try {
|
|
1222
|
+
result = await exec;
|
|
1223
|
+
}
|
|
1224
|
+
catch (err) {
|
|
1225
|
+
if (retryable(err, options)) {
|
|
1226
|
+
return fetchUserInfo(config, accessToken, expectedSubject, {
|
|
1227
|
+
...options,
|
|
1228
|
+
flag: retry,
|
|
1229
|
+
});
|
|
1230
|
+
}
|
|
1231
|
+
errorHandler(err);
|
|
1232
|
+
}
|
|
1233
|
+
oauth.getContentType(response) === 'application/jwt' &&
|
|
1234
|
+
(await nonRepudiation?.(response));
|
|
1235
|
+
return setDecryptedResponse(result, signed.get());
|
|
1236
|
+
}
|
|
1237
|
+
function retryable(err, options) {
|
|
1238
|
+
if (options?.DPoP && options.flag !== retry) {
|
|
1239
|
+
return oauth.isDPoPNonceError(err);
|
|
1240
|
+
}
|
|
1241
|
+
return false;
|
|
1242
|
+
}
|
|
1243
|
+
export async function tokenIntrospection(config, token, parameters) {
|
|
1244
|
+
checkConfig(config);
|
|
1245
|
+
const { as, c, auth, fetch, tlsOnly, nonRepudiation, timeout, decrypt } = int(config);
|
|
1246
|
+
const response = await oauth
|
|
1247
|
+
.introspectionRequest(as, c, auth, token, {
|
|
1248
|
+
[oauth.customFetch]: fetch,
|
|
1249
|
+
[oauth.allowInsecureRequests]: !tlsOnly,
|
|
1250
|
+
additionalParameters: new URLSearchParams(parameters),
|
|
1251
|
+
headers: new Headers(headers),
|
|
1252
|
+
signal: signal(timeout),
|
|
1253
|
+
})
|
|
1254
|
+
.catch(errorHandler);
|
|
1255
|
+
const signed = captureDecryptedResponse(decrypt);
|
|
1256
|
+
const result = await oauth
|
|
1257
|
+
.processIntrospectionResponse(as, c, response, {
|
|
1258
|
+
[oauth.jweDecrypt]: signed.decrypt,
|
|
1259
|
+
})
|
|
1260
|
+
.catch(errorHandler);
|
|
1261
|
+
oauth.getContentType(response) === 'application/token-introspection+jwt' &&
|
|
1262
|
+
(await nonRepudiation?.(response));
|
|
1263
|
+
return setDecryptedResponse(result, signed.get());
|
|
1264
|
+
}
|
|
1265
|
+
const retry = Symbol();
|
|
1266
|
+
export async function genericGrantRequest(config, grantType, parameters, options) {
|
|
1267
|
+
checkConfig(config);
|
|
1268
|
+
const { as, c, auth, fetch, tlsOnly, timeout, decrypt, nonRepudiation } = int(config);
|
|
1269
|
+
const response = await oauth
|
|
1270
|
+
.genericTokenEndpointRequest(as, c, auth, grantType, new URLSearchParams(parameters), {
|
|
1271
|
+
[oauth.customFetch]: fetch,
|
|
1272
|
+
[oauth.allowInsecureRequests]: !tlsOnly,
|
|
1273
|
+
DPoP: options?.DPoP,
|
|
1274
|
+
headers: new Headers(headers),
|
|
1275
|
+
signal: signal(timeout),
|
|
1276
|
+
})
|
|
1277
|
+
.catch(errorHandler);
|
|
1278
|
+
let recognizedTokenTypes;
|
|
1279
|
+
if (grantType === 'urn:ietf:params:oauth:grant-type:token-exchange') {
|
|
1280
|
+
recognizedTokenTypes = { n_a: () => { } };
|
|
1281
|
+
}
|
|
1282
|
+
const signed = captureDecryptedResponse(decrypt);
|
|
1283
|
+
const p = oauth.processGenericTokenEndpointResponse(as, c, response, {
|
|
1284
|
+
[oauth.jweDecrypt]: signed.decrypt,
|
|
1285
|
+
recognizedTokenTypes,
|
|
1286
|
+
});
|
|
1287
|
+
let result;
|
|
1288
|
+
try {
|
|
1289
|
+
result = await p;
|
|
1290
|
+
}
|
|
1291
|
+
catch (err) {
|
|
1292
|
+
if (retryable(err, options)) {
|
|
1293
|
+
return genericGrantRequest(config, grantType, parameters, {
|
|
1294
|
+
...options,
|
|
1295
|
+
flag: retry,
|
|
1296
|
+
});
|
|
1297
|
+
}
|
|
1298
|
+
errorHandler(err);
|
|
1299
|
+
}
|
|
1300
|
+
result.id_token && (await nonRepudiation?.(response));
|
|
1301
|
+
addHelpers(result);
|
|
1302
|
+
return setDecryptedResponse(result, signed.get());
|
|
1303
|
+
}
|
|
1304
|
+
export async function tokenRevocation(config, token, parameters) {
|
|
1305
|
+
checkConfig(config);
|
|
1306
|
+
const { as, c, auth, fetch, tlsOnly, timeout } = int(config);
|
|
1307
|
+
return oauth
|
|
1308
|
+
.revocationRequest(as, c, auth, token, {
|
|
1309
|
+
[oauth.customFetch]: fetch,
|
|
1310
|
+
[oauth.allowInsecureRequests]: !tlsOnly,
|
|
1311
|
+
additionalParameters: new URLSearchParams(parameters),
|
|
1312
|
+
headers: new Headers(headers),
|
|
1313
|
+
signal: signal(timeout),
|
|
1314
|
+
})
|
|
1315
|
+
.then(oauth.processRevocationResponse)
|
|
1316
|
+
.catch(errorHandler);
|
|
1317
|
+
}
|
|
1318
|
+
export async function fetchProtectedResource(config, accessToken, url, method, body, headers, options) {
|
|
1319
|
+
checkConfig(config);
|
|
1320
|
+
headers ||= new Headers();
|
|
1321
|
+
if (USER_AGENT && !headers.has('user-agent')) {
|
|
1322
|
+
headers.set('user-agent', USER_AGENT);
|
|
1323
|
+
}
|
|
1324
|
+
const { fetch, tlsOnly, timeout } = int(config);
|
|
1325
|
+
const exec = oauth.protectedResourceRequest(accessToken, method, url, headers, body, {
|
|
1326
|
+
[oauth.customFetch]: fetch,
|
|
1327
|
+
[oauth.allowInsecureRequests]: !tlsOnly,
|
|
1328
|
+
DPoP: options?.DPoP,
|
|
1329
|
+
signal: signal(timeout),
|
|
1330
|
+
});
|
|
1331
|
+
let result;
|
|
1332
|
+
try {
|
|
1333
|
+
result = await exec;
|
|
1334
|
+
}
|
|
1335
|
+
catch (err) {
|
|
1336
|
+
if (retryable(err, options) &&
|
|
1337
|
+
!webInstanceOf(body, 'ReadableStream')) {
|
|
1338
|
+
return fetchProtectedResource(config, accessToken, url, method, body, headers, {
|
|
1339
|
+
...options,
|
|
1340
|
+
flag: retry,
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
errorHandler(err);
|
|
1344
|
+
}
|
|
1345
|
+
return result;
|
|
1346
|
+
}
|
|
1347
|
+
//# sourceMappingURL=index.js.map
|