From 8cbd75caefa190f656ad54e99673113bdcb3b97c Mon Sep 17 00:00:00 2001 From: Jared Snider Date: Wed, 21 May 2025 15:29:04 -0400 Subject: [PATCH] PM-20532 - SendAccessToken model first draft --- .../send-access/models/send-access-token.ts | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 libs/common/src/auth/send-access/models/send-access-token.ts diff --git a/libs/common/src/auth/send-access/models/send-access-token.ts b/libs/common/src/auth/send-access/models/send-access-token.ts new file mode 100644 index 00000000000..a07cf06e1b1 --- /dev/null +++ b/libs/common/src/auth/send-access/models/send-access-token.ts @@ -0,0 +1,32 @@ +export class SendAccessToken { + constructor( + /** + * The access token string + */ + readonly token: string, + /** + * The time (in milliseconds since the epoch) when the token expires + */ + readonly expiresAt: number, + ) {} + + /** + * Builds an instance from our Identity token response data + * @param accessToken The `access_token` string + * @param expiresInSeconds The `expires_in` value (in seconds) + */ + static fromResponseData(accessToken: string, expiresInSeconds: number): SendAccessToken { + const expiresAtTimeStamp = Date.now() + expiresInSeconds * 1000; + return new SendAccessToken(accessToken, expiresAtTimeStamp); + } + + /** Returns whether the send access token is expired or not */ + isExpired(): boolean { + return Date.now() >= this.expiresAt; + } + + /** Returns how many full seconds remain until expiry. Returns 0 if expired. */ + timeUntilExpirySeconds(): number { + return Math.max(0, Math.floor((this.expiresAt - Date.now()) / 1_000)); + } +}