mirror of
https://github.com/bitwarden/browser
synced 2026-02-08 04:33:38 +00:00
PM-20532 - SendAccessToken model first draft
This commit is contained in:
32
libs/common/src/auth/send-access/models/send-access-token.ts
Normal file
32
libs/common/src/auth/send-access/models/send-access-token.ts
Normal file
@@ -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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user