@baukit/suite-client 0.10.32 → 0.11.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/README.md +42 -0
- package/dist/cjs/entitlements.d.ts +40 -0
- package/dist/cjs/entitlements.d.ts.map +1 -0
- package/dist/cjs/entitlements.js +129 -0
- package/dist/cjs/entitlements.js.map +1 -0
- package/dist/entitlements.d.ts +40 -0
- package/dist/entitlements.d.ts.map +1 -0
- package/dist/entitlements.js +124 -0
- package/dist/entitlements.js.map +1 -0
- package/dist/urls.d.ts +19 -0
- package/dist/urls.d.ts.map +1 -1
- package/dist/urls.js +44 -0
- package/dist/urls.js.map +1 -1
- package/package.json +17 -4
- package/src/entitlements.test.ts +120 -0
- package/src/entitlements.ts +168 -0
- package/src/urls.test.ts +93 -0
- package/src/urls.ts +71 -0
package/README.md
CHANGED
|
@@ -64,6 +64,34 @@ Register `suiteMessages` with `@baukit/localization-core`. Products replace
|
|
|
64
64
|
`{product}` and render labels for their own event types. Message key and interpolation
|
|
65
65
|
parity is checked across `en`, `de` and `es`.
|
|
66
66
|
|
|
67
|
+
## Hosted authorize URLs
|
|
68
|
+
|
|
69
|
+
Use `validateSuiteAuthorizePeer(peer, options)` when loading peer configuration.
|
|
70
|
+
`peer` has `webUrl` and optional `authorizeUrl`. The latter is the full hosted
|
|
71
|
+
page URL, including the authorizing product ID. HTTPS is required; enable
|
|
72
|
+
`options.allowLoopback` for loopback HTTP development. Credentials, queries and
|
|
73
|
+
fragments are rejected.
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { suiteAuthorizeUrl, validateSuiteAuthorizePeer } from '@baukit/suite-client';
|
|
77
|
+
|
|
78
|
+
const peer = {
|
|
79
|
+
webUrl: 'https://beta.example',
|
|
80
|
+
authorizeUrl: 'https://account.example/suite/authorize/beta',
|
|
81
|
+
};
|
|
82
|
+
validateSuiteAuthorizePeer(peer);
|
|
83
|
+
const url = suiteAuthorizeUrl(peer, {
|
|
84
|
+
client: 'alpha', state, codeChallenge, hint, hintDomain,
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
The configured path is preserved, including a trailing slash. The query uses
|
|
89
|
+
`client`, `state`, `code_challenge`, optional `hint` and `hint_domain`.
|
|
90
|
+
Without `authorizeUrl`, the endpoint stays `${webUrl}/suite/authorize`.
|
|
91
|
+
`SuiteClient.start` already returns the server's configured `authorizeUrl`;
|
|
92
|
+
`SuiteSession` uses it directly. Products building URLs themselves can use the
|
|
93
|
+
helper. Keep the local authorize page for deployments without hosted consent.
|
|
94
|
+
|
|
67
95
|
## Peer metadata at config time
|
|
68
96
|
|
|
69
97
|
The dependency-free `@baukit/suite-client/peers` subpath has ESM and CommonJS
|
|
@@ -87,3 +115,17 @@ config plugin that preserves existing entries.
|
|
|
87
115
|
|
|
88
116
|
See [suite adoption](../../../docs/platform/suite-events.md) for server wiring
|
|
89
117
|
and the protocol contract. This package adds no CLI template capability.
|
|
118
|
+
|
|
119
|
+
## Offline entitlement tickets
|
|
120
|
+
|
|
121
|
+
Import `verifyTicket` and `TicketVerificationError` from
|
|
122
|
+
`@baukit/suite-client/entitlements`. Pass the compact ticket, trusted `TicketJwks`
|
|
123
|
+
and `{ issuer, audience, now }`, with `now` in Unix seconds. The result is
|
|
124
|
+
`TicketClaims`; compare its `sub` with the current suite subject before granting
|
|
125
|
+
access. Products own public-key refresh and ticket storage.
|
|
126
|
+
|
|
127
|
+
Verification uses pure JavaScript Ed25519 on web and Hermes, without WebCrypto or
|
|
128
|
+
Node globals. It enforces the hub's strict expiry, seven-day maximum lifetime,
|
|
129
|
+
60-second issue-time allowance and exact claims schema. See
|
|
130
|
+
[platform adoption](../../../docs/platform/platform-receiver.md) for server setup
|
|
131
|
+
and stable error codes.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export type TicketErrorCode = 'ticket_invalid' | 'ticket_expired' | 'ticket_wrong_audience';
|
|
2
|
+
export declare class TicketVerificationError extends Error {
|
|
3
|
+
readonly code: TicketErrorCode;
|
|
4
|
+
constructor(code: TicketErrorCode);
|
|
5
|
+
}
|
|
6
|
+
export interface TicketClaims {
|
|
7
|
+
readonly iss: string;
|
|
8
|
+
readonly sub: string;
|
|
9
|
+
readonly aud: string;
|
|
10
|
+
readonly tier: string;
|
|
11
|
+
readonly features: readonly string[];
|
|
12
|
+
readonly rev: number;
|
|
13
|
+
readonly iat: number;
|
|
14
|
+
readonly exp: number;
|
|
15
|
+
readonly grace?: number;
|
|
16
|
+
}
|
|
17
|
+
export interface TicketJwk {
|
|
18
|
+
readonly kid: string;
|
|
19
|
+
readonly kty: string;
|
|
20
|
+
readonly crv: string;
|
|
21
|
+
readonly x: string;
|
|
22
|
+
readonly alg?: string;
|
|
23
|
+
readonly use?: string;
|
|
24
|
+
readonly key_ops?: readonly string[];
|
|
25
|
+
}
|
|
26
|
+
export interface TicketJwks {
|
|
27
|
+
readonly keys: readonly TicketJwk[];
|
|
28
|
+
}
|
|
29
|
+
export interface TicketVerificationOptions {
|
|
30
|
+
readonly issuer: string;
|
|
31
|
+
readonly audience: string;
|
|
32
|
+
/** Unix seconds. Expiry is exclusive; only iat has a 60-second allowance. */
|
|
33
|
+
readonly now: number;
|
|
34
|
+
}
|
|
35
|
+
/** Verify offline against trusted, embedded or previously fetched hub public keys.
|
|
36
|
+
* Compare the returned sub with the current suite subject before granting access.
|
|
37
|
+
* Refresh keys on an unknown kid through the product's trusted network adapter.
|
|
38
|
+
*/
|
|
39
|
+
export declare function verifyTicket(ticket: string, jwks: TicketJwks, options: TicketVerificationOptions): TicketClaims;
|
|
40
|
+
//# sourceMappingURL=entitlements.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entitlements.d.ts","sourceRoot":"","sources":["../../src/entitlements.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,eAAe,GAAG,gBAAgB,GAAG,gBAAgB,GAAG,uBAAuB,CAAC;AAE5F,qBAAa,uBAAwB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EAChB,OAAO,EAAE,yBAAyB,GACjC,YAAY,CAyCd"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TicketVerificationError = void 0;
|
|
4
|
+
exports.verifyTicket = verifyTicket;
|
|
5
|
+
const ed25519_1 = require("@noble/curves/ed25519");
|
|
6
|
+
class TicketVerificationError extends Error {
|
|
7
|
+
code;
|
|
8
|
+
constructor(code) {
|
|
9
|
+
super(code);
|
|
10
|
+
this.code = code;
|
|
11
|
+
this.name = 'TicketVerificationError';
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.TicketVerificationError = TicketVerificationError;
|
|
15
|
+
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
|
16
|
+
const maxTicketBytes = 32 * 1024;
|
|
17
|
+
const maxLifetimeSeconds = 7 * 24 * 60 * 60;
|
|
18
|
+
const iatLeewaySeconds = 60;
|
|
19
|
+
const maxKidLength = 256;
|
|
20
|
+
const maxJwksKeys = 64;
|
|
21
|
+
function invalid() {
|
|
22
|
+
throw new TicketVerificationError('ticket_invalid');
|
|
23
|
+
}
|
|
24
|
+
function record(value) {
|
|
25
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
26
|
+
invalid();
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
function nonempty(value) {
|
|
30
|
+
return typeof value === 'string' && value.length > 0;
|
|
31
|
+
}
|
|
32
|
+
function integer(value) {
|
|
33
|
+
return typeof value === 'number' && Number.isSafeInteger(value);
|
|
34
|
+
}
|
|
35
|
+
function decode(value) {
|
|
36
|
+
if (!/^[A-Za-z0-9_-]+$/u.test(value) || value.length % 4 === 1)
|
|
37
|
+
invalid();
|
|
38
|
+
const bytes = new Uint8Array(Math.floor((value.length * 6) / 8));
|
|
39
|
+
let bits = 0;
|
|
40
|
+
let buffer = 0;
|
|
41
|
+
let offset = 0;
|
|
42
|
+
for (const char of value) {
|
|
43
|
+
buffer = (buffer << 6) | alphabet.indexOf(char);
|
|
44
|
+
bits += 6;
|
|
45
|
+
if (bits >= 8) {
|
|
46
|
+
bits -= 8;
|
|
47
|
+
bytes[offset++] = (buffer >> bits) & 255;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if ((buffer & ((1 << bits) - 1)) !== 0)
|
|
51
|
+
invalid();
|
|
52
|
+
return bytes;
|
|
53
|
+
}
|
|
54
|
+
function json(value) {
|
|
55
|
+
// Hermes does not require TextDecoder, atob or Buffer for this path.
|
|
56
|
+
const escaped = Array.from(decode(value), (byte) => `%${byte.toString(16).padStart(2, '0')}`).join('');
|
|
57
|
+
return record(JSON.parse(decodeURIComponent(escaped)));
|
|
58
|
+
}
|
|
59
|
+
function claims(value, options) {
|
|
60
|
+
const allowed = ['iss', 'sub', 'aud', 'tier', 'features', 'rev', 'iat', 'exp', 'grace'];
|
|
61
|
+
if (Object.keys(value).some((key) => !allowed.includes(key)) ||
|
|
62
|
+
!nonempty(value['iss']) ||
|
|
63
|
+
!nonempty(value['sub']) ||
|
|
64
|
+
!nonempty(value['aud']) ||
|
|
65
|
+
!nonempty(value['tier']) ||
|
|
66
|
+
!Array.isArray(value['features']) ||
|
|
67
|
+
!value['features'].every((feature) => typeof feature === 'string') ||
|
|
68
|
+
!integer(value['rev']) ||
|
|
69
|
+
value['rev'] < 0 ||
|
|
70
|
+
!integer(value['iat']) ||
|
|
71
|
+
value['iat'] < 0 ||
|
|
72
|
+
!integer(value['exp']) ||
|
|
73
|
+
('grace' in value && !integer(value['grace'])))
|
|
74
|
+
invalid();
|
|
75
|
+
const result = value;
|
|
76
|
+
if (result.iss !== options.issuer ||
|
|
77
|
+
result.iat > options.now + iatLeewaySeconds ||
|
|
78
|
+
result.exp <= result.iat ||
|
|
79
|
+
result.exp - result.iat > maxLifetimeSeconds)
|
|
80
|
+
invalid();
|
|
81
|
+
if (result.exp <= options.now)
|
|
82
|
+
throw new TicketVerificationError('ticket_expired');
|
|
83
|
+
if (result.aud !== options.audience)
|
|
84
|
+
throw new TicketVerificationError('ticket_wrong_audience');
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
/** Verify offline against trusted, embedded or previously fetched hub public keys.
|
|
88
|
+
* Compare the returned sub with the current suite subject before granting access.
|
|
89
|
+
* Refresh keys on an unknown kid through the product's trusted network adapter.
|
|
90
|
+
*/
|
|
91
|
+
function verifyTicket(ticket, jwks, options) {
|
|
92
|
+
try {
|
|
93
|
+
if (ticket.length > maxTicketBytes ||
|
|
94
|
+
!integer(options.now) ||
|
|
95
|
+
!options.issuer ||
|
|
96
|
+
!options.audience)
|
|
97
|
+
invalid();
|
|
98
|
+
const [headerPart, payloadPart, signaturePart, extra] = ticket.split('.');
|
|
99
|
+
if (!headerPart || !payloadPart || !signaturePart || extra !== undefined)
|
|
100
|
+
invalid();
|
|
101
|
+
const header = json(headerPart);
|
|
102
|
+
if (header['alg'] !== 'EdDSA' ||
|
|
103
|
+
!nonempty(header['kid']) ||
|
|
104
|
+
header['kid'].length > maxKidLength ||
|
|
105
|
+
('typ' in header && header['typ'] !== 'JWT') ||
|
|
106
|
+
Object.keys(header).some((key) => !['alg', 'kid', 'typ'].includes(key)))
|
|
107
|
+
invalid();
|
|
108
|
+
if (jwks.keys.length > maxJwksKeys ||
|
|
109
|
+
new Set(jwks.keys.map((key) => key.kid)).size !== jwks.keys.length)
|
|
110
|
+
invalid();
|
|
111
|
+
const key = jwks.keys.find((key) => key.kid === header['kid']);
|
|
112
|
+
if (key?.kty !== 'OKP' ||
|
|
113
|
+
key.crv !== 'Ed25519' ||
|
|
114
|
+
(key.alg !== undefined && key.alg !== 'EdDSA') ||
|
|
115
|
+
(key.use !== undefined && key.use !== 'sig') ||
|
|
116
|
+
key.key_ops?.includes('verify') === false)
|
|
117
|
+
invalid();
|
|
118
|
+
const input = Uint8Array.from(`${headerPart}.${payloadPart}`, (char) => char.charCodeAt(0));
|
|
119
|
+
if (!ed25519_1.ed25519.verify(decode(signaturePart), input, decode(key.x), { zip215: false }))
|
|
120
|
+
invalid();
|
|
121
|
+
return claims(json(payloadPart), options);
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
if (error instanceof TicketVerificationError)
|
|
125
|
+
throw error;
|
|
126
|
+
return invalid();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=entitlements.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entitlements.js","sourceRoot":"","sources":["../../src/entitlements.ts"],"names":[],"mappings":";;;AA0HA,oCA6CC;AAvKD,mDAAgD;AAIhD,MAAa,uBAAwB,SAAQ,KAAK;IAC3B;IAArB,YAAqB,IAAqB;QACxC,KAAK,CAAC,IAAI,CAAC,CAAC;QADO,SAAI,GAAJ,IAAI,CAAiB;QAExC,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;IACxC,CAAC;CACF;AALD,0DAKC;AAiCD,MAAM,QAAQ,GAAG,kEAAkE,CAAC;AACpF,MAAM,cAAc,GAAG,EAAE,GAAG,IAAI,CAAC;AACjC,MAAM,kBAAkB,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC;AAC5C,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAC5B,MAAM,YAAY,GAAG,GAAG,CAAC;AACzB,MAAM,WAAW,GAAG,EAAE,CAAC;AAEvB,SAAS,OAAO;IACd,MAAM,IAAI,uBAAuB,CAAC,gBAAgB,CAAC,CAAC;AACtD,CAAC;AACD,SAAS,MAAM,CAAC,KAAc;IAC5B,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IACnF,OAAO,KAAgC,CAAC;AAC1C,CAAC;AACD,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAA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AAG,UAAU,IAAI,WAAW,EAAE,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;QAC5F,IAAI,CAAC,iBAAO,CAAC,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;YAAE,OAAO,EAAE,CAAC;QAC/F,OAAO,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,OAAO,CAAC,CAAC;IAC5C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,uBAAuB;YAAE,MAAM,KAAK,CAAC;QAC1D,OAAO,OAAO,EAAE,CAAC;IACnB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export type TicketErrorCode = 'ticket_invalid' | 'ticket_expired' | 'ticket_wrong_audience';
|
|
2
|
+
export declare class TicketVerificationError extends Error {
|
|
3
|
+
readonly code: TicketErrorCode;
|
|
4
|
+
constructor(code: TicketErrorCode);
|
|
5
|
+
}
|
|
6
|
+
export interface TicketClaims {
|
|
7
|
+
readonly iss: string;
|
|
8
|
+
readonly sub: string;
|
|
9
|
+
readonly aud: string;
|
|
10
|
+
readonly tier: string;
|
|
11
|
+
readonly features: readonly string[];
|
|
12
|
+
readonly rev: number;
|
|
13
|
+
readonly iat: number;
|
|
14
|
+
readonly exp: number;
|
|
15
|
+
readonly grace?: number;
|
|
16
|
+
}
|
|
17
|
+
export interface TicketJwk {
|
|
18
|
+
readonly kid: string;
|
|
19
|
+
readonly kty: string;
|
|
20
|
+
readonly crv: string;
|
|
21
|
+
readonly x: string;
|
|
22
|
+
readonly alg?: string;
|
|
23
|
+
readonly use?: string;
|
|
24
|
+
readonly key_ops?: readonly string[];
|
|
25
|
+
}
|
|
26
|
+
export interface TicketJwks {
|
|
27
|
+
readonly keys: readonly TicketJwk[];
|
|
28
|
+
}
|
|
29
|
+
export interface TicketVerificationOptions {
|
|
30
|
+
readonly issuer: string;
|
|
31
|
+
readonly audience: string;
|
|
32
|
+
/** Unix seconds. Expiry is exclusive; only iat has a 60-second allowance. */
|
|
33
|
+
readonly now: number;
|
|
34
|
+
}
|
|
35
|
+
/** Verify offline against trusted, embedded or previously fetched hub public keys.
|
|
36
|
+
* Compare the returned sub with the current suite subject before granting access.
|
|
37
|
+
* Refresh keys on an unknown kid through the product's trusted network adapter.
|
|
38
|
+
*/
|
|
39
|
+
export declare function verifyTicket(ticket: string, jwks: TicketJwks, options: TicketVerificationOptions): TicketClaims;
|
|
40
|
+
//# sourceMappingURL=entitlements.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entitlements.d.ts","sourceRoot":"","sources":["../src/entitlements.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,eAAe,GAAG,gBAAgB,GAAG,gBAAgB,GAAG,uBAAuB,CAAC;AAE5F,qBAAa,uBAAwB,SAAQ,KAAK;IACpC,QAAQ,CAAC,IAAI,EAAE,eAAe;gBAArB,IAAI,EAAE,eAAe;CAI3C;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;IACrC,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;CACzB;AAED,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACtC;AACD,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,IAAI,EAAE,SAAS,SAAS,EAAE,CAAC;CACrC;AACD,MAAM,WAAW,yBAAyB;IACxC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,6EAA6E;IAC7E,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AA8ED;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,UAAU,EAChB,OAAO,EAAE,yBAAyB,GACjC,YAAY,CAyCd"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { ed25519 } from '@noble/curves/ed25519';
|
|
2
|
+
export class TicketVerificationError extends Error {
|
|
3
|
+
code;
|
|
4
|
+
constructor(code) {
|
|
5
|
+
super(code);
|
|
6
|
+
this.code = code;
|
|
7
|
+
this.name = 'TicketVerificationError';
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
|
11
|
+
const maxTicketBytes = 32 * 1024;
|
|
12
|
+
const maxLifetimeSeconds = 7 * 24 * 60 * 60;
|
|
13
|
+
const iatLeewaySeconds = 60;
|
|
14
|
+
const maxKidLength = 256;
|
|
15
|
+
const maxJwksKeys = 64;
|
|
16
|
+
function invalid() {
|
|
17
|
+
throw new TicketVerificationError('ticket_invalid');
|
|
18
|
+
}
|
|
19
|
+
function record(value) {
|
|
20
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
21
|
+
invalid();
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
function nonempty(value) {
|
|
25
|
+
return typeof value === 'string' && value.length > 0;
|
|
26
|
+
}
|
|
27
|
+
function integer(value) {
|
|
28
|
+
return typeof value === 'number' && Number.isSafeInteger(value);
|
|
29
|
+
}
|
|
30
|
+
function decode(value) {
|
|
31
|
+
if (!/^[A-Za-z0-9_-]+$/u.test(value) || value.length % 4 === 1)
|
|
32
|
+
invalid();
|
|
33
|
+
const bytes = new Uint8Array(Math.floor((value.length * 6) / 8));
|
|
34
|
+
let bits = 0;
|
|
35
|
+
let buffer = 0;
|
|
36
|
+
let offset = 0;
|
|
37
|
+
for (const char of value) {
|
|
38
|
+
buffer = (buffer << 6) | alphabet.indexOf(char);
|
|
39
|
+
bits += 6;
|
|
40
|
+
if (bits >= 8) {
|
|
41
|
+
bits -= 8;
|
|
42
|
+
bytes[offset++] = (buffer >> bits) & 255;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if ((buffer & ((1 << bits) - 1)) !== 0)
|
|
46
|
+
invalid();
|
|
47
|
+
return bytes;
|
|
48
|
+
}
|
|
49
|
+
function json(value) {
|
|
50
|
+
// Hermes does not require TextDecoder, atob or Buffer for this path.
|
|
51
|
+
const escaped = Array.from(decode(value), (byte) => `%${byte.toString(16).padStart(2, '0')}`).join('');
|
|
52
|
+
return record(JSON.parse(decodeURIComponent(escaped)));
|
|
53
|
+
}
|
|
54
|
+
function claims(value, options) {
|
|
55
|
+
const allowed = ['iss', 'sub', 'aud', 'tier', 'features', 'rev', 'iat', 'exp', 'grace'];
|
|
56
|
+
if (Object.keys(value).some((key) => !allowed.includes(key)) ||
|
|
57
|
+
!nonempty(value['iss']) ||
|
|
58
|
+
!nonempty(value['sub']) ||
|
|
59
|
+
!nonempty(value['aud']) ||
|
|
60
|
+
!nonempty(value['tier']) ||
|
|
61
|
+
!Array.isArray(value['features']) ||
|
|
62
|
+
!value['features'].every((feature) => typeof feature === 'string') ||
|
|
63
|
+
!integer(value['rev']) ||
|
|
64
|
+
value['rev'] < 0 ||
|
|
65
|
+
!integer(value['iat']) ||
|
|
66
|
+
value['iat'] < 0 ||
|
|
67
|
+
!integer(value['exp']) ||
|
|
68
|
+
('grace' in value && !integer(value['grace'])))
|
|
69
|
+
invalid();
|
|
70
|
+
const result = value;
|
|
71
|
+
if (result.iss !== options.issuer ||
|
|
72
|
+
result.iat > options.now + iatLeewaySeconds ||
|
|
73
|
+
result.exp <= result.iat ||
|
|
74
|
+
result.exp - result.iat > maxLifetimeSeconds)
|
|
75
|
+
invalid();
|
|
76
|
+
if (result.exp <= options.now)
|
|
77
|
+
throw new TicketVerificationError('ticket_expired');
|
|
78
|
+
if (result.aud !== options.audience)
|
|
79
|
+
throw new TicketVerificationError('ticket_wrong_audience');
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
/** Verify offline against trusted, embedded or previously fetched hub public keys.
|
|
83
|
+
* Compare the returned sub with the current suite subject before granting access.
|
|
84
|
+
* Refresh keys on an unknown kid through the product's trusted network adapter.
|
|
85
|
+
*/
|
|
86
|
+
export function verifyTicket(ticket, jwks, options) {
|
|
87
|
+
try {
|
|
88
|
+
if (ticket.length > maxTicketBytes ||
|
|
89
|
+
!integer(options.now) ||
|
|
90
|
+
!options.issuer ||
|
|
91
|
+
!options.audience)
|
|
92
|
+
invalid();
|
|
93
|
+
const [headerPart, payloadPart, signaturePart, extra] = ticket.split('.');
|
|
94
|
+
if (!headerPart || !payloadPart || !signaturePart || extra !== undefined)
|
|
95
|
+
invalid();
|
|
96
|
+
const header = json(headerPart);
|
|
97
|
+
if (header['alg'] !== 'EdDSA' ||
|
|
98
|
+
!nonempty(header['kid']) ||
|
|
99
|
+
header['kid'].length > maxKidLength ||
|
|
100
|
+
('typ' in header && header['typ'] !== 'JWT') ||
|
|
101
|
+
Object.keys(header).some((key) => !['alg', 'kid', 'typ'].includes(key)))
|
|
102
|
+
invalid();
|
|
103
|
+
if (jwks.keys.length > maxJwksKeys ||
|
|
104
|
+
new Set(jwks.keys.map((key) => key.kid)).size !== jwks.keys.length)
|
|
105
|
+
invalid();
|
|
106
|
+
const key = jwks.keys.find((key) => key.kid === header['kid']);
|
|
107
|
+
if (key?.kty !== 'OKP' ||
|
|
108
|
+
key.crv !== 'Ed25519' ||
|
|
109
|
+
(key.alg !== undefined && key.alg !== 'EdDSA') ||
|
|
110
|
+
(key.use !== undefined && key.use !== 'sig') ||
|
|
111
|
+
key.key_ops?.includes('verify') === false)
|
|
112
|
+
invalid();
|
|
113
|
+
const input = Uint8Array.from(`${headerPart}.${payloadPart}`, (char) => char.charCodeAt(0));
|
|
114
|
+
if (!ed25519.verify(decode(signaturePart), input, decode(key.x), { zip215: false }))
|
|
115
|
+
invalid();
|
|
116
|
+
return claims(json(payloadPart), options);
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
if (error instanceof TicketVerificationError)
|
|
120
|
+
throw error;
|
|
121
|
+
return invalid();
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=entitlements.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entitlements.js","sourceRoot":"","sources":["../src/entitlements.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAIhD,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IAC3B;IAArB,YAAqB,IAAqB;QACxC,KAAK,CAAC,IAAI,CAAC,CAAC;QADO,SAAI,GAAJ,IAAI,CAAiB;QAExC,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;IACxC,CAAC;CACF;AAiCD,MAAM,QAAQ,GAAG,kEAAkE,CAAC;AACpF,MAAM,cAAc,GAAG,EAAE,GAAG,IAAI,CAAC;AACjC,MAAM,kBAA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,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,GAAG,KAAK,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;QAC/D,IACE,GAAG,EAAE,GAAG,KAAK,KAAK;YAClB,GAAG,CAAC,GAAG,KAAK,SAAS;YACrB,CAAC,GAAG,CAAC,GAAG,KAAK,SAAS,IAAI,GAAG,CAAC,GAAG,KAAK,OAAO,CAAC;YAC9C,CAAC,GAAG,CAAC,GAAG,KAAK,SAAS,IAAI,GAAG,CAAC,GAAG,KAAK,KAAK,CAAC;YAC5C,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAC,QAAQ,CAAC,KAAK,KAAK;YAEzC,OAAO,EAAE,CAAC;QACZ,MAAM,KAAK,GAAG,UAAU,CAAC,IAAI,CAAC,GAAG,UAAU,IAAI,WAAW,EAAE,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;QAC5F,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;YAAE,OAAO,EAAE,CAAC;QAC/F,OAAO,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,OAAO,CAAC,CAAC;IAC5C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,uBAAuB;YAAE,MAAM,KAAK,CAAC;QAC1D,OAAO,OAAO,EAAE,CAAC;IACnB,CAAC;AACH,CAAC"}
|
package/dist/urls.d.ts
CHANGED
|
@@ -14,4 +14,23 @@ export declare function openSuitePeer(peer: {
|
|
|
14
14
|
readonly openUrl: (url: string) => Promise<void>;
|
|
15
15
|
readonly allowLoopback?: boolean;
|
|
16
16
|
}): Promise<void>;
|
|
17
|
+
export interface SuiteAuthorizePeer {
|
|
18
|
+
readonly webUrl: string;
|
|
19
|
+
/** Full hosted consent URL, including the authorizing product ID. */
|
|
20
|
+
readonly authorizeUrl?: string;
|
|
21
|
+
}
|
|
22
|
+
/** Validate peer configuration before starting a connection. */
|
|
23
|
+
export declare function validateSuiteAuthorizePeer(peer: SuiteAuthorizePeer, options?: {
|
|
24
|
+
readonly allowLoopback?: boolean;
|
|
25
|
+
}): void;
|
|
26
|
+
/** Keep protocol query names identical for local and hosted consent pages. */
|
|
27
|
+
export declare function suiteAuthorizeUrl(peer: SuiteAuthorizePeer, input: {
|
|
28
|
+
readonly client: string;
|
|
29
|
+
readonly state: string;
|
|
30
|
+
readonly codeChallenge: string;
|
|
31
|
+
readonly hint?: string;
|
|
32
|
+
readonly hintDomain?: string;
|
|
33
|
+
}, options?: {
|
|
34
|
+
readonly allowLoopback?: boolean;
|
|
35
|
+
}): string;
|
|
17
36
|
//# sourceMappingURL=urls.d.ts.map
|
package/dist/urls.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"urls.d.ts","sourceRoot":"","sources":["../src/urls.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,QAAQ,EAAE,KAAK,GAAG,QAAQ,CAAC;IACpC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AACD,wBAAgB,cAAc,CAAC,MAAM,EAAE,iBAAiB,GAAG,MAAM,CAEhE;AACD,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,iBAAiB,GAAG,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAKxF;AACD,mFAAmF;AACnF,wBAAgB,gCAAgC,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAW3F;AACD,wBAAsB,aAAa,CACjC,IAAI,EAAE;IAAE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EA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AY,CAAC,EAAE,MAAM,CAAC;CAChC;AAED,gEAAgE;AAChE,wBAAgB,0BAA0B,CACxC,IAAI,EAAE,kBAAkB,EACxB,OAAO,GAAE;IAAE,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAA;CAAO,GACjD,IAAI,CAEN;AAsCD,8EAA8E;AAC9E,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,kBAAkB,EACxB,KAAK,EAAE;IACL,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;CAC9B,EACD,OAAO,GAAE;IAAE,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAA;CAAO,GACjD,MAAM,CAQR"}
|
package/dist/urls.js
CHANGED
|
@@ -43,4 +43,48 @@ export async function openSuitePeer(peer, options) {
|
|
|
43
43
|
}
|
|
44
44
|
await options.openUrl(peer.webUrl);
|
|
45
45
|
}
|
|
46
|
+
/** Validate peer configuration before starting a connection. */
|
|
47
|
+
export function validateSuiteAuthorizePeer(peer, options = {}) {
|
|
48
|
+
peerAuthorizeEndpoint(peer, options.allowLoopback);
|
|
49
|
+
}
|
|
50
|
+
function peerAuthorizeEndpoint(peer, allowLoopback = false) {
|
|
51
|
+
return authorizeEndpoint(peer.authorizeUrl ?? `${peer.webUrl.replace(/\/+$/, '')}/suite/authorize`, allowLoopback);
|
|
52
|
+
}
|
|
53
|
+
function hasInvalidUrlCharacters(value) {
|
|
54
|
+
for (const character of value) {
|
|
55
|
+
if (character <= ' ' || character === '\u007f' || character === '\\')
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
function authorizeEndpoint(value, allowLoopback = false) {
|
|
61
|
+
const url = new URL(value);
|
|
62
|
+
const authority = value.split('://')[1]?.split(/[/?#]/, 1)[0];
|
|
63
|
+
const loopback = url.hostname === 'localhost' ||
|
|
64
|
+
url.hostname === '[::1]' ||
|
|
65
|
+
/^127\.(?:\d{1,3}\.){2}\d{1,3}$/.test(url.hostname);
|
|
66
|
+
if (authority === undefined ||
|
|
67
|
+
authority.includes('@') ||
|
|
68
|
+
hasInvalidUrlCharacters(value) ||
|
|
69
|
+
url.username !== '' ||
|
|
70
|
+
url.password !== '' ||
|
|
71
|
+
value.includes('?') ||
|
|
72
|
+
value.includes('#') ||
|
|
73
|
+
(url.protocol !== 'https:' && !(url.protocol === 'http:' && allowLoopback && loopback))) {
|
|
74
|
+
throw new TypeError('Invalid suite authorize URL.');
|
|
75
|
+
}
|
|
76
|
+
return url;
|
|
77
|
+
}
|
|
78
|
+
/** Keep protocol query names identical for local and hosted consent pages. */
|
|
79
|
+
export function suiteAuthorizeUrl(peer, input, options = {}) {
|
|
80
|
+
const url = peerAuthorizeEndpoint(peer, options.allowLoopback);
|
|
81
|
+
url.searchParams.append('client', input.client);
|
|
82
|
+
url.searchParams.append('state', input.state);
|
|
83
|
+
url.searchParams.append('code_challenge', input.codeChallenge);
|
|
84
|
+
if (input.hint !== undefined)
|
|
85
|
+
url.searchParams.append('hint', input.hint);
|
|
86
|
+
if (input.hintDomain !== undefined)
|
|
87
|
+
url.searchParams.append('hint_domain', input.hintDomain);
|
|
88
|
+
return url.toString();
|
|
89
|
+
}
|
|
46
90
|
//# sourceMappingURL=urls.js.map
|
package/dist/urls.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"urls.js","sourceRoot":"","sources":["../src/urls.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,6BAA6B,CAAC;AAMvE,MAAM,UAAU,cAAc,CAAC,MAAyB;IACtD,OAAO,MAAM,CAAC,QAAQ,KAAK,KAAK,CAAC,CAAC,CAAC,GAA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QAAQ,EAAE,CAAC;AACxB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@baukit/suite-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.1",
|
|
4
4
|
"description": "Headless suite linking, authorization state and connected app clients.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Patrick Koss",
|
|
@@ -36,6 +36,16 @@
|
|
|
36
36
|
"types": "./dist/cjs/peers.d.ts",
|
|
37
37
|
"default": "./dist/cjs/peers.js"
|
|
38
38
|
}
|
|
39
|
+
},
|
|
40
|
+
"./entitlements": {
|
|
41
|
+
"import": {
|
|
42
|
+
"types": "./dist/entitlements.d.ts",
|
|
43
|
+
"default": "./dist/entitlements.js"
|
|
44
|
+
},
|
|
45
|
+
"require": {
|
|
46
|
+
"types": "./dist/cjs/entitlements.d.ts",
|
|
47
|
+
"default": "./dist/cjs/entitlements.js"
|
|
48
|
+
}
|
|
39
49
|
}
|
|
40
50
|
},
|
|
41
51
|
"files": [
|
|
@@ -47,7 +57,7 @@
|
|
|
47
57
|
"scripts": {
|
|
48
58
|
"build": "tsc -p tsconfig.json && tsc -p tsconfig.cjs.json && node scripts/build-cjs.mjs",
|
|
49
59
|
"prepare": "pnpm run build",
|
|
50
|
-
"test": "tsc -p tsconfig.test.json && vitest run --config ../../vitest.config.ts --maxWorkers=4 && node --test scripts/peers.test.cjs && node ../../scripts/test-packed-exports.mjs --load ./peers",
|
|
60
|
+
"test": "tsc -p tsconfig.test.json && vitest run --config ../../vitest.config.ts --maxWorkers=4 && node --test scripts/peers.test.cjs && node ../../scripts/test-packed-exports.mjs --load ./peers --load ./entitlements",
|
|
51
61
|
"lint": "eslint src --max-warnings=0",
|
|
52
62
|
"format:check": "prettier --check src scripts package.json turbo.json tsconfig.json tsconfig.test.json tsconfig.cjs.json"
|
|
53
63
|
},
|
|
@@ -59,7 +69,10 @@
|
|
|
59
69
|
"vitest": "5.0.3"
|
|
60
70
|
},
|
|
61
71
|
"peerDependencies": {
|
|
62
|
-
"@baukit/integrations-client": "^0.
|
|
63
|
-
"@baukit/localization-core": "^0.
|
|
72
|
+
"@baukit/integrations-client": "^0.11.1",
|
|
73
|
+
"@baukit/localization-core": "^0.11.1"
|
|
74
|
+
},
|
|
75
|
+
"dependencies": {
|
|
76
|
+
"@noble/curves": "1.9.7"
|
|
64
77
|
}
|
|
65
78
|
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { ed25519 } from '@noble/curves/ed25519';
|
|
4
|
+
import {
|
|
5
|
+
verifyTicket,
|
|
6
|
+
TicketVerificationError,
|
|
7
|
+
type TicketJwks,
|
|
8
|
+
type TicketClaims,
|
|
9
|
+
} from './entitlements.js';
|
|
10
|
+
|
|
11
|
+
interface Vector {
|
|
12
|
+
name: string;
|
|
13
|
+
ticket: string;
|
|
14
|
+
audience: string;
|
|
15
|
+
now: number;
|
|
16
|
+
expected: string;
|
|
17
|
+
claims?: TicketClaims;
|
|
18
|
+
}
|
|
19
|
+
interface Fixture {
|
|
20
|
+
issuer: string;
|
|
21
|
+
testSeedHex: string;
|
|
22
|
+
jwks: TicketJwks;
|
|
23
|
+
vectors: Vector[];
|
|
24
|
+
}
|
|
25
|
+
const fixture = JSON.parse(
|
|
26
|
+
readFileSync(
|
|
27
|
+
new URL('../../../../fixtures/entitlement-tickets/v1/vectors.json', import.meta.url),
|
|
28
|
+
'utf8',
|
|
29
|
+
),
|
|
30
|
+
) as Fixture;
|
|
31
|
+
|
|
32
|
+
describe('shared ticket vectors', () => {
|
|
33
|
+
it.each(fixture.vectors)('$name', (v) => {
|
|
34
|
+
const run = () =>
|
|
35
|
+
verifyTicket(v.ticket, fixture.jwks, {
|
|
36
|
+
issuer: fixture.issuer,
|
|
37
|
+
audience: v.audience,
|
|
38
|
+
now: v.now,
|
|
39
|
+
});
|
|
40
|
+
if (v.expected === 'ok') expect(run()).toEqual(v.claims);
|
|
41
|
+
else
|
|
42
|
+
expect(run).toThrow(
|
|
43
|
+
new TicketVerificationError(v.expected as TicketVerificationError['code']),
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
it('runs with the globals absent in Hermes', () => {
|
|
48
|
+
for (const name of ['crypto', 'Buffer', 'atob', 'btoa', 'TextEncoder', 'TextDecoder'])
|
|
49
|
+
vi.stubGlobal(name, undefined);
|
|
50
|
+
try {
|
|
51
|
+
const v = fixture.vectors[0];
|
|
52
|
+
if (!v) throw new Error('missing vector');
|
|
53
|
+
expect(
|
|
54
|
+
verifyTicket(v.ticket, fixture.jwks, {
|
|
55
|
+
issuer: fixture.issuer,
|
|
56
|
+
audience: v.audience,
|
|
57
|
+
now: v.now,
|
|
58
|
+
}),
|
|
59
|
+
).toEqual(v.claims);
|
|
60
|
+
} finally {
|
|
61
|
+
vi.unstubAllGlobals();
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
it('verifies UTF-8 claims without browser decoders', () => {
|
|
65
|
+
const v = fixture.vectors[0];
|
|
66
|
+
if (!v?.claims) throw new Error('missing claims');
|
|
67
|
+
const header = Buffer.from(
|
|
68
|
+
JSON.stringify({ alg: 'EdDSA', kid: fixture.jwks.keys[0]?.kid }),
|
|
69
|
+
).toString('base64url');
|
|
70
|
+
const expected = { ...v.claims, sub: 'suite|Grüße 🌍' };
|
|
71
|
+
const body = Buffer.from(JSON.stringify(expected)).toString('base64url');
|
|
72
|
+
const input = `${header}.${body}`;
|
|
73
|
+
const ticket = `${input}.${Buffer.from(ed25519.sign(Buffer.from(input), fixture.testSeedHex)).toString('base64url')}`;
|
|
74
|
+
expect(
|
|
75
|
+
verifyTicket(ticket, fixture.jwks, {
|
|
76
|
+
issuer: fixture.issuer,
|
|
77
|
+
audience: v.audience,
|
|
78
|
+
now: v.now,
|
|
79
|
+
}),
|
|
80
|
+
).toEqual(expected);
|
|
81
|
+
});
|
|
82
|
+
it('rejects duplicate kids and incompatible key metadata', () => {
|
|
83
|
+
const v = fixture.vectors[0];
|
|
84
|
+
const key = fixture.jwks.keys[0];
|
|
85
|
+
if (!v || !key) throw new Error('missing vector');
|
|
86
|
+
for (const keys of [
|
|
87
|
+
[key, key],
|
|
88
|
+
[{ ...key, use: 'enc' }],
|
|
89
|
+
[{ ...key, alg: 'HS256' }],
|
|
90
|
+
[{ ...key, key_ops: ['sign'] }],
|
|
91
|
+
]) {
|
|
92
|
+
expect(() =>
|
|
93
|
+
verifyTicket(
|
|
94
|
+
v.ticket,
|
|
95
|
+
{ keys },
|
|
96
|
+
{ issuer: fixture.issuer, audience: v.audience, now: v.now },
|
|
97
|
+
),
|
|
98
|
+
).toThrow('ticket_invalid');
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
it('loads the published subpath', async () => {
|
|
102
|
+
const exported = await import('@baukit/suite-client/entitlements');
|
|
103
|
+
expect(typeof exported.verifyTicket).toBe('function');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('rejects an unsigned ticket with alg none', () => {
|
|
107
|
+
const v = fixture.vectors[0];
|
|
108
|
+
if (!v) throw new Error('missing vector');
|
|
109
|
+
const header = Buffer.from(
|
|
110
|
+
JSON.stringify({ alg: 'none', kid: fixture.jwks.keys[0]?.kid }),
|
|
111
|
+
).toString('base64url');
|
|
112
|
+
const ticket = [header, ...v.ticket.split('.').slice(1)].join('.');
|
|
113
|
+
expect(() =>
|
|
114
|
+
verifyTicket(ticket, fixture.jwks, {
|
|
115
|
+
issuer: fixture.issuer,
|
|
116
|
+
audience: v.audience,
|
|
117
|
+
now: v.now,
|
|
118
|
+
}),
|
|
119
|
+
).toThrow('ticket_invalid');
|
|
120
|
+
});
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { ed25519 } from '@noble/curves/ed25519';
|
|
2
|
+
|
|
3
|
+
export type TicketErrorCode = 'ticket_invalid' | 'ticket_expired' | 'ticket_wrong_audience';
|
|
4
|
+
|
|
5
|
+
export class TicketVerificationError extends Error {
|
|
6
|
+
constructor(readonly code: TicketErrorCode) {
|
|
7
|
+
super(code);
|
|
8
|
+
this.name = 'TicketVerificationError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface TicketClaims {
|
|
13
|
+
readonly iss: string;
|
|
14
|
+
readonly sub: string;
|
|
15
|
+
readonly aud: string;
|
|
16
|
+
readonly tier: string;
|
|
17
|
+
readonly features: readonly string[];
|
|
18
|
+
readonly rev: number;
|
|
19
|
+
readonly iat: number;
|
|
20
|
+
readonly exp: number;
|
|
21
|
+
readonly grace?: number;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface TicketJwk {
|
|
25
|
+
readonly kid: string;
|
|
26
|
+
readonly kty: string;
|
|
27
|
+
readonly crv: string;
|
|
28
|
+
readonly x: string;
|
|
29
|
+
readonly alg?: string;
|
|
30
|
+
readonly use?: string;
|
|
31
|
+
readonly key_ops?: readonly string[];
|
|
32
|
+
}
|
|
33
|
+
export interface TicketJwks {
|
|
34
|
+
readonly keys: readonly TicketJwk[];
|
|
35
|
+
}
|
|
36
|
+
export interface TicketVerificationOptions {
|
|
37
|
+
readonly issuer: string;
|
|
38
|
+
readonly audience: string;
|
|
39
|
+
/** Unix seconds. Expiry is exclusive; only iat has a 60-second allowance. */
|
|
40
|
+
readonly now: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
|
|
44
|
+
const maxTicketBytes = 32 * 1024;
|
|
45
|
+
const maxLifetimeSeconds = 7 * 24 * 60 * 60;
|
|
46
|
+
const iatLeewaySeconds = 60;
|
|
47
|
+
const maxKidLength = 256;
|
|
48
|
+
const maxJwksKeys = 64;
|
|
49
|
+
|
|
50
|
+
function invalid(): never {
|
|
51
|
+
throw new TicketVerificationError('ticket_invalid');
|
|
52
|
+
}
|
|
53
|
+
function record(value: unknown): Record<string, unknown> {
|
|
54
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) invalid();
|
|
55
|
+
return value as Record<string, unknown>;
|
|
56
|
+
}
|
|
57
|
+
function nonempty(value: unknown): value is string {
|
|
58
|
+
return typeof value === 'string' && value.length > 0;
|
|
59
|
+
}
|
|
60
|
+
function integer(value: unknown): value is number {
|
|
61
|
+
return typeof value === 'number' && Number.isSafeInteger(value);
|
|
62
|
+
}
|
|
63
|
+
function decode(value: string): Uint8Array {
|
|
64
|
+
if (!/^[A-Za-z0-9_-]+$/u.test(value) || value.length % 4 === 1) invalid();
|
|
65
|
+
const bytes = new Uint8Array(Math.floor((value.length * 6) / 8));
|
|
66
|
+
let bits = 0;
|
|
67
|
+
let buffer = 0;
|
|
68
|
+
let offset = 0;
|
|
69
|
+
for (const char of value) {
|
|
70
|
+
buffer = (buffer << 6) | alphabet.indexOf(char);
|
|
71
|
+
bits += 6;
|
|
72
|
+
if (bits >= 8) {
|
|
73
|
+
bits -= 8;
|
|
74
|
+
bytes[offset++] = (buffer >> bits) & 255;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if ((buffer & ((1 << bits) - 1)) !== 0) invalid();
|
|
78
|
+
return bytes;
|
|
79
|
+
}
|
|
80
|
+
function json(value: string): Record<string, unknown> {
|
|
81
|
+
// Hermes does not require TextDecoder, atob or Buffer for this path.
|
|
82
|
+
const escaped = Array.from(
|
|
83
|
+
decode(value),
|
|
84
|
+
(byte) => `%${byte.toString(16).padStart(2, '0')}`,
|
|
85
|
+
).join('');
|
|
86
|
+
return record(JSON.parse(decodeURIComponent(escaped)) as unknown);
|
|
87
|
+
}
|
|
88
|
+
function claims(value: Record<string, unknown>, options: TicketVerificationOptions): TicketClaims {
|
|
89
|
+
const allowed = ['iss', 'sub', 'aud', 'tier', 'features', 'rev', 'iat', 'exp', 'grace'];
|
|
90
|
+
if (
|
|
91
|
+
Object.keys(value).some((key) => !allowed.includes(key)) ||
|
|
92
|
+
!nonempty(value['iss']) ||
|
|
93
|
+
!nonempty(value['sub']) ||
|
|
94
|
+
!nonempty(value['aud']) ||
|
|
95
|
+
!nonempty(value['tier']) ||
|
|
96
|
+
!Array.isArray(value['features']) ||
|
|
97
|
+
!value['features'].every((feature: unknown) => typeof feature === 'string') ||
|
|
98
|
+
!integer(value['rev']) ||
|
|
99
|
+
value['rev'] < 0 ||
|
|
100
|
+
!integer(value['iat']) ||
|
|
101
|
+
value['iat'] < 0 ||
|
|
102
|
+
!integer(value['exp']) ||
|
|
103
|
+
('grace' in value && !integer(value['grace']))
|
|
104
|
+
)
|
|
105
|
+
invalid();
|
|
106
|
+
const result = value as unknown as TicketClaims;
|
|
107
|
+
if (
|
|
108
|
+
result.iss !== options.issuer ||
|
|
109
|
+
result.iat > options.now + iatLeewaySeconds ||
|
|
110
|
+
result.exp <= result.iat ||
|
|
111
|
+
result.exp - result.iat > maxLifetimeSeconds
|
|
112
|
+
)
|
|
113
|
+
invalid();
|
|
114
|
+
if (result.exp <= options.now) throw new TicketVerificationError('ticket_expired');
|
|
115
|
+
if (result.aud !== options.audience) throw new TicketVerificationError('ticket_wrong_audience');
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Verify offline against trusted, embedded or previously fetched hub public keys.
|
|
120
|
+
* Compare the returned sub with the current suite subject before granting access.
|
|
121
|
+
* Refresh keys on an unknown kid through the product's trusted network adapter.
|
|
122
|
+
*/
|
|
123
|
+
export function verifyTicket(
|
|
124
|
+
ticket: string,
|
|
125
|
+
jwks: TicketJwks,
|
|
126
|
+
options: TicketVerificationOptions,
|
|
127
|
+
): TicketClaims {
|
|
128
|
+
try {
|
|
129
|
+
if (
|
|
130
|
+
ticket.length > maxTicketBytes ||
|
|
131
|
+
!integer(options.now) ||
|
|
132
|
+
!options.issuer ||
|
|
133
|
+
!options.audience
|
|
134
|
+
)
|
|
135
|
+
invalid();
|
|
136
|
+
const [headerPart, payloadPart, signaturePart, extra] = ticket.split('.');
|
|
137
|
+
if (!headerPart || !payloadPart || !signaturePart || extra !== undefined) invalid();
|
|
138
|
+
const header = json(headerPart);
|
|
139
|
+
if (
|
|
140
|
+
header['alg'] !== 'EdDSA' ||
|
|
141
|
+
!nonempty(header['kid']) ||
|
|
142
|
+
header['kid'].length > maxKidLength ||
|
|
143
|
+
('typ' in header && header['typ'] !== 'JWT') ||
|
|
144
|
+
Object.keys(header).some((key) => !['alg', 'kid', 'typ'].includes(key))
|
|
145
|
+
)
|
|
146
|
+
invalid();
|
|
147
|
+
if (
|
|
148
|
+
jwks.keys.length > maxJwksKeys ||
|
|
149
|
+
new Set(jwks.keys.map((key) => key.kid)).size !== jwks.keys.length
|
|
150
|
+
)
|
|
151
|
+
invalid();
|
|
152
|
+
const key = jwks.keys.find((key) => key.kid === header['kid']);
|
|
153
|
+
if (
|
|
154
|
+
key?.kty !== 'OKP' ||
|
|
155
|
+
key.crv !== 'Ed25519' ||
|
|
156
|
+
(key.alg !== undefined && key.alg !== 'EdDSA') ||
|
|
157
|
+
(key.use !== undefined && key.use !== 'sig') ||
|
|
158
|
+
key.key_ops?.includes('verify') === false
|
|
159
|
+
)
|
|
160
|
+
invalid();
|
|
161
|
+
const input = Uint8Array.from(`${headerPart}.${payloadPart}`, (char) => char.charCodeAt(0));
|
|
162
|
+
if (!ed25519.verify(decode(signaturePart), input, decode(key.x), { zip215: false })) invalid();
|
|
163
|
+
return claims(json(payloadPart), options);
|
|
164
|
+
} catch (error) {
|
|
165
|
+
if (error instanceof TicketVerificationError) throw error;
|
|
166
|
+
return invalid();
|
|
167
|
+
}
|
|
168
|
+
}
|
package/src/urls.test.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { suiteAuthorizeUrl, validateSuiteAuthorizePeer } from './urls.js';
|
|
3
|
+
|
|
4
|
+
const input = {
|
|
5
|
+
client: 'alpha',
|
|
6
|
+
state: 'request state',
|
|
7
|
+
codeChallenge: 'pkce-challenge',
|
|
8
|
+
hint: 'subject+hint',
|
|
9
|
+
hintDomain: 'shared',
|
|
10
|
+
};
|
|
11
|
+
const query =
|
|
12
|
+
'client=alpha&state=request+state&code_challenge=pkce-challenge&hint=subject%2Bhint&hint_domain=shared';
|
|
13
|
+
|
|
14
|
+
describe('suite authorize URLs', () => {
|
|
15
|
+
it.each(['https://x/', 'https://x///'])('trims trailing slashes from %s', (webUrl) => {
|
|
16
|
+
const peer = { webUrl };
|
|
17
|
+
expect(() => {
|
|
18
|
+
validateSuiteAuthorizePeer(peer);
|
|
19
|
+
}).not.toThrow();
|
|
20
|
+
expect(suiteAuthorizeUrl(peer, input)).toBe(`https://x/suite/authorize?${query}`);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('keeps the default endpoint and protocol query unchanged', () => {
|
|
24
|
+
expect(suiteAuthorizeUrl({ webUrl: 'https://beta.example' }, input)).toBe(
|
|
25
|
+
`https://beta.example/suite/authorize?${query}`,
|
|
26
|
+
);
|
|
27
|
+
expect(
|
|
28
|
+
suiteAuthorizeUrl(
|
|
29
|
+
{ webUrl: 'https://beta.example' },
|
|
30
|
+
{
|
|
31
|
+
client: 'alpha',
|
|
32
|
+
state: 'state',
|
|
33
|
+
codeChallenge: 'challenge',
|
|
34
|
+
},
|
|
35
|
+
),
|
|
36
|
+
).toBe(
|
|
37
|
+
'https://beta.example/suite/authorize?client=alpha&state=state&code_challenge=challenge',
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it.each(['/suite/authorize/beta', '/portal/suite/authorize/beta/'])(
|
|
42
|
+
'preserves the hosted path %s and every parameter',
|
|
43
|
+
(path) => {
|
|
44
|
+
const peer = {
|
|
45
|
+
webUrl: 'https://beta.example',
|
|
46
|
+
authorizeUrl: `https://account.example${path}`,
|
|
47
|
+
};
|
|
48
|
+
expect(() => {
|
|
49
|
+
validateSuiteAuthorizePeer(peer);
|
|
50
|
+
}).not.toThrow();
|
|
51
|
+
expect(suiteAuthorizeUrl(peer, input)).toBe(`https://account.example${path}?${query}`);
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
it.each([
|
|
56
|
+
'/suite/authorize/beta',
|
|
57
|
+
'//account.example/suite/authorize/beta',
|
|
58
|
+
'https:account.example/suite/authorize/beta',
|
|
59
|
+
'http://account.example/suite/authorize/beta',
|
|
60
|
+
'ftp://account.example/suite/authorize/beta',
|
|
61
|
+
'https://user:secret@account.example/suite/authorize/beta',
|
|
62
|
+
'https://user@account.example/suite/authorize/beta',
|
|
63
|
+
'https://@account.example/suite/authorize/beta',
|
|
64
|
+
'https://account.example/suite/authorize/beta?existing=value',
|
|
65
|
+
'https://account.example/suite/authorize/beta?',
|
|
66
|
+
'https://account.example/suite/authorize/beta#fragment',
|
|
67
|
+
'https://account.example/suite/authorize/beta#',
|
|
68
|
+
' https://account.example/suite/authorize/beta',
|
|
69
|
+
'https://account.example/\nsuite/authorize/beta',
|
|
70
|
+
])('rejects invalid configuration %s before starting', (authorizeUrl) => {
|
|
71
|
+
const peer = { webUrl: 'https://beta.example', authorizeUrl };
|
|
72
|
+
expect(() => {
|
|
73
|
+
validateSuiteAuthorizePeer(peer, { allowLoopback: true });
|
|
74
|
+
}).toThrow(TypeError);
|
|
75
|
+
expect(() => suiteAuthorizeUrl(peer, input, { allowLoopback: true })).toThrow(TypeError);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it.each(['localhost', '127.0.0.1', '127.5.4.3', '[::1]'])(
|
|
79
|
+
'requires loopback opt-in for %s',
|
|
80
|
+
(host) => {
|
|
81
|
+
const peer = {
|
|
82
|
+
webUrl: 'https://beta.example',
|
|
83
|
+
authorizeUrl: `http://${host}:4100/suite/authorize/beta`,
|
|
84
|
+
};
|
|
85
|
+
expect(() => {
|
|
86
|
+
validateSuiteAuthorizePeer(peer);
|
|
87
|
+
}).toThrow(TypeError);
|
|
88
|
+
expect(suiteAuthorizeUrl(peer, input, { allowLoopback: true })).toBe(
|
|
89
|
+
`${peer.authorizeUrl}?${query}`,
|
|
90
|
+
);
|
|
91
|
+
},
|
|
92
|
+
);
|
|
93
|
+
});
|
package/src/urls.ts
CHANGED
|
@@ -56,3 +56,74 @@ export async function openSuitePeer(
|
|
|
56
56
|
}
|
|
57
57
|
await options.openUrl(peer.webUrl);
|
|
58
58
|
}
|
|
59
|
+
|
|
60
|
+
export interface SuiteAuthorizePeer {
|
|
61
|
+
readonly webUrl: string;
|
|
62
|
+
/** Full hosted consent URL, including the authorizing product ID. */
|
|
63
|
+
readonly authorizeUrl?: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Validate peer configuration before starting a connection. */
|
|
67
|
+
export function validateSuiteAuthorizePeer(
|
|
68
|
+
peer: SuiteAuthorizePeer,
|
|
69
|
+
options: { readonly allowLoopback?: boolean } = {},
|
|
70
|
+
): void {
|
|
71
|
+
peerAuthorizeEndpoint(peer, options.allowLoopback);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function peerAuthorizeEndpoint(peer: SuiteAuthorizePeer, allowLoopback = false): URL {
|
|
75
|
+
return authorizeEndpoint(
|
|
76
|
+
peer.authorizeUrl ?? `${peer.webUrl.replace(/\/+$/, '')}/suite/authorize`,
|
|
77
|
+
allowLoopback,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function hasInvalidUrlCharacters(value: string): boolean {
|
|
82
|
+
for (const character of value) {
|
|
83
|
+
if (character <= ' ' || character === '\u007f' || character === '\\') return true;
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function authorizeEndpoint(value: string, allowLoopback = false): URL {
|
|
89
|
+
const url = new URL(value);
|
|
90
|
+
const authority = value.split('://')[1]?.split(/[/?#]/, 1)[0];
|
|
91
|
+
const loopback =
|
|
92
|
+
url.hostname === 'localhost' ||
|
|
93
|
+
url.hostname === '[::1]' ||
|
|
94
|
+
/^127\.(?:\d{1,3}\.){2}\d{1,3}$/.test(url.hostname);
|
|
95
|
+
if (
|
|
96
|
+
authority === undefined ||
|
|
97
|
+
authority.includes('@') ||
|
|
98
|
+
hasInvalidUrlCharacters(value) ||
|
|
99
|
+
url.username !== '' ||
|
|
100
|
+
url.password !== '' ||
|
|
101
|
+
value.includes('?') ||
|
|
102
|
+
value.includes('#') ||
|
|
103
|
+
(url.protocol !== 'https:' && !(url.protocol === 'http:' && allowLoopback && loopback))
|
|
104
|
+
) {
|
|
105
|
+
throw new TypeError('Invalid suite authorize URL.');
|
|
106
|
+
}
|
|
107
|
+
return url;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Keep protocol query names identical for local and hosted consent pages. */
|
|
111
|
+
export function suiteAuthorizeUrl(
|
|
112
|
+
peer: SuiteAuthorizePeer,
|
|
113
|
+
input: {
|
|
114
|
+
readonly client: string;
|
|
115
|
+
readonly state: string;
|
|
116
|
+
readonly codeChallenge: string;
|
|
117
|
+
readonly hint?: string;
|
|
118
|
+
readonly hintDomain?: string;
|
|
119
|
+
},
|
|
120
|
+
options: { readonly allowLoopback?: boolean } = {},
|
|
121
|
+
): string {
|
|
122
|
+
const url = peerAuthorizeEndpoint(peer, options.allowLoopback);
|
|
123
|
+
url.searchParams.append('client', input.client);
|
|
124
|
+
url.searchParams.append('state', input.state);
|
|
125
|
+
url.searchParams.append('code_challenge', input.codeChallenge);
|
|
126
|
+
if (input.hint !== undefined) url.searchParams.append('hint', input.hint);
|
|
127
|
+
if (input.hintDomain !== undefined) url.searchParams.append('hint_domain', input.hintDomain);
|
|
128
|
+
return url.toString();
|
|
129
|
+
}
|