Skip to content

Commit

Permalink
Merge pull request #21 from velocitycareerlabs/VL-6359-auth-token
Browse files Browse the repository at this point in the history
token expires in property
  • Loading branch information
michaelavoyan authored Dec 25, 2023
2 parents 67357a5 + ba371bd commit 06dc703
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
30 changes: 29 additions & 1 deletion packages/sdk/src/api/entities/VCLToken.ts
Original file line number Diff line number Diff line change
@@ -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<BigInt> {
return BigInt(this.jwtValue.payload[VCLToken.KeyExp]) || null;
}

static readonly KeyExp = "exp";
}
22 changes: 22 additions & 0 deletions packages/sdk/test/entities/VCLToken.test.ts
Original file line number Diff line number Diff line change
@@ -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));
});
});
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 06dc703

Please sign in to comment.