@bernierllc/nevar-auth 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -12
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +2 -2
- package/dist/index.d.ts +24 -3
- package/dist/index.js +139 -23
- package/dist/testing/index.d.ts +22 -5
- package/dist/testing/index.js +34 -16
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,17 +6,20 @@ Verifies nevar.io credentials (JWTs and typed `api_`/`mcp_` keys) and performs a
|
|
|
6
6
|
npm install @bernierllc/nevar-auth
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
**Contract:** rev
|
|
9
|
+
**Contract:** `ifc:platform/authentication` (`#ActorContext` rev 1), with the package-conformance decisions Q6, Q10, Q11, Q12, Q15 and Q16 implemented. Production issuer `https://auth.nevar.io`, staging issuer `https://auth.staging.nevar.io`.
|
|
10
10
|
|
|
11
11
|
## Rules this package enforces
|
|
12
12
|
|
|
13
|
-
- **The JWKS comes from OIDC discovery.** Only asymmetric `alg`s are accepted, and every token must carry a `kid
|
|
13
|
+
- **The JWKS comes from OIDC discovery.** Only asymmetric `alg`s are accepted, and every token must carry a `kid` Discovery and the JWKS are reused for about an hour, then refetched, so issuer key rotation needs no restart.
|
|
14
14
|
- **Every token has exactly one audience**, and the caller names the audience it expects. A token whose `aud` lists several audiences is refused.
|
|
15
|
-
- **
|
|
16
|
-
- **
|
|
15
|
+
- **JWTs must be RFC 9068 access tokens.** The header `typ` must be `at+jwt`. An ID token (`typ: JWT`) or a token with no `typ` is refused with `invalid_token`, even when it names a resource audience.
|
|
16
|
+
- **Introspection uses the endpoint discovery names.** The verifier posts to discovery's `introspection_endpoint`, and falls back to `<issuer>/oauth/introspect` only when discovery omits it. An endpoint that is present but not an absolute URL fails discovery; it never falls back. Like the token endpoint, it must be on the issuer's own origin, over https outside `mode: 'test'`, because it carries the client secret.
|
|
17
|
+
- **Typed keys are opaque, and only work on configured audiences.** `api_` and `mcp_` keys are checked only through RFC 7662 introspection. The surface of an audience comes from `resourceAudiences`, a set of exact audience URLs per surface, never from the host name: an `api_` key works only on an audience in `resourceAudiences.api`, an `mcp_` key only on one in `resourceAudiences.mcp`, and any other audience refuses both with `wrong_surface`. Omitting `resourceAudiences` is deprecated since 0.3.0 (removed in 1.0.0): the verifier falls back to the 0.2 first-DNS-label rule and prints a one-time warning.
|
|
18
|
+
- **JWTs are introspected too, after local verification.** A revoked grant is therefore denied on the very next call. `scope` and `nevar_grant_id` in the returned `ActorContext` come from the introspection response, not from the JWT; a response with no `scope` is refused. The response must also say `role: "authenticated"`: any other role (such as `service_role`), or none, is refused with `invalid_token`.
|
|
17
19
|
- **Identity comes only from the verified credential.** `verifyCredential` takes no actor header. A service acting for a user obtains a delegated token through RFC 8693 token exchange (`delegateActor`), and that token carries an `act` claim.
|
|
20
|
+
- **Delegation only talks to the issuer and returns only a verified token.** `delegateActor` requires a `token_endpoint` in discovery on the issuer's own origin, over https (plain http only in `mode: 'test'`). It authenticates with HTTP Basic using `introspectionClient`, and the actor (service) token's `aud` must be the issuer. The returned token is verified for the requested audience, including introspection, before `delegateActor` returns it; if that check cannot complete, the call fails.
|
|
18
21
|
- **When the issuer cannot be reached, the call fails.** You get `dependency_unavailable` with `retryable: true`. The verifier never falls back to cached or local keys.
|
|
19
|
-
- **Logs carry the grant ref, never the token.**
|
|
22
|
+
- **Logs carry the grant ref, never the token.** The grant ref is capped at 64 characters, since it may be read from a token that failed verification.
|
|
20
23
|
|
|
21
24
|
## Usage
|
|
22
25
|
|
|
@@ -26,6 +29,12 @@ import { createNevarAuth, NEVAR_ISSUERS, NevarAuthError } from '@bernierllc/neva
|
|
|
26
29
|
const auth = createNevarAuth({
|
|
27
30
|
issuer: NEVAR_ISSUERS.production,
|
|
28
31
|
introspectionClient: { clientId: process.env.NEVAR_AUTH_CLIENT_ID!, clientSecret: process.env.NEVAR_AUTH_CLIENT_SECRET! },
|
|
32
|
+
// Exact audience URLs per surface; an audience may be in only one set. Required from 1.0.0;
|
|
33
|
+
// omitting it is deprecated since 0.3.0 (see Migrating from 0.2).
|
|
34
|
+
resourceAudiences: {
|
|
35
|
+
api: ['https://api.nevar.io/v1/plan'],
|
|
36
|
+
mcp: ['https://mcp.nevar.io/v1/plan'],
|
|
37
|
+
},
|
|
29
38
|
logger, // optional, { warn(message, context) }; denials are logged with code + grantId
|
|
30
39
|
});
|
|
31
40
|
|
|
@@ -62,32 +71,47 @@ Each of these is a `NevarAuthError` with a `code`, a `retryable` flag and a `gra
|
|
|
62
71
|
| `wrong_surface` | The key's prefix does not match the audience's surface. |
|
|
63
72
|
| `expired` | The credential is past its `exp`. |
|
|
64
73
|
| `revoked` | Introspection reports the credential inactive. This covers both revoked and unknown credentials. |
|
|
65
|
-
| `invalid_delegation` | The token exchange was refused,
|
|
74
|
+
| `invalid_delegation` | The token exchange was refused, the token's `act` claim is malformed, or the exchanged token does not verify for the requested audience. |
|
|
66
75
|
| `dependency_unavailable` | Discovery, the JWKS, introspection or the token endpoint failed: unreachable, a 5xx or 429, a non-JSON body, or an unusable discovery document. This is the only **retryable** code. |
|
|
67
|
-
| `misconfigured` | Our setup is wrong. One of: the caller named no audience; `mode` is not exactly `'production'` or `'test'`, which `createNevarAuth` throws at construction; `mode: 'test'` points at a real nevar.io issuer; the issuer URL ends in `/`; the discovery `issuer` does not match; introspection refused our client credentials. |
|
|
76
|
+
| `misconfigured` | Our setup is wrong. One of: the caller named no audience; `mode` is not exactly `'production'` or `'test'`, which `createNevarAuth` throws at construction; `mode: 'test'` points at a real nevar.io issuer; the issuer URL ends in `/`; the discovery `issuer` does not match; introspection or the token endpoint refused our client credentials; `resourceAudiences` is not two arrays of non-empty strings, or lists one audience under both `api` and `mcp` (thrown at construction); discovery names no `token_endpoint`, or the token or introspection endpoint is on another origin than the issuer, or is non-https outside test mode. |
|
|
77
|
+
|
|
78
|
+
## Migrating from 0.2
|
|
79
|
+
|
|
80
|
+
- Pass `resourceAudiences` to `createNevarAuth`, listing every audience your service accepts typed keys on. In 0.3 a config without it still works on the 0.2 first-DNS-label rule and prints a one-time deprecation warning; from 1.0.0 construction throws `misconfigured` without it.
|
|
81
|
+
- `audienceSurface` is deprecated since 0.3.0 and removed in 1.0.0. It still returns the 0.2 first-label surface and warns once; the surface now comes from `resourceAudiences`.
|
|
82
|
+
- With `resourceAudiences` set (and always from 1.0.0), JWTs must carry `typ: at+jwt`, and introspection must return `scope` and `nevar_grant_id`: the verifier takes both from introspection, not the JWT. Under the deprecated 0.2 config an untyped JWT, or an introspection response without those fields, still works (the JWT's claims are used) and prints a one-time warning.
|
|
83
|
+
- Issuers must mint access tokens with `typ: at+jwt`, give service tokens `aud` = the issuer, and advertise a same-origin `token_endpoint` for delegation and a same-origin `introspection_endpoint` (or none).
|
|
68
84
|
|
|
69
85
|
## Testing: `@bernierllc/nevar-auth/testing`
|
|
70
86
|
|
|
71
87
|
```ts
|
|
72
88
|
import { createNevarAuth } from '@bernierllc/nevar-auth';
|
|
73
|
-
import { startTestIssuer, TEST_AUDIENCES } from '@bernierllc/nevar-auth/testing';
|
|
89
|
+
import { startTestIssuer, TEST_AUDIENCES, TEST_RESOURCE_AUDIENCES } from '@bernierllc/nevar-auth/testing';
|
|
74
90
|
|
|
75
91
|
const issuer = await startTestIssuer(); // a real HTTP server on 127.0.0.1
|
|
76
|
-
const auth = createNevarAuth({
|
|
92
|
+
const auth = createNevarAuth({
|
|
93
|
+
issuer: issuer.url,
|
|
94
|
+
introspectionClient: issuer.introspectionClient,
|
|
95
|
+
resourceAudiences: TEST_RESOURCE_AUDIENCES,
|
|
96
|
+
mode: 'test',
|
|
97
|
+
});
|
|
77
98
|
|
|
78
99
|
await auth.verifyCredential(issuer.tokens.valid, { audience: TEST_AUDIENCES.api });
|
|
79
100
|
await issuer.close();
|
|
80
101
|
```
|
|
81
102
|
|
|
82
103
|
**What the test issuer serves:**
|
|
83
|
-
- Discovery, the JWKS, introspection
|
|
104
|
+
- Discovery, the JWKS, introspection and token exchange. Introspection and token exchange both require Basic client credentials, and token exchange requires the actor token's `aud` to be the issuer.
|
|
105
|
+
- `startTestIssuer({ introspectionPath, advertiseIntrospection })` moves the introspection route (default `/oauth/introspect`) or leaves `introspection_endpoint` out of discovery.
|
|
84
106
|
|
|
85
107
|
**Sample credentials:**
|
|
86
|
-
- `issuer.tokens`: `valid`, `expired`, `wrongAudience`, `wrongIssuer`, `revoked`, `agent` and `service
|
|
108
|
+
- `issuer.tokens`: `valid`, `expired`, `wrongAudience`, `wrongIssuer`, `revoked`, `agent` and `service` (whose `aud` is the issuer).
|
|
109
|
+
- `TEST_RESOURCE_AUDIENCES`: the `resourceAudiences` set matching `TEST_AUDIENCES`.
|
|
87
110
|
- `TEST_KEYS`: one `api_` key and one `mcp_` key.
|
|
88
111
|
|
|
89
112
|
**Controls:**
|
|
90
|
-
- `issuer.sign(claims, { expiresIn, issuer, kid })` mints a custom token.
|
|
113
|
+
- `issuer.sign(claims, { expiresIn, issuer, kid, typ })` mints a custom token. `typ` defaults to `at+jwt`; `null` omits it.
|
|
114
|
+
- `issuer.overrideIntrospection(grantId, fields)` changes what introspection reports for a grant; a field set to `undefined` is removed.
|
|
91
115
|
- `issuer.revoke(grantId)` revokes a grant.
|
|
92
116
|
- `issuer.setUnavailable(endpoint)` makes an endpoint return 503, to simulate an outage.
|
|
93
117
|
|
package/dist/errors.d.ts
CHANGED
|
@@ -29,7 +29,7 @@ export type NevarAuthErrorCode =
|
|
|
29
29
|
export declare class NevarAuthError extends Error {
|
|
30
30
|
readonly code: NevarAuthErrorCode;
|
|
31
31
|
readonly retryable: boolean;
|
|
32
|
-
/** `nevar_grant_id` of the refused credential, when it could be read. Safe to log. */
|
|
32
|
+
/** `nevar_grant_id` of the refused credential, when it could be read, capped at 64 characters. Safe to log. */
|
|
33
33
|
readonly grantId: string | null;
|
|
34
34
|
constructor(code: NevarAuthErrorCode, message: string, options?: {
|
|
35
35
|
cause?: unknown;
|
package/dist/errors.js
CHANGED
|
@@ -9,13 +9,13 @@ Redistribution or use in other products or commercial offerings is not permitted
|
|
|
9
9
|
export class NevarAuthError extends Error {
|
|
10
10
|
code;
|
|
11
11
|
retryable;
|
|
12
|
-
/** `nevar_grant_id` of the refused credential, when it could be read. Safe to log. */
|
|
12
|
+
/** `nevar_grant_id` of the refused credential, when it could be read, capped at 64 characters. Safe to log. */
|
|
13
13
|
grantId;
|
|
14
14
|
constructor(code, message, options = {}) {
|
|
15
15
|
super(message, { cause: options.cause });
|
|
16
16
|
this.name = 'NevarAuthError';
|
|
17
17
|
this.code = code;
|
|
18
18
|
this.retryable = code === 'dependency_unavailable';
|
|
19
|
-
this.grantId = options.grantId ?? null;
|
|
19
|
+
this.grantId = options.grantId?.slice(0, 64) ?? null; // Q13: an unverified grant ref is capped at 64 characters
|
|
20
20
|
}
|
|
21
21
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ export declare const NEVAR_ISSUERS: {
|
|
|
4
4
|
readonly production: "https://auth.nevar.io";
|
|
5
5
|
readonly staging: "https://auth.staging.nevar.io";
|
|
6
6
|
};
|
|
7
|
-
/** `kid` of the fixture signing key in `@bernierllc/nevar-auth/testing`. Refused in production mode. */
|
|
8
7
|
export declare const TEST_KID = "test-only-not-production";
|
|
9
8
|
declare const ACTOR_TYPES: readonly ["user", "agent", "service"];
|
|
10
9
|
export type ActorType = (typeof ACTOR_TYPES)[number];
|
|
@@ -34,14 +33,32 @@ export interface ActorContext {
|
|
|
34
33
|
export interface NevarAuthLogger {
|
|
35
34
|
warn(message: string, context?: Record<string, unknown>): void;
|
|
36
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* The exact resource audiences typed keys may be used on (Q15). An `api_` key is accepted only when the expected
|
|
38
|
+
* audience is in `api`, an `mcp_` key only when it is in `mcp`. Every other audience refuses typed keys. The surface
|
|
39
|
+
* is never inferred from the hostname.
|
|
40
|
+
*/
|
|
41
|
+
export interface ResourceAudiences {
|
|
42
|
+
api: readonly string[];
|
|
43
|
+
mcp: readonly string[];
|
|
44
|
+
}
|
|
37
45
|
export interface NevarAuthConfig {
|
|
38
46
|
/** `NEVAR_ISSUERS.production`, `NEVAR_ISSUERS.staging`, or a test issuer's `url`. */
|
|
39
47
|
issuer: string;
|
|
40
|
-
/**
|
|
48
|
+
/**
|
|
49
|
+
* This service's client credentials, sent as HTTP Basic to RFC 7662 introspection and to the RFC 8693 token
|
|
50
|
+
* endpoint (Q3, Q6).
|
|
51
|
+
*/
|
|
41
52
|
introspectionClient: {
|
|
42
53
|
clientId: string;
|
|
43
54
|
clientSecret: string;
|
|
44
55
|
};
|
|
56
|
+
/**
|
|
57
|
+
* Exact API and MCP audience sets (Q15). The two sets must not share an audience.
|
|
58
|
+
* Omitting it is deprecated since 0.3.0 and required from 1.0.0: without it the surface falls back to the 0.2
|
|
59
|
+
* first-DNS-label rule (`api.…` / `mcp.…`) and a one-time warning is printed.
|
|
60
|
+
*/
|
|
61
|
+
resourceAudiences?: ResourceAudiences;
|
|
45
62
|
/** Default `production`, which refuses `kid` `test-only-not-production`. */
|
|
46
63
|
mode?: 'production' | 'test';
|
|
47
64
|
/** Receives one line per refused credential: code, grant ref, audience, correlation id. Never the token. */
|
|
@@ -76,5 +93,9 @@ export interface NevarAuth {
|
|
|
76
93
|
delegateActor(options: DelegateOptions): Promise<DelegatedToken>;
|
|
77
94
|
}
|
|
78
95
|
export declare function createNevarAuth(config: NevarAuthConfig): NevarAuth;
|
|
79
|
-
/**
|
|
96
|
+
/**
|
|
97
|
+
* `api` for `https://api.…`, `mcp` for `https://mcp.…`, otherwise `null`.
|
|
98
|
+
* @deprecated since 0.3.0; removed in 1.0.0; use `resourceAudiences` in `createNevarAuth`, which decides the surface
|
|
99
|
+
* from exact audience sets instead of the host name.
|
|
100
|
+
*/
|
|
80
101
|
export declare function audienceSurface(audience: string): 'api' | 'mcp' | null;
|
package/dist/index.js
CHANGED
|
@@ -14,6 +14,8 @@ export const NEVAR_ISSUERS = {
|
|
|
14
14
|
staging: 'https://auth.staging.nevar.io',
|
|
15
15
|
};
|
|
16
16
|
/** `kid` of the fixture signing key in `@bernierllc/nevar-auth/testing`. Refused in production mode. */
|
|
17
|
+
/** Q14: how long discovery metadata and its JWKS are reused before refetching. */
|
|
18
|
+
const DISCOVERY_TTL_MS = 60 * 60 * 1000;
|
|
17
19
|
export const TEST_KID = 'test-only-not-production';
|
|
18
20
|
/** Asymmetric algorithms only: a shared-secret `alg` is refused before any key lookup. */
|
|
19
21
|
const ASYMMETRIC_ALGORITHMS = ['RS256', 'RS384', 'RS512', 'PS256', 'PS384', 'PS512', 'ES256', 'ES384', 'ES512', 'EdDSA'];
|
|
@@ -33,11 +35,17 @@ export function createNevarAuth(config) {
|
|
|
33
35
|
if (mode === 'test' && Object.values(NEVAR_ISSUERS).includes(issuer)) {
|
|
34
36
|
throw new NevarAuthError('misconfigured', `mode 'test' is refused for the real issuer ${issuer}`);
|
|
35
37
|
}
|
|
38
|
+
const surfaces = config.resourceAudiences === undefined ? null : resourceSurfaces(config.resourceAudiences);
|
|
39
|
+
if (surfaces === null) {
|
|
40
|
+
warnOnce('config', 'createNevarAuth without resourceAudiences is deprecated since 0.3.0 and throws from 1.0.0; the surface falls ' +
|
|
41
|
+
'back to the first DNS label of the audience. Pass resourceAudiences: { api: [...], mcp: [...] }.');
|
|
42
|
+
}
|
|
36
43
|
const timeoutMs = config.timeoutMs ?? 5000;
|
|
37
44
|
// RFC 6749 §2.3.1: form-encode id and secret before base64.
|
|
38
45
|
const basicAuth = 'Basic ' +
|
|
39
46
|
Buffer.from(`${encodeURIComponent(introspectionClient.clientId)}:${encodeURIComponent(introspectionClient.clientSecret)}`).toString('base64');
|
|
40
47
|
let metadata;
|
|
48
|
+
let metadataAt = 0;
|
|
41
49
|
let jwks;
|
|
42
50
|
async function post(url, form, headers = {}) {
|
|
43
51
|
return http(url, {
|
|
@@ -70,16 +78,28 @@ export function createNevarAuth(config) {
|
|
|
70
78
|
return { status: res.status, body: body };
|
|
71
79
|
}
|
|
72
80
|
function discover() {
|
|
81
|
+
// Q14: discovery (and the JWKS it names) lives about an hour, so rotation needs no restart.
|
|
82
|
+
if (metadata && Date.now() - metadataAt > DISCOVERY_TTL_MS)
|
|
83
|
+
metadata = jwks = undefined;
|
|
84
|
+
if (!metadata)
|
|
85
|
+
metadataAt = Date.now();
|
|
73
86
|
metadata ??= (async () => {
|
|
74
87
|
const { status, body } = await http(`${issuer}/.well-known/openid-configuration`);
|
|
75
88
|
if (status !== 200 || typeof body.jwks_uri !== 'string') {
|
|
76
89
|
throw new NevarAuthError('dependency_unavailable', `OIDC discovery failed (${status})`);
|
|
77
90
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
91
|
+
for (const field of ['jwks_uri', 'introspection_endpoint', 'token_endpoint']) {
|
|
92
|
+
const value = body[field];
|
|
93
|
+
if (value === undefined && field !== 'jwks_uri')
|
|
94
|
+
continue;
|
|
95
|
+
try {
|
|
96
|
+
if (typeof value !== 'string')
|
|
97
|
+
throw new TypeError(`${field} is not a string`);
|
|
98
|
+
new URL(value);
|
|
99
|
+
}
|
|
100
|
+
catch (cause) {
|
|
101
|
+
throw new NevarAuthError('dependency_unavailable', `Discovery ${field} is not an absolute URL`, { cause });
|
|
102
|
+
}
|
|
83
103
|
}
|
|
84
104
|
if (body.issuer !== issuer) {
|
|
85
105
|
throw new NevarAuthError('misconfigured', `Discovery names issuer ${String(body.issuer)}, configured ${issuer}`);
|
|
@@ -93,7 +113,7 @@ export function createNevarAuth(config) {
|
|
|
93
113
|
}
|
|
94
114
|
const getKey = async (header, token) => {
|
|
95
115
|
const { jwks_uri } = await discover();
|
|
96
|
-
jwks ??= createRemoteJWKSet(new URL(jwks_uri), { timeoutDuration: timeoutMs });
|
|
116
|
+
jwks ??= createRemoteJWKSet(new URL(jwks_uri), { timeoutDuration: timeoutMs, cacheMaxAge: DISCOVERY_TTL_MS }); // Q14: an hour, not jose's 10-minute default
|
|
97
117
|
try {
|
|
98
118
|
return await jwks(header, token);
|
|
99
119
|
}
|
|
@@ -105,7 +125,10 @@ export function createNevarAuth(config) {
|
|
|
105
125
|
}
|
|
106
126
|
};
|
|
107
127
|
async function introspect(token, grantId) {
|
|
108
|
-
|
|
128
|
+
// Q10: discovery's introspection_endpoint; the conventional path only when discovery names none.
|
|
129
|
+
const { introspection_endpoint } = await discover();
|
|
130
|
+
const endpoint = requireIssuerEndpoint(introspection_endpoint ?? `${issuer}/oauth/introspect`, 'introspection_endpoint');
|
|
131
|
+
const { status, body } = await post(endpoint, { token }, { authorization: basicAuth });
|
|
109
132
|
if (status === 401 || status === 403) {
|
|
110
133
|
throw new NevarAuthError('misconfigured', `Introspection refused this service's client credentials (${status})`, { grantId });
|
|
111
134
|
}
|
|
@@ -123,8 +146,9 @@ export function createNevarAuth(config) {
|
|
|
123
146
|
let grantId = null;
|
|
124
147
|
let kid;
|
|
125
148
|
let alg;
|
|
149
|
+
let typ;
|
|
126
150
|
try {
|
|
127
|
-
({ kid, alg } = decodeProtectedHeader(token));
|
|
151
|
+
({ kid, alg, typ } = decodeProtectedHeader(token));
|
|
128
152
|
const grant = decodeJwt(token).nevar_grant_id; // unverified; used only as a log reference
|
|
129
153
|
grantId = typeof grant === 'string' ? grant : null;
|
|
130
154
|
}
|
|
@@ -146,6 +170,8 @@ export function createNevarAuth(config) {
|
|
|
146
170
|
audience,
|
|
147
171
|
algorithms: ASYMMETRIC_ALGORITHMS,
|
|
148
172
|
requiredClaims: ['exp', 'iss', 'aud', 'sub'],
|
|
173
|
+
// Q11 (RFC 9068): an ID token or any other typ is not an access token. The 0.2 contract did not check typ.
|
|
174
|
+
typ: surfaces === null ? undefined : 'at+jwt',
|
|
149
175
|
}));
|
|
150
176
|
}
|
|
151
177
|
catch (cause) {
|
|
@@ -155,12 +181,33 @@ export function createNevarAuth(config) {
|
|
|
155
181
|
throw new NevarAuthError('wrong_audience', 'Token names more than one audience', { grantId });
|
|
156
182
|
}
|
|
157
183
|
// Signature and claims hold; the issuer decides whether the grant is still live. Nothing caches a positive answer.
|
|
158
|
-
await introspect(token, grantId);
|
|
159
|
-
|
|
184
|
+
const introspection = await introspect(token, grantId);
|
|
185
|
+
// Q9: the issuer's introspection must say role authenticated, under every config; anything else (service_role, none) is refused.
|
|
186
|
+
if (introspection.role !== 'authenticated') {
|
|
187
|
+
throw new NevarAuthError('invalid_token', 'Introspection role is not authenticated', { grantId });
|
|
188
|
+
}
|
|
189
|
+
if (surfaces === null) {
|
|
190
|
+
// 0.2 contract: an untyped token, and an issuer whose introspection omits scope/grant, still work.
|
|
191
|
+
if (typ !== 'at+jwt') {
|
|
192
|
+
warnOnce('typ', 'accepting a JWT without typ at+jwt is deprecated since 0.3.0 and refused from 1.0.0; the issuer must mint RFC 9068 access tokens.');
|
|
193
|
+
}
|
|
194
|
+
if (introspection.scope === undefined || introspection.nevar_grant_id === undefined) {
|
|
195
|
+
warnOnce('introspection', 'reading scope or nevar_grant_id from the JWT when introspection omits them is deprecated since 0.3.0 and refused from 1.0.0; the issuer must return both from introspection.');
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
...payload,
|
|
199
|
+
scope: introspection.scope ?? payload.scope,
|
|
200
|
+
nevar_grant_id: introspection.nevar_grant_id ?? payload.nevar_grant_id,
|
|
201
|
+
role: introspection.role,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
// Q12: the current scope and grant are the issuer's answer now, not what the JWT said when it was minted.
|
|
205
|
+
return { ...payload, scope: introspection.scope, nevar_grant_id: introspection.nevar_grant_id, role: introspection.role };
|
|
160
206
|
}
|
|
161
207
|
async function verifyKey(key, audience) {
|
|
162
208
|
const prefix = key.slice(0, 3);
|
|
163
|
-
|
|
209
|
+
const surface = surfaces === null ? labelSurface(audience) : surfaces.get(audience);
|
|
210
|
+
if (surface !== prefix) {
|
|
164
211
|
throw new NevarAuthError('wrong_surface', `A ${prefix}_ key is not accepted on ${audience}`);
|
|
165
212
|
}
|
|
166
213
|
const claims = await introspect(key, null);
|
|
@@ -178,15 +225,32 @@ export function createNevarAuth(config) {
|
|
|
178
225
|
}
|
|
179
226
|
return claims;
|
|
180
227
|
}
|
|
228
|
+
async function verify(credential, audience) {
|
|
229
|
+
if (typeof credential !== 'string')
|
|
230
|
+
throw new NevarAuthError('invalid_token', 'Credential is not a string');
|
|
231
|
+
const token = credential.replace(/^Bearer\s+/i, '').trim();
|
|
232
|
+
const claims = /^(api|mcp)_/.test(token) ? await verifyKey(token, audience) : await verifyJwt(token, audience);
|
|
233
|
+
return toActorContext(claims, audience);
|
|
234
|
+
}
|
|
235
|
+
/** Q10, Q16: introspection and the exchange carry our client secret, so they go only to https on the issuer's origin. */
|
|
236
|
+
function requireIssuerEndpoint(value, field) {
|
|
237
|
+
if (!value)
|
|
238
|
+
throw new NevarAuthError('misconfigured', `Discovery has no ${field}`);
|
|
239
|
+
const endpoint = new URL(value);
|
|
240
|
+
if (endpoint.origin !== new URL(issuer).origin) {
|
|
241
|
+
throw new NevarAuthError('misconfigured', `${field} ${endpoint.origin} is not on the issuer origin`);
|
|
242
|
+
}
|
|
243
|
+
// Test mode is refused for the real issuers at construction, so only a local test issuer may use http.
|
|
244
|
+
if (endpoint.protocol !== 'https:' && mode !== 'test') {
|
|
245
|
+
throw new NevarAuthError('misconfigured', `${field} must be https, got ${endpoint.protocol}`);
|
|
246
|
+
}
|
|
247
|
+
return endpoint.href;
|
|
248
|
+
}
|
|
181
249
|
return {
|
|
182
250
|
async verifyCredential(credential, { audience, correlationId }) {
|
|
183
251
|
try {
|
|
184
252
|
requireAudience(audience);
|
|
185
|
-
|
|
186
|
-
throw new NevarAuthError('invalid_token', 'Credential is not a string');
|
|
187
|
-
const token = credential.replace(/^Bearer\s+/i, '').trim();
|
|
188
|
-
const claims = /^(api|mcp)_/.test(token) ? await verifyKey(token, audience) : await verifyJwt(token, audience);
|
|
189
|
-
return toActorContext(claims, audience);
|
|
253
|
+
return await verify(credential, audience);
|
|
190
254
|
}
|
|
191
255
|
catch (error) {
|
|
192
256
|
const authError = error instanceof NevarAuthError ? error : new NevarAuthError('invalid_token', 'Verification failed', { cause: error });
|
|
@@ -202,20 +266,35 @@ export function createNevarAuth(config) {
|
|
|
202
266
|
async delegateActor({ serviceToken, subjectToken, audience, correlationId }) {
|
|
203
267
|
try {
|
|
204
268
|
requireAudience(audience);
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
throw new NevarAuthError('misconfigured', 'Discovery has no token_endpoint');
|
|
208
|
-
const { status, body } = await post(token_endpoint, {
|
|
269
|
+
const tokenEndpoint = requireIssuerEndpoint((await discover()).token_endpoint, 'token_endpoint');
|
|
270
|
+
const { status, body } = await post(tokenEndpoint, {
|
|
209
271
|
grant_type: 'urn:ietf:params:oauth:grant-type:token-exchange',
|
|
210
272
|
subject_token: subjectToken,
|
|
211
273
|
subject_token_type: TOKEN_TYPE_ACCESS,
|
|
212
274
|
actor_token: serviceToken,
|
|
213
275
|
actor_token_type: TOKEN_TYPE_ACCESS,
|
|
214
276
|
audience,
|
|
215
|
-
});
|
|
277
|
+
}, { authorization: basicAuth }); // Q6: the exchange authenticates this service, like introspection
|
|
278
|
+
if (status === 401 || status === 403) {
|
|
279
|
+
throw new NevarAuthError('misconfigured', `Token exchange refused this service's client credentials (${status})`);
|
|
280
|
+
}
|
|
216
281
|
if (status !== 200 || typeof body.access_token !== 'string') {
|
|
217
282
|
throw new NevarAuthError('invalid_delegation', `Token exchange refused: ${String(body.error ?? status)}${typeof body.error_description === 'string' ? ` (${body.error_description})` : ''}`);
|
|
218
283
|
}
|
|
284
|
+
// Q16: hand back only a token that verifies for the audience asked for. An outage while checking it stays
|
|
285
|
+
// dependency_unavailable (fail closed, never return the unchecked token); any other failure means the issuer
|
|
286
|
+
// returned something other than what was requested.
|
|
287
|
+
try {
|
|
288
|
+
await verify(body.access_token, audience);
|
|
289
|
+
}
|
|
290
|
+
catch (cause) {
|
|
291
|
+
if (cause instanceof NevarAuthError && cause.code === 'dependency_unavailable')
|
|
292
|
+
throw cause;
|
|
293
|
+
throw new NevarAuthError('invalid_delegation', 'The exchanged token does not verify for the requested audience', {
|
|
294
|
+
cause,
|
|
295
|
+
grantId: cause instanceof NevarAuthError ? cause.grantId : null,
|
|
296
|
+
});
|
|
297
|
+
}
|
|
219
298
|
return { token: body.access_token, expiresIn: typeof body.expires_in === 'number' ? body.expires_in : null };
|
|
220
299
|
}
|
|
221
300
|
catch (error) {
|
|
@@ -233,8 +312,16 @@ function requireAudience(audience) {
|
|
|
233
312
|
throw new NevarAuthError('misconfigured', 'The caller must name the audience it expects');
|
|
234
313
|
}
|
|
235
314
|
}
|
|
236
|
-
|
|
237
|
-
|
|
315
|
+
const warned = new Set();
|
|
316
|
+
/** One deprecation line per process per legacy contract, so a hot path cannot flood the log. */
|
|
317
|
+
function warnOnce(key, message) {
|
|
318
|
+
if (warned.has(key))
|
|
319
|
+
return;
|
|
320
|
+
warned.add(key);
|
|
321
|
+
console.warn(`[@bernierllc/nevar-auth] DEPRECATED: ${message}`);
|
|
322
|
+
}
|
|
323
|
+
/** The 0.2 rule: `api` for `https://api.…`, `mcp` for `https://mcp.…`, otherwise `null`. */
|
|
324
|
+
function labelSurface(audience) {
|
|
238
325
|
let host;
|
|
239
326
|
try {
|
|
240
327
|
host = new URL(audience).hostname;
|
|
@@ -245,6 +332,35 @@ export function audienceSurface(audience) {
|
|
|
245
332
|
const label = host.split('.')[0];
|
|
246
333
|
return label === 'api' || label === 'mcp' ? label : null;
|
|
247
334
|
}
|
|
335
|
+
/**
|
|
336
|
+
* `api` for `https://api.…`, `mcp` for `https://mcp.…`, otherwise `null`.
|
|
337
|
+
* @deprecated since 0.3.0; removed in 1.0.0; use `resourceAudiences` in `createNevarAuth`, which decides the surface
|
|
338
|
+
* from exact audience sets instead of the host name.
|
|
339
|
+
*/
|
|
340
|
+
export function audienceSurface(audience) {
|
|
341
|
+
warnOnce('audienceSurface', 'audienceSurface is deprecated since 0.3.0 and removed in 1.0.0; use resourceAudiences.');
|
|
342
|
+
return labelSurface(audience);
|
|
343
|
+
}
|
|
344
|
+
/** Map each configured exact audience to its surface; refuse a missing, malformed or overlapping configuration. */
|
|
345
|
+
function resourceSurfaces(audiences) {
|
|
346
|
+
const surfaces = new Map();
|
|
347
|
+
for (const surface of ['api', 'mcp']) {
|
|
348
|
+
const list = audiences?.[surface];
|
|
349
|
+
if (!Array.isArray(list)) {
|
|
350
|
+
throw new NevarAuthError('misconfigured', `resourceAudiences.${surface} must be an array of exact audience URLs`);
|
|
351
|
+
}
|
|
352
|
+
for (const audience of list) {
|
|
353
|
+
if (typeof audience !== 'string' || audience === '') {
|
|
354
|
+
throw new NevarAuthError('misconfigured', `resourceAudiences.${surface} has a non-string or empty audience`);
|
|
355
|
+
}
|
|
356
|
+
if (surfaces.get(audience) === (surface === 'api' ? 'mcp' : 'api')) {
|
|
357
|
+
throw new NevarAuthError('misconfigured', `Audience ${audience} is configured for both api and mcp`);
|
|
358
|
+
}
|
|
359
|
+
surfaces.set(audience, surface);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return surfaces;
|
|
363
|
+
}
|
|
248
364
|
function jwtError(cause, grantId) {
|
|
249
365
|
if (cause instanceof NevarAuthError)
|
|
250
366
|
return cause;
|
package/dist/testing/index.d.ts
CHANGED
|
@@ -27,6 +27,11 @@ export declare const TEST_AUDIENCES: {
|
|
|
27
27
|
/** Same surface as `api`, different resource: a token for `api` is refused here. */
|
|
28
28
|
readonly otherApi: "https://api.nevar.io/v1/mail";
|
|
29
29
|
};
|
|
30
|
+
/** The `resourceAudiences` config matching `TEST_AUDIENCES`: pass it to `createNevarAuth` in tests. */
|
|
31
|
+
export declare const TEST_RESOURCE_AUDIENCES: {
|
|
32
|
+
readonly api: readonly ["https://api.nevar.io/v1/plan", "https://api.nevar.io/v1/mail"];
|
|
33
|
+
readonly mcp: readonly ["https://mcp.nevar.io/v1/plan"];
|
|
34
|
+
};
|
|
30
35
|
export declare const TEST_SUBJECTS: {
|
|
31
36
|
readonly user: "00000000-0000-4000-8000-000000000001";
|
|
32
37
|
readonly agent: "00000000-0000-4000-8000-000000000002";
|
|
@@ -38,7 +43,7 @@ export declare const TEST_ORGS: {
|
|
|
38
43
|
readonly primary: "org_test_primary";
|
|
39
44
|
readonly other: "org_test_other";
|
|
40
45
|
};
|
|
41
|
-
/** Client credentials the test issuer
|
|
46
|
+
/** Client credentials the test issuer requires at introspection and at the token endpoint. */
|
|
42
47
|
export declare const TEST_INTROSPECTION_CLIENT: {
|
|
43
48
|
readonly clientId: "test-only-client";
|
|
44
49
|
readonly clientSecret: "test-only-secret:with+special%chars";
|
|
@@ -74,6 +79,15 @@ export interface SignOptions {
|
|
|
74
79
|
issuer?: string;
|
|
75
80
|
/** Header `kid`. Default `test-only-not-production`; `null` omits it. */
|
|
76
81
|
kid?: string | null;
|
|
82
|
+
/** Header `typ`. Default `at+jwt` (an RFC 9068 access token); `null` omits it. Use `'JWT'` for an ID token. */
|
|
83
|
+
typ?: string | null;
|
|
84
|
+
}
|
|
85
|
+
export interface TestIssuerOptions {
|
|
86
|
+
port?: number;
|
|
87
|
+
/** Path introspection is served at. Default `/oauth/introspect`. */
|
|
88
|
+
introspectionPath?: string;
|
|
89
|
+
/** Whether discovery names `introspection_endpoint`. Default `true`. */
|
|
90
|
+
advertiseIntrospection?: boolean;
|
|
77
91
|
}
|
|
78
92
|
export interface TestIssuer {
|
|
79
93
|
/** Issuer identifier and base URL, e.g. `http://127.0.0.1:54321`. */
|
|
@@ -87,7 +101,7 @@ export interface TestIssuer {
|
|
|
87
101
|
/** Validly signed, but its grant is revoked at the issuer. */
|
|
88
102
|
revoked: string;
|
|
89
103
|
agent: string;
|
|
90
|
-
/** A service token: the `serviceToken` for `delegateActor`. */
|
|
104
|
+
/** A service token with `aud` = this issuer (Q6): the `serviceToken` for `delegateActor`. */
|
|
91
105
|
service: string;
|
|
92
106
|
};
|
|
93
107
|
keys: typeof TEST_KEYS;
|
|
@@ -96,11 +110,14 @@ export interface TestIssuer {
|
|
|
96
110
|
sign(claims?: Record<string, unknown>, options?: SignOptions): Promise<string>;
|
|
97
111
|
/** Revoke a grant: introspection reports every token and key with this `nevar_grant_id` inactive from now on. */
|
|
98
112
|
revoke(grantId: string): void;
|
|
113
|
+
/**
|
|
114
|
+
* From now on, introspection of any token or key with this `nevar_grant_id` reports these fields instead of the
|
|
115
|
+
* credential's own (e.g. a narrowed `scope`). `undefined` removes a field from the response.
|
|
116
|
+
*/
|
|
117
|
+
overrideIntrospection(grantId: string, fields: Record<string, unknown>): void;
|
|
99
118
|
/** Make an endpoint answer 503 (or recover with `false`), to test `dependency_unavailable`. */
|
|
100
119
|
setUnavailable(endpoint: TestEndpoint, unavailable?: boolean): void;
|
|
101
120
|
close(): Promise<void>;
|
|
102
121
|
}
|
|
103
122
|
/** Start a test issuer on 127.0.0.1. Serves discovery, JWKS, RFC 7662 introspection and RFC 8693 token exchange. */
|
|
104
|
-
export declare function startTestIssuer(options?:
|
|
105
|
-
port?: number;
|
|
106
|
-
}): Promise<TestIssuer>;
|
|
123
|
+
export declare function startTestIssuer(options?: TestIssuerOptions): Promise<TestIssuer>;
|
package/dist/testing/index.js
CHANGED
|
@@ -12,7 +12,7 @@ Redistribution or use in other products or commercial offerings is not permitted
|
|
|
12
12
|
* production mode refuses. Use `createNevarAuth({ mode: 'test', issuer: testIssuer.url, ... })` in tests.
|
|
13
13
|
*/
|
|
14
14
|
import { createServer } from 'node:http';
|
|
15
|
-
import { importJWK, jwtVerify, SignJWT } from 'jose';
|
|
15
|
+
import { errors as joseErrors, importJWK, jwtVerify, SignJWT } from 'jose';
|
|
16
16
|
import { TEST_KID } from '../index.js';
|
|
17
17
|
export { TEST_KID } from '../index.js';
|
|
18
18
|
/** Test-only ES256 private key. Never trusted in production mode. */
|
|
@@ -35,6 +35,11 @@ export const TEST_AUDIENCES = {
|
|
|
35
35
|
/** Same surface as `api`, different resource: a token for `api` is refused here. */
|
|
36
36
|
otherApi: 'https://api.nevar.io/v1/mail',
|
|
37
37
|
};
|
|
38
|
+
/** The `resourceAudiences` config matching `TEST_AUDIENCES`: pass it to `createNevarAuth` in tests. */
|
|
39
|
+
export const TEST_RESOURCE_AUDIENCES = {
|
|
40
|
+
api: [TEST_AUDIENCES.api, TEST_AUDIENCES.otherApi],
|
|
41
|
+
mcp: [TEST_AUDIENCES.mcp],
|
|
42
|
+
};
|
|
38
43
|
export const TEST_SUBJECTS = {
|
|
39
44
|
user: '00000000-0000-4000-8000-000000000001',
|
|
40
45
|
agent: '00000000-0000-4000-8000-000000000002',
|
|
@@ -43,7 +48,7 @@ export const TEST_SUBJECTS = {
|
|
|
43
48
|
otherUser: '00000000-0000-4000-8000-000000000004',
|
|
44
49
|
};
|
|
45
50
|
export const TEST_ORGS = { primary: 'org_test_primary', other: 'org_test_other' };
|
|
46
|
-
/** Client credentials the test issuer
|
|
51
|
+
/** Client credentials the test issuer requires at introspection and at the token endpoint. */
|
|
47
52
|
export const TEST_INTROSPECTION_CLIENT = { clientId: 'test-only-client', clientSecret: 'test-only-secret:with+special%chars' };
|
|
48
53
|
/** Opaque typed keys the test issuer knows. Never JWTs. */
|
|
49
54
|
export const TEST_KEYS = {
|
|
@@ -73,6 +78,8 @@ const ACCESS_TOKEN_TYPE = 'urn:ietf:params:oauth:token-type:access_token';
|
|
|
73
78
|
const WRONG_ISSUER = 'https://auth.wrong-issuer.invalid';
|
|
74
79
|
/** Start a test issuer on 127.0.0.1. Serves discovery, JWKS, RFC 7662 introspection and RFC 8693 token exchange. */
|
|
75
80
|
export async function startTestIssuer(options = {}) {
|
|
81
|
+
const introspectionPath = options.introspectionPath ?? '/oauth/introspect';
|
|
82
|
+
const overrides = new Map();
|
|
76
83
|
const privateKey = await importJWK(TEST_PRIVATE_JWK, 'ES256');
|
|
77
84
|
const publicKey = await importJWK(TEST_JWKS.keys[0], 'ES256');
|
|
78
85
|
const revoked = new Set([TEST_GRANTS.revoked]);
|
|
@@ -84,10 +91,11 @@ export async function startTestIssuer(options = {}) {
|
|
|
84
91
|
if (merged[key] === undefined)
|
|
85
92
|
delete merged[key];
|
|
86
93
|
const kid = opts.kid === undefined ? TEST_KID : opts.kid;
|
|
94
|
+
const typ = opts.typ === undefined ? 'at+jwt' : opts.typ;
|
|
87
95
|
const now = Math.floor(Date.now() / 1000);
|
|
88
96
|
const expiresIn = opts.expiresIn === undefined ? '10m' : opts.expiresIn;
|
|
89
97
|
const jwt = new SignJWT(merged)
|
|
90
|
-
.setProtectedHeader(kid === null ? {
|
|
98
|
+
.setProtectedHeader({ alg: 'ES256', ...(kid === null ? {} : { kid }), ...(typ === null ? {} : { typ }) })
|
|
91
99
|
.setIssuer(opts.issuer ?? url)
|
|
92
100
|
.setIssuedAt(typeof expiresIn === 'number' && expiresIn < 0 ? now + expiresIn - 60 : now);
|
|
93
101
|
if (expiresIn !== null)
|
|
@@ -114,23 +122,29 @@ export async function startTestIssuer(options = {}) {
|
|
|
114
122
|
iss: url,
|
|
115
123
|
},
|
|
116
124
|
});
|
|
117
|
-
/** Our own signature + issuer + expiry, and the grant not revoked.
|
|
118
|
-
async function live(token) {
|
|
125
|
+
/** Our own signature + issuer + expiry (+ `audience` when given), and the grant not revoked. */
|
|
126
|
+
async function live(token, audience) {
|
|
119
127
|
try {
|
|
120
|
-
const { payload } = await jwtVerify(token, publicKey, { issuer: url });
|
|
128
|
+
const { payload } = await jwtVerify(token, publicKey, { issuer: url, audience });
|
|
121
129
|
return revoked.has(String(payload.nevar_grant_id)) ? null : payload;
|
|
122
130
|
}
|
|
123
|
-
catch {
|
|
124
|
-
|
|
131
|
+
catch (error) {
|
|
132
|
+
if (error instanceof joseErrors.JOSEError)
|
|
133
|
+
return null; // not ours, expired or tampered: inactive per RFC 7662
|
|
134
|
+
throw error; // a bug in the test issuer, not a bad credential
|
|
125
135
|
}
|
|
126
136
|
}
|
|
127
137
|
async function introspect(form) {
|
|
128
138
|
const token = form.get('token') ?? '';
|
|
129
139
|
const key = keyClaims()[token];
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
140
|
+
const claims = key ? (revoked.has(String(key.nevar_grant_id)) ? null : key) : await live(token);
|
|
141
|
+
if (!claims)
|
|
142
|
+
return { active: false };
|
|
143
|
+
const response = { active: true, ...claims, ...overrides.get(String(claims.nevar_grant_id)) };
|
|
144
|
+
for (const field of Object.keys(response))
|
|
145
|
+
if (response[field] === undefined)
|
|
146
|
+
delete response[field];
|
|
147
|
+
return response;
|
|
134
148
|
}
|
|
135
149
|
async function exchange(form) {
|
|
136
150
|
if (form.get('grant_type') !== TOKEN_EXCHANGE)
|
|
@@ -140,11 +154,11 @@ export async function startTestIssuer(options = {}) {
|
|
|
140
154
|
return [400, { error: 'invalid_target', error_description: 'exactly one audience' }];
|
|
141
155
|
}
|
|
142
156
|
const subject = await live(form.get('subject_token') ?? '');
|
|
143
|
-
const actor = await live(form.get('actor_token') ?? '');
|
|
157
|
+
const actor = await live(form.get('actor_token') ?? '', url); // Q6: the actor token is for the issuer itself
|
|
144
158
|
if (!subject)
|
|
145
159
|
return [400, { error: 'invalid_grant', error_description: 'subject_token is not active' }];
|
|
146
160
|
if (!actor || actor.nevar_actor_type !== 'service') {
|
|
147
|
-
return [400, { error: 'invalid_grant', error_description: 'actor_token must be an active service token' }];
|
|
161
|
+
return [400, { error: 'invalid_grant', error_description: 'actor_token must be an active service token for this issuer' }];
|
|
148
162
|
}
|
|
149
163
|
const { iat: _iat, exp: _exp, iss: _iss, aud: _aud, jti: _jti, act: priorAct, ...claims } = subject;
|
|
150
164
|
const act = priorAct ? { sub: actor.sub, act: priorAct } : { sub: actor.sub };
|
|
@@ -159,7 +173,7 @@ export async function startTestIssuer(options = {}) {
|
|
|
159
173
|
const routes = {
|
|
160
174
|
'GET /.well-known/openid-configuration': 'discovery',
|
|
161
175
|
'GET /.well-known/jwks.json': 'jwks',
|
|
162
|
-
|
|
176
|
+
[`POST ${introspectionPath}`]: 'introspection',
|
|
163
177
|
'POST /oauth/token': 'token',
|
|
164
178
|
};
|
|
165
179
|
const endpoint = routes[`${req.method} ${path}`];
|
|
@@ -178,7 +192,7 @@ export async function startTestIssuer(options = {}) {
|
|
|
178
192
|
issuer: url,
|
|
179
193
|
jwks_uri: `${url}/.well-known/jwks.json`,
|
|
180
194
|
token_endpoint: `${url}/oauth/token`,
|
|
181
|
-
introspection_endpoint: `${url}
|
|
195
|
+
...(options.advertiseIntrospection === false ? {} : { introspection_endpoint: `${url}${introspectionPath}` }),
|
|
182
196
|
grant_types_supported: [TOKEN_EXCHANGE],
|
|
183
197
|
id_token_signing_alg_values_supported: ['ES256'],
|
|
184
198
|
});
|
|
@@ -190,6 +204,8 @@ export async function startTestIssuer(options = {}) {
|
|
|
190
204
|
return send(200, await introspect(form));
|
|
191
205
|
}
|
|
192
206
|
case 'token': {
|
|
207
|
+
if (!basicAuthMatches(req.headers.authorization))
|
|
208
|
+
return send(401, { error: 'invalid_client' });
|
|
193
209
|
const [status, body] = await exchange(form);
|
|
194
210
|
return send(status, body);
|
|
195
211
|
}
|
|
@@ -211,6 +227,7 @@ export async function startTestIssuer(options = {}) {
|
|
|
211
227
|
nevar_actor_type: 'service',
|
|
212
228
|
nevar_owner_sub: TEST_SUBJECTS.user,
|
|
213
229
|
nevar_grant_id: TEST_GRANTS.service,
|
|
230
|
+
aud: url,
|
|
214
231
|
};
|
|
215
232
|
const tokens = {
|
|
216
233
|
valid: await sign(),
|
|
@@ -233,6 +250,7 @@ export async function startTestIssuer(options = {}) {
|
|
|
233
250
|
introspectionClient: TEST_INTROSPECTION_CLIENT,
|
|
234
251
|
sign,
|
|
235
252
|
revoke: (grantId) => void revoked.add(grantId),
|
|
253
|
+
overrideIntrospection: (grantId, fields) => void overrides.set(grantId, { ...overrides.get(grantId), ...fields }),
|
|
236
254
|
setUnavailable: (endpoint, value = true) => void (value ? unavailable.add(endpoint) : unavailable.delete(endpoint)),
|
|
237
255
|
close: () => new Promise((resolve, reject) => {
|
|
238
256
|
server.closeAllConnections();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bernierllc/nevar-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Verifier for Nevar central auth (auth.nevar.io): JWT via OIDC discovery + JWKS, api_/mcp_ keys via RFC 7662 introspection, RFC 8693 delegation, plus a real-HTTP test issuer.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|