|
| 1 | +import * as jwt from "jsonwebtoken"; |
1 | 2 | import { afterEach, describe, expect, it, vi } from "vitest"; |
2 | 3 |
|
3 | 4 | import { AccountUserService } from "../src/internal/account-users/account-user-service.js"; |
@@ -258,6 +259,109 @@ describe("PeekAccessService.getProductService", () => { |
258 | 259 | }); |
259 | 260 | }); |
260 | 261 |
|
| 262 | +const PEEK_REGISTRY_ISSUER = "app_registry_v2"; |
| 263 | +const PEEK_REGISTRY_AUDIENCE = "Joken"; // still used when minting test tokens |
| 264 | + |
| 265 | +const SAMPLE_USER_PAYLOAD = { |
| 266 | + email: "admin@peek.com", |
| 267 | + id: "null", |
| 268 | + is_admin: false, |
| 269 | + locale: "en", |
| 270 | + name: "Admin User", |
| 271 | +}; |
| 272 | + |
| 273 | +function mintRegistryToken( |
| 274 | + secret: string, |
| 275 | + overrides: Record<string, unknown> = {}, |
| 276 | +): string { |
| 277 | + return jwt.sign( |
| 278 | + { display_version: "0.0.11", user: SAMPLE_USER_PAYLOAD, ...overrides }, |
| 279 | + secret, |
| 280 | + { |
| 281 | + subject: "8c1f32b4-ab3c-4e20-82b7-844ea9e03bc9", |
| 282 | + issuer: PEEK_REGISTRY_ISSUER, |
| 283 | + audience: PEEK_REGISTRY_AUDIENCE, |
| 284 | + jwtid: "7d3d42c5-5724-489e-8380-a33abfc14936", |
| 285 | + expiresIn: 60, |
| 286 | + notBefore: 0, |
| 287 | + }, |
| 288 | + ); |
| 289 | +} |
| 290 | + |
| 291 | +describe("PeekAccessService.verifyPeekAuthToken", () => { |
| 292 | + it("returns fully typed claims from a valid Peek registry token", () => { |
| 293 | + const service = new PeekAccessService(REQUIRED_CONFIG); |
| 294 | + const token = mintRegistryToken(REQUIRED_CONFIG.jwtSecret); |
| 295 | + |
| 296 | + const claims = service.verifyPeekAuthToken(token); |
| 297 | + |
| 298 | + expect(claims.installId).toBe("8c1f32b4-ab3c-4e20-82b7-844ea9e03bc9"); |
| 299 | + expect(claims.displayVersion).toBe("0.0.11"); |
| 300 | + }); |
| 301 | + |
| 302 | + it("maps the nested user object to typed fields", () => { |
| 303 | + const service = new PeekAccessService(REQUIRED_CONFIG); |
| 304 | + const token = mintRegistryToken(REQUIRED_CONFIG.jwtSecret); |
| 305 | + |
| 306 | + const { user } = service.verifyPeekAuthToken(token); |
| 307 | + |
| 308 | + expect(user.email).toBe("admin@peek.com"); |
| 309 | + expect(user.id).toBe("null"); |
| 310 | + expect(user.isAdmin).toBe(false); |
| 311 | + expect(user.locale).toBe("en"); |
| 312 | + expect(user.name).toBe("Admin User"); |
| 313 | + }); |
| 314 | + |
| 315 | + it("maps is_admin: true correctly", () => { |
| 316 | + const service = new PeekAccessService(REQUIRED_CONFIG); |
| 317 | + const token = mintRegistryToken(REQUIRED_CONFIG.jwtSecret, { |
| 318 | + user: { ...SAMPLE_USER_PAYLOAD, is_admin: true }, |
| 319 | + }); |
| 320 | + |
| 321 | + expect(service.verifyPeekAuthToken(token).user.isAdmin).toBe(true); |
| 322 | + }); |
| 323 | + |
| 324 | + it("throws on a token signed with a different secret", () => { |
| 325 | + const service = new PeekAccessService(REQUIRED_CONFIG); |
| 326 | + const token = mintRegistryToken("wrong-secret"); |
| 327 | + |
| 328 | + expect(() => service.verifyPeekAuthToken(token)).toThrow(); |
| 329 | + }); |
| 330 | + |
| 331 | + it("throws on a token with a different issuer", () => { |
| 332 | + const service = new PeekAccessService(REQUIRED_CONFIG); |
| 333 | + const token = jwt.sign({ user: SAMPLE_USER_PAYLOAD }, REQUIRED_CONFIG.jwtSecret, { |
| 334 | + issuer: "wrong-issuer", |
| 335 | + audience: PEEK_REGISTRY_AUDIENCE, |
| 336 | + expiresIn: 60, |
| 337 | + }); |
| 338 | + |
| 339 | + expect(() => service.verifyPeekAuthToken(token)).toThrow(); |
| 340 | + }); |
| 341 | + |
| 342 | + it("throws on an expired token", () => { |
| 343 | + const service = new PeekAccessService(REQUIRED_CONFIG); |
| 344 | + const token = jwt.sign( |
| 345 | + { display_version: "0.0.11", user: SAMPLE_USER_PAYLOAD }, |
| 346 | + REQUIRED_CONFIG.jwtSecret, |
| 347 | + { |
| 348 | + subject: "8c1f32b4-ab3c-4e20-82b7-844ea9e03bc9", |
| 349 | + issuer: PEEK_REGISTRY_ISSUER, |
| 350 | + audience: PEEK_REGISTRY_AUDIENCE, |
| 351 | + expiresIn: -1, |
| 352 | + }, |
| 353 | + ); |
| 354 | + |
| 355 | + expect(() => service.verifyPeekAuthToken(token)).toThrow(); |
| 356 | + }); |
| 357 | + |
| 358 | + it("throws on a malformed token string", () => { |
| 359 | + const service = new PeekAccessService(REQUIRED_CONFIG); |
| 360 | + |
| 361 | + expect(() => service.verifyPeekAuthToken("not.a.jwt")).toThrow(); |
| 362 | + }); |
| 363 | +}); |
| 364 | + |
261 | 365 | describe("PeekAccessService v2 mode", () => { |
262 | 366 | it("uses the app-registry sandbox base URL by default", async () => { |
263 | 367 | const { fetchFn, calls } = makeEmptyFetch(); |
|
0 commit comments