@extantape/payload-plugin-hats 0.1.5 → 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/dist/stepup.js ADDED
@@ -0,0 +1,26 @@
1
+ /** The key a good code is remembered under: the linked set with `rememberPer: 'set'` (when the account is in one), else the account. */
2
+ export const memoryKey = (opts, account, set) => (opts.stepUp?.rememberPer === 'set' && set ? `set:${set}` : account);
3
+ /** Whether a good code was given recently enough to count. */
4
+ export const remembered = (opts, steppedUp, account, set) => {
5
+ const at = steppedUp?.[memoryKey(opts, account, set)];
6
+ return Boolean(at && Date.now() - new Date(at).getTime() < (opts.stepUp?.rememberMinutes ?? 180) * 60_000);
7
+ };
8
+ /**
9
+ * Does moving to `hat` on `target` need a code (before any memory is taken into account)? Without `stepUpWhen` that is
10
+ * the hat's `sensitive` flag. With it, the site's answer decides, and only an exact `false` skips the code: `true`, a
11
+ * throw, a rejection or any other answer (`'no'`, `0`, `undefined`) asks.
12
+ */
13
+ export async function needsStepUp(opts, args) {
14
+ const when = opts.stepUpWhen;
15
+ if (!when)
16
+ return Boolean(args.hat.sensitive);
17
+ try {
18
+ // The target as it would be worn: the account's last hat is all of its roles, so nothing is narrowed.
19
+ const user = args.hat.id === args.hats.at(-1)?.id ? args.target : opts.wear(args.target, args.hat.id);
20
+ const answer = await when({ req: args.req, from: args.from, to: { user, hat: args.hat.id } });
21
+ return answer !== false;
22
+ }
23
+ catch {
24
+ return true;
25
+ }
26
+ }
package/dist/types.d.ts CHANGED
@@ -5,6 +5,13 @@ export type Hat = {
5
5
  label: string;
6
6
  sensitive?: boolean;
7
7
  };
8
+ /** Who is asking, passed to the site's two-step checks along with the target account. */
9
+ export type StepUpContext = {
10
+ /** The account that actually signed in: the person (`via` when this session was switched in, else the account). */
11
+ signedIn: HatsUser;
12
+ /** The linked set's id, or null when the target account is in none. */
13
+ set: string | null;
14
+ };
8
15
  /** A user record as the plugin sees it. */
9
16
  export type HatsUser = TypedUser & {
10
17
  id: string | number;
@@ -44,16 +51,38 @@ export type HatsOptions = {
44
51
  /** The site's second factor, asked before switching into a sensitive hat. */
45
52
  stepUp?: {
46
53
  /** Whether the account has a second factor set up at all. May look it up (async), since it's often a hidden field. */
47
- available: (user: HatsUser) => boolean | Promise<boolean>;
54
+ available: (user: HatsUser, ctx: StepUpContext) => boolean | Promise<boolean>;
48
55
  /** Check a code for `user`. Resolve true only for a good code. */
49
56
  verify: (args: {
50
57
  req: PayloadRequest;
51
58
  user: HatsUser;
52
59
  code: string;
53
- }) => boolean | Promise<boolean>;
54
- /** How long a good code is remembered, per account, in minutes. Default 180 (three hours). */
60
+ } & StepUpContext) => boolean | Promise<boolean>;
61
+ /** How long a good code is remembered, in minutes. Default 180 (three hours). */
55
62
  rememberMinutes?: number;
63
+ /**
64
+ * What a good code is remembered for. `'account'` (default): the account it was given for. `'set'`: every account
65
+ * in the same linked set, so one two-step per person; an account in no set is still remembered on its own.
66
+ */
67
+ rememberPer?: 'account' | 'set';
56
68
  };
69
+ /**
70
+ * Decides whether a switch needs a code, in place of the hat's `sensitive` flag. Called with the account and hat
71
+ * worn now (`from`, as narrowed) and the account and hat it would become (`to`, as it would be worn). Answer `true`
72
+ * to ask for a code (unless one is remembered). Only an exact `false` skips the code: a throw or any other answer
73
+ * asks. Default: the target hat's `sensitive` flag.
74
+ */
75
+ stepUpWhen?: (args: {
76
+ req: PayloadRequest;
77
+ from: {
78
+ user: HatsUser;
79
+ hat: string | null;
80
+ };
81
+ to: {
82
+ user: HatsUser;
83
+ hat: string;
84
+ };
85
+ }) => boolean | Promise<boolean>;
57
86
  /**
58
87
  * Who sees the switch. Default: anyone whose own account, or another account in their linked set, has more than
59
88
  * one hat. Switching is refused where the switch isn't shown.
@@ -140,7 +169,7 @@ export type HatsEvent = {
140
169
  sessions: number;
141
170
  reason: string;
142
171
  };
143
- export type ResolvedHatsOptions = Required<Omit<HatsOptions, 'stepUp' | 'canApproveLinks' | 'blocked' | 'limit' | 'onEvent' | 'confirmURL'>> & Pick<HatsOptions, 'stepUp' | 'canApproveLinks' | 'blocked' | 'limit' | 'onEvent' | 'confirmURL'>;
172
+ export type ResolvedHatsOptions = Required<Omit<HatsOptions, 'stepUp' | 'stepUpWhen' | 'canApproveLinks' | 'blocked' | 'limit' | 'onEvent' | 'confirmURL'>> & Pick<HatsOptions, 'stepUp' | 'stepUpWhen' | 'canApproveLinks' | 'blocked' | 'limit' | 'onEvent' | 'confirmURL'>;
144
173
  export declare const LINKS = "hat-links";
145
174
  export declare const MEMBERS = "hat-members";
146
175
  export declare const SESSIONS = "hat-sessions";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@extantape/payload-plugin-hats",
3
- "version": "0.1.5",
3
+ "version": "0.3.1",
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",
@@ -55,10 +55,13 @@
55
55
  },
56
56
  "devDependencies": {
57
57
  "@payloadcms/db-mongodb": "3.92.1",
58
+ "@testing-library/dom": "^10.4.2",
59
+ "@testing-library/react": "^16.3.3",
58
60
  "@types/node": "^24.0.0",
59
61
  "@types/react": "^19.3.0",
60
62
  "@types/react-dom": "^19.3.0",
61
63
  "graphql": "^16.14.2",
64
+ "jsdom": "^31.0.1",
62
65
  "mongodb-memory-server": "^11.3.0",
63
66
  "payload": "3.92.1",
64
67
  "react": "^19.3.1",