@extantape/payload-plugin-hats 0.8.0 → 0.8.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/index.d.ts CHANGED
@@ -2,6 +2,7 @@ import type { PayloadRequest, Plugin } from 'payload';
2
2
  import { type SwitchInput, type SwitchResult } from './endpoints.js';
3
3
  import { type HatsOptions } from './types.js';
4
4
  export { hatsStateOf } from './state.js';
5
+ export { rememberKey, rememberedAt } from './stepup.js';
5
6
  export { applyHat, cleanupSessions, revokeSessions } from './sessions.js';
6
7
  export { LINKS, MEMBERS, REQUESTS, SESSIONS } from './types.js';
7
8
  export type { SwitchInput, SwitchResult };
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ import { REMEMBER_PER } from './stepup.js';
5
5
  import { asPlugin, changeWhere, holdEvents, newToken, ownHook, relId, removeWhere, serial, unmarkForeignHooks, watchTransactions } from './state.js';
6
6
  import { LINKS, MEMBERS, REQUESTS, SESSIONS } from './types.js';
7
7
  export { hatsStateOf } from './state.js';
8
+ export { rememberKey, rememberedAt } from './stepup.js';
8
9
  export { applyHat, cleanupSessions, revokeSessions } from './sessions.js';
9
10
  export { LINKS, MEMBERS, REQUESTS, SESSIONS } from './types.js';
10
11
  export { approveLink, checkLinks, linkAccounts } from './link.js';
package/dist/stepup.d.ts CHANGED
@@ -9,6 +9,20 @@ export declare const REMEMBER_PER: readonly unknown[];
9
9
  * value that isn't one of the three, has no key: nothing is remembered and nothing is written.
10
10
  */
11
11
  export declare const memoryKey: (opts: ResolvedHatsOptions, account: string, set: string | null, hat?: string | null) => string | null;
12
+ /**
13
+ * The same key as a pure function, for a site that reads `steppedUp` on a session itself (for example to check a code
14
+ * given for a wider hat). `per` is `stepUp.rememberPer` as the site set it. Returns `null` when there is no key.
15
+ */
16
+ export declare const rememberKey: (per: unknown, w: {
17
+ account: string;
18
+ set?: string | null;
19
+ hat?: string | null;
20
+ }) => string | null;
21
+ /**
22
+ * When a code was given under `key`, as an ISO string, or `null` when it wasn't or `steppedUp` has no such entry.
23
+ * Reads own entries only. It does not check the age: compare it with `stepUp.rememberMinutes` (default 180).
24
+ */
25
+ export declare const rememberedAt: (steppedUp: Record<string, string> | null | undefined, key: string | null) => string | null;
12
26
  /** Whether a good code was given recently enough to count. */
13
27
  export declare const remembered: (opts: ResolvedHatsOptions, steppedUp: Record<string, string> | null | undefined, account: string, set: string | null, hat?: string | null) => boolean;
14
28
  /**
package/dist/stepup.js CHANGED
@@ -6,20 +6,29 @@ export const REMEMBER_PER = ['account', 'set', 'hat'];
6
6
  * together, so an entry written under `'account'` or `'set'` never matches it. A missing hat under `'hat'`, or a
7
7
  * value that isn't one of the three, has no key: nothing is remembered and nothing is written.
8
8
  */
9
- export const memoryKey = (opts, account, set, hat) => {
10
- const per = opts.stepUp?.rememberPer;
9
+ export const memoryKey = (opts, account, set, hat) => rememberKey(opts.stepUp?.rememberPer, { account, set, hat });
10
+ /**
11
+ * The same key as a pure function, for a site that reads `steppedUp` on a session itself (for example to check a code
12
+ * given for a wider hat). `per` is `stepUp.rememberPer` as the site set it. Returns `null` when there is no key.
13
+ */
14
+ export const rememberKey = (per, w) => {
11
15
  if (per === undefined || per === 'account')
12
- return account;
16
+ return w.account;
13
17
  if (per === 'set')
14
- return set ? `set:${set}` : account;
18
+ return w.set ? `set:${w.set}` : w.account;
15
19
  if (per === 'hat')
16
- return typeof hat === 'string' && hat ? `hat:${JSON.stringify([account, hat])}` : null;
20
+ return typeof w.hat === 'string' && w.hat ? `hat:${JSON.stringify([w.account, w.hat])}` : null;
17
21
  return null;
18
22
  };
23
+ /**
24
+ * When a code was given under `key`, as an ISO string, or `null` when it wasn't or `steppedUp` has no such entry.
25
+ * Reads own entries only. It does not check the age: compare it with `stepUp.rememberMinutes` (default 180).
26
+ */
27
+ export const rememberedAt = (steppedUp, key) => key === null || !steppedUp || !Object.hasOwn(steppedUp, key) ? null : typeof steppedUp[key] === 'string' ? steppedUp[key] : null;
19
28
  /** Whether a good code was given recently enough to count. */
20
29
  export const remembered = (opts, steppedUp, account, set, hat) => {
21
30
  const key = memoryKey(opts, account, set, hat);
22
- const at = key === null || !steppedUp || !Object.hasOwn(steppedUp, key) ? null : steppedUp[key];
31
+ const at = rememberedAt(steppedUp, key);
23
32
  return Boolean(at && Date.now() - new Date(at).getTime() < (opts.stepUp?.rememberMinutes ?? 180) * 60_000);
24
33
  };
25
34
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@extantape/payload-plugin-hats",
3
- "version": "0.8.0",
3
+ "version": "0.8.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",