From ba371bdb5920c3e84a113922836a029bf0eda752 Mon Sep 17 00:00:00 2001 From: Michael Avoyan Date: Mon, 25 Dec 2023 13:21:45 +0200 Subject: [PATCH] token expires in property --- packages/sdk/src/api/entities/VCLToken.ts | 30 ++++++++++++++++++- packages/sdk/test/entities/VCLToken.test.ts | 22 ++++++++++++++ .../resources/valid/TokenMocks.ts | 7 +++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 packages/sdk/test/entities/VCLToken.test.ts create mode 100644 packages/sdk/test/infrastructure/resources/valid/TokenMocks.ts diff --git a/packages/sdk/src/api/entities/VCLToken.ts b/packages/sdk/src/api/entities/VCLToken.ts index 517ab21..7a8f714 100644 --- a/packages/sdk/src/api/entities/VCLToken.ts +++ b/packages/sdk/src/api/entities/VCLToken.ts @@ -1,3 +1,31 @@ +import VCLJwt from "./VCLJwt" + export default class VCLToken { - constructor(public readonly value: string) {} + /** + * token value represented as jwt string + */ + public readonly value: string + /** + * token value represented as VCLJwt object + */ + public readonly jwtValue: VCLJwt + + constructor(value: string | VCLJwt) { + if (typeof value === 'string') { + this.value = value; + this.jwtValue = VCLJwt.fromEncodedJwt(value); + } else { + this.value = value?.encodedJwt ?? ''; + this.jwtValue = value ?? VCLJwt.fromEncodedJwt(''); + } + } + + /** + * token expiration period in milliseconds + */ + public get expiresIn(): Nullish { + return BigInt(this.jwtValue.payload[VCLToken.KeyExp]) || null; + } + + static readonly KeyExp = "exp"; } diff --git a/packages/sdk/test/entities/VCLToken.test.ts b/packages/sdk/test/entities/VCLToken.test.ts new file mode 100644 index 0000000..d79e98c --- /dev/null +++ b/packages/sdk/test/entities/VCLToken.test.ts @@ -0,0 +1,22 @@ +import VCLToken from "../../src/api/entities/VCLToken"; +import TokenMocks from "../infrastructure/resources/valid/TokenMocks"; + +describe("VCLToken Tests", () => { + let subject: VCLToken; + + test("testToken1", () => { + subject = new VCLToken(TokenMocks.TokenStr); + + expect(subject.value).toBe(TokenMocks.TokenStr); + expect(subject.jwtValue.encodedJwt).toBe(TokenMocks.TokenStr); + expect(subject.expiresIn).toBe(BigInt(1704020514)); + }); + + test("testToken2", () => { + subject = new VCLToken(TokenMocks.TokenJwt); + + expect(subject.value).toBe(TokenMocks.TokenJwt.encodedJwt); + expect(subject.jwtValue.encodedJwt).toBe(TokenMocks.TokenJwt.encodedJwt); + expect(subject.expiresIn).toBe(BigInt(1704020514)); + }); +}); diff --git a/packages/sdk/test/infrastructure/resources/valid/TokenMocks.ts b/packages/sdk/test/infrastructure/resources/valid/TokenMocks.ts new file mode 100644 index 0000000..872a20c --- /dev/null +++ b/packages/sdk/test/infrastructure/resources/valid/TokenMocks.ts @@ -0,0 +1,7 @@ +import VCLJwt from "../../../../src/api/entities/VCLJwt" + +export default class TokenMocks { + static readonly TokenStr = + "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ.eyJqdGkiOiI2NTg4MGY5ZThkMjY3NWE0NTBhZDVhYjgiLCJpc3MiOiJkaWQ6aW9uOkVpQXBNTGRNYjROUGI4c2FlOS1oWEdIUDc5VzFnaXNBcFZTRTgwVVNQRWJ0SkEiLCJhdWQiOiJkaWQ6aW9uOkVpQXBNTGRNYjROUGI4c2FlOS1oWEdIUDc5VzFnaXNBcFZTRTgwVVNQRWJ0SkEiLCJleHAiOjE3MDQwMjA1MTQsInN1YiI6IjYzODZmODI0ZTc3NDc4OWM0MDNjOTZhMCIsImlhdCI6MTcwMzQxNTcxNH0.AJwKvQ_YNviFTjcuoJUR7ZHFEIbKY9zLCJv4DfC_PPk3Q-15rwKucYy8GdlfKnHLioBA5X37lpG-js8EztEKDg" + static readonly TokenJwt = VCLJwt.fromEncodedJwt(TokenMocks.TokenStr) +} \ No newline at end of file