mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-17 08:05:19 +00:00
Fix JWT token usage in GitBook embed SDK (#3922)
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@gitbook/embed": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix signed JWT token usage
|
||||||
@@ -56,6 +56,7 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/react": "catalog:",
|
"@types/react": "catalog:",
|
||||||
|
"bun-types": "catalog:",
|
||||||
"react": "catalog:",
|
"react": "catalog:",
|
||||||
"tsdown": "catalog:",
|
"tsdown": "catalog:",
|
||||||
"typescript": "catalog:",
|
"typescript": "catalog:",
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
"react": "*"
|
"react": "*"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"bun-types": "catalog:",
|
||||||
"@types/react": "catalog:",
|
"@types/react": "catalog:",
|
||||||
"tsdown": "catalog:",
|
"tsdown": "catalog:",
|
||||||
"typescript": "catalog:",
|
"typescript": "catalog:",
|
||||||
@@ -33,6 +34,7 @@
|
|||||||
"build-lib": "tsdown",
|
"build-lib": "tsdown",
|
||||||
"build-standalone": "bun build src/standalone/index.ts --bundle --minify --outdir=standalone",
|
"build-standalone": "bun build src/standalone/index.ts --bundle --minify --outdir=standalone",
|
||||||
"clean": "rm -rf ./dist",
|
"clean": "rm -rf ./dist",
|
||||||
|
"unit": "bun test",
|
||||||
"typecheck": "tsc --noEmit",
|
"typecheck": "tsc --noEmit",
|
||||||
"dev": "bun run build -- --watch ./src",
|
"dev": "bun run build -- --watch ./src",
|
||||||
"publish-to-npm": "../../scripts/publish-if-new.sh"
|
"publish-to-npm": "../../scripts/publish-if-new.sh"
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
import { describe, expect, it } from 'bun:test';
|
||||||
|
|
||||||
|
import { createGitBook } from './createGitBook';
|
||||||
|
|
||||||
|
describe('createGitBook.getFrameURL', () => {
|
||||||
|
it('builds the embed URL when the site URL has no trailing slash', () => {
|
||||||
|
const client = createGitBook({ siteURL: 'https://example.com/docs' });
|
||||||
|
|
||||||
|
const url = new URL(client.getFrameURL({}));
|
||||||
|
|
||||||
|
expect(url.origin).toBe('https://example.com');
|
||||||
|
expect(url.pathname).toBe('/docs/~gitbook/embed');
|
||||||
|
expect(url.searchParams.toString()).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles a trailing slash and adds visitor parameters', () => {
|
||||||
|
const client = createGitBook({ siteURL: 'https://example.com/docs/' });
|
||||||
|
|
||||||
|
const url = new URL(
|
||||||
|
client.getFrameURL({
|
||||||
|
visitor: {
|
||||||
|
token: 'signed-token',
|
||||||
|
unsignedClaims: {
|
||||||
|
role: 'editor',
|
||||||
|
count: 3,
|
||||||
|
enabled: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(url.pathname).toBe('/docs/~gitbook/embed');
|
||||||
|
expect(url.searchParams.get('jwt_token')).toBe('signed-token');
|
||||||
|
expect(url.searchParams.get('visitor.role')).toBe('editor');
|
||||||
|
expect(url.searchParams.get('visitor.count')).toBe('3');
|
||||||
|
expect(url.searchParams.get('visitor.enabled')).toBe('false');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -43,7 +43,7 @@ export function createGitBook(options: CreateGitBookOptions) {
|
|||||||
url.pathname = `${url.pathname.endsWith('/') ? url.pathname : `${url.pathname}/`}~gitbook/embed`;
|
url.pathname = `${url.pathname.endsWith('/') ? url.pathname : `${url.pathname}/`}~gitbook/embed`;
|
||||||
|
|
||||||
if (frameOptions.visitor?.token) {
|
if (frameOptions.visitor?.token) {
|
||||||
url.searchParams.set('token', frameOptions.visitor.token);
|
url.searchParams.set('jwt_token', frameOptions.visitor.token);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (frameOptions.visitor?.unsignedClaims) {
|
if (frameOptions.visitor?.unsignedClaims) {
|
||||||
|
|||||||
@@ -15,7 +15,9 @@
|
|||||||
"isolatedModules": true,
|
"isolatedModules": true,
|
||||||
"jsx": "react-jsx",
|
"jsx": "react-jsx",
|
||||||
"incremental": true,
|
"incremental": true,
|
||||||
"types": []
|
"types": [
|
||||||
|
"bun-types" // add Bun global
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"include": ["src/**/*.ts", "src/**/*.tsx"],
|
"include": ["src/**/*.ts", "src/**/*.tsx"],
|
||||||
"exclude": ["node_modules"]
|
"exclude": ["node_modules"]
|
||||||
|
|||||||
Reference in New Issue
Block a user