@bernierllc/nevar-auth 0.3.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 CHANGED
@@ -10,16 +10,16 @@ npm install @bernierllc/nevar-auth
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
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
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
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.
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`.
19
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
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.
21
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.
22
- - **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.
23
23
 
24
24
  ## Usage
25
25
 
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];
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'];
@@ -43,6 +45,7 @@ export function createNevarAuth(config) {
43
45
  const basicAuth = 'Basic ' +
44
46
  Buffer.from(`${encodeURIComponent(introspectionClient.clientId)}:${encodeURIComponent(introspectionClient.clientSecret)}`).toString('base64');
45
47
  let metadata;
48
+ let metadataAt = 0;
46
49
  let jwks;
47
50
  async function post(url, form, headers = {}) {
48
51
  return http(url, {
@@ -75,6 +78,11 @@ export function createNevarAuth(config) {
75
78
  return { status: res.status, body: body };
76
79
  }
77
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();
78
86
  metadata ??= (async () => {
79
87
  const { status, body } = await http(`${issuer}/.well-known/openid-configuration`);
80
88
  if (status !== 200 || typeof body.jwks_uri !== 'string') {
@@ -105,7 +113,7 @@ export function createNevarAuth(config) {
105
113
  }
106
114
  const getKey = async (header, token) => {
107
115
  const { jwks_uri } = await discover();
108
- 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
109
117
  try {
110
118
  return await jwks(header, token);
111
119
  }
@@ -174,6 +182,10 @@ export function createNevarAuth(config) {
174
182
  }
175
183
  // Signature and claims hold; the issuer decides whether the grant is still live. Nothing caches a positive answer.
176
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
+ }
177
189
  if (surfaces === null) {
178
190
  // 0.2 contract: an untyped token, and an issuer whose introspection omits scope/grant, still work.
179
191
  if (typ !== 'at+jwt') {
@@ -186,10 +198,11 @@ export function createNevarAuth(config) {
186
198
  ...payload,
187
199
  scope: introspection.scope ?? payload.scope,
188
200
  nevar_grant_id: introspection.nevar_grant_id ?? payload.nevar_grant_id,
201
+ role: introspection.role,
189
202
  };
190
203
  }
191
204
  // Q12: the current scope and grant are the issuer's answer now, not what the JWT said when it was minted.
192
- return { ...payload, scope: introspection.scope, nevar_grant_id: introspection.nevar_grant_id };
205
+ return { ...payload, scope: introspection.scope, nevar_grant_id: introspection.nevar_grant_id, role: introspection.role };
193
206
  }
194
207
  async function verifyKey(key, audience) {
195
208
  const prefix = key.slice(0, 3);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bernierllc/nevar-auth",
3
- "version": "0.3.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",