-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from velocitycareerlabs/VL-6359-auth-token
token expires in property
- Loading branch information
Showing
3 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); |
7 changes: 7 additions & 0 deletions
7
packages/sdk/test/infrastructure/resources/valid/TokenMocks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |