@ultimat3/auth 31.38.1 → 31.40.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.
Files changed (2) hide show
  1. package/package.json +4 -4
  2. package/src/api-keys.ts +18 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/auth",
3
- "version": "31.38.1",
3
+ "version": "31.40.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.38.1",
54
- "@ultimat3/db": "31.38.1",
55
- "@ultimat3/schema": "31.38.1"
53
+ "@ultimat3/core": "31.40.0",
54
+ "@ultimat3/db": "31.40.0",
55
+ "@ultimat3/schema": "31.40.0"
56
56
  }
57
57
  }
package/src/api-keys.ts CHANGED
@@ -7,7 +7,7 @@
7
7
 
8
8
  import { type Clock, randomHex, systemClock, timingSafeEqual } from '@ultimat3/core';
9
9
  import type { ApiKeyStore, AuthApiKeyRecord, AuthUser, UserStore } from './adapter';
10
- import { apiKeyEnvInvalid, apiKeyInvalid, apiKeyScopeWildcard } from './errors';
10
+ import { AuthError, apiKeyEnvInvalid, apiKeyInvalid, apiKeyScopeWildcard } from './errors';
11
11
  import type { PolicyActor } from './policy-bridge';
12
12
  import { actorFromApiKey, isWildcardScope } from './policy-bridge';
13
13
  import { randomToken, sha256Hex } from './tokens';
@@ -69,8 +69,24 @@ export interface IssuedApiKey {
69
69
  /** What `parseApiKey` can split back out: no `_`, and nothing a log would render two ways. */
70
70
  const API_KEY_ENV = /^[a-z0-9-]+$/;
71
71
 
72
+ /**
73
+ * An expiry that is not an instant. `X_CONFIG_INVALID`, borrowed as `createSession`'s
74
+ * `inheritedInstantInvalid` borrows it: `now >= NaN` is false, so the memory store kept the key
75
+ * forever, and Postgres answered a raw `invalid input syntax` instead of a coded refusal.
76
+ */
77
+ const expiresAtInvalid = (): AuthError =>
78
+ new AuthError({
79
+ code: 'X_CONFIG_INVALID',
80
+ cause:
81
+ 'issueApiKey({ expiresAt }) is an Invalid Date, and stored as an expiry it would never pass, because every comparison against NaN is false',
82
+ fix: "issueApiKey({ env, scopes, expiresAt: new Date('2027-01-01T00:00:00Z') }) // a parseable instant, or omit expiresAt for a key that lives until revoked",
83
+ meta: { field: 'expiresAt' },
84
+ });
85
+
72
86
  export function issueApiKey(input: IssueApiKeyInput): IssuedApiKey {
73
87
  if (!API_KEY_ENV.test(input.env)) throw apiKeyEnvInvalid(input.env);
88
+ const expiresAt = input.expiresAt ?? null;
89
+ if (expiresAt !== null && !Number.isFinite(expiresAt.getTime())) throw expiresAtInvalid();
74
90
  const wildcard = input.scopes.find(isWildcardScope);
75
91
  if (wildcard !== undefined) throw apiKeyScopeWildcard(wildcard);
76
92
  const clock = input.clock ?? systemClock;
@@ -88,7 +104,7 @@ export function issueApiKey(input: IssueApiKeyInput): IssuedApiKey {
88
104
  orgId: input.orgId ?? null,
89
105
  scopes: [...input.scopes],
90
106
  lastUsedAt: null,
91
- expiresAt: input.expiresAt ?? null,
107
+ expiresAt,
92
108
  revokedAt: null,
93
109
  createdAt: clock.now(),
94
110
  },