axiam-sdk 1.0.0-alpha21 → 1.0.0-alpha24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -2
- package/dist/{authzCore-DuRI1aJf.d.cts → authzCore-CA9zFm1n.d.cts} +38 -4
- package/dist/{authzCore-DtSN59if.d.ts → authzCore-a-wZL2P3.d.ts} +38 -4
- package/dist/{client-B_FqmZ0i.d.cts → client-CtT9nHQP.d.cts} +1 -1
- package/dist/{client-Dn1fs5MH.d.ts → client-utwuwc9m.d.ts} +1 -1
- package/dist/{cookieJar-Cl8Lq9dL.d.ts → cookieJar-C9fqLqbh.d.ts} +29 -6
- package/dist/{cookieJar-BTDnjjbL.d.cts → cookieJar-DeJKczxD.d.cts} +29 -6
- package/dist/grpc/index.d.cts +5 -5
- package/dist/grpc/index.d.ts +5 -5
- package/dist/grpc/index.js +81 -14
- package/dist/grpc/index.js.map +1 -1
- package/dist/grpc/index.mjs +81 -14
- package/dist/grpc/index.mjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +32 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +32 -7
- package/dist/index.mjs.map +1 -1
- package/dist/{jwks-BgojMGC5.d.cts → jwks-DEiwxlku.d.cts} +84 -3
- package/dist/{jwks-BgojMGC5.d.ts → jwks-DEiwxlku.d.ts} +84 -3
- package/dist/middleware/index.d.cts +6 -6
- package/dist/middleware/index.d.ts +6 -6
- package/dist/middleware/index.js +177 -138
- package/dist/middleware/index.js.map +1 -1
- package/dist/middleware/index.mjs +176 -139
- package/dist/middleware/index.mjs.map +1 -1
- package/dist/nestjs/index.d.cts +2 -2
- package/dist/nestjs/index.d.ts +2 -2
- package/dist/node/index.d.cts +94 -6
- package/dist/node/index.d.ts +94 -6
- package/dist/node/index.js +183 -16
- package/dist/node/index.js.map +1 -1
- package/dist/node/index.mjs +179 -18
- package/dist/node/index.mjs.map +1 -1
- package/dist/{oidcState-CkFuCjpM.d.ts → oidcState-CfebuJ0n.d.ts} +2 -2
- package/dist/{oidcState-BGxqWUun.d.cts → oidcState-tHRAg5bu.d.cts} +2 -2
- package/dist/rest/index.d.cts +2 -2
- package/dist/rest/index.d.ts +2 -2
- package/dist/rest/index.js +32 -7
- package/dist/rest/index.js.map +1 -1
- package/dist/rest/index.mjs +32 -7
- package/dist/rest/index.mjs.map +1 -1
- package/dist/{session-BWUWhhyB.d.cts → session-ti3C9ZJU.d.cts} +28 -1
- package/dist/{session-BWUWhhyB.d.ts → session-ti3C9ZJU.d.ts} +28 -1
- package/package.json +2 -2
|
@@ -91,10 +91,77 @@ interface AxiamClaims {
|
|
|
91
91
|
}
|
|
92
92
|
/** Path (relative to the client base URL) of the org-wide JWKS endpoint. */
|
|
93
93
|
declare const JWKS_PATH = "/oauth2/jwks";
|
|
94
|
+
/**
|
|
95
|
+
* Clock-skew leeway applied to the `exp` and `nbf` checks (CONTRACT.md §10.1
|
|
96
|
+
* rule 7).
|
|
97
|
+
*
|
|
98
|
+
* A **named, bounded, non-configurable** constant, fixed at the contract's
|
|
99
|
+
* RECOMMENDED 60 seconds. Rule 7 forbids both an inline literal and an
|
|
100
|
+
* operator-settable value that could be widened without bound, so there is
|
|
101
|
+
* deliberately no option to override it — widening the window is a source
|
|
102
|
+
* change, reviewable as such.
|
|
103
|
+
*/
|
|
104
|
+
declare const CLOCK_SKEW_LEEWAY_SEC = 60;
|
|
105
|
+
/**
|
|
106
|
+
* The verification policy {@link Verifier.verifyAccessToken} enforces, per
|
|
107
|
+
* CONTRACT.md §10.1.
|
|
108
|
+
*
|
|
109
|
+
* `expectedTenantId` is **required** (rule 4): the `/oauth2/jwks` trust
|
|
110
|
+
* anchor is organization-wide, so a valid signature proves only "some tenant
|
|
111
|
+
* in this organization". `expectedIssuer` and `expectedAudience` are
|
|
112
|
+
* **conditional** (rules 5 and 6) — omitted means the check is not performed,
|
|
113
|
+
* and the SDK never assumes a default issuer.
|
|
114
|
+
*/
|
|
115
|
+
interface AccessTokenExpectations {
|
|
116
|
+
/**
|
|
117
|
+
* §10.1 rule 4 — the tenant this resource server is configured for. The
|
|
118
|
+
* token's `tenant_id` claim MUST equal it; an absent claim, an empty
|
|
119
|
+
* expectation, or a mismatch all reject (fail closed).
|
|
120
|
+
*/
|
|
121
|
+
expectedTenantId: string;
|
|
122
|
+
/** §10.1 rule 5 — expected `iss`. Omit for no issuer check. */
|
|
123
|
+
expectedIssuer?: string;
|
|
124
|
+
/**
|
|
125
|
+
* §10.1 rule 6 — expected `aud`. Omit for no audience check. A resource
|
|
126
|
+
* server guarding user-facing routes SHOULD pass `"axiam:user"`.
|
|
127
|
+
*/
|
|
128
|
+
expectedAudience?: string;
|
|
129
|
+
}
|
|
94
130
|
/** Verifies an AXIAM access token's EdDSA signature against the org-wide remote JWKS. */
|
|
95
131
|
interface Verifier {
|
|
96
|
-
/**
|
|
97
|
-
|
|
132
|
+
/**
|
|
133
|
+
* Verify `token` against the **complete** CONTRACT.md §10.1 minimum
|
|
134
|
+
* local-verification set and return its claims.
|
|
135
|
+
*
|
|
136
|
+
* | § | rule | how it is enforced |
|
|
137
|
+
* |---|---|---|
|
|
138
|
+
* | 1 | signature | `algorithms: ['EdDSA']` is checked by `jose` against the JWS header *before* the remote key set function is invoked, so `alg: none` and an HS-signed token bearing an EdDSA `kid` are rejected without a key lookup. |
|
|
139
|
+
* | 2 | `exp` | `requiredClaims: ['exp']` — `jose` only checks `exp` *if present* by default, so an absent `exp` (a permanent credential) would otherwise sail through. A non-numeric `exp` is rejected by `jose`'s own type check. |
|
|
140
|
+
* | 3 | `nbf` | honoured by `jose` when present, bounded by {@link CLOCK_SKEW_LEEWAY_SEC}. |
|
|
141
|
+
* | 4 | `tenant_id` | asserted against {@link AccessTokenExpectations.expectedTenantId}; absent claim, absent expectation, or mismatch all reject. |
|
|
142
|
+
* | 5 | `iss` | checked only when `expectedIssuer` is supplied. |
|
|
143
|
+
* | 6 | `aud` | checked only when `expectedAudience` is supplied. |
|
|
144
|
+
* | 7 | clock skew | {@link CLOCK_SKEW_LEEWAY_SEC}, passed as `clockTolerance`. |
|
|
145
|
+
*
|
|
146
|
+
* Rejects with the underlying error on any failed rule; the §10 middleware
|
|
147
|
+
* wraps that into an `AuthError`.
|
|
148
|
+
*/
|
|
149
|
+
verifyAccessToken(token: string, expectations: AccessTokenExpectations): Promise<AxiamClaims>;
|
|
150
|
+
/**
|
|
151
|
+
* Verify **only** the EdDSA signature of `token` — CONTRACT.md §10.1's
|
|
152
|
+
* "raw signature-only primitive".
|
|
153
|
+
*
|
|
154
|
+
* @remarks
|
|
155
|
+
* **This is not a guard.** It performs no `exp`, `nbf`, `tenant_id`, `iss`
|
|
156
|
+
* or `aud` check whatsoever: an expired token, a not-yet-valid token, and a
|
|
157
|
+
* token minted for a *different tenant* in the same organization all pass.
|
|
158
|
+
* It exists only for integrators deliberately implementing their own
|
|
159
|
+
* policy on top of the signature — the `Unchecked` suffix is there to make
|
|
160
|
+
* that omission obvious at the call site. Anything guarding a route MUST
|
|
161
|
+
* use {@link Verifier.verifyAccessToken} (or, better, the §10 middleware
|
|
162
|
+
* built on it).
|
|
163
|
+
*/
|
|
164
|
+
verifySignatureOnlyUnchecked(token: string): Promise<AxiamClaims>;
|
|
98
165
|
}
|
|
99
166
|
/**
|
|
100
167
|
* The relying-party half of the same verifier (CONTRACT.md §12.4): validates
|
|
@@ -148,5 +215,19 @@ declare function createVerifier(baseUrl: string): JwksVerifier;
|
|
|
148
215
|
* `verifyAccessToken` and `verifyIdToken`.
|
|
149
216
|
*/
|
|
150
217
|
declare function createJwksVerifier(jwksUri: string): JwksVerifier;
|
|
218
|
+
/**
|
|
219
|
+
* CONTRACT.md §10.1 rule 4 — assert the token's `tenant_id` claim against the
|
|
220
|
+
* tenant the relying party is configured for.
|
|
221
|
+
*
|
|
222
|
+
* Exported so the §10 middleware can re-assert it on the guard side, where it
|
|
223
|
+
* cannot be bypassed by a caller-supplied {@link Verifier} implementation
|
|
224
|
+
* that ignores its expectations.
|
|
225
|
+
*
|
|
226
|
+
* @param claim - the raw `tenant_id` claim value off the verified payload.
|
|
227
|
+
* @param expected - the configured tenant.
|
|
228
|
+
* @throws AuthError when no tenant is configured, when the claim is absent or
|
|
229
|
+
* not a non-empty string, or when the two differ.
|
|
230
|
+
*/
|
|
231
|
+
declare function assertTenantClaim(claim: unknown, expected: string | undefined): void;
|
|
151
232
|
|
|
152
|
-
export { type
|
|
233
|
+
export { type AccessTokenExpectations as A, CLOCK_SKEW_LEEWAY_SEC as C, ID_TOKEN_ALG as I, JWKS_PATH as J, MAX_CLOCK_SKEW_SEC as M, type Verifier as V, type AxiamClaims as a, type IdTokenClaims as b, type IdTokenExpectations as c, type IdTokenVerifier as d, type JwksVerifier as e, assertTenantClaim as f, createJwksVerifier as g, createVerifier as h };
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Request, RequestHandler } from 'express';
|
|
2
|
-
import { a as AuthzLogger, E as ErrorBody, b as AxiamIdentity, V as VerifiableSession, A as AuthzVerifiableSession, c as ResourceSpec, R as RequireAccessOptions } from '../authzCore-
|
|
3
|
-
export { d as AuthzChecker, C as CheckOutcome, e as ResourceParamRef, f as ResourceResolutionError, g as ResourceResolver, h as assertAuthzClient, i as authenticateRequest, j as evaluateAccess, k as fromParam, l as hasAnyRole, r as resolveResourceId } from '../authzCore-
|
|
4
|
-
import { d as OidcClient, j as OidcStateStore, k as OidcTokenSet, i as OidcStateEntry } from '../oidcState-
|
|
5
|
-
export { A as AuthorizationRequest, I as IntrospectParams, a as IntrospectionResult, L as LoginClientCredentialsParams, M as MIN_DISCOVERY_TTL_MS, b as MemoryOidcStateStore, O as OIDC_STATE_TTL_MS, c as OidcBeginParams, e as OidcClientOptions, f as OidcConfiguration, g as OidcExchangeParams, h as OidcRefreshParams, R as RevokeParams, m as SsoCompleteParams, n as SsoCompleteResult, o as SsoStartParams, p as SsoStartResult, q as createOidcClient } from '../oidcState-
|
|
2
|
+
import { a as AuthzLogger, E as ErrorBody, b as AxiamIdentity, V as VerifiableSession, A as AuthzVerifiableSession, c as ResourceSpec, R as RequireAccessOptions } from '../authzCore-CA9zFm1n.cjs';
|
|
3
|
+
export { d as AuthzChecker, C as CheckOutcome, e as ResourceParamRef, f as ResourceResolutionError, g as ResourceResolver, h as assertAuthzClient, i as authenticateRequest, j as evaluateAccess, k as fromParam, l as hasAnyRole, r as resolveResourceId } from '../authzCore-CA9zFm1n.cjs';
|
|
4
|
+
import { d as OidcClient, j as OidcStateStore, k as OidcTokenSet, i as OidcStateEntry } from '../oidcState-tHRAg5bu.cjs';
|
|
5
|
+
export { A as AuthorizationRequest, I as IntrospectParams, a as IntrospectionResult, L as LoginClientCredentialsParams, M as MIN_DISCOVERY_TTL_MS, b as MemoryOidcStateStore, O as OIDC_STATE_TTL_MS, c as OidcBeginParams, e as OidcClientOptions, f as OidcConfiguration, g as OidcExchangeParams, h as OidcRefreshParams, R as RevokeParams, m as SsoCompleteParams, n as SsoCompleteResult, o as SsoStartParams, p as SsoStartResult, q as createOidcClient } from '../oidcState-tHRAg5bu.cjs';
|
|
6
6
|
import { FastifyRequest, FastifyReply, FastifyPluginAsync } from 'fastify';
|
|
7
|
-
export { A as
|
|
7
|
+
export { A as AccessTokenExpectations, a as AxiamClaims, C as CLOCK_SKEW_LEEWAY_SEC, b as IdTokenClaims, V as Verifier, f as assertTenantClaim } from '../jwks-DEiwxlku.cjs';
|
|
8
8
|
import '../types-BVkMoqLU.cjs';
|
|
9
9
|
import '../sensitive-BC_s7Rrd.cjs';
|
|
10
|
-
import '../session-
|
|
10
|
+
import '../session-ti3C9ZJU.cjs';
|
|
11
11
|
import 'axios';
|
|
12
12
|
|
|
13
13
|
/** Configuration shared by the login and callback handlers (CONTRACT.md §12). */
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Request, RequestHandler } from 'express';
|
|
2
|
-
import { a as AuthzLogger, E as ErrorBody, b as AxiamIdentity, V as VerifiableSession, A as AuthzVerifiableSession, c as ResourceSpec, R as RequireAccessOptions } from '../authzCore-
|
|
3
|
-
export { d as AuthzChecker, C as CheckOutcome, e as ResourceParamRef, f as ResourceResolutionError, g as ResourceResolver, h as assertAuthzClient, i as authenticateRequest, j as evaluateAccess, k as fromParam, l as hasAnyRole, r as resolveResourceId } from '../authzCore-
|
|
4
|
-
import { d as OidcClient, j as OidcStateStore, k as OidcTokenSet, i as OidcStateEntry } from '../oidcState-
|
|
5
|
-
export { A as AuthorizationRequest, I as IntrospectParams, a as IntrospectionResult, L as LoginClientCredentialsParams, M as MIN_DISCOVERY_TTL_MS, b as MemoryOidcStateStore, O as OIDC_STATE_TTL_MS, c as OidcBeginParams, e as OidcClientOptions, f as OidcConfiguration, g as OidcExchangeParams, h as OidcRefreshParams, R as RevokeParams, m as SsoCompleteParams, n as SsoCompleteResult, o as SsoStartParams, p as SsoStartResult, q as createOidcClient } from '../oidcState-
|
|
2
|
+
import { a as AuthzLogger, E as ErrorBody, b as AxiamIdentity, V as VerifiableSession, A as AuthzVerifiableSession, c as ResourceSpec, R as RequireAccessOptions } from '../authzCore-a-wZL2P3.js';
|
|
3
|
+
export { d as AuthzChecker, C as CheckOutcome, e as ResourceParamRef, f as ResourceResolutionError, g as ResourceResolver, h as assertAuthzClient, i as authenticateRequest, j as evaluateAccess, k as fromParam, l as hasAnyRole, r as resolveResourceId } from '../authzCore-a-wZL2P3.js';
|
|
4
|
+
import { d as OidcClient, j as OidcStateStore, k as OidcTokenSet, i as OidcStateEntry } from '../oidcState-CfebuJ0n.js';
|
|
5
|
+
export { A as AuthorizationRequest, I as IntrospectParams, a as IntrospectionResult, L as LoginClientCredentialsParams, M as MIN_DISCOVERY_TTL_MS, b as MemoryOidcStateStore, O as OIDC_STATE_TTL_MS, c as OidcBeginParams, e as OidcClientOptions, f as OidcConfiguration, g as OidcExchangeParams, h as OidcRefreshParams, R as RevokeParams, m as SsoCompleteParams, n as SsoCompleteResult, o as SsoStartParams, p as SsoStartResult, q as createOidcClient } from '../oidcState-CfebuJ0n.js';
|
|
6
6
|
import { FastifyRequest, FastifyReply, FastifyPluginAsync } from 'fastify';
|
|
7
|
-
export { A as
|
|
7
|
+
export { A as AccessTokenExpectations, a as AxiamClaims, C as CLOCK_SKEW_LEEWAY_SEC, b as IdTokenClaims, V as Verifier, f as assertTenantClaim } from '../jwks-DEiwxlku.js';
|
|
8
8
|
import '../types-BVkMoqLU.js';
|
|
9
9
|
import '../sensitive-BC_s7Rrd.js';
|
|
10
|
-
import '../session-
|
|
10
|
+
import '../session-ti3C9ZJU.js';
|
|
11
11
|
import 'axios';
|
|
12
12
|
|
|
13
13
|
/** Configuration shared by the login and callback handlers (CONTRACT.md §12). */
|
package/dist/middleware/index.js
CHANGED
|
@@ -402,24 +402,190 @@ async function completeOidcLogin(options, query) {
|
|
|
402
402
|
}
|
|
403
403
|
};
|
|
404
404
|
}
|
|
405
|
+
var MAX_CLOCK_SKEW_SEC = 60;
|
|
406
|
+
function idTokenAuthError(reason, message) {
|
|
407
|
+
return new AuthError(`id_token validation failed (${reason}): ${message}`, reason);
|
|
408
|
+
}
|
|
409
|
+
function resolveClockSkewSec(clockSkewSec) {
|
|
410
|
+
if (clockSkewSec === void 0) {
|
|
411
|
+
return MAX_CLOCK_SKEW_SEC;
|
|
412
|
+
}
|
|
413
|
+
return Math.min(Math.max(clockSkewSec, 0), MAX_CLOCK_SKEW_SEC);
|
|
414
|
+
}
|
|
415
|
+
function constantTimeEquals(a, b) {
|
|
416
|
+
const left = Buffer.from(a, "utf8");
|
|
417
|
+
const right = Buffer.from(b, "utf8");
|
|
418
|
+
if (left.length !== right.length) {
|
|
419
|
+
return false;
|
|
420
|
+
}
|
|
421
|
+
return crypto.timingSafeEqual(left, right);
|
|
422
|
+
}
|
|
423
|
+
var ID_TOKEN_ALG = "EdDSA";
|
|
424
|
+
function assertIdTokenAlg(alg) {
|
|
425
|
+
if (alg !== ID_TOKEN_ALG) {
|
|
426
|
+
throw idTokenAuthError(
|
|
427
|
+
"invalid_alg",
|
|
428
|
+
`expected alg "${ID_TOKEN_ALG}", got ${typeof alg === "string" ? `"${alg}"` : "no alg header"}`
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
function checkIdTokenClaims(claims, expectations, nowSec = Math.floor(Date.now() / 1e3)) {
|
|
433
|
+
const skew = resolveClockSkewSec(expectations.clockSkewSec);
|
|
434
|
+
if (claims.iss !== expectations.issuer) {
|
|
435
|
+
throw idTokenAuthError("invalid_issuer", "iss does not equal the discovery document issuer");
|
|
436
|
+
}
|
|
437
|
+
const audiences = Array.isArray(claims.aud) ? claims.aud : [claims.aud];
|
|
438
|
+
if (!audiences.includes(expectations.clientId)) {
|
|
439
|
+
throw idTokenAuthError("invalid_audience", "aud does not contain this client_id");
|
|
440
|
+
}
|
|
441
|
+
if (audiences.length > 1 && claims.azp !== expectations.clientId) {
|
|
442
|
+
throw idTokenAuthError(
|
|
443
|
+
"invalid_audience",
|
|
444
|
+
"aud holds multiple audiences and azp is absent or does not equal this client_id"
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
if (typeof claims.exp !== "number") {
|
|
448
|
+
throw idTokenAuthError("token_expired", "exp claim is missing or not a number");
|
|
449
|
+
}
|
|
450
|
+
if (claims.exp + skew <= nowSec) {
|
|
451
|
+
throw idTokenAuthError("token_expired", "exp is in the past");
|
|
452
|
+
}
|
|
453
|
+
if (typeof claims.iat !== "number") {
|
|
454
|
+
throw idTokenAuthError("token_expired", "iat claim is missing or not a number");
|
|
455
|
+
}
|
|
456
|
+
if (claims.iat - skew > nowSec) {
|
|
457
|
+
throw idTokenAuthError("token_expired", "iat is in the future");
|
|
458
|
+
}
|
|
459
|
+
if (typeof claims.nbf === "number" && claims.nbf - skew > nowSec) {
|
|
460
|
+
throw idTokenAuthError("token_expired", "nbf is in the future");
|
|
461
|
+
}
|
|
462
|
+
if (expectations.nonce !== void 0) {
|
|
463
|
+
if (typeof claims.nonce !== "string" || !constantTimeEquals(claims.nonce, expectations.nonce)) {
|
|
464
|
+
throw idTokenAuthError("nonce_mismatch", "nonce claim is absent or does not match the request nonce");
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
return claims;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// src/node/jwks.ts
|
|
471
|
+
var COOLDOWN_DURATION_MS = 6e4;
|
|
472
|
+
var TIMEOUT_DURATION_MS = 5e3;
|
|
473
|
+
var CLOCK_SKEW_LEEWAY_SEC = 60;
|
|
474
|
+
function createJwksVerifier(jwksUri) {
|
|
475
|
+
let jwksPromise = null;
|
|
476
|
+
async function getJwks() {
|
|
477
|
+
if (!jwksPromise) {
|
|
478
|
+
jwksPromise = import('jose').then(
|
|
479
|
+
({ createRemoteJWKSet }) => createRemoteJWKSet(new URL(jwksUri), {
|
|
480
|
+
cooldownDuration: COOLDOWN_DURATION_MS,
|
|
481
|
+
timeoutDuration: TIMEOUT_DURATION_MS
|
|
482
|
+
})
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
return jwksPromise;
|
|
486
|
+
}
|
|
487
|
+
return {
|
|
488
|
+
async verifyAccessToken(token, expectations) {
|
|
489
|
+
const { jwtVerify } = await import('jose');
|
|
490
|
+
const jwks = await getJwks();
|
|
491
|
+
const { payload } = await jwtVerify(token, jwks, {
|
|
492
|
+
algorithms: ["EdDSA"],
|
|
493
|
+
requiredClaims: ["exp"],
|
|
494
|
+
clockTolerance: CLOCK_SKEW_LEEWAY_SEC,
|
|
495
|
+
...expectations.expectedIssuer !== void 0 ? { issuer: expectations.expectedIssuer } : {},
|
|
496
|
+
...expectations.expectedAudience !== void 0 ? { audience: expectations.expectedAudience } : {}
|
|
497
|
+
});
|
|
498
|
+
assertTenantClaim(payload.tenant_id, expectations.expectedTenantId);
|
|
499
|
+
return payload;
|
|
500
|
+
},
|
|
501
|
+
async verifySignatureOnlyUnchecked(token) {
|
|
502
|
+
const { jwtVerify } = await import('jose');
|
|
503
|
+
const jwks = await getJwks();
|
|
504
|
+
const { payload } = await jwtVerify(token, jwks, { algorithms: ["EdDSA"] });
|
|
505
|
+
return payload;
|
|
506
|
+
},
|
|
507
|
+
async verifyIdToken(token, expectations) {
|
|
508
|
+
const { decodeProtectedHeader, jwtVerify } = await import('jose');
|
|
509
|
+
let header;
|
|
510
|
+
try {
|
|
511
|
+
header = decodeProtectedHeader(token);
|
|
512
|
+
} catch {
|
|
513
|
+
throw idTokenAuthError("invalid_alg", "token is not a well-formed JWS");
|
|
514
|
+
}
|
|
515
|
+
assertIdTokenAlg(header.alg);
|
|
516
|
+
if (!header.kid) {
|
|
517
|
+
throw idTokenAuthError("unknown_kid", "token carries no kid header");
|
|
518
|
+
}
|
|
519
|
+
const jwks = await getJwks();
|
|
520
|
+
const skew = resolveClockSkewSec(expectations.clockSkewSec);
|
|
521
|
+
let claims;
|
|
522
|
+
try {
|
|
523
|
+
const { payload } = await jwtVerify(token, jwks, {
|
|
524
|
+
algorithms: [ID_TOKEN_ALG],
|
|
525
|
+
clockTolerance: skew
|
|
526
|
+
});
|
|
527
|
+
claims = payload;
|
|
528
|
+
} catch (err) {
|
|
529
|
+
throw mapJoseVerifyError(err);
|
|
530
|
+
}
|
|
531
|
+
return checkIdTokenClaims(claims, { ...expectations, clockSkewSec: skew });
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
function assertTenantClaim(claim, expected) {
|
|
536
|
+
if (typeof expected !== "string" || expected === "") {
|
|
537
|
+
throw new AuthError(
|
|
538
|
+
"no configured tenant to verify the token against; refusing the request (CONTRACT.md \xA710.1 rule 4 fails closed)"
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
if (typeof claim !== "string" || claim === "") {
|
|
542
|
+
throw new AuthError("invalid tenant_id claim");
|
|
543
|
+
}
|
|
544
|
+
if (claim !== expected) {
|
|
545
|
+
warnOnceIfComparandLooksLikeASlug(claim, expected);
|
|
546
|
+
throw new AuthError("token tenant_id does not match configured tenant");
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
550
|
+
var slugComparandWarned = false;
|
|
551
|
+
function warnOnceIfComparandLooksLikeASlug(claim, expected) {
|
|
552
|
+
if (slugComparandWarned) return;
|
|
553
|
+
if (!UUID_RE.test(claim) || UUID_RE.test(expected)) return;
|
|
554
|
+
slugComparandWarned = true;
|
|
555
|
+
console.warn(
|
|
556
|
+
`AXIAM: the tenant this guard was configured with ("${expected}") is not a UUID, but access tokens carry the tenant UUID in their \`tenant_id\` claim, so this guard will reject every request. Configure it with the tenant UUID, not the slug. (CONTRACT.md \xA710.1 rule 4; logged once per process, and it does not affect the rejection itself.)`
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
function mapJoseVerifyError(err) {
|
|
560
|
+
const code = typeof err === "object" && err !== null && "code" in err ? String(err.code) : "";
|
|
561
|
+
switch (code) {
|
|
562
|
+
case "ERR_JWKS_NO_MATCHING_KEY":
|
|
563
|
+
return idTokenAuthError("unknown_kid", "no JWKS key matches the token kid");
|
|
564
|
+
case "ERR_JWT_EXPIRED":
|
|
565
|
+
return idTokenAuthError("token_expired", "exp is in the past");
|
|
566
|
+
case "ERR_JWT_CLAIM_VALIDATION_FAILED":
|
|
567
|
+
return idTokenAuthError("token_expired", "a time-based claim (nbf/iat) is not yet valid");
|
|
568
|
+
default:
|
|
569
|
+
return idTokenAuthError("invalid_signature", "signature verification failed");
|
|
570
|
+
}
|
|
571
|
+
}
|
|
405
572
|
|
|
406
573
|
// src/middleware/verifyCore.ts
|
|
407
574
|
async function authenticateRequest(session, token) {
|
|
408
575
|
let claims;
|
|
409
576
|
try {
|
|
410
|
-
claims = await session.jwksVerifier.verifyAccessToken(token
|
|
577
|
+
claims = await session.jwksVerifier.verifyAccessToken(token, {
|
|
578
|
+
expectedTenantId: session.tenantHeaderValue,
|
|
579
|
+
expectedIssuer: session.expectedIssuer,
|
|
580
|
+
expectedAudience: session.expectedAudience
|
|
581
|
+
});
|
|
411
582
|
} catch (err) {
|
|
412
583
|
throw new AuthError(err instanceof Error ? err.message : "invalid or expired token");
|
|
413
584
|
}
|
|
414
585
|
if (!claims.sub) {
|
|
415
586
|
throw new AuthError("invalid sub claim");
|
|
416
587
|
}
|
|
417
|
-
|
|
418
|
-
throw new AuthError("invalid tenant_id claim");
|
|
419
|
-
}
|
|
420
|
-
if (claims.tenant_id !== session.tenantHeaderValue) {
|
|
421
|
-
throw new AuthError("token tenant_id does not match configured tenant");
|
|
422
|
-
}
|
|
588
|
+
assertTenantClaim(claims.tenant_id, session.tenantHeaderValue);
|
|
423
589
|
const roles = (claims.scope ?? "").split(" ").filter(Boolean);
|
|
424
590
|
return {
|
|
425
591
|
userId: claims.sub,
|
|
@@ -688,135 +854,6 @@ var oidcLoginPlugin = (options) => {
|
|
|
688
854
|
plugin[/* @__PURE__ */ Symbol.for("fastify.display-name")] = "axiam-oidc-login";
|
|
689
855
|
return plugin;
|
|
690
856
|
};
|
|
691
|
-
var MAX_CLOCK_SKEW_SEC = 60;
|
|
692
|
-
function idTokenAuthError(reason, message) {
|
|
693
|
-
return new AuthError(`id_token validation failed (${reason}): ${message}`, reason);
|
|
694
|
-
}
|
|
695
|
-
function resolveClockSkewSec(clockSkewSec) {
|
|
696
|
-
if (clockSkewSec === void 0) {
|
|
697
|
-
return MAX_CLOCK_SKEW_SEC;
|
|
698
|
-
}
|
|
699
|
-
return Math.min(Math.max(clockSkewSec, 0), MAX_CLOCK_SKEW_SEC);
|
|
700
|
-
}
|
|
701
|
-
function constantTimeEquals(a, b) {
|
|
702
|
-
const left = Buffer.from(a, "utf8");
|
|
703
|
-
const right = Buffer.from(b, "utf8");
|
|
704
|
-
if (left.length !== right.length) {
|
|
705
|
-
return false;
|
|
706
|
-
}
|
|
707
|
-
return crypto.timingSafeEqual(left, right);
|
|
708
|
-
}
|
|
709
|
-
var ID_TOKEN_ALG = "EdDSA";
|
|
710
|
-
function assertIdTokenAlg(alg) {
|
|
711
|
-
if (alg !== ID_TOKEN_ALG) {
|
|
712
|
-
throw idTokenAuthError(
|
|
713
|
-
"invalid_alg",
|
|
714
|
-
`expected alg "${ID_TOKEN_ALG}", got ${typeof alg === "string" ? `"${alg}"` : "no alg header"}`
|
|
715
|
-
);
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
function checkIdTokenClaims(claims, expectations, nowSec = Math.floor(Date.now() / 1e3)) {
|
|
719
|
-
const skew = resolveClockSkewSec(expectations.clockSkewSec);
|
|
720
|
-
if (claims.iss !== expectations.issuer) {
|
|
721
|
-
throw idTokenAuthError("invalid_issuer", "iss does not equal the discovery document issuer");
|
|
722
|
-
}
|
|
723
|
-
const audiences = Array.isArray(claims.aud) ? claims.aud : [claims.aud];
|
|
724
|
-
if (!audiences.includes(expectations.clientId)) {
|
|
725
|
-
throw idTokenAuthError("invalid_audience", "aud does not contain this client_id");
|
|
726
|
-
}
|
|
727
|
-
if (audiences.length > 1 && claims.azp !== expectations.clientId) {
|
|
728
|
-
throw idTokenAuthError(
|
|
729
|
-
"invalid_audience",
|
|
730
|
-
"aud holds multiple audiences and azp is absent or does not equal this client_id"
|
|
731
|
-
);
|
|
732
|
-
}
|
|
733
|
-
if (typeof claims.exp !== "number") {
|
|
734
|
-
throw idTokenAuthError("token_expired", "exp claim is missing or not a number");
|
|
735
|
-
}
|
|
736
|
-
if (claims.exp + skew <= nowSec) {
|
|
737
|
-
throw idTokenAuthError("token_expired", "exp is in the past");
|
|
738
|
-
}
|
|
739
|
-
if (typeof claims.iat !== "number") {
|
|
740
|
-
throw idTokenAuthError("token_expired", "iat claim is missing or not a number");
|
|
741
|
-
}
|
|
742
|
-
if (claims.iat - skew > nowSec) {
|
|
743
|
-
throw idTokenAuthError("token_expired", "iat is in the future");
|
|
744
|
-
}
|
|
745
|
-
if (typeof claims.nbf === "number" && claims.nbf - skew > nowSec) {
|
|
746
|
-
throw idTokenAuthError("token_expired", "nbf is in the future");
|
|
747
|
-
}
|
|
748
|
-
if (expectations.nonce !== void 0) {
|
|
749
|
-
if (typeof claims.nonce !== "string" || !constantTimeEquals(claims.nonce, expectations.nonce)) {
|
|
750
|
-
throw idTokenAuthError("nonce_mismatch", "nonce claim is absent or does not match the request nonce");
|
|
751
|
-
}
|
|
752
|
-
}
|
|
753
|
-
return claims;
|
|
754
|
-
}
|
|
755
|
-
|
|
756
|
-
// src/node/jwks.ts
|
|
757
|
-
var COOLDOWN_DURATION_MS = 6e4;
|
|
758
|
-
var TIMEOUT_DURATION_MS = 5e3;
|
|
759
|
-
function createJwksVerifier(jwksUri) {
|
|
760
|
-
let jwksPromise = null;
|
|
761
|
-
async function getJwks() {
|
|
762
|
-
if (!jwksPromise) {
|
|
763
|
-
jwksPromise = import('jose').then(
|
|
764
|
-
({ createRemoteJWKSet }) => createRemoteJWKSet(new URL(jwksUri), {
|
|
765
|
-
cooldownDuration: COOLDOWN_DURATION_MS,
|
|
766
|
-
timeoutDuration: TIMEOUT_DURATION_MS
|
|
767
|
-
})
|
|
768
|
-
);
|
|
769
|
-
}
|
|
770
|
-
return jwksPromise;
|
|
771
|
-
}
|
|
772
|
-
return {
|
|
773
|
-
async verifyAccessToken(token) {
|
|
774
|
-
const { jwtVerify } = await import('jose');
|
|
775
|
-
const jwks = await getJwks();
|
|
776
|
-
const { payload } = await jwtVerify(token, jwks, { algorithms: ["EdDSA"] });
|
|
777
|
-
return payload;
|
|
778
|
-
},
|
|
779
|
-
async verifyIdToken(token, expectations) {
|
|
780
|
-
const { decodeProtectedHeader, jwtVerify } = await import('jose');
|
|
781
|
-
let header;
|
|
782
|
-
try {
|
|
783
|
-
header = decodeProtectedHeader(token);
|
|
784
|
-
} catch {
|
|
785
|
-
throw idTokenAuthError("invalid_alg", "token is not a well-formed JWS");
|
|
786
|
-
}
|
|
787
|
-
assertIdTokenAlg(header.alg);
|
|
788
|
-
if (!header.kid) {
|
|
789
|
-
throw idTokenAuthError("unknown_kid", "token carries no kid header");
|
|
790
|
-
}
|
|
791
|
-
const jwks = await getJwks();
|
|
792
|
-
const skew = resolveClockSkewSec(expectations.clockSkewSec);
|
|
793
|
-
let claims;
|
|
794
|
-
try {
|
|
795
|
-
const { payload } = await jwtVerify(token, jwks, {
|
|
796
|
-
algorithms: [ID_TOKEN_ALG],
|
|
797
|
-
clockTolerance: skew
|
|
798
|
-
});
|
|
799
|
-
claims = payload;
|
|
800
|
-
} catch (err) {
|
|
801
|
-
throw mapJoseVerifyError(err);
|
|
802
|
-
}
|
|
803
|
-
return checkIdTokenClaims(claims, { ...expectations, clockSkewSec: skew });
|
|
804
|
-
}
|
|
805
|
-
};
|
|
806
|
-
}
|
|
807
|
-
function mapJoseVerifyError(err) {
|
|
808
|
-
const code = typeof err === "object" && err !== null && "code" in err ? String(err.code) : "";
|
|
809
|
-
switch (code) {
|
|
810
|
-
case "ERR_JWKS_NO_MATCHING_KEY":
|
|
811
|
-
return idTokenAuthError("unknown_kid", "no JWKS key matches the token kid");
|
|
812
|
-
case "ERR_JWT_EXPIRED":
|
|
813
|
-
return idTokenAuthError("token_expired", "exp is in the past");
|
|
814
|
-
case "ERR_JWT_CLAIM_VALIDATION_FAILED":
|
|
815
|
-
return idTokenAuthError("token_expired", "a time-based claim (nbf/iat) is not yet valid");
|
|
816
|
-
default:
|
|
817
|
-
return idTokenAuthError("invalid_signature", "signature verification failed");
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
857
|
var CODE_CHALLENGE_METHOD_S256 = "S256";
|
|
821
858
|
var CSPRNG_BYTES = 32;
|
|
822
859
|
function randomUrlSafeToken(bytes = CSPRNG_BYTES) {
|
|
@@ -845,7 +882,7 @@ var RESERVED_AUTHORIZE_PARAMS = /* @__PURE__ */ new Set([
|
|
|
845
882
|
"code_challenge",
|
|
846
883
|
"code_challenge_method"
|
|
847
884
|
]);
|
|
848
|
-
var
|
|
885
|
+
var UUID_RE2 = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
849
886
|
var DiscoveryCache = class {
|
|
850
887
|
#documents = /* @__PURE__ */ new Map();
|
|
851
888
|
#inFlight = /* @__PURE__ */ new Map();
|
|
@@ -1348,7 +1385,7 @@ var OidcClient = class {
|
|
|
1348
1385
|
"this operation requires a tenant_id UUID for the /oauth2 query parameter: pass tenantId explicitly, or construct the client with the tenantId (UUID) form (CONTRACT.md \xA712.3 rule 4)."
|
|
1349
1386
|
);
|
|
1350
1387
|
}
|
|
1351
|
-
if (!
|
|
1388
|
+
if (!UUID_RE2.test(candidate)) {
|
|
1352
1389
|
throw new AuthError(
|
|
1353
1390
|
"tenant_id must be a UUID for the /oauth2 query parameter; a tenant slug cannot be substituted (CONTRACT.md \xA712.3 rule 4)."
|
|
1354
1391
|
);
|
|
@@ -1497,6 +1534,7 @@ var MemoryOidcStateStore = class {
|
|
|
1497
1534
|
};
|
|
1498
1535
|
|
|
1499
1536
|
exports.ACCESS_COOKIE_NAME = ACCESS_COOKIE_NAME;
|
|
1537
|
+
exports.CLOCK_SKEW_LEEWAY_SEC = CLOCK_SKEW_LEEWAY_SEC;
|
|
1500
1538
|
exports.CSRF_COOKIE_NAME = CSRF_COOKIE_NAME;
|
|
1501
1539
|
exports.CSRF_HEADER_NAME = CSRF_HEADER_NAME;
|
|
1502
1540
|
exports.MIN_DISCOVERY_TTL_MS = MIN_DISCOVERY_TTL_MS;
|
|
@@ -1505,6 +1543,7 @@ exports.OIDC_STATE_TTL_MS = OIDC_STATE_TTL_MS;
|
|
|
1505
1543
|
exports.OidcClient = OidcClient;
|
|
1506
1544
|
exports.ResourceResolutionError = ResourceResolutionError;
|
|
1507
1545
|
exports.assertAuthzClient = assertAuthzClient;
|
|
1546
|
+
exports.assertTenantClaim = assertTenantClaim;
|
|
1508
1547
|
exports.authenticateRequest = authenticateRequest;
|
|
1509
1548
|
exports.axiamMiddleware = axiamMiddleware;
|
|
1510
1549
|
exports.axiamPlugin = axiamPlugin;
|