@ultimat3/auth 31.6.0 → 31.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -663,7 +663,10 @@ the `resolveToken`. Stores: `memoryAuthServerStore()`, `postgresAuthServerStore(
663
663
  over `SQL_AUTH_SERVER_TABLES`, which the boot applies. A code is spent only by a request that could
664
664
  redeem it: client, PKCE verifier, `redirect_uri` and `resource` are checked on `getCode` before
665
665
  `takeCode` marks it used, so a request without the verifier neither burns a login nor trips the
666
- replay revocation. `pollDevice(hash, clientId, at)` stamps only the issuing client's poll. Full table and the rule each test holds:
666
+ replay revocation. `pollDevice(hash, clientId, at)` stamps only the issuing client's poll. A client
667
+ authenticates only with its registered `token_endpoint_auth_method` (the other is `invalid_client`),
668
+ and gets refresh tokens only if its `grantTypes` list `refresh_token` (else refreshing is
669
+ `unauthorized_client`). Full table and the rule each test holds:
667
670
  [wiki Auth](https://github.com/developerz-ai/ultimate/wiki/Auth#authorization-server).
668
671
 
669
672
  | File | Job |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/auth",
3
- "version": "31.6.0",
3
+ "version": "31.7.0",
4
4
  "description": "Sessions, passwords, OAuth, MFA and api keys — resolved to one Actor",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -50,8 +50,8 @@
50
50
  "test": "bun test"
51
51
  },
52
52
  "dependencies": {
53
- "@ultimat3/core": "31.6.0",
54
- "@ultimat3/db": "31.6.0",
55
- "@ultimat3/schema": "31.6.0"
53
+ "@ultimat3/core": "31.7.0",
54
+ "@ultimat3/db": "31.7.0",
55
+ "@ultimat3/schema": "31.7.0"
56
56
  }
57
57
  }
@@ -25,8 +25,13 @@ const CLIENT_METADATA_CACHE_MAX = 1_000;
25
25
  const ONE_ATTEMPT = Object.freeze({ attempts: 1, base: 0, max: 0, jitter: 'none' as const });
26
26
  const LOOPBACK = new Set(['localhost', '127.0.0.1', '[::1]']);
27
27
 
28
- /** https anywhere, http only on loopback (RFC 8252 §7.3), never a fragment (RFC 6749 §3.1.2). */
28
+ /**
29
+ * https anywhere, http only on loopback (RFC 8252 §7.3), never a fragment (RFC 6749 §3.1.2), never
30
+ * whitespace: the URL parser would strip or encode it, so the byte-for-byte match could not hold,
31
+ * and the Postgres store space-joins the list — a space would split one URI into two.
32
+ */
29
33
  export function isRedirectUri(value: string): boolean {
34
+ if (/\s/.test(value)) return false;
30
35
  const url = URL.parse(value);
31
36
  if (url === null || url.hash !== '' || value.includes('#')) return false;
32
37
  return url.protocol === 'https:' || (url.protocol === 'http:' && LOOPBACK.has(url.hostname));
@@ -170,18 +175,23 @@ export async function storeClient(
170
175
  return { client, clientSecret };
171
176
  }
172
177
 
173
- /** What a token request presented about the client. */
178
+ /** What a token request presented about the client, and how. */
174
179
  export interface ClientCredentials {
175
180
  readonly clientId: string;
176
181
  readonly secret: string | null;
182
+ /** The RFC 6749 §2.3.1 method the request used: the header, the body, or no secret at all. */
183
+ readonly method: ClientAuthMethod;
177
184
  }
178
185
 
179
- /** A public client presents no secret; a confidential one must, and it must match. */
186
+ /**
187
+ * A public client presents no secret; a confidential one must, by the method it registered
188
+ * (RFC 7591 `token_endpoint_auth_method`), and it must match.
189
+ */
180
190
  export function clientAuthenticates(
181
191
  client: AuthServerClient,
182
192
  presented: ClientCredentials,
183
193
  ): boolean {
184
194
  if (client.secretHash === null) return presented.secret === null;
185
- if (presented.secret === null) return false;
195
+ if (presented.secret === null || presented.method !== client.authMethod) return false;
186
196
  return timingSafeEqual(sha256Hex(presented.secret), client.secretHash);
187
197
  }
@@ -4,7 +4,12 @@
4
4
 
5
5
  import { timingSafeEqual } from '@ultimat3/core';
6
6
  import type { ResolvedAuthServerConfig } from './authorization-server-config';
7
- import type { AuthGrantRecord, AuthTokenRecord, ResolvedGrant } from './authorization-server-types';
7
+ import type {
8
+ AuthGrantRecord,
9
+ AuthServerClient,
10
+ AuthTokenRecord,
11
+ ResolvedGrant,
12
+ } from './authorization-server-types';
8
13
  import { base64Url, randomToken, sha256Hex } from './tokens';
9
14
 
10
15
  /** Prefixes a secret scanner can match. Carry nothing; the token is 32 random bytes after them. */
@@ -86,12 +91,18 @@ export async function tokenAnswer(
86
91
  };
87
92
  }
88
93
 
89
- /** A new refresh token, stored, for a grant that has none yet. */
94
+ /**
95
+ * A new refresh token, stored, for a grant that has none yet — only when the server enables the
96
+ * grant AND the client is registered for it, the same rule `exchangeRefresh` holds it to.
97
+ */
90
98
  export async function firstRefresh(
91
99
  config: ResolvedAuthServerConfig,
100
+ client: AuthServerClient,
92
101
  grantId: string,
93
102
  ): Promise<string | null> {
94
- if (!config.grants.has('refresh_token')) return null;
103
+ if (!config.grants.has('refresh_token') || !client.grantTypes.includes('refresh_token')) {
104
+ return null;
105
+ }
95
106
  const refresh = refreshRecord(config, grantId);
96
107
  await config.store.putToken(refresh.record);
97
108
  return refresh.token;
@@ -126,10 +126,12 @@ export function readClientCredentials(
126
126
  const named = form.get('client_id');
127
127
  if (named !== undefined && named !== clientId)
128
128
  return { refused: 'client_id disagrees with Basic' };
129
- return { clientId, secret: secret === '' ? null : secret };
129
+ return { clientId, secret: secret === '' ? null : secret, method: 'client_secret_basic' };
130
130
  }
131
131
  const clientId = form.get('client_id');
132
132
  if (clientId === undefined || clientId === '') return { refused: 'client_id is required' };
133
133
  const secret = form.get('client_secret');
134
- return { clientId, secret: secret === undefined || secret === '' ? null : secret };
134
+ return secret === undefined || secret === ''
135
+ ? { clientId, secret: null, method: 'none' }
136
+ : { clientId, secret, method: 'client_secret_post' };
135
137
  }
@@ -59,7 +59,7 @@ export async function exchangeCode(
59
59
  if (record.expiresAt.getTime() <= now.getTime()) return invalidGrant('the code expired');
60
60
  const grant = await config.store.getGrant(record.grantId);
61
61
  if (grant === null || grant.revokedAt !== null) return invalidGrant('the grant is revoked');
62
- const refresh = await firstRefresh(config, grant.id);
62
+ const refresh = await firstRefresh(config, client, grant.id);
63
63
  return tokenAnswer(config, grant, config.accessSeconds, refresh);
64
64
  }
65
65
 
@@ -68,6 +68,10 @@ export async function exchangeRefresh(
68
68
  form: Form,
69
69
  client: AuthServerClient,
70
70
  ): Promise<TokenOutcome> {
71
+ // A registration narrowed after the token was minted takes effect now, not at the grant's end.
72
+ if (!client.grantTypes.includes('refresh_token')) {
73
+ return refused('unauthorized_client', 'this client may not use refresh_token');
74
+ }
71
75
  const presented = form.get('refresh_token');
72
76
  if (presented === undefined) return refused('invalid_request', 'refresh_token is required');
73
77
  const now = config.clock.now();
@@ -184,5 +184,10 @@ export async function pollDevice(
184
184
  revokedAt: null,
185
185
  };
186
186
  await config.store.putGrant(grant);
187
- return tokenAnswer(config, grant, config.accessSeconds, await firstRefresh(config, grant.id));
187
+ return tokenAnswer(
188
+ config,
189
+ grant,
190
+ config.accessSeconds,
191
+ await firstRefresh(config, client, grant.id),
192
+ );
188
193
  }