@mittwald/ext-bridge 1.6.0-next.9 → 1.6.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/MIGRATION.md +19 -0
- package/USAGE.md +23 -1
- package/dist/js/config/extractHostConfig.mjs +1 -2
- package/dist/js/config/getConfig.mjs +1 -2
- package/dist/js/config/parse.mjs +1 -2
- package/dist/js/config/schemas.mjs +1 -2
- package/dist/js/environment.mjs +1 -2
- package/dist/js/error.mjs +3 -4
- package/dist/js/error.mjs.map +1 -1
- package/dist/js/getExtBridge.mjs +1 -2
- package/dist/js/global-browser.mjs +1 -2
- package/dist/js/i18next.mjs +1 -2
- package/dist/js/index-browser.mjs +1 -2
- package/dist/js/integrations/i18next/modules/remoteLanguageDetectorModule.mjs +1 -2
- package/dist/js/lib/assertBrowserEnv.mjs +1 -2
- package/dist/js/lib/controllablePromise.mjs +1 -2
- package/dist/js/react/hooks/useConfig.mjs +1 -2
- package/dist/js/react/hooks/useExtBridge.mjs +1 -2
- package/dist/js/react/hooks/useLanguage.mjs +1 -2
- package/dist/js/readiness.mjs +1 -2
- package/dist/js/sessionToken/decode.mjs +1 -2
- package/dist/js/sessionToken/getAccessToken.mjs +1 -2
- package/dist/js/sessionToken/getSessionToken.mjs +1 -2
- package/dist/js/sessionToken/index.mjs +1 -2
- package/dist/js/sessionToken/publicKeys/fetchPublicKey.mjs +7 -5
- package/dist/js/sessionToken/publicKeys/fetchPublicKey.mjs.map +1 -1
- package/dist/js/sessionToken/publicKeys/index.mjs +1 -2
- package/dist/js/sessionToken/publicKeys/publicKeys.mjs +35 -6
- package/dist/js/sessionToken/publicKeys/publicKeys.mjs.map +1 -1
- package/dist/js/sessionToken/publicKeys/serial.mjs +6 -0
- package/dist/js/sessionToken/publicKeys/serial.mjs.map +1 -0
- package/dist/js/sessionToken/schemas.mjs +3 -3
- package/dist/js/sessionToken/schemas.mjs.map +1 -1
- package/dist/js/sessionToken/verify.mjs +15 -4
- package/dist/js/sessionToken/verify.mjs.map +1 -1
- package/dist/types/error.d.ts +1 -1
- package/dist/types/index-node.d.ts +1 -1
- package/dist/types/sessionToken/index.d.ts +1 -1
- package/dist/types/sessionToken/publicKeys/index.d.ts +1 -1
- package/dist/types/sessionToken/publicKeys/publicKeys.d.ts +1 -0
- package/dist/types/sessionToken/publicKeys/serial.d.ts +2 -0
- package/dist/types/sessionToken/verify.d.ts +8 -1
- package/package.json +6 -5
package/MIGRATION.md
CHANGED
|
@@ -2,6 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
---
|
|
4
4
|
|
|
5
|
+
## From version `1.5.63` to `>=1.5.64`
|
|
6
|
+
|
|
7
|
+
### `verify` requires your `extensionId`
|
|
8
|
+
|
|
9
|
+
Session tokens now carry the extension ID as audience (`aud`). `verify` takes
|
|
10
|
+
your extension ID and rejects every session token issued for another extension.
|
|
11
|
+
Without `extensionId` it throws an `ExtBridgeError`:
|
|
12
|
+
|
|
13
|
+
```diff
|
|
14
|
+
import { verify } from "@mittwald/ext-bridge/node";
|
|
15
|
+
|
|
16
|
+
- const decoded = await verify(sessionToken);
|
|
17
|
+
+ const decoded = await verify(sessionToken, {
|
|
18
|
+
+ extensionId: process.env.EXTENSION_ID!,
|
|
19
|
+
+ });
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
5
24
|
## From version `0.2.0-alpha.1050` to `>=0.2.0-alpha.1051`
|
|
6
25
|
|
|
7
26
|
### Context parameters are typed, and config values are never `null`
|
package/USAGE.md
CHANGED
|
@@ -65,7 +65,9 @@ const sessionToken = await getSessionToken();
|
|
|
65
65
|
|
|
66
66
|
// 2. in your backend — verify before trusting anything in it
|
|
67
67
|
import { verify, getAccessToken } from "@mittwald/ext-bridge/node";
|
|
68
|
-
const decoded = await verify(sessionToken
|
|
68
|
+
const decoded = await verify(sessionToken, {
|
|
69
|
+
extensionId: process.env.EXTENSION_ID!, // JWT signature + audience check
|
|
70
|
+
});
|
|
69
71
|
|
|
70
72
|
// 3. exchange it for an mStudio API token
|
|
71
73
|
const { publicToken, expiry } = await getAccessToken(
|
|
@@ -76,11 +78,31 @@ const { publicToken, expiry } = await getAccessToken(
|
|
|
76
78
|
|
|
77
79
|
- **Always `verify` first.** A session token arrives from the browser; without
|
|
78
80
|
the signature check its contents are attacker-controlled.
|
|
81
|
+
- **`extensionId` is required.** A valid signature only proves mStudio issued
|
|
82
|
+
the token — not that it was issued for _your_ extension. `verify` rejects
|
|
83
|
+
session tokens issued for any other extension, and throws if `extensionId` is
|
|
84
|
+
missing.
|
|
79
85
|
- The extension secret belongs in your backend's environment, never in the
|
|
80
86
|
bundle.
|
|
81
87
|
- `getAccessToken` returns an `expiry` — respect it rather than caching the
|
|
82
88
|
token indefinitely.
|
|
83
89
|
|
|
90
|
+
## Allow only mStudio to embed your frontend
|
|
91
|
+
|
|
92
|
+
Your extension's frontend is meant to run inside mStudio only.
|
|
93
|
+
|
|
94
|
+
- **Required:** send a `Content-Security-Policy` **response header** with
|
|
95
|
+
`frame-ancestors` limited to the mStudio origins:
|
|
96
|
+
`frame-ancestors https://*.mittwald.de https://*.mittwald.systems`. No bare
|
|
97
|
+
`*`, no `https:`. A `<meta>` tag does not work for `frame-ancestors`.
|
|
98
|
+
- `RemoteRoot` accepts messages only from the mStudio origins
|
|
99
|
+
(`https://*.mittwald.de`, `https://*.mittwald.systems`) and from your app's
|
|
100
|
+
own origin. For local development against another host, pass the extra origins
|
|
101
|
+
as `allowedHostOrigins`.
|
|
102
|
+
- Paths the host sends (`onHostPathnameChanged`) are always relative app paths
|
|
103
|
+
like `/settings?tab=1`. Anything else (absolute URLs, `//host`, `javascript:`)
|
|
104
|
+
is dropped before it reaches your router.
|
|
105
|
+
|
|
84
106
|
## Stability
|
|
85
107
|
|
|
86
108
|
The config schema carries an explicit contract: existing properties are never
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
// src/config/extractHostConfig.ts
|
|
2
2
|
var extractHostConfig = (config) => ({
|
|
3
3
|
language: config.language,
|
|
4
4
|
theme: config.theme
|
|
5
5
|
});
|
|
6
|
-
//#endregion
|
|
7
6
|
export { extractHostConfig };
|
|
8
7
|
|
|
9
8
|
//# sourceMappingURL=extractHostConfig.mjs.map
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { getExtBridge } from "../getExtBridge.mjs";
|
|
2
|
-
|
|
2
|
+
// src/config/getConfig.ts
|
|
3
3
|
var getConfig = async () => {
|
|
4
4
|
return (await getExtBridge()).config;
|
|
5
5
|
};
|
|
6
|
-
//#endregion
|
|
7
6
|
export { getConfig };
|
|
8
7
|
|
|
9
8
|
//# sourceMappingURL=getConfig.mjs.map
|
package/dist/js/config/parse.mjs
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import { ExtBridgeError } from "../error.mjs";
|
|
2
2
|
import { config } from "./schemas.mjs";
|
|
3
|
-
|
|
3
|
+
// src/config/parse.ts
|
|
4
4
|
var parseConfig = (something) => {
|
|
5
5
|
const parsed = config.safeParse(something);
|
|
6
6
|
if (parsed.error) throw new ExtBridgeError("Invalid config: " + parsed.error.message);
|
|
7
7
|
return parsed.data;
|
|
8
8
|
};
|
|
9
|
-
//#endregion
|
|
10
9
|
export { parseConfig };
|
|
11
10
|
|
|
12
11
|
//# sourceMappingURL=parse.mjs.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
|
|
2
|
+
// src/config/schemas.ts
|
|
3
3
|
/**
|
|
4
4
|
* Schema of an optional config value: `string | undefined` to a consumer.
|
|
5
5
|
*
|
|
@@ -59,7 +59,6 @@ var contextParameters = z.object({
|
|
|
59
59
|
* and the runtime parse must agree about unknown keys.
|
|
60
60
|
*/
|
|
61
61
|
var config = baseConfig.extend(contextParameters.shape).catchall(optionalString);
|
|
62
|
-
//#endregion
|
|
63
62
|
export { config };
|
|
64
63
|
|
|
65
64
|
//# sourceMappingURL=schemas.mjs.map
|
package/dist/js/environment.mjs
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { env } from "std-env";
|
|
2
|
-
|
|
2
|
+
// src/environment.ts
|
|
3
3
|
/** @internal */
|
|
4
4
|
var MW_EXT_API_URL = env["MW_EXT_API_URL"] ?? "https://api.mittwald.de";
|
|
5
|
-
//#endregion
|
|
6
5
|
export { MW_EXT_API_URL };
|
|
7
6
|
|
|
8
7
|
//# sourceMappingURL=environment.mjs.map
|
package/dist/js/error.mjs
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
// src/error.ts
|
|
2
2
|
var ExtBridgeError = class extends Error {
|
|
3
|
-
constructor(message) {
|
|
4
|
-
super(message);
|
|
3
|
+
constructor(message, options) {
|
|
4
|
+
super(message, options);
|
|
5
5
|
this.message = message;
|
|
6
6
|
}
|
|
7
7
|
};
|
|
8
8
|
ExtBridgeError.prototype.name = "ExtBridgeError";
|
|
9
|
-
//#endregion
|
|
10
9
|
export { ExtBridgeError };
|
|
11
10
|
|
|
12
11
|
//# sourceMappingURL=error.mjs.map
|
package/dist/js/error.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"error.mjs","names":[],"sources":["../../src/error.ts"],"sourcesContent":["export class ExtBridgeError extends Error {\n constructor(message: string) {\n super(message);\n this.message = message;\n }\n}\n\nExtBridgeError.prototype.name = \"ExtBridgeError\";\n"],"mappings":";AAAA,IAAa,iBAAb,cAAoC,MAAM;CACxC,YAAY,SAAiB;
|
|
1
|
+
{"version":3,"file":"error.mjs","names":[],"sources":["../../src/error.ts"],"sourcesContent":["export class ExtBridgeError extends Error {\n constructor(message: string, options?: ErrorOptions) {\n super(message, options);\n this.message = message;\n }\n}\n\nExtBridgeError.prototype.name = \"ExtBridgeError\";\n"],"mappings":";AAAA,IAAa,iBAAb,cAAoC,MAAM;CACxC,YAAY,SAAiB,SAAwB;EACnD,MAAM,SAAS,OAAO;EACtB,KAAK,UAAU;CACjB;AACF;AAEA,eAAe,UAAU,OAAO"}
|
package/dist/js/getExtBridge.mjs
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { initExtBridge } from "./global-browser.mjs";
|
|
2
|
-
|
|
2
|
+
// src/getExtBridge.ts
|
|
3
3
|
var getExtBridge = async () => {
|
|
4
4
|
initExtBridge();
|
|
5
5
|
await globalThis.mwExtBridge.readiness.isReady();
|
|
6
6
|
return globalThis.mwExtBridge;
|
|
7
7
|
};
|
|
8
|
-
//#endregion
|
|
9
8
|
export { getExtBridge };
|
|
10
9
|
|
|
11
10
|
//# sourceMappingURL=getExtBridge.mjs.map
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { readinessApi } from "./readiness.mjs";
|
|
2
|
-
|
|
2
|
+
// src/global-browser.ts
|
|
3
3
|
if (typeof globalThis.mwExtBridge !== "undefined") console.warn("mwExtBridge is already defined. The @mittwald/ext-bridge package is probably installed multiple times.");
|
|
4
4
|
var mwExtBridge = { readiness: readinessApi };
|
|
5
5
|
function initExtBridge() {
|
|
6
6
|
globalThis.mwExtBridge = mwExtBridge;
|
|
7
7
|
}
|
|
8
|
-
//#endregion
|
|
9
8
|
export { initExtBridge, mwExtBridge };
|
|
10
9
|
|
|
11
10
|
//# sourceMappingURL=global-browser.mjs.map
|
package/dist/js/i18next.mjs
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { remoteLanguageDetectorModule };
|
|
1
|
+
export { remoteLanguageDetectorModule } from "./integrations/i18next/modules/remoteLanguageDetectorModule.mjs";
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { initExtBridge } from "./global-browser.mjs";
|
|
2
2
|
import { getConfig } from "./config/getConfig.mjs";
|
|
3
3
|
import { getSessionToken } from "./sessionToken/getSessionToken.mjs";
|
|
4
|
-
|
|
4
|
+
// src/index-browser.ts
|
|
5
5
|
initExtBridge();
|
|
6
|
-
//#endregion
|
|
7
6
|
export { getConfig, getSessionToken, initExtBridge };
|
|
8
7
|
|
|
9
8
|
//# sourceMappingURL=index-browser.mjs.map
|
|
@@ -2,7 +2,7 @@ import { isBrowser } from "../../../lib/assertBrowserEnv.mjs";
|
|
|
2
2
|
import { getConfig } from "../../../config/getConfig.mjs";
|
|
3
3
|
import "../../../index-browser.mjs";
|
|
4
4
|
import "i18next";
|
|
5
|
-
|
|
5
|
+
// src/integrations/i18next/modules/remoteLanguageDetectorModule.ts
|
|
6
6
|
var remoteLanguageDetectorModule = () => ({
|
|
7
7
|
type: "languageDetector",
|
|
8
8
|
async: true,
|
|
@@ -10,7 +10,6 @@ var remoteLanguageDetectorModule = () => ({
|
|
|
10
10
|
if (isBrowser) return (await getConfig()).language;
|
|
11
11
|
}
|
|
12
12
|
});
|
|
13
|
-
//#endregion
|
|
14
13
|
export { remoteLanguageDetectorModule };
|
|
15
14
|
|
|
16
15
|
//# sourceMappingURL=remoteLanguageDetectorModule.mjs.map
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { ExtBridgeError } from "../error.mjs";
|
|
2
|
-
|
|
2
|
+
// src/lib/assertBrowserEnv.ts
|
|
3
3
|
var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
|
|
4
4
|
var assertBrowserEnv = () => {
|
|
5
5
|
if (!isBrowser) throw new ExtBridgeError("Ext Bridge can only be used in a browser environment. To opt-out from SSR you can use <BrowserOnly> from '@mittwald/flow-remote-react-components'.");
|
|
6
6
|
};
|
|
7
|
-
//#endregion
|
|
8
7
|
export { assertBrowserEnv, isBrowser };
|
|
9
8
|
|
|
10
9
|
//# sourceMappingURL=assertBrowserEnv.mjs.map
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ExtBridgeError } from "../error.mjs";
|
|
2
|
-
|
|
2
|
+
// src/lib/controllablePromise.ts
|
|
3
3
|
var controllablePromise = () => {
|
|
4
4
|
let resolve = () => {
|
|
5
5
|
throw new ExtBridgeError("Unexpected call of resolve()");
|
|
@@ -16,7 +16,6 @@ var controllablePromise = () => {
|
|
|
16
16
|
reject
|
|
17
17
|
];
|
|
18
18
|
};
|
|
19
|
-
//#endregion
|
|
20
19
|
export { controllablePromise };
|
|
21
20
|
|
|
22
21
|
//# sourceMappingURL=controllablePromise.mjs.map
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { useExtBridge } from "./useExtBridge.mjs";
|
|
2
|
-
|
|
2
|
+
// src/react/hooks/useConfig.ts
|
|
3
3
|
var useConfig = () => {
|
|
4
4
|
return useExtBridge().config;
|
|
5
5
|
};
|
|
6
|
-
//#endregion
|
|
7
6
|
export { useConfig };
|
|
8
7
|
|
|
9
8
|
//# sourceMappingURL=useConfig.mjs.map
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { getExtBridge } from "../../getExtBridge.mjs";
|
|
2
2
|
import { usePromise } from "@mittwald/react-use-promise";
|
|
3
|
-
|
|
3
|
+
// src/react/hooks/useExtBridge.ts
|
|
4
4
|
var useExtBridge = () => {
|
|
5
5
|
return usePromise(getExtBridge, []);
|
|
6
6
|
};
|
|
7
|
-
//#endregion
|
|
8
7
|
export { useExtBridge };
|
|
9
8
|
|
|
10
9
|
//# sourceMappingURL=useExtBridge.mjs.map
|
package/dist/js/readiness.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { parseConfig } from "./config/parse.mjs";
|
|
|
3
3
|
import { assertBrowserEnv } from "./lib/assertBrowserEnv.mjs";
|
|
4
4
|
import { controllablePromise } from "./lib/controllablePromise.mjs";
|
|
5
5
|
import { extractHostConfig } from "./config/extractHostConfig.mjs";
|
|
6
|
-
|
|
6
|
+
// src/readiness.ts
|
|
7
7
|
var timeoutMs = 7500;
|
|
8
8
|
var [readiness, resolveReadiness] = controllablePromise();
|
|
9
9
|
var readinessApi = {
|
|
@@ -34,7 +34,6 @@ var readinessApi = {
|
|
|
34
34
|
resolveReadiness();
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
|
-
//#endregion
|
|
38
37
|
export { readinessApi };
|
|
39
38
|
|
|
40
39
|
//# sourceMappingURL=readiness.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ExtBridgeError } from "../error.mjs";
|
|
2
2
|
import { sessionTokenPayload } from "./schemas.mjs";
|
|
3
3
|
import { decodeJwt } from "jose";
|
|
4
|
-
|
|
4
|
+
// src/sessionToken/decode.ts
|
|
5
5
|
var decode = (jwt) => {
|
|
6
6
|
const { sub, aud: ignoredAud, exp: ignoredExp, iat: ignoredIat, iss: ignoredIss, jti: ignoredJti, nbf: ignoredNbf, ...rest } = decodeJwt(jwt);
|
|
7
7
|
const parsed = sessionTokenPayload.safeParse({
|
|
@@ -11,7 +11,6 @@ var decode = (jwt) => {
|
|
|
11
11
|
if (parsed.error) throw new ExtBridgeError("Session token payload invalid: " + parsed.error.message);
|
|
12
12
|
return parsed.data;
|
|
13
13
|
};
|
|
14
|
-
//#endregion
|
|
15
14
|
export { decode };
|
|
16
15
|
|
|
17
16
|
//# sourceMappingURL=decode.mjs.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { MW_EXT_API_URL } from "../environment.mjs";
|
|
2
2
|
import axios from "axios";
|
|
3
|
-
|
|
3
|
+
// src/sessionToken/getAccessToken.ts
|
|
4
4
|
var getAccessToken = async (sessionToken, extensionSecret) => {
|
|
5
5
|
return (await axios({
|
|
6
6
|
url: `${MW_EXT_API_URL}/v2/authenticate-session-token`,
|
|
@@ -12,7 +12,6 @@ var getAccessToken = async (sessionToken, extensionSecret) => {
|
|
|
12
12
|
method: "POST"
|
|
13
13
|
})).data;
|
|
14
14
|
};
|
|
15
|
-
//#endregion
|
|
16
15
|
export { getAccessToken };
|
|
17
16
|
|
|
18
17
|
//# sourceMappingURL=getAccessToken.mjs.map
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { getExtBridge } from "../getExtBridge.mjs";
|
|
2
|
-
|
|
2
|
+
// src/sessionToken/getSessionToken.ts
|
|
3
3
|
var getSessionToken = async () => {
|
|
4
4
|
return (await getExtBridge()).connection.getSessionToken();
|
|
5
5
|
};
|
|
6
|
-
//#endregion
|
|
7
6
|
export { getSessionToken };
|
|
8
7
|
|
|
9
8
|
//# sourceMappingURL=getSessionToken.mjs.map
|
|
@@ -1,17 +1,19 @@
|
|
|
1
|
+
import { ExtBridgeError } from "../../error.mjs";
|
|
1
2
|
import { MW_EXT_API_URL } from "../../environment.mjs";
|
|
2
3
|
import axios from "axios";
|
|
3
|
-
|
|
4
|
+
// src/sessionToken/publicKeys/fetchPublicKey.ts
|
|
4
5
|
var fetchPublicKey = async (serial) => {
|
|
5
|
-
|
|
6
|
-
url: `${MW_EXT_API_URL}/v2/public-keys/${serial}`,
|
|
6
|
+
const response = await axios({
|
|
7
|
+
url: `${MW_EXT_API_URL}/v2/public-keys/${encodeURIComponent(serial)}`,
|
|
7
8
|
validateStatus: (status) => status === 200,
|
|
8
9
|
params: {
|
|
9
10
|
format: "spki",
|
|
10
11
|
purpose: "session_token"
|
|
11
12
|
}
|
|
12
|
-
})
|
|
13
|
+
});
|
|
14
|
+
if (typeof response.data !== "object" || response.data === null || typeof response.data.key !== "string" || response.data.key.length === 0) throw new ExtBridgeError("Public key response invalid");
|
|
15
|
+
return response.data.key;
|
|
13
16
|
};
|
|
14
|
-
//#endregion
|
|
15
17
|
export { fetchPublicKey };
|
|
16
18
|
|
|
17
19
|
//# sourceMappingURL=fetchPublicKey.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchPublicKey.mjs","names":[],"sources":["../../../../src/sessionToken/publicKeys/fetchPublicKey.ts"],"sourcesContent":["import { MW_EXT_API_URL } from \"@/environment\";\nimport axios from \"axios\";\n\ninterface ApiResponse {\n algorithm: string;\n key: string;\n serial: string;\n}\n\nexport const fetchPublicKey = async (serial: string) => {\n const response = await axios<ApiResponse>({\n url: `${MW_EXT_API_URL}/v2/public-keys/${serial}`,\n validateStatus: (status) => status === 200,\n params: {\n format: \"spki\",\n purpose: \"session_token\",\n },\n });\n return response.data.key;\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetchPublicKey.mjs","names":[],"sources":["../../../../src/sessionToken/publicKeys/fetchPublicKey.ts"],"sourcesContent":["import { MW_EXT_API_URL } from \"@/environment\";\nimport { ExtBridgeError } from \"@/error\";\nimport axios from \"axios\";\n\ninterface ApiResponse {\n algorithm: string;\n key: string;\n serial: string;\n}\n\nexport const fetchPublicKey = async (serial: string) => {\n const response = await axios<ApiResponse>({\n url: `${MW_EXT_API_URL}/v2/public-keys/${encodeURIComponent(serial)}`,\n validateStatus: (status) => status === 200,\n params: {\n format: \"spki\",\n purpose: \"session_token\",\n },\n });\n\n if (\n typeof response.data !== \"object\" ||\n response.data === null ||\n typeof response.data.key !== \"string\" ||\n response.data.key.length === 0\n ) {\n throw new ExtBridgeError(\"Public key response invalid\");\n }\n\n return response.data.key;\n};\n"],"mappings":";;;;AAUA,IAAa,iBAAiB,OAAO,WAAmB;CACtD,MAAM,WAAW,MAAM,MAAmB;EACxC,KAAK,GAAG,eAAe,kBAAkB,mBAAmB,MAAM;EAClE,iBAAiB,WAAW,WAAW;EACvC,QAAQ;GACN,QAAQ;GACR,SAAS;EACX;CACF,CAAC;CAED,IACE,OAAO,SAAS,SAAS,YACzB,SAAS,SAAS,QAClB,OAAO,SAAS,KAAK,QAAQ,YAC7B,SAAS,KAAK,IAAI,WAAW,GAE7B,MAAM,IAAI,eAAe,6BAA6B;CAGxD,OAAO,SAAS,KAAK;AACvB"}
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { getPublicKey };
|
|
1
|
+
export { getPublicKey } from "./publicKeys.mjs";
|
|
@@ -1,16 +1,45 @@
|
|
|
1
|
+
import { ExtBridgeError } from "../../error.mjs";
|
|
2
|
+
import { isValidPublicKeySerial } from "./serial.mjs";
|
|
1
3
|
import { fetchPublicKey } from "./fetchPublicKey.mjs";
|
|
2
4
|
import { importSPKI } from "jose";
|
|
3
|
-
|
|
5
|
+
import axios from "axios";
|
|
6
|
+
// src/sessionToken/publicKeys/publicKeys.ts
|
|
4
7
|
var cache = /* @__PURE__ */ new Map();
|
|
8
|
+
var failedLookupCache = /* @__PURE__ */ new Map();
|
|
9
|
+
var FAILED_LOOKUP_TTL_MS = 6e4;
|
|
10
|
+
var FAILED_LOOKUP_MAX_ENTRIES = 1e3;
|
|
11
|
+
var isDefiniteFailure = (error) => {
|
|
12
|
+
if (axios.isAxiosError(error)) return error.response?.status === 404;
|
|
13
|
+
return true;
|
|
14
|
+
};
|
|
15
|
+
var cacheFailedLookup = (serial) => {
|
|
16
|
+
if (failedLookupCache.size >= FAILED_LOOKUP_MAX_ENTRIES) {
|
|
17
|
+
const oldestSerial = failedLookupCache.keys().next().value;
|
|
18
|
+
if (oldestSerial !== void 0) failedLookupCache.delete(oldestSerial);
|
|
19
|
+
}
|
|
20
|
+
failedLookupCache.set(serial, Date.now() + FAILED_LOOKUP_TTL_MS);
|
|
21
|
+
};
|
|
5
22
|
var getPublicKey = async (serial) => {
|
|
23
|
+
if (!isValidPublicKeySerial(serial)) throw new ExtBridgeError("Session token public key serial invalid");
|
|
6
24
|
const cached = cache.get(serial);
|
|
7
25
|
if (cached) return cached;
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
26
|
+
const failedLookupExpiry = failedLookupCache.get(serial);
|
|
27
|
+
if (failedLookupExpiry !== void 0) {
|
|
28
|
+
if (failedLookupExpiry > Date.now()) throw new ExtBridgeError("Public key lookup for this serial failed recently");
|
|
29
|
+
failedLookupCache.delete(serial);
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
const publicKeyFromApi = await fetchPublicKey(serial);
|
|
33
|
+
const cryptoKey = await importSPKI(publicKeyFromApi, "Ed25519");
|
|
34
|
+
cache.set(serial, cryptoKey);
|
|
35
|
+
failedLookupCache.delete(serial);
|
|
36
|
+
return cryptoKey;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (isDefiniteFailure(error)) cacheFailedLookup(serial);
|
|
39
|
+
if (error instanceof ExtBridgeError) throw error;
|
|
40
|
+
throw new ExtBridgeError("Public key lookup failed", { cause: error });
|
|
41
|
+
}
|
|
12
42
|
};
|
|
13
|
-
//#endregion
|
|
14
43
|
export { getPublicKey };
|
|
15
44
|
|
|
16
45
|
//# sourceMappingURL=publicKeys.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"publicKeys.mjs","names":[],"sources":["../../../../src/sessionToken/publicKeys/publicKeys.ts"],"sourcesContent":["import { fetchPublicKey } from \"@/sessionToken/publicKeys/fetchPublicKey\";\nimport { importSPKI } from \"jose\";\n\nconst cache = new Map<string, CryptoKey>();\n\nexport const getPublicKey = async (serial: string): Promise<CryptoKey> => {\n const cached = cache.get(serial);\n if (cached) {\n return cached;\n }\n\n const publicKeyFromApi = await fetchPublicKey(serial);\n
|
|
1
|
+
{"version":3,"file":"publicKeys.mjs","names":[],"sources":["../../../../src/sessionToken/publicKeys/publicKeys.ts"],"sourcesContent":["import { ExtBridgeError } from \"@/error\";\nimport { fetchPublicKey } from \"@/sessionToken/publicKeys/fetchPublicKey\";\nimport { isValidPublicKeySerial } from \"@/sessionToken/publicKeys/serial\";\nimport axios from \"axios\";\nimport { importSPKI } from \"jose\";\n\nconst cache = new Map<string, CryptoKey>();\nconst failedLookupCache = new Map<string, number>();\nconst FAILED_LOOKUP_TTL_MS = 60_000;\nconst FAILED_LOOKUP_MAX_ENTRIES = 1000;\n\nconst isDefiniteFailure = (error: unknown): boolean => {\n if (axios.isAxiosError(error)) {\n return error.response?.status === 404;\n }\n\n return true;\n};\n\nconst cacheFailedLookup = (serial: string) => {\n if (failedLookupCache.size >= FAILED_LOOKUP_MAX_ENTRIES) {\n const oldestSerial = failedLookupCache.keys().next().value;\n if (oldestSerial !== undefined) {\n failedLookupCache.delete(oldestSerial);\n }\n }\n\n failedLookupCache.set(serial, Date.now() + FAILED_LOOKUP_TTL_MS);\n};\n\nexport const getPublicKey = async (serial: string): Promise<CryptoKey> => {\n if (!isValidPublicKeySerial(serial)) {\n throw new ExtBridgeError(\"Session token public key serial invalid\");\n }\n\n const cached = cache.get(serial);\n if (cached) {\n return cached;\n }\n\n const failedLookupExpiry = failedLookupCache.get(serial);\n if (failedLookupExpiry !== undefined) {\n if (failedLookupExpiry > Date.now()) {\n throw new ExtBridgeError(\n \"Public key lookup for this serial failed recently\",\n );\n }\n failedLookupCache.delete(serial);\n }\n\n try {\n const publicKeyFromApi = await fetchPublicKey(serial);\n const cryptoKey = await importSPKI(publicKeyFromApi, \"Ed25519\");\n\n cache.set(serial, cryptoKey);\n failedLookupCache.delete(serial);\n return cryptoKey;\n } catch (error) {\n if (isDefiniteFailure(error)) {\n cacheFailedLookup(serial);\n }\n\n if (error instanceof ExtBridgeError) {\n throw error;\n }\n\n throw new ExtBridgeError(\"Public key lookup failed\", { cause: error });\n }\n};\n\nexport const resetPublicKeyCaches = () => {\n cache.clear();\n failedLookupCache.clear();\n};\n"],"mappings":";;;;;;AAMA,IAAM,wBAAQ,IAAI,IAAuB;AACzC,IAAM,oCAAoB,IAAI,IAAoB;AAClD,IAAM,uBAAuB;AAC7B,IAAM,4BAA4B;AAElC,IAAM,qBAAqB,UAA4B;CACrD,IAAI,MAAM,aAAa,KAAK,GAC1B,OAAO,MAAM,UAAU,WAAW;CAGpC,OAAO;AACT;AAEA,IAAM,qBAAqB,WAAmB;CAC5C,IAAI,kBAAkB,QAAQ,2BAA2B;EACvD,MAAM,eAAe,kBAAkB,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;EACrD,IAAI,iBAAiB,KAAA,GACnB,kBAAkB,OAAO,YAAY;CAEzC;CAEA,kBAAkB,IAAI,QAAQ,KAAK,IAAI,IAAI,oBAAoB;AACjE;AAEA,IAAa,eAAe,OAAO,WAAuC;CACxE,IAAI,CAAC,uBAAuB,MAAM,GAChC,MAAM,IAAI,eAAe,yCAAyC;CAGpE,MAAM,SAAS,MAAM,IAAI,MAAM;CAC/B,IAAI,QACF,OAAO;CAGT,MAAM,qBAAqB,kBAAkB,IAAI,MAAM;CACvD,IAAI,uBAAuB,KAAA,GAAW;EACpC,IAAI,qBAAqB,KAAK,IAAI,GAChC,MAAM,IAAI,eACR,mDACF;EAEF,kBAAkB,OAAO,MAAM;CACjC;CAEA,IAAI;EACF,MAAM,mBAAmB,MAAM,eAAe,MAAM;EACpD,MAAM,YAAY,MAAM,WAAW,kBAAkB,SAAS;EAE9D,MAAM,IAAI,QAAQ,SAAS;EAC3B,kBAAkB,OAAO,MAAM;EAC/B,OAAO;CACT,SAAS,OAAO;EACd,IAAI,kBAAkB,KAAK,GACzB,kBAAkB,MAAM;EAG1B,IAAI,iBAAiB,gBACnB,MAAM;EAGR,MAAM,IAAI,eAAe,4BAA4B,EAAE,OAAO,MAAM,CAAC;CACvE;AACF"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// src/sessionToken/publicKeys/serial.ts
|
|
2
|
+
var publicKeySerialPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
3
|
+
var isValidPublicKeySerial = (serial) => publicKeySerialPattern.test(serial);
|
|
4
|
+
export { isValidPublicKeySerial, publicKeySerialPattern };
|
|
5
|
+
|
|
6
|
+
//# sourceMappingURL=serial.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"serial.mjs","names":[],"sources":["../../../../src/sessionToken/publicKeys/serial.ts"],"sourcesContent":["export const publicKeySerialPattern =\n /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;\n\nexport const isValidPublicKeySerial = (serial: string): boolean =>\n publicKeySerialPattern.test(serial);\n"],"mappings":";AAAA,IAAa,yBACX;AAEF,IAAa,0BAA0B,WACrC,uBAAuB,KAAK,MAAM"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { publicKeySerialPattern } from "./publicKeys/serial.mjs";
|
|
1
2
|
import { z } from "zod";
|
|
2
|
-
|
|
3
|
+
// src/sessionToken/schemas.ts
|
|
3
4
|
var extensionScopes = z.string().array();
|
|
4
5
|
var extensionContext = z.enum(["project", "customer"]);
|
|
5
6
|
var sessionTokenPayload = z.object({
|
|
@@ -11,10 +12,9 @@ var sessionTokenPayload = z.object({
|
|
|
11
12
|
context: extensionContext,
|
|
12
13
|
scopes: extensionScopes,
|
|
13
14
|
authenticatableWithoutSecret: z.boolean(),
|
|
14
|
-
publicKeySerial: z.string(),
|
|
15
|
+
publicKeySerial: z.string().regex(publicKeySerialPattern),
|
|
15
16
|
variantKey: z.string().optional().nullable()
|
|
16
17
|
});
|
|
17
|
-
//#endregion
|
|
18
18
|
export { extensionContext, extensionScopes, sessionTokenPayload };
|
|
19
19
|
|
|
20
20
|
//# sourceMappingURL=schemas.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schemas.mjs","names":[],"sources":["../../../src/sessionToken/schemas.ts"],"sourcesContent":["import { z } from \"zod\";\n\nexport const extensionScopes = z.string().array();\nexport const extensionContext = z.enum([\"project\", \"customer\"]);\n\nexport const sessionTokenPayload = z.object({\n sessionId: z.string(),\n userId: z.string(),\n extensionId: z.string(),\n extensionInstanceId: z.string(),\n contextId: z.string(),\n context: extensionContext,\n scopes: extensionScopes,\n authenticatableWithoutSecret: z.boolean(),\n publicKeySerial: z.string(),\n variantKey: z.string().optional().nullable(),\n});\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"schemas.mjs","names":[],"sources":["../../../src/sessionToken/schemas.ts"],"sourcesContent":["import { publicKeySerialPattern } from \"@/sessionToken/publicKeys/serial\";\nimport { z } from \"zod\";\n\nexport const extensionScopes = z.string().array();\nexport const extensionContext = z.enum([\"project\", \"customer\"]);\n\nexport const sessionTokenPayload = z.object({\n sessionId: z.string(),\n userId: z.string(),\n extensionId: z.string(),\n extensionInstanceId: z.string(),\n contextId: z.string(),\n context: extensionContext,\n scopes: extensionScopes,\n authenticatableWithoutSecret: z.boolean(),\n publicKeySerial: z.string().regex(publicKeySerialPattern),\n variantKey: z.string().optional().nullable(),\n});\n"],"mappings":";;;AAGA,IAAa,kBAAkB,EAAE,OAAO,CAAC,CAAC,MAAM;AAChD,IAAa,mBAAmB,EAAE,KAAK,CAAC,WAAW,UAAU,CAAC;AAE9D,IAAa,sBAAsB,EAAE,OAAO;CAC1C,WAAW,EAAE,OAAO;CACpB,QAAQ,EAAE,OAAO;CACjB,aAAa,EAAE,OAAO;CACtB,qBAAqB,EAAE,OAAO;CAC9B,WAAW,EAAE,OAAO;CACpB,SAAS;CACT,QAAQ;CACR,8BAA8B,EAAE,QAAQ;CACxC,iBAAiB,EAAE,OAAO,CAAC,CAAC,MAAM,sBAAsB;CACxD,YAAY,EAAE,OAAO,CAAC,CAAC,SAAS,CAAC,CAAC,SAAS;AAC7C,CAAC"}
|
|
@@ -1,15 +1,26 @@
|
|
|
1
|
+
import { ExtBridgeError } from "../error.mjs";
|
|
1
2
|
import { decode } from "./decode.mjs";
|
|
2
3
|
import { getPublicKey } from "./publicKeys/publicKeys.mjs";
|
|
3
4
|
import "./publicKeys/index.mjs";
|
|
4
5
|
import { jwtVerify } from "jose";
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
import { JWTClaimValidationFailed } from "jose/errors";
|
|
7
|
+
// src/sessionToken/verify.ts
|
|
8
|
+
var verify = async (sessionToken, options) => {
|
|
9
|
+
const extensionId = options?.extensionId;
|
|
10
|
+
if (typeof extensionId !== "string" || extensionId === "") throw new ExtBridgeError("verify() requires `extensionId` to check the session token was issued for your extension");
|
|
7
11
|
const decoded = decode(sessionToken);
|
|
8
12
|
const publicKey = await getPublicKey(decoded.publicKeySerial);
|
|
9
|
-
|
|
13
|
+
try {
|
|
14
|
+
await jwtVerify(sessionToken, publicKey, {
|
|
15
|
+
audience: extensionId,
|
|
16
|
+
algorithms: ["EdDSA"]
|
|
17
|
+
});
|
|
18
|
+
} catch (error) {
|
|
19
|
+
if (error instanceof JWTClaimValidationFailed && error.claim === "aud") throw new ExtBridgeError("Session token was not issued for this extension");
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
10
22
|
return decoded;
|
|
11
23
|
};
|
|
12
|
-
//#endregion
|
|
13
24
|
export { verify };
|
|
14
25
|
|
|
15
26
|
//# sourceMappingURL=verify.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"verify.mjs","names":[],"sources":["../../../src/sessionToken/verify.ts"],"sourcesContent":["import { decode } from \"@/sessionToken/decode\";\nimport { getPublicKey } from \"@/sessionToken/publicKeys\";\nimport { jwtVerify } from \"jose\";\n\nexport const verify = async (sessionToken: string) => {\n const decoded = decode(sessionToken);\n const publicKey = await getPublicKey(decoded.publicKeySerial);\n await jwtVerify(sessionToken, publicKey);\n return decoded;\n};\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"verify.mjs","names":[],"sources":["../../../src/sessionToken/verify.ts"],"sourcesContent":["import { ExtBridgeError } from \"@/error\";\nimport { decode } from \"@/sessionToken/decode\";\nimport { getPublicKey } from \"@/sessionToken/publicKeys\";\nimport { jwtVerify } from \"jose\";\nimport { JWTClaimValidationFailed } from \"jose/errors\";\n\nexport interface VerifyOptions {\n /**\n * The ID of your extension. The session token is only accepted if it was\n * issued for this extension (`aud` claim).\n */\n extensionId: string;\n}\n\nexport const verify = async (sessionToken: string, options: VerifyOptions) => {\n const extensionId = options?.extensionId;\n if (typeof extensionId !== \"string\" || extensionId === \"\") {\n throw new ExtBridgeError(\n \"verify() requires `extensionId` to check the session token was issued for your extension\",\n );\n }\n\n const decoded = decode(sessionToken);\n const publicKey = await getPublicKey(decoded.publicKeySerial);\n\n try {\n await jwtVerify(sessionToken, publicKey, {\n audience: extensionId,\n algorithms: [\"EdDSA\"],\n });\n } catch (error) {\n if (error instanceof JWTClaimValidationFailed && error.claim === \"aud\") {\n throw new ExtBridgeError(\n \"Session token was not issued for this extension\",\n );\n }\n throw error;\n }\n\n return decoded;\n};\n"],"mappings":";;;;;;;AAcA,IAAa,SAAS,OAAO,cAAsB,YAA2B;CAC5E,MAAM,cAAc,SAAS;CAC7B,IAAI,OAAO,gBAAgB,YAAY,gBAAgB,IACrD,MAAM,IAAI,eACR,0FACF;CAGF,MAAM,UAAU,OAAO,YAAY;CACnC,MAAM,YAAY,MAAM,aAAa,QAAQ,eAAe;CAE5D,IAAI;EACF,MAAM,UAAU,cAAc,WAAW;GACvC,UAAU;GACV,YAAY,CAAC,OAAO;EACtB,CAAC;CACH,SAAS,OAAO;EACd,IAAI,iBAAiB,4BAA4B,MAAM,UAAU,OAC/D,MAAM,IAAI,eACR,iDACF;EAEF,MAAM;CACR;CAEA,OAAO;AACT"}
|
package/dist/types/error.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { verify } from './sessionToken';
|
|
1
|
+
export { verify, type VerifyOptions } from './sessionToken';
|
|
2
2
|
export { getAccessToken } from './sessionToken/getAccessToken';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { getPublicKey } from './publicKeys';
|
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
export
|
|
1
|
+
export interface VerifyOptions {
|
|
2
|
+
/**
|
|
3
|
+
* The ID of your extension. The session token is only accepted if it was
|
|
4
|
+
* issued for this extension (`aud` claim).
|
|
5
|
+
*/
|
|
6
|
+
extensionId: string;
|
|
7
|
+
}
|
|
8
|
+
export declare const verify: (sessionToken: string, options: VerifyOptions) => Promise<{
|
|
2
9
|
sessionId: string;
|
|
3
10
|
userId: string;
|
|
4
11
|
extensionId: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mittwald/ext-bridge",
|
|
3
|
-
"version": "1.6.0
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Bridge for mStudio embedded frontend extensions",
|
|
6
6
|
"homepage": "https://flow.mittwald.de",
|
|
@@ -38,7 +38,8 @@
|
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "vite build --config vite.build.config.ts",
|
|
40
40
|
"clean": "rimraf dist",
|
|
41
|
-
"test:compile": "tsc --noEmit && tsc --noEmit -p tsconfig.vite.json"
|
|
41
|
+
"test:compile": "tsc --noEmit && tsc --noEmit -p tsconfig.vite.json",
|
|
42
|
+
"test:unit": "vitest run"
|
|
42
43
|
},
|
|
43
44
|
"dependencies": {
|
|
44
45
|
"@mittwald/react-use-promise": "^4.2.2",
|
|
@@ -48,8 +49,8 @@
|
|
|
48
49
|
"zod": "^4.4.3"
|
|
49
50
|
},
|
|
50
51
|
"devDependencies": {
|
|
51
|
-
"@mittwald/flow-core": "1.6.0
|
|
52
|
-
"@mittwald/typescript-config": "1.6.0
|
|
52
|
+
"@mittwald/flow-core": "1.6.0",
|
|
53
|
+
"@mittwald/typescript-config": "1.6.0",
|
|
53
54
|
"@types/node": "^25.9.3",
|
|
54
55
|
"@types/react": "^19.2",
|
|
55
56
|
"@types/react-dom": "^19.2",
|
|
@@ -80,5 +81,5 @@
|
|
|
80
81
|
"optional": true
|
|
81
82
|
}
|
|
82
83
|
},
|
|
83
|
-
"gitHead": "
|
|
84
|
+
"gitHead": "44a8962b7f7009c33fd954b837da37148a28ae2f"
|
|
84
85
|
}
|