@extantape/payload-plugin-hats 0.7.0 → 0.8.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 +13 -2
- package/dist/endpoints.js +5 -3
- package/dist/index.js +3 -0
- package/dist/stepup.d.ts +10 -3
- package/dist/stepup.js +21 -4
- package/dist/types.d.ts +5 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,16 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
Linked accounts and role hats for Payload. One person links their accounts and switches between them, and an account with several roles acts in one at a time. Switching up into a sensitive role asks for a second factor.
|
|
4
4
|
|
|
5
|
-
**Status:** early, 0.x. All
|
|
5
|
+
**Status:** early, 0.x. All 366 tests pass. The server ones run against a real Payload 3.92 and MongoDB, with made-up roles and a fake second factor. The redesigned switch has unit tests for its order, gating, button markup, code screen and focus targets, and was run in a browser at 390 and 1280 px wide on a local copy of a site with made-up accounts: keyboard open, Escape, the focus trap, Cancel back to the hat, a one-tap switch and the note after it (that browser run was before the button and note changes below, and wasn't repeated). A real authenticator code wasn't tried with the new switch; the server refused the made-up code, as it should. It isn't in use on any site yet.
|
|
6
6
|
|
|
7
7
|
## What it does
|
|
8
8
|
|
|
9
9
|
- **Linked accounts.** Up to 4 accounts per person. Linking needs both emails confirmed and an approver who isn't in the set. Unlinking is self-serve and immediate.
|
|
10
10
|
- **A link request answers the same way whatever the address.** The asker gets the same answer, the same note by email, the same records to read and the same wait (`linkAnswerMs`, default 2000 ms) whether or not the address has an account, whatever state it is in. Only an account that can be linked is invited, one open invitation per account from everyone (a unique key in the database holds this, even for requests that arrive together), and nothing joins a set until it confirms. Then the asker confirms from their own email. Open requests live in a `hat-requests` collection that no user can read and only the plugin can write, at most `maxLinked × 2` per account.
|
|
11
11
|
- **Hats.** Wearing a hat narrows the user before `req.user` exists, so every access rule follows it: admin, REST, GraphQL and the Local API with `req`. If narrowing fails, the request is signed out, never given full roles.
|
|
12
|
-
- **Step-up.** Switching into a sensitive hat asks for that account's second factor, remembered for 180 minutes (`stepUp.rememberMinutes`) per account,
|
|
12
|
+
- **Step-up.** Switching into a sensitive hat asks for that account's second factor, remembered for 180 minutes (`stepUp.rememberMinutes`) per account, per linked set or per hat (see below).
|
|
13
13
|
- **Step up on gain.** Pass `stepUpWhen` to decide for yourself when a switch asks for a code, in place of the `sensitive` flag. It gets the hat worn now (`from`) and the one it would become (`to`), each with the user as it would be worn, so a site can ask only when `to` holds a role or permission that `from` doesn't. Going down asks nothing, going up or sideways asks. Only an exact `false` skips the code. `true`, a throw or any other answer asks, so a broken check never lets a switch through.
|
|
14
14
|
- **One code for the whole set.** `stepUp.rememberPer: 'set'` remembers a good code for every account in the same linked set, for the same 3 hours, so one two-step covers the person. The default, `'account'`, is unchanged. An account in no set is remembered on its own either way. `stepUp.available` and `stepUp.verify` also get `signedIn` (the account that actually signed in) and `set` (the linked set's id, or `null`), so a site can check against one two-step for the person. `user` stays the account being switched into.
|
|
15
|
+
- **One code per hat.** `stepUp.rememberPer: 'hat'` remembers a good code for that account and that hat only. Another hat on the same account asks again, and so does the same hat on a linked account. Pick it when a site's hats differ a lot in what they can do, for example a moderator hat and an owner hat on one account, and a code given for the smaller one shouldn't open the bigger one. It asks more often, so `'account'` suits a site whose sensitive hats are close to each other.
|
|
15
16
|
- **Hidden hats.** Set `hidden: true` on a hat in `hatsOf` and it isn't offered: it's left out of the switch, out of its Back to row, and out of `GET /hats`. A linked switch that names no hat goes to the account's narrowest hat only when that hat is offered, and is refused otherwise (`chooseHat`); it never moves up to a wider one. A switch into a hidden hat is refused through `POST /hats/switch` and `switchSession` alike, with the same `chooseHat` answer an unknown hat gets, before any code is asked for. Only a missing `hidden` or exactly `false` offers a hat: any other value, `'false'` or `null` included, hides it. See **What hidden doesn't do** below.
|
|
16
17
|
- **Revocation.** Unlinking, deleting a row, losing a role or deleting an account ends every session switched into or through it. Switched sessions also re-check their set on every request. An ended session's record is kept, marked revoked, so a session that Payload writes back by accident (two writes to an account's sessions at once) stays signed out. Old records are deleted once their Payload session is gone, at most 50 per switch. A switched session whose signed-in account no longer exists is refused and ended, even if the clean-up after that account's delete didn't finish. If the database fails during that clean-up, or while an unlink dissolves a set, the call answers with an error. A single delete or unlink with a transaction is then undone; in other cases the account can be deleted with some of its link rows left (see the limits under Upgrading).
|
|
17
18
|
- **Linking from server code.** `linkAccounts` and `approveLink` link and approve from your own code, with no emails and the same approval rule as the admin form. `checkLinks` lists sets written by hand that break the rules. See **Linking from your own server code** below.
|
|
@@ -57,6 +58,16 @@ hats({
|
|
|
57
58
|
})
|
|
58
59
|
```
|
|
59
60
|
|
|
61
|
+
`stepUp.rememberPer` takes one of three values. Any other value stops startup.
|
|
62
|
+
|
|
63
|
+
| Value | A good code covers | Asks again for |
|
|
64
|
+
| --- | --- | --- |
|
|
65
|
+
| `'account'` (default) | every hat on the account it was given for | another account |
|
|
66
|
+
| `'set'` | every hat on every account in the same linked set (an account in no set: that account) | an account outside the set |
|
|
67
|
+
| `'hat'` | the one hat on the one account it was given for | any other hat, and the same hat on another account |
|
|
68
|
+
|
|
69
|
+
The three are alternatives and don't combine: there is no per-set-and-hat value. A hat belongs to one account's `hatsOf`, and the same hat id on a linked account can stand for different roles, so `'hat'` always counts the account too and never widens to the set. Every value expires after `rememberMinutes`, and only asks when `sensitive` or `stepUpWhen` says the switch needs a code. The memory is kept per session under the value in use, so after changing `rememberPer` on a running site, codes remembered under the old value stop counting and people are asked once more.
|
|
70
|
+
|
|
60
71
|
`wear` must return a narrowed copy of the user, never the user itself: Hats refuses the request if it gets the same object back, if a hat adds one the account doesn't hold, or if an ordinary hat keeps a sensitive one.
|
|
61
72
|
|
|
62
73
|
Endpoints, under the API route:
|
package/dist/endpoints.js
CHANGED
|
@@ -91,7 +91,7 @@ const me = {
|
|
|
91
91
|
const mine = await membershipOf(req, String(full.id));
|
|
92
92
|
const from = { user: req.user, hat: worn?.id ?? null };
|
|
93
93
|
const setId = mine?.status === 'approved' ? idOf(mine.link) : null;
|
|
94
|
-
const asks = async (target, targetHats, h, sameAccount) => !(sameAccount && h.id === worn?.id) && (await needsStepUp(opts, { req, from, target, hats: targetHats, hat: h })) && !remembered(opts, s?.steppedUp, String(target.id), setId);
|
|
94
|
+
const asks = async (target, targetHats, h, sameAccount) => !(sameAccount && h.id === worn?.id) && (await needsStepUp(opts, { req, from, target, hats: targetHats, hat: h })) && !remembered(opts, s?.steppedUp, String(target.id), setId, h.id);
|
|
95
95
|
// The set as this account may see it: an account nobody has confirmed yet stays hidden, so a request never
|
|
96
96
|
// reveals whether an email has an account here.
|
|
97
97
|
const set = [];
|
|
@@ -238,7 +238,7 @@ async function switchCore(req, body) {
|
|
|
238
238
|
let gaveCode = false;
|
|
239
239
|
const setId = (await membershipOf(req, to).then((m) => (m?.status === 'approved' ? idOf(m.link) : null))) ?? null;
|
|
240
240
|
const from = { user: req.user, hat: fromHat };
|
|
241
|
-
if (hat.id !== nowWearing && (await needsStepUp(opts, { req, from, target: target, hats, hat: hat })) && !remembered(opts, steppedUp, to, setId)) {
|
|
241
|
+
if (hat.id !== nowWearing && (await needsStepUp(opts, { req, from, target: target, hats, hat: hat })) && !remembered(opts, steppedUp, to, setId, hat.id)) {
|
|
242
242
|
if (!opts.stepUp)
|
|
243
243
|
refuse('This site has no second factor for switching up.', 403, 'noSecondFactor');
|
|
244
244
|
// Only an exact `true` passes either check; a truthy answer such as `{ valid: false }`, or a thrown error, is a no.
|
|
@@ -251,7 +251,9 @@ async function switchCore(req, body) {
|
|
|
251
251
|
return { ok: false, status: 401, reason: 'needsCode', message: 'Enter a code from that account’s authenticator app.' };
|
|
252
252
|
if (!(await exactlyTrue(() => stepUp.verify({ req, user: target, code, ...ctx }))))
|
|
253
253
|
refuse('That code didn’t work. Try a new one.', 403, 'wrongCode');
|
|
254
|
-
|
|
254
|
+
const key = memoryKey(opts, to, setId, hat.id);
|
|
255
|
+
if (key !== null)
|
|
256
|
+
steppedUp[key] = new Date().toISOString();
|
|
255
257
|
gaveCode = true;
|
|
256
258
|
}
|
|
257
259
|
const users = req.payload.collections[opts.users].config;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { hatCollections } from './collections.js';
|
|
2
2
|
import { hatEndpoints, runSwitch } from './endpoints.js';
|
|
3
3
|
import { revokeSessions, wrapStrategies } from './sessions.js';
|
|
4
|
+
import { REMEMBER_PER } from './stepup.js';
|
|
4
5
|
import { asPlugin, changeWhere, holdEvents, newToken, ownHook, relId, removeWhere, serial, unmarkForeignHooks, watchTransactions } from './state.js';
|
|
5
6
|
import { LINKS, MEMBERS, REQUESTS, SESSIONS } from './types.js';
|
|
6
7
|
export { hatsStateOf } from './state.js';
|
|
@@ -40,6 +41,8 @@ export const hats = (options) => (config) => {
|
|
|
40
41
|
throw new Error('hats: maxLinked must be at least 2.');
|
|
41
42
|
if (typeof opts.linkAnswerMs !== 'number' || !Number.isFinite(opts.linkAnswerMs) || opts.linkAnswerMs < 0 || opts.linkAnswerMs > 2_147_483_647)
|
|
42
43
|
throw new Error('hats: linkAnswerMs must be a number of milliseconds, from 0 to 2147483647.');
|
|
44
|
+
if (opts.stepUp && opts.stepUp.rememberPer !== undefined && !REMEMBER_PER.includes(opts.stepUp.rememberPer))
|
|
45
|
+
throw new Error("hats: stepUp.rememberPer must be 'account', 'set' or 'hat'.");
|
|
43
46
|
const users = (config.collections ?? []).find((c) => c.slug === opts.users);
|
|
44
47
|
if (!users?.auth)
|
|
45
48
|
throw new Error(`hats: no auth collection "${opts.users}".`);
|
package/dist/stepup.d.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import type { PayloadRequest } from 'payload';
|
|
2
2
|
import type { Hat, HatsUser, ResolvedHatsOptions } from './types.js';
|
|
3
|
-
/**
|
|
4
|
-
export declare const
|
|
3
|
+
/** Every value `stepUp.rememberPer` takes. Startup refuses any other. */
|
|
4
|
+
export declare const REMEMBER_PER: readonly unknown[];
|
|
5
|
+
/**
|
|
6
|
+
* The key a good code is remembered under, or `null` when there is none to use. `'account'` (the default): the
|
|
7
|
+
* account. `'set'`: the linked set when the account is in one, else the account. `'hat'`: the account and the hat
|
|
8
|
+
* together, so an entry written under `'account'` or `'set'` never matches it. A missing hat under `'hat'`, or a
|
|
9
|
+
* value that isn't one of the three, has no key: nothing is remembered and nothing is written.
|
|
10
|
+
*/
|
|
11
|
+
export declare const memoryKey: (opts: ResolvedHatsOptions, account: string, set: string | null, hat?: string | null) => string | null;
|
|
5
12
|
/** Whether a good code was given recently enough to count. */
|
|
6
|
-
export declare const remembered: (opts: ResolvedHatsOptions, steppedUp: Record<string, string> | null | undefined, account: string, set: string | null) => boolean;
|
|
13
|
+
export declare const remembered: (opts: ResolvedHatsOptions, steppedUp: Record<string, string> | null | undefined, account: string, set: string | null, hat?: string | null) => boolean;
|
|
7
14
|
/**
|
|
8
15
|
* Does moving to `hat` on `target` need a code (before any memory is taken into account)? Without `stepUpWhen` that is
|
|
9
16
|
* the hat's `sensitive` flag. With it, the site's answer decides, and only an exact `false` skips the code: `true`, a
|
package/dist/stepup.js
CHANGED
|
@@ -1,8 +1,25 @@
|
|
|
1
|
-
/**
|
|
2
|
-
export const
|
|
1
|
+
/** Every value `stepUp.rememberPer` takes. Startup refuses any other. */
|
|
2
|
+
export const REMEMBER_PER = ['account', 'set', 'hat'];
|
|
3
|
+
/**
|
|
4
|
+
* The key a good code is remembered under, or `null` when there is none to use. `'account'` (the default): the
|
|
5
|
+
* account. `'set'`: the linked set when the account is in one, else the account. `'hat'`: the account and the hat
|
|
6
|
+
* together, so an entry written under `'account'` or `'set'` never matches it. A missing hat under `'hat'`, or a
|
|
7
|
+
* value that isn't one of the three, has no key: nothing is remembered and nothing is written.
|
|
8
|
+
*/
|
|
9
|
+
export const memoryKey = (opts, account, set, hat) => {
|
|
10
|
+
const per = opts.stepUp?.rememberPer;
|
|
11
|
+
if (per === undefined || per === 'account')
|
|
12
|
+
return account;
|
|
13
|
+
if (per === 'set')
|
|
14
|
+
return set ? `set:${set}` : account;
|
|
15
|
+
if (per === 'hat')
|
|
16
|
+
return typeof hat === 'string' && hat ? `hat:${JSON.stringify([account, hat])}` : null;
|
|
17
|
+
return null;
|
|
18
|
+
};
|
|
3
19
|
/** Whether a good code was given recently enough to count. */
|
|
4
|
-
export const remembered = (opts, steppedUp, account, set) => {
|
|
5
|
-
const
|
|
20
|
+
export const remembered = (opts, steppedUp, account, set, hat) => {
|
|
21
|
+
const key = memoryKey(opts, account, set, hat);
|
|
22
|
+
const at = key === null || !steppedUp || !Object.hasOwn(steppedUp, key) ? null : steppedUp[key];
|
|
6
23
|
return Boolean(at && Date.now() - new Date(at).getTime() < (opts.stepUp?.rememberMinutes ?? 180) * 60_000);
|
|
7
24
|
};
|
|
8
25
|
/**
|
package/dist/types.d.ts
CHANGED
|
@@ -69,10 +69,12 @@ export type HatsOptions = {
|
|
|
69
69
|
/** How long a good code is remembered, in minutes. Default 180 (three hours). */
|
|
70
70
|
rememberMinutes?: number;
|
|
71
71
|
/**
|
|
72
|
-
* What a good code is remembered for. `'account'` (default): the account it was given for. `'set'`:
|
|
73
|
-
* in the same linked set, so one two-step per person; an account in no set is still remembered on
|
|
72
|
+
* What a good code is remembered for. `'account'` (default): the account it was given for, whichever hat. `'set'`:
|
|
73
|
+
* every account in the same linked set, so one two-step per person; an account in no set is still remembered on
|
|
74
|
+
* its own. `'hat'`: that account and that hat only, so another hat on the same account asks again, and so does
|
|
75
|
+
* the same hat id on another account, linked or not. Any other value refuses to start.
|
|
74
76
|
*/
|
|
75
|
-
rememberPer?: 'account' | 'set';
|
|
77
|
+
rememberPer?: 'account' | 'set' | 'hat';
|
|
76
78
|
};
|
|
77
79
|
/**
|
|
78
80
|
* Decides whether a switch needs a code, in place of the hat's `sensitive` flag. Called with the account and hat
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@extantape/payload-plugin-hats",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Linked accounts and role hats for Payload: one person switches between their approved accounts, and acts in one role at a time, with a second factor before switching up.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"payload",
|