@commmerce/app-bridge 0.0.0-stage → 1.0.0-rc.3
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/LICENSE.md +17 -0
- package/README.md +79 -2
- package/dist/app-bridge.js +1 -0
- package/dist/client.d.ts +82 -0
- package/dist/client.js +223 -0
- package/dist/host.d.ts +52 -0
- package/dist/host.js +156 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/protocol.d.ts +206 -0
- package/dist/protocol.js +114 -0
- package/dist/reference-host.d.ts +20 -0
- package/dist/reference-host.js +108 -0
- package/dist/session-token.d.ts +50 -0
- package/dist/session-token.js +93 -0
- package/package.json +48 -4
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session tokens: short-lived JWTs (HS256) that tell an app's backend which store and staff member is using it.
|
|
3
|
+
* The admin's backend signs them with the app's secret; the app's backend verifies them with the same secret.
|
|
4
|
+
* Uses Web Crypto only, so it runs in browsers, Node 18+, Deno, Bun and edge runtimes.
|
|
5
|
+
* Never put the app secret in browser code: `signSessionToken` is for the admin's server and for tests.
|
|
6
|
+
*/
|
|
7
|
+
import { matchesOrigin, isOriginPattern, parseOriginPattern } from './protocol.js';
|
|
8
|
+
const enc = new TextEncoder();
|
|
9
|
+
const dec = new TextDecoder();
|
|
10
|
+
function b64url(bytes) {
|
|
11
|
+
let s = '';
|
|
12
|
+
bytes.forEach((b) => { s += String.fromCharCode(b); });
|
|
13
|
+
return btoa(s).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
14
|
+
}
|
|
15
|
+
function fromB64url(s) {
|
|
16
|
+
if (!/^[A-Za-z0-9_-]*$/.test(s))
|
|
17
|
+
throw new SessionTokenError('malformed', 'Token is not base64url');
|
|
18
|
+
const bin = atob(s.replace(/-/g, '+').replace(/_/g, '/') + '='.repeat((4 - (s.length % 4)) % 4));
|
|
19
|
+
return Uint8Array.from(bin, (c) => c.charCodeAt(0));
|
|
20
|
+
}
|
|
21
|
+
const key = (secret, usage) => crypto.subtle.importKey('raw', enc.encode(secret), { name: 'HMAC', hash: 'SHA-256' }, false, [usage]);
|
|
22
|
+
/** Why a token was refused. Answer the request with 401. */
|
|
23
|
+
export class SessionTokenError extends Error {
|
|
24
|
+
constructor(code, message) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = 'SessionTokenError';
|
|
27
|
+
this.code = code;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** Signs a session token. Fills `iat`, `nbf`, `exp` (60 s) and `jti` if they are missing. Server-side only. */
|
|
31
|
+
export async function signSessionToken(claims, secret) {
|
|
32
|
+
if (!secret)
|
|
33
|
+
throw new TypeError('signSessionToken: secret is required');
|
|
34
|
+
const now = Math.floor(Date.now() / 1000);
|
|
35
|
+
const full = { iat: now, nbf: now, exp: now + 60, jti: crypto.randomUUID(), ...claims };
|
|
36
|
+
const head = b64url(enc.encode(JSON.stringify({ alg: 'HS256', typ: 'JWT' })));
|
|
37
|
+
const body = b64url(enc.encode(JSON.stringify(full)));
|
|
38
|
+
const sig = await crypto.subtle.sign('HMAC', await key(secret, 'sign'), enc.encode(`${head}.${body}`));
|
|
39
|
+
return `${head}.${body}.${b64url(new Uint8Array(sig))}`;
|
|
40
|
+
}
|
|
41
|
+
/** Reads a token's claims WITHOUT checking it. Never trust the result; use `verifySessionToken` on your server. */
|
|
42
|
+
export function decodeClaims(token) {
|
|
43
|
+
const parts = String(token).split('.');
|
|
44
|
+
if (parts.length !== 3)
|
|
45
|
+
throw new SessionTokenError('malformed', 'Token must have three parts');
|
|
46
|
+
try {
|
|
47
|
+
return JSON.parse(dec.decode(fromB64url(parts[1])));
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
if (e instanceof SessionTokenError)
|
|
51
|
+
throw e;
|
|
52
|
+
throw new SessionTokenError('malformed', 'Token claims are not JSON');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Verifies a session token on your backend: HS256 only (other algorithms, including `none`, are refused),
|
|
57
|
+
* the signature, `exp`, `nbf`, `aud` and optionally `iss`. Returns the claims, or throws `SessionTokenError`.
|
|
58
|
+
*/
|
|
59
|
+
export async function verifySessionToken(token, secret, options) {
|
|
60
|
+
if (!secret)
|
|
61
|
+
throw new TypeError('verifySessionToken: secret is required');
|
|
62
|
+
if (!options?.audience)
|
|
63
|
+
throw new TypeError('verifySessionToken: options.audience (your API key) is required');
|
|
64
|
+
if (options.issuer && isOriginPattern(options.issuer))
|
|
65
|
+
parseOriginPattern(options.issuer, 'verifySessionToken: options.issuer'); // a bad setting throws TypeError, not a 401
|
|
66
|
+
const parts = String(token).split('.');
|
|
67
|
+
if (parts.length !== 3)
|
|
68
|
+
throw new SessionTokenError('malformed', 'Token must have three parts');
|
|
69
|
+
let header;
|
|
70
|
+
try {
|
|
71
|
+
header = JSON.parse(dec.decode(fromB64url(parts[0])));
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
throw new SessionTokenError('malformed', 'Token header is not JSON');
|
|
75
|
+
}
|
|
76
|
+
if (header.alg !== 'HS256')
|
|
77
|
+
throw new SessionTokenError('algorithm', `Algorithm ${header.alg} is not allowed`);
|
|
78
|
+
const ok = await crypto.subtle.verify('HMAC', await key(secret, 'verify'), fromB64url(parts[2]), enc.encode(`${parts[0]}.${parts[1]}`));
|
|
79
|
+
if (!ok)
|
|
80
|
+
throw new SessionTokenError('signature', 'Signature does not match');
|
|
81
|
+
const c = decodeClaims(token);
|
|
82
|
+
const now = options.now ?? Math.floor(Date.now() / 1000);
|
|
83
|
+
const tol = options.clockTolerance ?? 5;
|
|
84
|
+
if (typeof c.exp !== 'number' || now - tol >= c.exp)
|
|
85
|
+
throw new SessionTokenError('expired', 'Token has expired');
|
|
86
|
+
if (typeof c.nbf === 'number' && now + tol < c.nbf)
|
|
87
|
+
throw new SessionTokenError('not-yet-valid', 'Token is not valid yet');
|
|
88
|
+
if (c.aud !== options.audience)
|
|
89
|
+
throw new SessionTokenError('audience', 'Token is for another app');
|
|
90
|
+
if (options.issuer && !matchesOrigin(options.issuer, c.iss))
|
|
91
|
+
throw new SessionTokenError('issuer', 'Token was issued by someone else');
|
|
92
|
+
return c;
|
|
93
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@commmerce/app-bridge",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0-rc.3",
|
|
4
|
+
"description": "Connects an app embedded in the Commmerce admin to the admin: toasts, modals, navigation, save bar, resource picker, loading bar and session tokens.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"commmerce",
|
|
7
|
+
"app-bridge",
|
|
8
|
+
"postmessage",
|
|
9
|
+
"iframe",
|
|
10
|
+
"embedded-app"
|
|
11
|
+
],
|
|
12
|
+
"author": "Commmerce",
|
|
13
|
+
"homepage": "https://commmerce.com/blog/commmerceui/",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"email": "support@commmerce.com"
|
|
16
|
+
},
|
|
17
|
+
"license": "SEE LICENSE IN LICENSE.md",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"main": "./dist/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"default": "./dist/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./session-token": {
|
|
27
|
+
"types": "./dist/session-token.d.ts",
|
|
28
|
+
"default": "./dist/session-token.js"
|
|
29
|
+
},
|
|
30
|
+
"./app-bridge.js": "./dist/app-bridge.js"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"LICENSE.md",
|
|
35
|
+
"README.md"
|
|
36
|
+
],
|
|
37
|
+
"sideEffects": false,
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "tsc -p tsconfig.json && esbuild src/index.ts --bundle --minify --format=esm --target=es2021 --outfile=dist/app-bridge.js",
|
|
43
|
+
"test": "vitest run"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@commmerce/ui": "1.0.0-rc.3",
|
|
47
|
+
"@vitest/browser": "^2.1.8",
|
|
48
|
+
"playwright": "^1.49.0"
|
|
49
|
+
}
|
|
50
|
+
}
|