Skip to content

Commit e309914

Browse files
committed
use jwt instead of self-written format for tokens
1 parent 119cd0e commit e309914

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

packages/sandbox/allurerc.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,7 @@ export default defineConfig({
115115
matcher: ({ labels }) => labels.some(({ name, value }) => name === "env" && value === "bar"),
116116
},
117117
},
118+
allureService: {
119+
accessToken: "eyJhbGciOiJIUzI1NiJ9.eyJwcm9qZWN0SWQiOiIwOTU1NDZlNS05ZmVkLTQ4MmItYTdkZC0yNGEwMDFmYmI4MzciLCJ1cmwiOiJodHRwOi8vbG9jYWxob3N0Ojk5OTEiLCJpYXQiOjE3Njc4NjEzNDcsImlzcyI6ImFsbHVyZS1zZXJ2aWNlIn0.xXRc3x3VBzKTnzZCtnW-dy23adWpG0puOROSOKG1wM4"
120+
}
118121
});

packages/service/src/service.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,33 @@ export class AllureServiceClient {
1515
throw new Error("Allure service access token is required");
1616
}
1717

18-
const [prefix, body] = atob(config.accessToken).split(":");
18+
const { iss, projectId, url: baseUrl } = this.decodeToken(config.accessToken) ?? {};
1919

20-
if (prefix !== "allure-service") {
21-
throw new Error("Invalid access token");
22-
}
23-
24-
const { projectId, url: baseUrl } = JSON.parse(atob(body));
25-
26-
if (!baseUrl || !projectId) {
20+
if (iss !== "allure-service" || !baseUrl || !projectId) {
2721
throw new Error("Invalid access token");
2822
}
2923

3024
this.#url = baseUrl;
3125
this.#client = createServiceHttpClient(this.#url, config.accessToken);
3226
}
3327

28+
decodeToken(token: string) {
29+
try {
30+
const base64Url = token.split(".")[1];
31+
const base64 = base64Url.replace(/-/g, "+").replace(/_/g, "/");
32+
const jsonPayload = decodeURIComponent(
33+
atob(base64)
34+
.split("")
35+
.map((c) => `%${`00${c.charCodeAt(0).toString(16)}`.slice(-2)}`)
36+
.join(""),
37+
);
38+
39+
return JSON.parse(jsonPayload);
40+
} catch {
41+
return undefined;
42+
}
43+
}
44+
3445
/**
3546
* Returns user profile and current project
3647
*/
@@ -98,7 +109,9 @@ export class AllureServiceClient {
98109
async downloadHistory(payload: { branch: string; limit?: number }) {
99110
const { branch, limit } = payload;
100111
const { history } = await this.#client.get<{ history: HistoryDataPoint[] }>(
101-
limit ? `/projects/history/${encodeURIComponent(branch)}?limit=${limit}` : `/projects/history/${encodeURIComponent(branch)}`,
112+
limit
113+
? `/projects/history/${encodeURIComponent(branch)}?limit=${limit}`
114+
: `/projects/history/${encodeURIComponent(branch)}`,
102115
);
103116

104117
return history;

0 commit comments

Comments
 (0)