@basaltkit/auth 5.1.0 → 5.2.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 +2 -2
- package/dist/apikeys.d.ts +8 -3
- package/dist/apikeys.js +7 -5
- package/dist/routes.js +2 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -655,7 +655,7 @@ Options (`AuthOptions` / `AuthPluginOptions` — the plugin accepts the same min
|
|
|
655
655
|
|
|
656
656
|
- `authRoutes({ register?, password?, rateLimit? })`: `POST /auth/register`, `POST /auth/login`, `POST /auth/refresh`, `POST /auth/logout`, `GET /auth/me`, `POST /auth/verify/request`, `POST /auth/verify`, `POST /auth/password/forgot`, `POST /auth/password/reset`. These are regular routes — you can omit or replace any of them. `POST /auth/logout` takes an optional `{ refreshToken }`: with none (or no body) it ends the cookie / `x-session-id` session and expires the cookie — a cross-site cookie-only logout is refused (`403 AUTH_CSRF_REJECTED`). `register: 'open' | 'closed' | RegisterPolicy` sets who may sign up (default: the plugin's `registerPolicy`, open without one): `'closed'` answers a static `404 AUTH_REGISTRATION_CLOSED`; a refusing policy answers the same `202` as a success and creates nothing.
|
|
657
657
|
- Every `authRoutes()`, `mfaRoutes()` and `oauthRoutes()` route declares `meta.account: true` (about the caller, not a tenant's data — `@basaltkit/teams`' membership guard lets non-members through) and, except MFA disable, `meta.mfa: false` (reachable under `requireMfa`). `ACCOUNT_META` exports the pair for your own profile routes.
|
|
658
|
-
- `apiKeyRoutes()`: `POST /apikeys`, `GET /apikeys[?includeExpired=true]`, `PATCH /apikeys/:id`, `POST /apikeys/:id/rotate`, `DELETE /apikeys/:id` (login session only — API keys are refused; scoped to the current tenant/user, anything else is 404). `POST /apikeys` accepts an optional `expiresAt` Unix timestamp in milliseconds, `allowedIps` (addresses/CIDRs) and `meta`; expired keys are rejected and omitted from listings unless `includeExpired=true`. `PATCH` changes `name`, `scopes`, `expiresAt` (`null` = never), `allowedIps` and `meta` in one write; rotate takes an optional `graceMs` (0 by default, at most 30 days) and returns the new key once. An expired or revoked key cannot be renewed or rotated. Service keys (`ApiKeys.issue({ serviceId })`, no user, scopes only) are programmatic-only.
|
|
658
|
+
- `apiKeyRoutes()`: `POST /apikeys`, `GET /apikeys[?includeExpired=true]`, `PATCH /apikeys/:id`, `POST /apikeys/:id/rotate`, `DELETE /apikeys/:id` (login session only — API keys are refused; scoped to the current tenant/user, anything else is 404). `POST /apikeys` requires `scopes` (`["*"]` for full power, written out; since 5.2) and accepts an optional `expiresAt` Unix timestamp in milliseconds, `allowedIps` (addresses/CIDRs) and `meta`; expired keys are rejected and omitted from listings unless `includeExpired=true`. `PATCH` changes `name`, `scopes`, `expiresAt` (`null` = never), `allowedIps` and `meta` in one write; rotate takes an optional `graceMs` (0 by default, at most 30 days) and returns the new key once. An expired or revoked key cannot be renewed or rotated. Service keys (`ApiKeys.issue({ serviceId })`, no user, scopes only) are programmatic-only.
|
|
659
659
|
- **Idempotency and API keys.** `idempotencyPlugin` (from `@basaltkit/http`) scopes a replay by the raw bytes of the credential headers, not by the key's id: a `POST` retried after a rotation, or with the key moved from `x-api-key` to `Authorization: Bearer`, runs again instead of replaying. And when the client sends `Idempotency-Key`, the `201` of `POST /apikeys` and `POST /apikeys/:id/rotate`, which carries the plaintext key, is stored in the idempotency store for its `ttlMs` (24 h by default), so protect a shared store (Redis) as secret material, or mint keys without an idempotency key.
|
|
660
660
|
- `mfaRoutes()`: `POST /auth/mfa/enroll`, `POST /auth/mfa/activate`, `GET /auth/mfa/status`, `POST /auth/mfa/disable`.
|
|
661
661
|
- `oauthRoutes({ callbackBaseUrl, successRedirect?, bindingCookie?, rateLimit? })`: `GET /auth/oauth/:provider` and `GET /auth/oauth/:provider/callback` for each configured provider (rate-limited by default).
|
|
@@ -671,7 +671,7 @@ Options (`ApiKeysPluginOptions`):
|
|
|
671
671
|
| `users` | `UserSource` | No | — | If given, a key with `userId` also populates `ctx().user`. |
|
|
672
672
|
| `now` | `() => number` | No | `Date.now` | Injectable clock (tests). |
|
|
673
673
|
|
|
674
|
-
`ApiKeys` methods: `issue(input)` (returns `{ record, key }` — the plain-text key only appears here), `verify(presented)`, `list(filter)`, `get(id)`, `revoke(id)`. Helper `scopesSatisfy(granted, required)`.
|
|
674
|
+
`ApiKeys` methods: `issue(input)` (returns `{ record, key }` — the plain-text key only appears here; `input.scopes` is required, `ApiKeyInputError` without it), `verify(presented)`, `list(filter)`, `get(id)`, `revoke(id)`. Helper `scopesSatisfy(granted, required)`.
|
|
675
675
|
|
|
676
676
|
### Exported utilities
|
|
677
677
|
|
package/dist/apikeys.d.ts
CHANGED
|
@@ -18,8 +18,8 @@ export declare class ApiKeyNotFoundError extends BasaltError {
|
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
20
20
|
* Invalid input to an API key operation: a bad `allowedIps` entry, `meta` over
|
|
21
|
-
* its limits, `userId` together with `serviceId`, a malformed `serviceId
|
|
22
|
-
* `scopes`, or a rotation `graceMs` out of range.
|
|
21
|
+
* its limits, `userId` together with `serviceId`, a malformed `serviceId`,
|
|
22
|
+
* missing or malformed `scopes`, or a rotation `graceMs` out of range.
|
|
23
23
|
*/
|
|
24
24
|
export declare class ApiKeyInputError extends BasaltError {
|
|
25
25
|
readonly status = 400;
|
|
@@ -59,7 +59,12 @@ export declare const apiKeyDisplayPrefix: (presented: string) => string | undefi
|
|
|
59
59
|
export declare const scopesSatisfy: (granted: readonly string[], required: readonly string[]) => boolean;
|
|
60
60
|
export interface IssueApiKeyInput {
|
|
61
61
|
name: string;
|
|
62
|
-
|
|
62
|
+
/**
|
|
63
|
+
* What the key may do, e.g. `['reports:read']`. Required (#581): a key
|
|
64
|
+
* issued without it used to get `['*']`, every permission its owner has,
|
|
65
|
+
* by omission. Write `['*']` when that is what the key is for.
|
|
66
|
+
*/
|
|
67
|
+
scopes: string[];
|
|
63
68
|
tenantId?: string;
|
|
64
69
|
/** The user the key acts as. Mutually exclusive with `serviceId`. */
|
|
65
70
|
userId?: string;
|
package/dist/apikeys.js
CHANGED
|
@@ -25,8 +25,8 @@ export class ApiKeyNotFoundError extends BasaltError {
|
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
27
|
* Invalid input to an API key operation: a bad `allowedIps` entry, `meta` over
|
|
28
|
-
* its limits, `userId` together with `serviceId`, a malformed `serviceId
|
|
29
|
-
* `scopes`, or a rotation `graceMs` out of range.
|
|
28
|
+
* its limits, `userId` together with `serviceId`, a malformed `serviceId`,
|
|
29
|
+
* missing or malformed `scopes`, or a rotation `graceMs` out of range.
|
|
30
30
|
*/
|
|
31
31
|
export class ApiKeyInputError extends BasaltError {
|
|
32
32
|
status = 400;
|
|
@@ -174,8 +174,10 @@ export class ApiKeys {
|
|
|
174
174
|
throw new ApiKeyInputError('serviceId must be 1-128 printable characters without spaces.');
|
|
175
175
|
}
|
|
176
176
|
}
|
|
177
|
-
if (input.scopes
|
|
178
|
-
|
|
177
|
+
if (input.scopes === undefined) {
|
|
178
|
+
throw new ApiKeyInputError("scopes is required: name what the key may do (['reports:read']), or ['*'] for a key with every permission its owner has.");
|
|
179
|
+
}
|
|
180
|
+
assertScopes(input.scopes);
|
|
179
181
|
const allowedIps = input.allowedIps !== undefined ? allowedIpsOf(input.allowedIps) : [];
|
|
180
182
|
const meta = input.meta !== undefined ? normalizeMeta(input.meta) : {};
|
|
181
183
|
const { key, hash, prefix } = mintSecret();
|
|
@@ -184,7 +186,7 @@ export class ApiKeys {
|
|
|
184
186
|
name: input.name,
|
|
185
187
|
prefix,
|
|
186
188
|
hash,
|
|
187
|
-
scopes: input.scopes
|
|
189
|
+
scopes: [...input.scopes],
|
|
188
190
|
createdAt: this.now(),
|
|
189
191
|
...(input.expiresAt !== undefined ? { expiresAt: input.expiresAt } : {}),
|
|
190
192
|
...(input.tenantId !== undefined ? { tenantId: input.tenantId } : {}),
|
package/dist/routes.js
CHANGED
|
@@ -233,7 +233,8 @@ export function apiKeyRoutes() {
|
|
|
233
233
|
meta: { auth: true, apiKey: false },
|
|
234
234
|
body: z.object({
|
|
235
235
|
name: z.string().min(1).max(100),
|
|
236
|
-
|
|
236
|
+
// Required (#581): a key's power is chosen, never defaulted to '*'.
|
|
237
|
+
scopes: z.array(z.string().min(1)),
|
|
237
238
|
expiresAt: z.number().int().positive().optional(),
|
|
238
239
|
allowedIps: z.array(z.string()).optional(),
|
|
239
240
|
meta: meta.optional(),
|