@boost33/shell-data 0.0.0-stage → 0.1.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/CHANGELOG.md +19 -0
- package/README.md +52 -2
- package/dist/index.d.ts +38 -0
- package/dist/index.js +35 -0
- package/dist/index.js.map +1 -0
- package/dist/links.d.ts +11 -0
- package/dist/links.js +29 -0
- package/dist/links.js.map +1 -0
- package/dist/person.d.ts +9 -0
- package/dist/person.js +30 -0
- package/dist/person.js.map +1 -0
- package/dist/products.d.ts +17 -0
- package/dist/products.js +96 -0
- package/dist/products.js.map +1 -0
- package/dist/profile.d.ts +10 -0
- package/dist/profile.js +28 -0
- package/dist/profile.js.map +1 -0
- package/dist/session.d.ts +158 -0
- package/dist/session.js +275 -0
- package/dist/session.js.map +1 -0
- package/dist/tokens.d.ts +14 -0
- package/dist/tokens.js +60 -0
- package/dist/tokens.js.map +1 -0
- package/dist/types.d.ts +135 -0
- package/dist/types.js +11 -0
- package/dist/types.js.map +1 -0
- package/package.json +57 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
Changes to the published surface of `@boost33/shell-data`. Consumers upgrade by hand, as with `@boost33/ui`, so this file is the only notice they get.
|
|
4
|
+
|
|
5
|
+
## 0.1.0 — 2026-11-13
|
|
6
|
+
|
|
7
|
+
### Added — `getBackdropChromeData`, everything the Backdrop chrome draws, from one call (B33-1726)
|
|
8
|
+
|
|
9
|
+
- `person` (sync, from the claims), `profileName`, `products` and `tokens` (promises that never reject), and `links` (sync, absolute). The shapes match `BackdropStandardChrome`'s props in `@boost33/ui`.
|
|
10
|
+
- Scope is the Backdrop chrome only. The 0.8 shell's switcher and wallet chip are not covered: that shell is removed after the Backdrop launch.
|
|
11
|
+
- Server only (`server-only`). It reads no secret of its own; every key arrives in `config`.
|
|
12
|
+
- Depends on `@boost33/ledger-client` 2.3.0, pinned exactly. It has no peer dependencies.
|
|
13
|
+
|
|
14
|
+
### Added — `@boost33/shell-data/session`, the session gate every app runs before it decides the design
|
|
15
|
+
|
|
16
|
+
- `gateRequest(request, { identityUrl, client, verify })` for `proxy.ts` / middleware, plus `refreshSession`, `ctxLooksExpired`, `cookiePair`, `SESSION_GATE_MATCHER`, `matcherIsNavigationOnly`, `markBounce` and `isRepeatBounce`.
|
|
17
|
+
- Lifted from Passport's production gate (boost33-passport, B33-1751), itself a copy of Boost Voice's. It follows boost33-identity's contract §5.1 point by point. The only changes are parameters for what was Passport's own: the caller slug, the verifier, and the fallback host. It adds the §5.1.7 one-bounce guard helpers.
|
|
18
|
+
- `next` (>=15) and `jose` (>=6) are optional peer dependencies, needed for this entry only.
|
|
19
|
+
|
package/README.md
CHANGED
|
@@ -1,3 +1,53 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @boost33/shell-data
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Server-only reads that hand the Backdrop chrome in `@boost33/ui` exactly the data it draws, so no app assembles its rail, account menu or token chip by hand (B33-1726).
|
|
4
|
+
|
|
5
|
+
```tsx
|
|
6
|
+
import { getBackdropChromeData } from '@boost33/shell-data'
|
|
7
|
+
import { BackdropStandardChrome } from '@boost33/ui/backdrop'
|
|
8
|
+
|
|
9
|
+
const data = getBackdropChromeData({ claims, cookies: { sid, ctx }, config })
|
|
10
|
+
return <BackdropStandardChrome {...data} variant="app" place="boostscore" labels={labels} linkComponent={Link}>{page}</BackdropStandardChrome>
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
- **`claims`**: the claims your app already verified from `b33_ctx`. This package does not verify them again, and it never reads `design`. Deciding which design a person sees is your app's `isBackdropPreview`, and only that.
|
|
14
|
+
- **`cookies`**: the request's `b33_sid` and `b33_ctx` values.
|
|
15
|
+
- **`config`**: origins and keys from your server environment: `identityUrl`, `hubUrl`, `identityKey`, `appId`, and `ledger: { url, key, app }`. Optionally `docsUrl` and `supportEmail`.
|
|
16
|
+
|
|
17
|
+
`person` and `links` are ready at once. `profileName`, `products` and `tokens` are promises. All three start together and none ever rejects, so the chrome streams each one in its own Suspense boundary. Each read fails alone, and the chrome always renders.
|
|
18
|
+
|
|
19
|
+
| Read | Source | On failure |
|
|
20
|
+
| --- | --- | --- |
|
|
21
|
+
| `products` | Identity `GET /api/v1/products`, as the viewer (their cookies) | the last good list, `stale: true`; else `null` (global rail items only) |
|
|
22
|
+
| `profileName` | Identity `GET /api/v1/profile`, with your service key | `null`: the `name` claim stays |
|
|
23
|
+
| `tokens` | `@boost33/ledger-client` `tokenSplit()` | `{ kind: 'unknown' }`; no wallet gives `{ kind: 'none' }` |
|
|
24
|
+
|
|
25
|
+
The package returns no words. Labels, date formats and the mailto subject are your app's, in its language. App order is Identity's, as returned; nothing here hardcodes a product list.
|
|
26
|
+
|
|
27
|
+
## The session gate: `@boost33/shell-data/session`
|
|
28
|
+
|
|
29
|
+
Refreshes an expired `b33_ctx` before the page renders, exactly as boost33-identity's contract §5.1 requires. It is lifted from Passport's production gate (B33-1751). Without it, a person on the Backdrop preview flips back to the classic design whenever their ten-minute `b33_ctx` runs out.
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
// src/proxy.ts (Next 16) or middleware.ts
|
|
33
|
+
import type { NextRequest } from 'next/server'
|
|
34
|
+
import { gateRequest } from '@boost33/shell-data/session'
|
|
35
|
+
|
|
36
|
+
export function proxy(request: NextRequest) {
|
|
37
|
+
return gateRequest(request, { identityUrl, client: 'boostscore', verify })
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Copy SESSION_GATE_MATCHER here AS A LITERAL. Next ignores an imported matcher.
|
|
41
|
+
export const config = { matcher: [ /* … */ ] }
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
- **`client`**: your slug in Identity's `APPS`, sent as `x-boost33-client`.
|
|
45
|
+
- **`verify(token)`**: your full `jwtVerify` of a `b33_ctx`, returning `true` or `false`. No claim crosses back, and the gate never reads `design`.
|
|
46
|
+
- **Outcomes**: one call to Identity, capped at 1.5s.
|
|
47
|
+
- Refreshed: both cookies are forwarded verbatim, and this request is rewritten so the page sees the new token.
|
|
48
|
+
- `401`: both cookies are cleared and the person goes to sign in.
|
|
49
|
+
- Timeout, `5xx` or `429`: nothing changes and nothing is forwarded.
|
|
50
|
+
- **The matcher**: `matcherIsNavigationOnly(config.matcher)` in your tests checks your copy. You may narrow its paths, but you may not drop the prefetch, `_next/` or `api/` exclusions.
|
|
51
|
+
- **One-bounce guard (§5.1.7)**: if your page redirects to Identity when it finds no claims, pass `markBounce(returnUrl)` as the return URL. On arrival, send `isRepeatBounce(url)` to a terminal page instead of back to Identity.
|
|
52
|
+
|
|
53
|
+
`next` and `jose` are optional peer dependencies, needed only for this entry.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import 'server-only';
|
|
2
|
+
import { type TokenSplitReader } from './tokens.js';
|
|
3
|
+
import type { BackdropChromeData, ChromeClaims, ChromeConfig } from './types.js';
|
|
4
|
+
export type { BackdropChromeData, ChromeClaims, ChromeConfig, ChromeLinks, ChromePerson, ChromeProduct, ChromeProducts, ChromeTokens, } from './types.js';
|
|
5
|
+
export type GetBackdropChromeDataInput = {
|
|
6
|
+
/** Claims the app has already verified from `b33_ctx`. The package does not verify them again. */
|
|
7
|
+
claims: ChromeClaims;
|
|
8
|
+
/** The request's two session cookies. `ctx` may be null when only `b33_sid` was sent. */
|
|
9
|
+
cookies: {
|
|
10
|
+
sid: string;
|
|
11
|
+
ctx: string | null;
|
|
12
|
+
};
|
|
13
|
+
config: ChromeConfig;
|
|
14
|
+
};
|
|
15
|
+
/** Overrides for tests. An app passes nothing. */
|
|
16
|
+
export type GetBackdropChromeDataDeps = {
|
|
17
|
+
fetch?: typeof fetch;
|
|
18
|
+
tokenSplit?: TokenSplitReader;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Everything the Backdrop chrome draws, from one call (B33-1726).
|
|
22
|
+
*
|
|
23
|
+
* Spread the result onto `BackdropStandardChrome` in @boost33/ui. `person`
|
|
24
|
+
* and `links` are ready at once. `profileName`, `products` and `tokens` are
|
|
25
|
+
* promises, started together here and never rejecting, so the chrome can
|
|
26
|
+
* stream each inside its own Suspense boundary instead of waiting for the
|
|
27
|
+
* slowest read. Each read fails alone, and the chrome always renders:
|
|
28
|
+
*
|
|
29
|
+
* - Identity's products unreachable: the last good list (`stale: true`), or
|
|
30
|
+
* else null, which draws the rail's global items and no apps.
|
|
31
|
+
* - Identity's profile unreachable: null, so the `name` claim stays.
|
|
32
|
+
* - The ledger unreachable or unclear: tokens `{ kind: 'unknown' }`.
|
|
33
|
+
*
|
|
34
|
+
* Server only: it reads service keys and must never reach a browser bundle.
|
|
35
|
+
* It reads no secret of its own; every key comes in `config`, from the app's
|
|
36
|
+
* server environment.
|
|
37
|
+
*/
|
|
38
|
+
export declare function getBackdropChromeData(input: GetBackdropChromeDataInput, deps?: GetBackdropChromeDataDeps): BackdropChromeData;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import 'server-only';
|
|
2
|
+
import { linksFrom } from './links.js';
|
|
3
|
+
import { personFrom } from './person.js';
|
|
4
|
+
import { readProducts } from './products.js';
|
|
5
|
+
import { readProfileName } from './profile.js';
|
|
6
|
+
import { ledgerReader, readTokens } from './tokens.js';
|
|
7
|
+
/**
|
|
8
|
+
* Everything the Backdrop chrome draws, from one call (B33-1726).
|
|
9
|
+
*
|
|
10
|
+
* Spread the result onto `BackdropStandardChrome` in @boost33/ui. `person`
|
|
11
|
+
* and `links` are ready at once. `profileName`, `products` and `tokens` are
|
|
12
|
+
* promises, started together here and never rejecting, so the chrome can
|
|
13
|
+
* stream each inside its own Suspense boundary instead of waiting for the
|
|
14
|
+
* slowest read. Each read fails alone, and the chrome always renders:
|
|
15
|
+
*
|
|
16
|
+
* - Identity's products unreachable: the last good list (`stale: true`), or
|
|
17
|
+
* else null, which draws the rail's global items and no apps.
|
|
18
|
+
* - Identity's profile unreachable: null, so the `name` claim stays.
|
|
19
|
+
* - The ledger unreachable or unclear: tokens `{ kind: 'unknown' }`.
|
|
20
|
+
*
|
|
21
|
+
* Server only: it reads service keys and must never reach a browser bundle.
|
|
22
|
+
* It reads no secret of its own; every key comes in `config`, from the app's
|
|
23
|
+
* server environment.
|
|
24
|
+
*/
|
|
25
|
+
export function getBackdropChromeData(input, deps = {}) {
|
|
26
|
+
const fetchImpl = deps.fetch ?? fetch;
|
|
27
|
+
return {
|
|
28
|
+
person: personFrom(input.claims),
|
|
29
|
+
profileName: readProfileName({ sid: input.cookies.sid, config: input.config }, fetchImpl),
|
|
30
|
+
products: readProducts(input, fetchImpl),
|
|
31
|
+
tokens: readTokens(input.claims, deps.tokenSplit ?? ledgerReader(input.config)),
|
|
32
|
+
links: linksFrom(input.config),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,aAAa,CAAA;AACpB,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAC9C,OAAO,EAAE,YAAY,EAAE,UAAU,EAAyB,MAAM,aAAa,CAAA;AA4B7E;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,qBAAqB,CACnC,KAAiC,EACjC,OAAkC,EAAE;IAEpC,MAAM,SAAS,GAAG,IAAI,CAAC,KAAK,IAAI,KAAK,CAAA;IACrC,OAAO;QACL,MAAM,EAAE,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC;QAChC,WAAW,EAAE,eAAe,CAAC,EAAE,GAAG,EAAE,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,EAAE,SAAS,CAAC;QACzF,QAAQ,EAAE,YAAY,CAAC,KAAK,EAAE,SAAS,CAAC;QACxC,MAAM,EAAE,UAAU,CAAC,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,UAAU,IAAI,YAAY,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QAC/E,KAAK,EAAE,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC;KAC/B,CAAA;AACH,CAAC"}
|
package/dist/links.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ChromeConfig, ChromeLinks } from './types.js';
|
|
2
|
+
export declare const DEFAULT_DOCS_URL = "https://docs.boost33.com";
|
|
3
|
+
export declare const DEFAULT_SUPPORT_EMAIL = "support@boost33.com";
|
|
4
|
+
/**
|
|
5
|
+
* Every link the chrome draws that no read decides. Absolute, so the same
|
|
6
|
+
* account menu works from every app and from the Hub itself; the paths are
|
|
7
|
+
* the Hub's and Identity's own routes (boost33-hub src/app/backdrop/panels.tsx).
|
|
8
|
+
* The Wallet and buy links are not here: whether they exist depends on the
|
|
9
|
+
* product list, so they arrive with it.
|
|
10
|
+
*/
|
|
11
|
+
export declare function linksFrom(config: ChromeConfig): ChromeLinks;
|
package/dist/links.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export const DEFAULT_DOCS_URL = 'https://docs.boost33.com';
|
|
2
|
+
export const DEFAULT_SUPPORT_EMAIL = 'support@boost33.com';
|
|
3
|
+
const origin = (url) => url.replace(/\/+$/, '');
|
|
4
|
+
/**
|
|
5
|
+
* Every link the chrome draws that no read decides. Absolute, so the same
|
|
6
|
+
* account menu works from every app and from the Hub itself; the paths are
|
|
7
|
+
* the Hub's and Identity's own routes (boost33-hub src/app/backdrop/panels.tsx).
|
|
8
|
+
* The Wallet and buy links are not here: whether they exist depends on the
|
|
9
|
+
* product list, so they arrive with it.
|
|
10
|
+
*/
|
|
11
|
+
export function linksFrom(config) {
|
|
12
|
+
const hub = origin(config.hubUrl);
|
|
13
|
+
const identity = origin(config.identityUrl);
|
|
14
|
+
return {
|
|
15
|
+
home: `${hub}/`,
|
|
16
|
+
settings: `${hub}/settings`,
|
|
17
|
+
profile: `${hub}/settings/profile`,
|
|
18
|
+
company: `${hub}/settings/org/general`,
|
|
19
|
+
team: `${hub}/settings/org/team`,
|
|
20
|
+
billing: `${hub}/settings/org/billing`,
|
|
21
|
+
notifications: `${hub}/settings/notifications`,
|
|
22
|
+
security: `${identity}/account/security`,
|
|
23
|
+
chooseAccount: `${identity}/choose-account`,
|
|
24
|
+
signOut: `${identity}/signout`,
|
|
25
|
+
docs: config.docsUrl ?? DEFAULT_DOCS_URL,
|
|
26
|
+
support: `mailto:${config.supportEmail ?? DEFAULT_SUPPORT_EMAIL}`,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=links.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"links.js","sourceRoot":"","sources":["../src/links.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,gBAAgB,GAAG,0BAA0B,CAAA;AAC1D,MAAM,CAAC,MAAM,qBAAqB,GAAG,qBAAqB,CAAA;AAE1D,MAAM,MAAM,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;AAEvD;;;;;;GAMG;AACH,MAAM,UAAU,SAAS,CAAC,MAAoB;IAC5C,MAAM,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;IACjC,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC,WAAW,CAAC,CAAA;IAC3C,OAAO;QACL,IAAI,EAAE,GAAG,GAAG,GAAG;QACf,QAAQ,EAAE,GAAG,GAAG,WAAW;QAC3B,OAAO,EAAE,GAAG,GAAG,mBAAmB;QAClC,OAAO,EAAE,GAAG,GAAG,uBAAuB;QACtC,IAAI,EAAE,GAAG,GAAG,oBAAoB;QAChC,OAAO,EAAE,GAAG,GAAG,uBAAuB;QACtC,aAAa,EAAE,GAAG,GAAG,yBAAyB;QAC9C,QAAQ,EAAE,GAAG,QAAQ,mBAAmB;QACxC,aAAa,EAAE,GAAG,QAAQ,iBAAiB;QAC3C,OAAO,EAAE,GAAG,QAAQ,UAAU;QAC9B,IAAI,EAAE,MAAM,CAAC,OAAO,IAAI,gBAAgB;QACxC,OAAO,EAAE,UAAU,MAAM,CAAC,YAAY,IAAI,qBAAqB,EAAE;KAClE,CAAA;AACH,CAAC"}
|
package/dist/person.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ChromeClaims, ChromePerson } from './types.js';
|
|
2
|
+
/** The account this person is acting in, or undefined when they are on none. */
|
|
3
|
+
export declare function actingAccount(claims: ChromeClaims): ChromeClaims['accounts'][number] | undefined;
|
|
4
|
+
/**
|
|
5
|
+
* One or two letters for the avatar: the first and last word of the name, or
|
|
6
|
+
* else the first letter of the email. Never empty, so the avatar always draws.
|
|
7
|
+
*/
|
|
8
|
+
export declare function initialsOf(name: string | null, email: string): string;
|
|
9
|
+
export declare function personFrom(claims: ChromeClaims): ChromePerson;
|
package/dist/person.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** The account this person is acting in, or undefined when they are on none. */
|
|
2
|
+
export function actingAccount(claims) {
|
|
3
|
+
return claims.act === null ? undefined : claims.accounts.find((account) => account.id === claims.act);
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* One or two letters for the avatar: the first and last word of the name, or
|
|
7
|
+
* else the first letter of the email. Never empty, so the avatar always draws.
|
|
8
|
+
*/
|
|
9
|
+
export function initialsOf(name, email) {
|
|
10
|
+
const words = (name ?? '').trim().split(/\s+/).filter(Boolean);
|
|
11
|
+
const first = words[0]?.[0];
|
|
12
|
+
const last = words.length > 1 ? words[words.length - 1]?.[0] : undefined;
|
|
13
|
+
const fromName = `${first ?? ''}${last ?? ''}`;
|
|
14
|
+
if (fromName)
|
|
15
|
+
return fromName.toUpperCase();
|
|
16
|
+
return (email.trim()[0] ?? '?').toUpperCase();
|
|
17
|
+
}
|
|
18
|
+
export function personFrom(claims) {
|
|
19
|
+
const account = actingAccount(claims);
|
|
20
|
+
const orgName = claims.orgName?.trim();
|
|
21
|
+
return {
|
|
22
|
+
name: claims.name,
|
|
23
|
+
email: claims.email,
|
|
24
|
+
initials: initialsOf(claims.name, claims.email),
|
|
25
|
+
org: orgName ? { name: orgName } : null,
|
|
26
|
+
role: account?.role ?? null,
|
|
27
|
+
otherAccounts: Math.max(0, claims.accounts.length - (account ? 1 : 0)),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=person.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"person.js","sourceRoot":"","sources":["../src/person.ts"],"names":[],"mappings":"AAEA,gFAAgF;AAChF,MAAM,UAAU,aAAa,CAAC,MAAoB;IAChD,OAAO,MAAM,CAAC,GAAG,KAAK,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,KAAK,MAAM,CAAC,GAAG,CAAC,CAAA;AACvG,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,UAAU,CAAC,IAAmB,EAAE,KAAa;IAC3D,MAAM,KAAK,GAAG,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;IAC9D,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;IAC3B,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;IACxE,MAAM,QAAQ,GAAG,GAAG,KAAK,IAAI,EAAE,GAAG,IAAI,IAAI,EAAE,EAAE,CAAA;IAC9C,IAAI,QAAQ;QAAE,OAAO,QAAQ,CAAC,WAAW,EAAE,CAAA;IAC3C,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,WAAW,EAAE,CAAA;AAC/C,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,MAAoB;IAC7C,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,CAAA;IACrC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,EAAE,IAAI,EAAE,CAAA;IACtC,OAAO;QACL,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,QAAQ,EAAE,UAAU,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC;QAC/C,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,IAAI;QACvC,IAAI,EAAE,OAAO,EAAE,IAAI,IAAI,IAAI;QAC3B,aAAa,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;KACvE,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ChromeClaims, ChromeConfig, ChromeProducts } from './types.js';
|
|
2
|
+
export declare const READ_TIMEOUT_MS = 5000;
|
|
3
|
+
/** For tests: forget every cached list. */
|
|
4
|
+
export declare function forgetProducts(): void;
|
|
5
|
+
/**
|
|
6
|
+
* Identity's `GET /api/v1/products`, as this person, by forwarding their two
|
|
7
|
+
* session cookies (no service key: Identity filters the list by audience for
|
|
8
|
+
* the viewer, B33-1725). Five seconds, then the last good list, then null.
|
|
9
|
+
*/
|
|
10
|
+
export declare function readProducts(input: {
|
|
11
|
+
claims: ChromeClaims;
|
|
12
|
+
cookies: {
|
|
13
|
+
sid: string;
|
|
14
|
+
ctx: string | null;
|
|
15
|
+
};
|
|
16
|
+
config: ChromeConfig;
|
|
17
|
+
}, fetchImpl: typeof fetch): Promise<ChromeProducts | null>;
|
package/dist/products.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { actingAccount } from './person.js';
|
|
2
|
+
import { DEFAULT_SUPPORT_EMAIL } from './links.js';
|
|
3
|
+
const WALLET_SLUG = 'wallet';
|
|
4
|
+
const PINNED = 4;
|
|
5
|
+
export const READ_TIMEOUT_MS = 5_000;
|
|
6
|
+
/**
|
|
7
|
+
* The last list that read cleanly, per Identity origin, without `entitled`:
|
|
8
|
+
* that half is per person and is rebuilt from the claims on a fallback.
|
|
9
|
+
* Module scope, so it lives as long as the server process, as the Hub's does.
|
|
10
|
+
*/
|
|
11
|
+
const lastGood = new Map();
|
|
12
|
+
/** For tests: forget every cached list. */
|
|
13
|
+
export function forgetProducts() {
|
|
14
|
+
lastGood.clear();
|
|
15
|
+
}
|
|
16
|
+
function parseRegistry(body) {
|
|
17
|
+
if (!body || typeof body !== 'object')
|
|
18
|
+
return null;
|
|
19
|
+
const products = body.products;
|
|
20
|
+
if (!Array.isArray(products) || products.length === 0)
|
|
21
|
+
return null;
|
|
22
|
+
const entries = [];
|
|
23
|
+
for (const p of products) {
|
|
24
|
+
if (!p || typeof p !== 'object')
|
|
25
|
+
return null;
|
|
26
|
+
const { slug, name, url, live, entitled } = p;
|
|
27
|
+
if (typeof slug !== 'string' || typeof name !== 'string' || typeof url !== 'string')
|
|
28
|
+
return null;
|
|
29
|
+
if (typeof live !== 'boolean' || typeof entitled !== 'boolean')
|
|
30
|
+
return null;
|
|
31
|
+
entries.push({ slug, name, url, live, entitled });
|
|
32
|
+
}
|
|
33
|
+
return entries;
|
|
34
|
+
}
|
|
35
|
+
/** The Hub's `cardState`, without its words: open, soon, or ask support. */
|
|
36
|
+
function stateOf(entry, hasAccount, supportEmail) {
|
|
37
|
+
if (!entry.live || !hasAccount)
|
|
38
|
+
return { state: 'soon', href: null };
|
|
39
|
+
if (!entry.entitled)
|
|
40
|
+
return { state: 'ask', href: `mailto:${supportEmail}` };
|
|
41
|
+
return { state: 'open', href: entry.url };
|
|
42
|
+
}
|
|
43
|
+
const shortNameOf = (name) => name.replace(/^Boost\s+/i, '') || name;
|
|
44
|
+
function shape(entries, hasAccount, supportEmail, stale) {
|
|
45
|
+
const wallet = entries.find((entry) => entry.slug === WALLET_SLUG);
|
|
46
|
+
const walletHref = wallet?.live ? wallet.url : null;
|
|
47
|
+
const all = entries
|
|
48
|
+
.filter((entry) => entry.slug !== WALLET_SLUG)
|
|
49
|
+
.map((entry) => ({ slug: entry.slug, name: entry.name, shortName: shortNameOf(entry.name), ...stateOf(entry, hasAccount, supportEmail) }));
|
|
50
|
+
return {
|
|
51
|
+
all,
|
|
52
|
+
pinned: all.slice(0, PINNED).map((product) => product.slug),
|
|
53
|
+
walletHref,
|
|
54
|
+
buyTokensHref: walletHref ? `${walletHref.replace(/\/+$/, '')}/subscribe` : null,
|
|
55
|
+
stale,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Identity's `GET /api/v1/products`, as this person, by forwarding their two
|
|
60
|
+
* session cookies (no service key: Identity filters the list by audience for
|
|
61
|
+
* the viewer, B33-1725). Five seconds, then the last good list, then null.
|
|
62
|
+
*/
|
|
63
|
+
export async function readProducts(input, fetchImpl) {
|
|
64
|
+
const identity = input.config.identityUrl.replace(/\/+$/, '');
|
|
65
|
+
const supportEmail = input.config.supportEmail ?? DEFAULT_SUPPORT_EMAIL;
|
|
66
|
+
const account = actingAccount(input.claims);
|
|
67
|
+
const hasAccount = account !== undefined;
|
|
68
|
+
const fallback = () => {
|
|
69
|
+
const cached = lastGood.get(identity);
|
|
70
|
+
if (!cached)
|
|
71
|
+
return null;
|
|
72
|
+
const entitled = (slug) => slug === WALLET_SLUG ? hasAccount : hasAccount && (account?.products ?? []).includes(slug);
|
|
73
|
+
return shape(cached.map((entry) => ({ ...entry, entitled: entitled(entry.slug) })), hasAccount, supportEmail, true);
|
|
74
|
+
};
|
|
75
|
+
try {
|
|
76
|
+
const cookie = [`b33_sid=${input.cookies.sid}`, input.cookies.ctx ? `b33_ctx=${input.cookies.ctx}` : null]
|
|
77
|
+
.filter(Boolean)
|
|
78
|
+
.join('; ');
|
|
79
|
+
const res = await fetchImpl(`${identity}/api/v1/products`, {
|
|
80
|
+
headers: { cookie },
|
|
81
|
+
cache: 'no-store',
|
|
82
|
+
signal: AbortSignal.timeout(READ_TIMEOUT_MS),
|
|
83
|
+
});
|
|
84
|
+
if (!res.ok)
|
|
85
|
+
return fallback();
|
|
86
|
+
const entries = parseRegistry(await res.json().catch(() => null));
|
|
87
|
+
if (!entries)
|
|
88
|
+
return fallback();
|
|
89
|
+
lastGood.set(identity, entries.map(({ slug, name, url, live }) => ({ slug, name, url, live })));
|
|
90
|
+
return shape(entries, hasAccount, supportEmail, false);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
return fallback();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=products.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"products.js","sourceRoot":"","sources":["../src/products.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAA;AAGlD,MAAM,WAAW,GAAG,QAAQ,CAAA;AAC5B,MAAM,MAAM,GAAG,CAAC,CAAA;AAChB,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,CAAA;A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E,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,WAAW,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;aACvG,MAAM,CAAC,OAAO,CAAC;aACf,IAAI,CAAC,IAAI,CAAC,CAAA;QACb,MAAM,GAAG,GAAG,MAAM,SAAS,CAAC,GAAG,QAAQ,kBAAkB,EAAE;YACzD,OAAO,EAAE,EAAE,MAAM,EAAE;YACnB,KAAK,EAAE,UAAU;YACjB,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,eAAe,CAAC;SAC7C,CAAC,CAAA;QACF,IAAI,CAAC,GAAG,CAAC,EAAE;YAAE,OAAO,QAAQ,EAAE,CAAA;QAC9B,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC,CAAA;QACjE,IAAI,CAAC,OAAO;YAAE,OAAO,QAAQ,EAAE,CAAA;QAC/B,QAAQ,CAAC,GAAG,CAAC,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAA;QAC/F,OAAO,KAAK,CAAC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,KAAK,CAAC,CAAA;IACxD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,QAAQ,EAAE,CAAA;IACnB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ChromeConfig } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Identity's `GET /api/v1/profile`, with this app's service key acting for the
|
|
4
|
+
* person's session. Only `person.displayName` is read. Null on anything else,
|
|
5
|
+
* which tells the chrome to keep the `name` claim.
|
|
6
|
+
*/
|
|
7
|
+
export declare function readProfileName(input: {
|
|
8
|
+
sid: string;
|
|
9
|
+
config: ChromeConfig;
|
|
10
|
+
}, fetchImpl: typeof fetch): Promise<string | null>;
|
package/dist/profile.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { READ_TIMEOUT_MS } from './products.js';
|
|
2
|
+
/**
|
|
3
|
+
* Identity's `GET /api/v1/profile`, with this app's service key acting for the
|
|
4
|
+
* person's session. Only `person.displayName` is read. Null on anything else,
|
|
5
|
+
* which tells the chrome to keep the `name` claim.
|
|
6
|
+
*/
|
|
7
|
+
export async function readProfileName(input, fetchImpl) {
|
|
8
|
+
try {
|
|
9
|
+
const res = await fetchImpl(`${input.config.identityUrl.replace(/\/+$/, '')}/api/v1/profile`, {
|
|
10
|
+
headers: {
|
|
11
|
+
authorization: `Bearer ${input.config.identityKey}`,
|
|
12
|
+
'x-boost33-app': input.config.appId,
|
|
13
|
+
'x-boost33-actor-session': input.sid,
|
|
14
|
+
},
|
|
15
|
+
cache: 'no-store',
|
|
16
|
+
signal: AbortSignal.timeout(READ_TIMEOUT_MS),
|
|
17
|
+
});
|
|
18
|
+
if (!res.ok)
|
|
19
|
+
return null;
|
|
20
|
+
const body = (await res.json().catch(() => null));
|
|
21
|
+
const name = body?.person?.displayName;
|
|
22
|
+
return typeof name === 'string' && name.trim() ? name.trim() : null;
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=profile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"profile.js","sourceRoot":"","sources":["../src/profile.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAA;AAG/C;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CACnC,KAA4C,EAC5C,SAAuB;IAEvB,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,MAAM,SAAS,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,iBAAiB,EAAE;YAC5F,OAAO,EAAE;gBACP,aAAa,EAAE,UAAU,KAAK,CAAC,MAAM,CAAC,WAAW,EAAE;gBACnD,eAAe,EAAE,KAAK,CAAC,MAAM,CAAC,KAAK;gBACnC,yBAAyB,EAAE,KAAK,CAAC,GAAG;aACrC;YACD,KAAK,EAAE,UAAU;YACjB,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,eAAe,CAAC;SAC7C,CAAC,CAAA;QACF,IAAI,CAAC,GAAG,CAAC,EAAE;YAAE,OAAO,IAAI,CAAA;QACxB,MAAM,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,CAAkD,CAAA;QAClG,MAAM,IAAI,GAAG,IAAI,EAAE,MAAM,EAAE,WAAW,CAAA;QACtC,OAAO,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAA;IACrE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAA;IACb,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { NextResponse, type NextRequest } from 'next/server.js';
|
|
2
|
+
export declare const CTX_COOKIE = "b33_ctx";
|
|
3
|
+
export declare const SID_COOKIE = "b33_sid";
|
|
4
|
+
/** Refresh this long before `exp` (§5.1.4): `jwtVerify` allows no clock skew. */
|
|
5
|
+
export declare const REFRESH_MARGIN_SECONDS = 30;
|
|
6
|
+
/** The ceiling the contract sets on the call (§5.1.5). */
|
|
7
|
+
export declare const REFRESH_TIMEOUT_MS = 1500;
|
|
8
|
+
/**
|
|
9
|
+
* Whether `b33_ctx` should be refreshed before this request renders.
|
|
10
|
+
*
|
|
11
|
+
* Reads `exp` with an UNVERIFIED decode (§5.1.4). That is safe only because the
|
|
12
|
+
* answer decides nothing but whether to call identity, and only while the page
|
|
13
|
+
* runs a full `jwtVerify` behind the gate on every path: a forged early `exp`
|
|
14
|
+
* buys a refresh that succeeds or fails on `b33_sid` alone, and a forged late
|
|
15
|
+
* one skips the refresh and meets the page's verify. It returns a boolean and
|
|
16
|
+
* nothing else, so no claim from the decode can leave the gate.
|
|
17
|
+
*
|
|
18
|
+
* A missing or undecodable cookie reads as expired: with `b33_sid` present a
|
|
19
|
+
* refresh is exactly how a person gets a fresh one.
|
|
20
|
+
*
|
|
21
|
+
* Known and accepted (§5.1.4): this detects an expired token, not a forged or
|
|
22
|
+
* retired-key one. During a key rotation a token signed by a retired key with a
|
|
23
|
+
* live `exp` passes here, fails at the page, and takes the sign-in redirect for
|
|
24
|
+
* up to the token's remaining ten minutes.
|
|
25
|
+
*/
|
|
26
|
+
export declare function ctxLooksExpired(cookie: string | undefined, nowSeconds?: number): boolean;
|
|
27
|
+
export type RefreshOutcome =
|
|
28
|
+
/** Identity re-issued the session. Forward these verbatim (§5.1.3). */
|
|
29
|
+
{
|
|
30
|
+
kind: 'refreshed';
|
|
31
|
+
setCookies: string[];
|
|
32
|
+
}
|
|
33
|
+
/** 401: the session is genuinely dead. Clear both cookies, sign in (§5.1.7). */
|
|
34
|
+
| {
|
|
35
|
+
kind: 'dead';
|
|
36
|
+
}
|
|
37
|
+
/** Timeout, network error, 5xx or 429: identity is unwell, the session is
|
|
38
|
+
* probably fine. Fall through unchanged (§5.1.7). Never a 5xx of our own. */
|
|
39
|
+
| {
|
|
40
|
+
kind: 'unwell';
|
|
41
|
+
status: number | null;
|
|
42
|
+
};
|
|
43
|
+
export type RefreshOptions = {
|
|
44
|
+
identityUrl: string;
|
|
45
|
+
/** This request's own Cookie header, carrying `b33_sid`. */
|
|
46
|
+
cookieHeader: string;
|
|
47
|
+
/** This app's slug in identity's `APPS`, sent as `x-boost33-client` (§5.1.6). */
|
|
48
|
+
client: string;
|
|
49
|
+
fetchImpl?: typeof fetch;
|
|
50
|
+
timeoutMs?: number;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* One server-to-server call to identity's `/auth/refresh`, relaying this
|
|
54
|
+
* request's `b33_sid`. Not a redirect through identity (§5.1, opening).
|
|
55
|
+
*/
|
|
56
|
+
export declare function refreshSession({ identityUrl, cookieHeader, client, fetchImpl, timeoutMs, }: RefreshOptions): Promise<RefreshOutcome>;
|
|
57
|
+
/**
|
|
58
|
+
* The `name=value` pair at the front of a Set-Cookie header, for rewriting the
|
|
59
|
+
* inbound Cookie header (§5.1.2). Only the pair is read; the header itself is
|
|
60
|
+
* forwarded untouched (§5.1.3).
|
|
61
|
+
*/
|
|
62
|
+
export declare function cookiePair(setCookie: string): {
|
|
63
|
+
name: string;
|
|
64
|
+
value: string;
|
|
65
|
+
} | null;
|
|
66
|
+
export type GateOptions = {
|
|
67
|
+
identityUrl: string;
|
|
68
|
+
/** This app's slug in identity's `APPS`, sent as `x-boost33-client` (§5.1.6). */
|
|
69
|
+
client: string;
|
|
70
|
+
/**
|
|
71
|
+
* The app's own full verify of a `b33_ctx` (its `jwtVerify` against identity's
|
|
72
|
+
* JWKS, issuer and audience): true when it verifies. Only a yes or no crosses
|
|
73
|
+
* back; no claim leaves the gate.
|
|
74
|
+
*/
|
|
75
|
+
verify: (token: string) => Promise<boolean>;
|
|
76
|
+
/** For tests: replaces the call to identity. */
|
|
77
|
+
refresh?: (cookieHeader: string) => Promise<RefreshOutcome>;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* The request gate: step 2 of identity's contract §5, with §5.1's server-side
|
|
81
|
+
* refresh. Run it on navigations only (SESSION_GATE_MATCHER).
|
|
82
|
+
*
|
|
83
|
+
* `b33_sid` present, `b33_ctx` absent or past `exp - 30s`
|
|
84
|
+
* → POST identity's /auth/refresh, relaying `b33_sid` (one call, 1.5s cap)
|
|
85
|
+
* → refreshed: verify the new `b33_ctx` with the app's verifier, forward
|
|
86
|
+
* identity's Set-Cookie headers verbatim on the response, AND rewrite
|
|
87
|
+
* this request's Cookie header so the page rendering it reads the new
|
|
88
|
+
* token (§5.1.2; without that the page still redirects)
|
|
89
|
+
* → 401: the session is dead. Clear both cookies and sign in.
|
|
90
|
+
* → anything else (timeout, network, 5xx, 429), or a re-issued token that
|
|
91
|
+
* does not verify: fall through unchanged, forwarding nothing. The page's
|
|
92
|
+
* own full verify then fails on the expired token and takes the path it
|
|
93
|
+
* always has. Never a pass: nothing here lets a request through on a
|
|
94
|
+
* token that was not verified.
|
|
95
|
+
*
|
|
96
|
+
* Everything else (no `b33_sid`, a live `b33_ctx`) passes through untouched,
|
|
97
|
+
* and the page decides from claims it verifies in full. This gate never tells
|
|
98
|
+
* the page who is signed in, and never reads `design`.
|
|
99
|
+
*
|
|
100
|
+
* The gate itself never redirects on the unwell path, so it cannot loop. An
|
|
101
|
+
* app whose page redirects to identity when it finds no claims needs the
|
|
102
|
+
* one-bounce guard §5.1.7 asks for: see `markBounce` and `isRepeatBounce`.
|
|
103
|
+
*/
|
|
104
|
+
export declare function gateRequest(request: NextRequest, options: GateOptions): Promise<NextResponse>;
|
|
105
|
+
export declare const BOUNCE_PARAM = "b33_bounced";
|
|
106
|
+
/** The return URL with the bounce mark added. Pass it as identity's `return`. */
|
|
107
|
+
export declare function markBounce(returnTo: string | URL): string;
|
|
108
|
+
/** Whether this arrival is already the bounce back from identity. */
|
|
109
|
+
export declare function isRepeatBounce(url: string | URL): boolean;
|
|
110
|
+
/**
|
|
111
|
+
* The matcher the gate must run behind (§5.1.1): navigations only, meaning a
|
|
112
|
+
* document request that accepts HTML, or an in-app navigation, which Next sends
|
|
113
|
+
* as an RSC fetch marked `rsc: 1`. Never a prefetch, so hovering links cannot
|
|
114
|
+
* start a refresh storm; never Next's own assets or a file with an extension.
|
|
115
|
+
* API routes authenticate on their own (service keys), so `api/` is excluded.
|
|
116
|
+
*
|
|
117
|
+
* Copy this value into your proxy's `export const config` AS A LITERAL. Next
|
|
118
|
+
* reads the matcher at build time and ignores a value held in a variable or
|
|
119
|
+
* imported, silently, which would run the gate on every request.
|
|
120
|
+
*/
|
|
121
|
+
export declare const SESSION_GATE_MATCHER: readonly [{
|
|
122
|
+
readonly source: "/((?!_next/|api/|.*\\..*).*)";
|
|
123
|
+
readonly has: readonly [{
|
|
124
|
+
readonly type: "header";
|
|
125
|
+
readonly key: "accept";
|
|
126
|
+
readonly value: "(.*)text/html(.*)";
|
|
127
|
+
}];
|
|
128
|
+
readonly missing: readonly [{
|
|
129
|
+
readonly type: "header";
|
|
130
|
+
readonly key: "next-router-prefetch";
|
|
131
|
+
}, {
|
|
132
|
+
readonly type: "header";
|
|
133
|
+
readonly key: "purpose";
|
|
134
|
+
readonly value: "prefetch";
|
|
135
|
+
}];
|
|
136
|
+
}, {
|
|
137
|
+
readonly source: "/((?!_next/|api/|.*\\..*).*)";
|
|
138
|
+
readonly has: readonly [{
|
|
139
|
+
readonly type: "header";
|
|
140
|
+
readonly key: "rsc";
|
|
141
|
+
readonly value: "1";
|
|
142
|
+
}];
|
|
143
|
+
readonly missing: readonly [{
|
|
144
|
+
readonly type: "header";
|
|
145
|
+
readonly key: "next-router-prefetch";
|
|
146
|
+
}, {
|
|
147
|
+
readonly type: "header";
|
|
148
|
+
readonly key: "purpose";
|
|
149
|
+
readonly value: "prefetch";
|
|
150
|
+
}];
|
|
151
|
+
}];
|
|
152
|
+
/**
|
|
153
|
+
* For an app's tests: whether its literal matcher copy is SESSION_GATE_MATCHER,
|
|
154
|
+
* or narrows only its paths (Outbound scopes it to its Backdrop routes). A
|
|
155
|
+
* matcher that drops the prefetch exclusion or the `_next/`/`api/` exclusions
|
|
156
|
+
* fails.
|
|
157
|
+
*/
|
|
158
|
+
export declare function matcherIsNavigationOnly(matcher: unknown): boolean;
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
// No `server-only` here, unlike the package's root entry: this module runs in
|
|
2
|
+
// Next's request-interception layer (proxy/middleware), which is not React's
|
|
3
|
+
// server build, so `server-only` would throw on import there. It is still
|
|
4
|
+
// server code: it is only ever imported from proxy.ts.
|
|
5
|
+
import { decodeJwt } from 'jose';
|
|
6
|
+
import { NextResponse } from 'next/server.js';
|
|
7
|
+
/*
|
|
8
|
+
* The session gate: refreshing an expired `b33_ctx` before a page renders, as
|
|
9
|
+
* boost33-identity's docs/Boost33-Identity-Contract-v1.md §5.1 requires. That
|
|
10
|
+
* section is normative; its numbered requirements are cited below.
|
|
11
|
+
*
|
|
12
|
+
* Lifted from Passport's production gate (boost33-passport src/lib/ctx-refresh.ts
|
|
13
|
+
* and src/lib/session-gate.ts, B33-1751, itself a copy of Boost Voice's
|
|
14
|
+
* src/lib/identity/refresh.ts). The only changes are the parameters that were
|
|
15
|
+
* Passport's own: the caller slug, the verifier, and the fallback host. Every
|
|
16
|
+
* app calls this one gate, so the next fix lands once.
|
|
17
|
+
*
|
|
18
|
+
* Why every Backdrop app needs it: `b33_ctx` lives ten minutes. Without a
|
|
19
|
+
* refresh before the render, a page that reads the `design` claim from an
|
|
20
|
+
* expired cookie finds no claims and draws the classic design, so a person on
|
|
21
|
+
* the preview flips between designs every ten minutes or so.
|
|
22
|
+
*
|
|
23
|
+
* Use it from the request-interception layer, never a page (§5.1.1): a Server
|
|
24
|
+
* Component cannot set a cookie. In `src/proxy.ts` on Next 16 (or middleware):
|
|
25
|
+
*
|
|
26
|
+
* export function proxy(request: NextRequest) {
|
|
27
|
+
* return gateRequest(request, { identityUrl, client: 'boostscore', verify })
|
|
28
|
+
* }
|
|
29
|
+
*
|
|
30
|
+
* and copy SESSION_GATE_MATCHER's value into `export const config` there, as a
|
|
31
|
+
* literal: Next reads the matcher at build time and ignores an imported value.
|
|
32
|
+
* `matcherIsNavigationOnly` checks that copy in your tests.
|
|
33
|
+
*/
|
|
34
|
+
export const CTX_COOKIE = 'b33_ctx';
|
|
35
|
+
export const SID_COOKIE = 'b33_sid';
|
|
36
|
+
/** Refresh this long before `exp` (§5.1.4): `jwtVerify` allows no clock skew. */
|
|
37
|
+
export const REFRESH_MARGIN_SECONDS = 30;
|
|
38
|
+
/** The ceiling the contract sets on the call (§5.1.5). */
|
|
39
|
+
export const REFRESH_TIMEOUT_MS = 1_500;
|
|
40
|
+
/**
|
|
41
|
+
* Whether `b33_ctx` should be refreshed before this request renders.
|
|
42
|
+
*
|
|
43
|
+
* Reads `exp` with an UNVERIFIED decode (§5.1.4). That is safe only because the
|
|
44
|
+
* answer decides nothing but whether to call identity, and only while the page
|
|
45
|
+
* runs a full `jwtVerify` behind the gate on every path: a forged early `exp`
|
|
46
|
+
* buys a refresh that succeeds or fails on `b33_sid` alone, and a forged late
|
|
47
|
+
* one skips the refresh and meets the page's verify. It returns a boolean and
|
|
48
|
+
* nothing else, so no claim from the decode can leave the gate.
|
|
49
|
+
*
|
|
50
|
+
* A missing or undecodable cookie reads as expired: with `b33_sid` present a
|
|
51
|
+
* refresh is exactly how a person gets a fresh one.
|
|
52
|
+
*
|
|
53
|
+
* Known and accepted (§5.1.4): this detects an expired token, not a forged or
|
|
54
|
+
* retired-key one. During a key rotation a token signed by a retired key with a
|
|
55
|
+
* live `exp` passes here, fails at the page, and takes the sign-in redirect for
|
|
56
|
+
* up to the token's remaining ten minutes.
|
|
57
|
+
*/
|
|
58
|
+
export function ctxLooksExpired(cookie, nowSeconds = Date.now() / 1000) {
|
|
59
|
+
if (!cookie)
|
|
60
|
+
return true;
|
|
61
|
+
try {
|
|
62
|
+
const { exp } = decodeJwt(cookie);
|
|
63
|
+
if (typeof exp !== 'number')
|
|
64
|
+
return true;
|
|
65
|
+
return exp - REFRESH_MARGIN_SECONDS <= nowSeconds;
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* One server-to-server call to identity's `/auth/refresh`, relaying this
|
|
73
|
+
* request's `b33_sid`. Not a redirect through identity (§5.1, opening).
|
|
74
|
+
*/
|
|
75
|
+
export async function refreshSession({ identityUrl, cookieHeader, client, fetchImpl = fetch, timeoutMs = REFRESH_TIMEOUT_MS, }) {
|
|
76
|
+
let res;
|
|
77
|
+
try {
|
|
78
|
+
res = await fetchImpl(`${identityUrl.replace(/\/+$/, '')}/api/v1/auth/refresh`, {
|
|
79
|
+
method: 'POST',
|
|
80
|
+
headers: {
|
|
81
|
+
cookie: cookieHeader,
|
|
82
|
+
// §5.1.6: the caller, unproven, so NOT x-boost33-app, which identity
|
|
83
|
+
// checks against a service key this request does not carry.
|
|
84
|
+
'x-boost33-client': client,
|
|
85
|
+
},
|
|
86
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
87
|
+
cache: 'no-store',
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return { kind: 'unwell', status: null };
|
|
92
|
+
}
|
|
93
|
+
// Only a 401 means the session is dead. Collapsing anything else into it would
|
|
94
|
+
// sign out everybody during an identity blip, silently (§5.1.7).
|
|
95
|
+
if (res.status === 401)
|
|
96
|
+
return { kind: 'dead' };
|
|
97
|
+
if (!res.ok)
|
|
98
|
+
return { kind: 'unwell', status: res.status };
|
|
99
|
+
// §5.1.3: both cookies come back (`b33_sid` slides forward too). Forward all.
|
|
100
|
+
return { kind: 'refreshed', setCookies: res.headers.getSetCookie() };
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* The `name=value` pair at the front of a Set-Cookie header, for rewriting the
|
|
104
|
+
* inbound Cookie header (§5.1.2). Only the pair is read; the header itself is
|
|
105
|
+
* forwarded untouched (§5.1.3).
|
|
106
|
+
*/
|
|
107
|
+
export function cookiePair(setCookie) {
|
|
108
|
+
const pair = setCookie.split(';', 1)[0] ?? '';
|
|
109
|
+
const eq = pair.indexOf('=');
|
|
110
|
+
if (eq <= 0)
|
|
111
|
+
return null;
|
|
112
|
+
return { name: pair.slice(0, eq).trim(), value: pair.slice(eq + 1).trim() };
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The request gate: step 2 of identity's contract §5, with §5.1's server-side
|
|
116
|
+
* refresh. Run it on navigations only (SESSION_GATE_MATCHER).
|
|
117
|
+
*
|
|
118
|
+
* `b33_sid` present, `b33_ctx` absent or past `exp - 30s`
|
|
119
|
+
* → POST identity's /auth/refresh, relaying `b33_sid` (one call, 1.5s cap)
|
|
120
|
+
* → refreshed: verify the new `b33_ctx` with the app's verifier, forward
|
|
121
|
+
* identity's Set-Cookie headers verbatim on the response, AND rewrite
|
|
122
|
+
* this request's Cookie header so the page rendering it reads the new
|
|
123
|
+
* token (§5.1.2; without that the page still redirects)
|
|
124
|
+
* → 401: the session is dead. Clear both cookies and sign in.
|
|
125
|
+
* → anything else (timeout, network, 5xx, 429), or a re-issued token that
|
|
126
|
+
* does not verify: fall through unchanged, forwarding nothing. The page's
|
|
127
|
+
* own full verify then fails on the expired token and takes the path it
|
|
128
|
+
* always has. Never a pass: nothing here lets a request through on a
|
|
129
|
+
* token that was not verified.
|
|
130
|
+
*
|
|
131
|
+
* Everything else (no `b33_sid`, a live `b33_ctx`) passes through untouched,
|
|
132
|
+
* and the page decides from claims it verifies in full. This gate never tells
|
|
133
|
+
* the page who is signed in, and never reads `design`.
|
|
134
|
+
*
|
|
135
|
+
* The gate itself never redirects on the unwell path, so it cannot loop. An
|
|
136
|
+
* app whose page redirects to identity when it finds no claims needs the
|
|
137
|
+
* one-bounce guard §5.1.7 asks for: see `markBounce` and `isRepeatBounce`.
|
|
138
|
+
*/
|
|
139
|
+
export async function gateRequest(request, options) {
|
|
140
|
+
if (!request.cookies.get(SID_COOKIE)?.value)
|
|
141
|
+
return NextResponse.next();
|
|
142
|
+
if (!ctxLooksExpired(request.cookies.get(CTX_COOKIE)?.value))
|
|
143
|
+
return NextResponse.next();
|
|
144
|
+
const refresh = options.refresh ??
|
|
145
|
+
((cookieHeader) => refreshSession({ identityUrl: options.identityUrl, cookieHeader, client: options.client }));
|
|
146
|
+
const outcome = await refresh(request.headers.get('cookie') ?? '');
|
|
147
|
+
if (outcome.kind === 'dead') {
|
|
148
|
+
// §5.1.7: no retry; identity would only bounce them back.
|
|
149
|
+
const signin = new URL(`${options.identityUrl.replace(/\/+$/, '')}/signin`);
|
|
150
|
+
signin.searchParams.set('return', returnUrl(request));
|
|
151
|
+
const response = NextResponse.redirect(signin);
|
|
152
|
+
for (const name of [SID_COOKIE, CTX_COOKIE])
|
|
153
|
+
response.headers.append('set-cookie', expiredCookie(name, request));
|
|
154
|
+
return response;
|
|
155
|
+
}
|
|
156
|
+
if (outcome.kind === 'unwell')
|
|
157
|
+
return NextResponse.next();
|
|
158
|
+
// The re-issued token is verified here, not only by the page, so a refresh
|
|
159
|
+
// whose token would not verify (wrong key, keys unreachable) changes nothing:
|
|
160
|
+
// no cookie is forwarded, and the page takes the path it took before.
|
|
161
|
+
const jar = new Map(request.cookies.getAll().map((c) => [c.name, c.value]));
|
|
162
|
+
for (const raw of outcome.setCookies) {
|
|
163
|
+
const pair = cookiePair(raw);
|
|
164
|
+
if (pair)
|
|
165
|
+
jar.set(pair.name, pair.value);
|
|
166
|
+
}
|
|
167
|
+
const fresh = jar.get(CTX_COOKIE);
|
|
168
|
+
if (!fresh || !(await options.verify(fresh).catch(() => false)))
|
|
169
|
+
return NextResponse.next();
|
|
170
|
+
const headers = new Headers(request.headers);
|
|
171
|
+
headers.set('cookie', [...jar].map(([name, value]) => `${name}=${value}`).join('; '));
|
|
172
|
+
const response = NextResponse.next({ request: { headers } });
|
|
173
|
+
// §5.1.3: verbatim, never rebuilt. Both cookies: `b33_sid` slides forward too.
|
|
174
|
+
for (const raw of outcome.setCookies)
|
|
175
|
+
response.headers.append('set-cookie', raw);
|
|
176
|
+
return response;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Where identity sends them back to after sign-in: the forwarded host first, so
|
|
180
|
+
* it is the address the person used rather than the one the platform routed to.
|
|
181
|
+
*/
|
|
182
|
+
function returnUrl(request) {
|
|
183
|
+
const { pathname, search } = request.nextUrl;
|
|
184
|
+
const host = request.headers.get('x-forwarded-host') ?? request.headers.get('host');
|
|
185
|
+
if (!host)
|
|
186
|
+
return `${request.nextUrl.origin}${pathname}${search}`;
|
|
187
|
+
const local = host.startsWith('localhost') || host.startsWith('127.0.0.1');
|
|
188
|
+
const proto = request.headers.get('x-forwarded-proto') ?? (local ? 'http' : 'https');
|
|
189
|
+
return `${proto}://${host}${pathname}${search}`;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Expire one of identity's cookies. They are set on `.boost33.com`, so a
|
|
193
|
+
* host-only expiry would leave the shared cookie alive; the domain is used only
|
|
194
|
+
* when this host is under it, so local development still clears.
|
|
195
|
+
*/
|
|
196
|
+
function expiredCookie(name, request) {
|
|
197
|
+
const host = (request.headers.get('x-forwarded-host') ?? request.nextUrl.hostname).split(':')[0] ?? '';
|
|
198
|
+
const shared = host === 'boost33.com' || host.endsWith('.boost33.com');
|
|
199
|
+
const domain = shared ? '; Domain=.boost33.com' : '';
|
|
200
|
+
const secure = shared || request.nextUrl.protocol === 'https:' ? '; Secure' : '';
|
|
201
|
+
return `${name}=; Path=/${domain}; Max-Age=0; HttpOnly; SameSite=Lax${secure}`;
|
|
202
|
+
}
|
|
203
|
+
/* ── The one-bounce guard (§5.1.7) ─────────────────────────────────────────
|
|
204
|
+
*
|
|
205
|
+
* For an app's own redirect to identity when a page finds no claims, which is
|
|
206
|
+
* the path an unwell identity takes. Mark the return URL on the way out; on the
|
|
207
|
+
* way back, a page that still finds no claims and sees the mark sends the
|
|
208
|
+
* person to a terminal page instead of to identity again. Without it, an
|
|
209
|
+
* unwell identity is an infinite redirect rather than a slow page.
|
|
210
|
+
*/
|
|
211
|
+
export const BOUNCE_PARAM = 'b33_bounced';
|
|
212
|
+
/** The return URL with the bounce mark added. Pass it as identity's `return`. */
|
|
213
|
+
export function markBounce(returnTo) {
|
|
214
|
+
const url = new URL(returnTo);
|
|
215
|
+
url.searchParams.set(BOUNCE_PARAM, '1');
|
|
216
|
+
return url.toString();
|
|
217
|
+
}
|
|
218
|
+
/** Whether this arrival is already the bounce back from identity. */
|
|
219
|
+
export function isRepeatBounce(url) {
|
|
220
|
+
return new URL(url).searchParams.get(BOUNCE_PARAM) === '1';
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* The matcher the gate must run behind (§5.1.1): navigations only, meaning a
|
|
224
|
+
* document request that accepts HTML, or an in-app navigation, which Next sends
|
|
225
|
+
* as an RSC fetch marked `rsc: 1`. Never a prefetch, so hovering links cannot
|
|
226
|
+
* start a refresh storm; never Next's own assets or a file with an extension.
|
|
227
|
+
* API routes authenticate on their own (service keys), so `api/` is excluded.
|
|
228
|
+
*
|
|
229
|
+
* Copy this value into your proxy's `export const config` AS A LITERAL. Next
|
|
230
|
+
* reads the matcher at build time and ignores a value held in a variable or
|
|
231
|
+
* imported, silently, which would run the gate on every request.
|
|
232
|
+
*/
|
|
233
|
+
export const SESSION_GATE_MATCHER = [
|
|
234
|
+
{
|
|
235
|
+
source: '/((?!_next/|api/|.*\\..*).*)',
|
|
236
|
+
has: [{ type: 'header', key: 'accept', value: '(.*)text/html(.*)' }],
|
|
237
|
+
missing: [
|
|
238
|
+
{ type: 'header', key: 'next-router-prefetch' },
|
|
239
|
+
{ type: 'header', key: 'purpose', value: 'prefetch' },
|
|
240
|
+
],
|
|
241
|
+
},
|
|
242
|
+
{
|
|
243
|
+
source: '/((?!_next/|api/|.*\\..*).*)',
|
|
244
|
+
has: [{ type: 'header', key: 'rsc', value: '1' }],
|
|
245
|
+
missing: [
|
|
246
|
+
{ type: 'header', key: 'next-router-prefetch' },
|
|
247
|
+
{ type: 'header', key: 'purpose', value: 'prefetch' },
|
|
248
|
+
],
|
|
249
|
+
},
|
|
250
|
+
];
|
|
251
|
+
/**
|
|
252
|
+
* For an app's tests: whether its literal matcher copy is SESSION_GATE_MATCHER,
|
|
253
|
+
* or narrows only its paths (Outbound scopes it to its Backdrop routes). A
|
|
254
|
+
* matcher that drops the prefetch exclusion or the `_next/`/`api/` exclusions
|
|
255
|
+
* fails.
|
|
256
|
+
*/
|
|
257
|
+
export function matcherIsNavigationOnly(matcher) {
|
|
258
|
+
if (!Array.isArray(matcher) || matcher.length === 0)
|
|
259
|
+
return false;
|
|
260
|
+
return matcher.every((entry) => {
|
|
261
|
+
if (!entry || typeof entry !== 'object')
|
|
262
|
+
return false;
|
|
263
|
+
const { source, has, missing } = entry;
|
|
264
|
+
if (typeof source !== 'string')
|
|
265
|
+
return false;
|
|
266
|
+
const scoped = !source.startsWith('/((?!');
|
|
267
|
+
if (!scoped && !(source.includes('_next/') && source.includes('api/')))
|
|
268
|
+
return false;
|
|
269
|
+
const json = JSON.stringify;
|
|
270
|
+
const navigation = SESSION_GATE_MATCHER.some((ref) => json(has) === json(ref.has));
|
|
271
|
+
const noPrefetch = json(missing) === json(SESSION_GATE_MATCHER[0].missing);
|
|
272
|
+
return navigation && noPrefetch;
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
//# sourceMappingURL=session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,6EAA6E;AAC7E,0EAA0E;AAC1E,uDAAuD;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,MAAM,CAAA;AAChC,OAAO,EAAE,YAAY,EAAoB,MAAM,gBAAgB,CAAA;AAE/D;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH,MAAM,CAAC,MAAM,UAAU,GAAG,SAAS,CAAA;AACnC,MAAM,CAAC,MAAM,UAAU,GAAG,SAAS,CAAA;AAEnC,iFAAiF;AACjF,MAAM,CAAC,MAAM,sBAAsB,GAAG,EAAE,CAAA;AAExC,0DAA0D;AAC1D,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,CAAA;AAEvC;;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ACrD,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,KAA+D,CAAA;QAChG,IAAI,OAAO,MAAM,KAAK,QAAQ;YAAE,OAAO,KAAK,CAAA;QAC5C,MAAM,MAAM,GAAG,CAAC,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,CAAA;QAC1C,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC;YAAE,OAAO,KAAK,CAAA;QACpF,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAA;QAC3B,MAAM,UAAU,GAAG,oBAAoB,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAA;QAClF,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,IAAI,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAA;QAC1E,OAAO,UAAU,IAAI,UAAU,CAAA;IACjC,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
package/dist/tokens.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type TokenSplitResult } from '@boost33/ledger-client';
|
|
2
|
+
import type { ChromeClaims, ChromeConfig, ChromeTokens } from './types.js';
|
|
3
|
+
export type TokenSplitReader = (walletId: string) => Promise<TokenSplitResult>;
|
|
4
|
+
/** The real reader: `@boost33/ledger-client`'s `tokenSplit()`, built per call. */
|
|
5
|
+
export declare function ledgerReader(config: ChromeConfig): TokenSplitReader;
|
|
6
|
+
/**
|
|
7
|
+
* The ledger's split in the chip's terms (B33-1726, from the Hub's toTokenChip):
|
|
8
|
+
* known gives both halves; overdrawn and negative_bucket give the true total
|
|
9
|
+
* and no split; unavailable, account_not_found, a throw, or a figure that is
|
|
10
|
+
* not a safe integer give unknown. No account or no wallet gives none.
|
|
11
|
+
* `nextGrantAt` stays an ISO string: formatting a date is the app's, in its
|
|
12
|
+
* language.
|
|
13
|
+
*/
|
|
14
|
+
export declare function readTokens(claims: ChromeClaims, read: TokenSplitReader): Promise<ChromeTokens>;
|
package/dist/tokens.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { createLedgerClient } from '@boost33/ledger-client';
|
|
2
|
+
import { actingAccount } from './person.js';
|
|
3
|
+
import { READ_TIMEOUT_MS } from './products.js';
|
|
4
|
+
/** The real reader: `@boost33/ledger-client`'s `tokenSplit()`, built per call. */
|
|
5
|
+
export function ledgerReader(config) {
|
|
6
|
+
return (walletId) => createLedgerClient({
|
|
7
|
+
// The client's URL includes `/api/v1`; the config holds the origin.
|
|
8
|
+
url: `${config.ledger.url.replace(/\/+$/, '')}/api/v1`,
|
|
9
|
+
key: config.ledger.key,
|
|
10
|
+
app: config.ledger.app,
|
|
11
|
+
readTimeoutMs: READ_TIMEOUT_MS,
|
|
12
|
+
}).tokenSplit(walletId);
|
|
13
|
+
}
|
|
14
|
+
/** An integer string the chip can draw exactly, or null. */
|
|
15
|
+
function figure(value) {
|
|
16
|
+
const n = Number(value);
|
|
17
|
+
return Number.isSafeInteger(n) ? n : null;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The ledger's split in the chip's terms (B33-1726, from the Hub's toTokenChip):
|
|
21
|
+
* known gives both halves; overdrawn and negative_bucket give the true total
|
|
22
|
+
* and no split; unavailable, account_not_found, a throw, or a figure that is
|
|
23
|
+
* not a safe integer give unknown. No account or no wallet gives none.
|
|
24
|
+
* `nextGrantAt` stays an ISO string: formatting a date is the app's, in its
|
|
25
|
+
* language.
|
|
26
|
+
*/
|
|
27
|
+
export async function readTokens(claims, read) {
|
|
28
|
+
const walletId = actingAccount(claims)?.walletId;
|
|
29
|
+
if (!walletId)
|
|
30
|
+
return { kind: 'none' };
|
|
31
|
+
let result;
|
|
32
|
+
try {
|
|
33
|
+
result = await read(walletId);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return { kind: 'unknown' };
|
|
37
|
+
}
|
|
38
|
+
if (!result.ok)
|
|
39
|
+
return { kind: 'unknown' };
|
|
40
|
+
const { split } = result;
|
|
41
|
+
if (split.kind === 'unavailable')
|
|
42
|
+
return { kind: 'unknown' };
|
|
43
|
+
const granted = figure(split.granted);
|
|
44
|
+
const purchased = figure(split.purchased);
|
|
45
|
+
if (granted === null || purchased === null || !Number.isSafeInteger(granted + purchased))
|
|
46
|
+
return { kind: 'unknown' };
|
|
47
|
+
if (split.kind !== 'known')
|
|
48
|
+
return { kind: 'totalOnly', total: granted + purchased };
|
|
49
|
+
const cycle = result.grantTokensPerCycle === null ? null : figure(result.grantTokensPerCycle);
|
|
50
|
+
const next = result.nextGrantAt !== null && !Number.isNaN(Date.parse(result.nextGrantAt)) ? result.nextGrantAt : null;
|
|
51
|
+
return {
|
|
52
|
+
kind: 'known',
|
|
53
|
+
total: granted + purchased,
|
|
54
|
+
granted,
|
|
55
|
+
purchased,
|
|
56
|
+
grantPerCycle: cycle !== null && cycle > 0 ? cycle : null,
|
|
57
|
+
nextGrantAt: next,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=tokens.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tokens.js","sourceRoot":"","sources":["../src/tokens.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAyB,MAAM,wBAAwB,CAAA;AAClF,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAA;AAK/C,kFAAkF;AAClF,MAAM,UAAU,YAAY,CAAC,MAAoB;IAC/C,OAAO,CAAC,QAAQ,EAAE,EAAE,CAClB,kBAAkB,CAAC;QACjB,oEAAoE;QACpE,GAAG,EAAE,GAAG,MAAM,CAAC,MAAM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zC,IAAI,OAAO,KAAK,IAAI,IAAI,SAAS,KAAK,IAAI,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,OAAO,GAAG,SAAS,CAAC;QAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,CAAA;IACpH,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO;QAAE,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,KAAK,EAAE,OAAO,GAAG,SAAS,EAAE,CAAA;IACpF,MAAM,KAAK,GAAG,MAAM,CAAC,mBAAmB,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,mBAAmB,CAAC,CAAA;IAC7F,MAAM,IAAI,GAAG,MAAM,CAAC,WAAW,KAAK,IAAI,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAA;IACrH,OAAO;QACL,IAAI,EAAE,OAAO;QACb,KAAK,EAAE,OAAO,GAAG,SAAS;QAC1B,OAAO;QACP,SAAS;QACT,aAAa,EAAE,KAAK,KAAK,IAAI,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI;QACzD,WAAW,EAAE,IAAI;KAClB,CAAA;AACH,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The verified session claims, as each app already reads them from `b33_ctx`.
|
|
3
|
+
*
|
|
4
|
+
* Structural on purpose: only the fields read here are named, so an app passes
|
|
5
|
+
* its own claims type unchanged. `design` is deliberately absent: the package
|
|
6
|
+
* never decides which design a person sees. Each app's `isBackdropPreview` is
|
|
7
|
+
* the only reader of that claim, and the chrome is only rendered after it has
|
|
8
|
+
* said "backdrop".
|
|
9
|
+
*/
|
|
10
|
+
export type ChromeClaims = {
|
|
11
|
+
email: string;
|
|
12
|
+
name: string | null;
|
|
13
|
+
orgName?: string | null;
|
|
14
|
+
act: string | null;
|
|
15
|
+
accounts: ReadonlyArray<{
|
|
16
|
+
id: string;
|
|
17
|
+
role: 'admin' | 'member';
|
|
18
|
+
products: readonly string[];
|
|
19
|
+
walletId?: string | null;
|
|
20
|
+
}>;
|
|
21
|
+
};
|
|
22
|
+
export type ChromeConfig = {
|
|
23
|
+
/** Identity's origin, e.g. `https://id.boost33.com`. No trailing path. */
|
|
24
|
+
identityUrl: string;
|
|
25
|
+
/** The Hub's origin, e.g. `https://app.boost33.com`. Account rows link here. */
|
|
26
|
+
hubUrl: string;
|
|
27
|
+
/** Defaults to `https://docs.boost33.com`. */
|
|
28
|
+
docsUrl?: string;
|
|
29
|
+
/** Defaults to `support@boost33.com`. */
|
|
30
|
+
supportEmail?: string;
|
|
31
|
+
/** This app's Identity service key, for the profile read. Server env only. */
|
|
32
|
+
identityKey: string;
|
|
33
|
+
/** This app's slug as Identity knows it, sent as `x-boost33-app`. */
|
|
34
|
+
appId: string;
|
|
35
|
+
ledger: {
|
|
36
|
+
/** The ledger's origin. `/api/v1` is appended here. */
|
|
37
|
+
url: string;
|
|
38
|
+
key: string;
|
|
39
|
+
app: string;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export type ChromePerson = {
|
|
43
|
+
/** The `name` claim. `profileName` may replace it once it resolves. */
|
|
44
|
+
name: string | null;
|
|
45
|
+
email: string;
|
|
46
|
+
/** One or two letters, from the name or else the email. Never empty. */
|
|
47
|
+
initials: string;
|
|
48
|
+
org: {
|
|
49
|
+
name: string;
|
|
50
|
+
} | null;
|
|
51
|
+
role: 'admin' | 'member' | null;
|
|
52
|
+
/** How many other accounts this person can switch to. */
|
|
53
|
+
otherAccounts: number;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* One product as the chrome may show it.
|
|
57
|
+
*
|
|
58
|
+
* - `open`: live, and this account has it. `href` is the product.
|
|
59
|
+
* - `soon`: not live yet, or this person has no account. `href` is null.
|
|
60
|
+
* - `ask`: live, but this account does not have it. `href` is a mailto to
|
|
61
|
+
* support, with no subject: a subject would be words, and words are the app's.
|
|
62
|
+
*/
|
|
63
|
+
export type ChromeProduct = {
|
|
64
|
+
slug: string;
|
|
65
|
+
name: string;
|
|
66
|
+
/** `name` without a leading "Boost " ("Boost Score" → "Score"), for the rail. */
|
|
67
|
+
shortName: string;
|
|
68
|
+
state: 'open' | 'soon' | 'ask';
|
|
69
|
+
href: string | null;
|
|
70
|
+
};
|
|
71
|
+
export type ChromeProducts = {
|
|
72
|
+
/** Every product except the Wallet, in the order Identity returned them. */
|
|
73
|
+
all: ChromeProduct[];
|
|
74
|
+
/**
|
|
75
|
+
* Slugs pinned on the rail: the first four of `all`. Identity's order, never a
|
|
76
|
+
* list baked in here (CLAUDE.md in @boost33/ui: "the product list is served,
|
|
77
|
+
* never hardcoded"); Identity serving an explicit order is a ticket of its own.
|
|
78
|
+
*/
|
|
79
|
+
pinned: string[];
|
|
80
|
+
/** The Wallet, when it is live. Null hides it from the rail. */
|
|
81
|
+
walletHref: string | null;
|
|
82
|
+
/** Where the tokens popover's buy action goes: the Wallet's `/subscribe`. Null when the Wallet is not live. */
|
|
83
|
+
buyTokensHref: string | null;
|
|
84
|
+
/** True when Identity was unreachable and this is the last list that read cleanly. */
|
|
85
|
+
stale: boolean;
|
|
86
|
+
};
|
|
87
|
+
export type ChromeTokens =
|
|
88
|
+
/** Both halves of the balance. `grantPerCycle` and `nextGrantAt` are null when the ledger has none. */
|
|
89
|
+
{
|
|
90
|
+
kind: 'known';
|
|
91
|
+
total: number;
|
|
92
|
+
granted: number;
|
|
93
|
+
purchased: number;
|
|
94
|
+
grantPerCycle: number | null;
|
|
95
|
+
nextGrantAt: string | null;
|
|
96
|
+
}
|
|
97
|
+
/** Overdrawn, or a negative bucket: the true total, and no split to draw. */
|
|
98
|
+
| {
|
|
99
|
+
kind: 'totalOnly';
|
|
100
|
+
total: number;
|
|
101
|
+
}
|
|
102
|
+
/** The ledger could not say: a disabled chip with no popover. */
|
|
103
|
+
| {
|
|
104
|
+
kind: 'unknown';
|
|
105
|
+
}
|
|
106
|
+
/** No account, or an account with no wallet: no chip at all. */
|
|
107
|
+
| {
|
|
108
|
+
kind: 'none';
|
|
109
|
+
};
|
|
110
|
+
/** Every link the chrome draws that does not depend on a read. All absolute. */
|
|
111
|
+
export type ChromeLinks = {
|
|
112
|
+
home: string;
|
|
113
|
+
settings: string;
|
|
114
|
+
profile: string;
|
|
115
|
+
company: string;
|
|
116
|
+
team: string;
|
|
117
|
+
billing: string;
|
|
118
|
+
notifications: string;
|
|
119
|
+
security: string;
|
|
120
|
+
chooseAccount: string;
|
|
121
|
+
signOut: string;
|
|
122
|
+
docs: string;
|
|
123
|
+
support: string;
|
|
124
|
+
};
|
|
125
|
+
export type BackdropChromeData = {
|
|
126
|
+
/** Drawn at once, from the claims. */
|
|
127
|
+
person: ChromePerson;
|
|
128
|
+
/** Identity's display name. Null means keep `person.name`. Never rejects. */
|
|
129
|
+
profileName: Promise<string | null>;
|
|
130
|
+
/** Null when Identity is unreachable and no list has read cleanly yet. Never rejects. */
|
|
131
|
+
products: Promise<ChromeProducts | null>;
|
|
132
|
+
/** Never rejects; failure is `{ kind: 'unknown' }`. */
|
|
133
|
+
tokens: Promise<ChromeTokens>;
|
|
134
|
+
links: ChromeLinks;
|
|
135
|
+
};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The shapes the Backdrop chrome in @boost33/ui draws from (B33-1726).
|
|
3
|
+
*
|
|
4
|
+
* `BackdropStandardChrome` takes these as props of the same names, so an app
|
|
5
|
+
* spreads the result of `getBackdropChromeData()` straight onto it. Every
|
|
6
|
+
* field is plain, serialisable data: no words (an app passes its own labels to
|
|
7
|
+
* the chrome), no callbacks, no components. The two packages are agreed on
|
|
8
|
+
* these shapes; changing one is a change to both.
|
|
9
|
+
*/
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@boost33/shell-data",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Server-only reads that hand the Backdrop chrome in @boost33/ui exactly the data it draws.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=22"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist",
|
|
15
|
+
"CHANGELOG.md"
|
|
16
|
+
],
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"import": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./session": {
|
|
24
|
+
"types": "./dist/session.d.ts",
|
|
25
|
+
"import": "./dist/session.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"typecheck": "tsc --noEmit",
|
|
30
|
+
"test": "vitest run",
|
|
31
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
32
|
+
"prepublishOnly": "npm run typecheck && npm run test && npm run build"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@boost33/ledger-client": "2.3.0",
|
|
36
|
+
"server-only": "0.0.1"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"jose": ">=6.0.0",
|
|
40
|
+
"next": ">=15.0.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependenciesMeta": {
|
|
43
|
+
"jose": {
|
|
44
|
+
"optional": true
|
|
45
|
+
},
|
|
46
|
+
"next": {
|
|
47
|
+
"optional": true
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/node": "^24.0.0",
|
|
52
|
+
"jose": "^6.2.17",
|
|
53
|
+
"next": "^16.4.6",
|
|
54
|
+
"react": "^19.3.1",
|
|
55
|
+
"react-dom": "^19.3.1",
|
|
56
|
+
"typescript": "^5.7.2",
|
|
57
|
+
"vitest": "^4.1.10"
|
|
58
|
+
}
|
|
59
|
+
}
|