From 5f5e7186cc90bf2dfc2e6777289a08f7b430676e Mon Sep 17 00:00:00 2001 From: Jared Snider Date: Thu, 22 May 2025 16:09:00 -0400 Subject: [PATCH] PM-20532 - WIP --- .../send-access-token.request.ts | 19 ++++++------ .../abstractions/send-token.service.ts | 31 +++++++++---------- .../services/send-token.service.ts | 11 +++++-- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/libs/common/src/auth/models/request/identity-token/send-access-token.request.ts b/libs/common/src/auth/models/request/identity-token/send-access-token.request.ts index 1bccbb51ef2..f80644704bb 100644 --- a/libs/common/src/auth/models/request/identity-token/send-access-token.request.ts +++ b/libs/common/src/auth/models/request/identity-token/send-access-token.request.ts @@ -1,6 +1,7 @@ import { ClientType } from "../../../../enums"; import { GrantType, GrantTypes } from "../../../enums/grant-type.enum"; import { Scope, Scopes } from "../../../enums/scopes.enum"; +import { SendAccessCredentials } from "../../../send-access/abstractions/send-token.service"; export type SendAccessTokenPasswordPayload = { password: string }; export type SendAccessTokenEmailOtpPayload = { email: string; otp: string }; @@ -28,11 +29,7 @@ export class SendAccessTokenRequest { constructor( public sendId: string, - - public password?: string, - - public email?: string, - public otp?: string, + public sendAccessCredentials?: SendAccessCredentials, ) {} /** @@ -47,10 +44,14 @@ export class SendAccessTokenRequest { send_id: this.sendId, }; - if (this.password) { - return { ...base, password: this.password }; - } else if (this.email && this.otp) { - return { ...base, email: this.email, otp: this.otp }; + if (this.sendAccessCredentials && this.sendAccessCredentials.type === "password") { + return { ...base, password: this.sendAccessCredentials.password }; + } else if (this.sendAccessCredentials && this.sendAccessCredentials.type === "email-otp") { + return { + ...base, + email: this.sendAccessCredentials.email, + otp: this.sendAccessCredentials.otp, + }; } else { return base; } diff --git a/libs/common/src/auth/send-access/abstractions/send-token.service.ts b/libs/common/src/auth/send-access/abstractions/send-token.service.ts index 83b7f04d432..f728e235f1e 100644 --- a/libs/common/src/auth/send-access/abstractions/send-token.service.ts +++ b/libs/common/src/auth/send-access/abstractions/send-token.service.ts @@ -1,16 +1,17 @@ -export interface SendPasswordCredentials { +export type SendAccessCredentialsType = "password" | "email-otp"; + +export type SendPasswordCredentials = { + type: "password"; password: string; -} -export interface SendEmailOtpCredentials { +}; +export type SendEmailOtpCredentials = { + type: "email-otp"; email: string; otp: string; -} +}; export type SendAccessCredentials = SendPasswordCredentials | SendEmailOtpCredentials; export abstract class SendTokenService { - // TODO: talk with Tools about what expected behavior is for expired access tokens. - // Do we implement any local TTL or do we just rely on the server to return a 401 and then we handle that in the api service? - // SendAccessTokens need to be stored in session storage once retrieved. // All SendAccessTokens are scoped to a specific send id so all getting and setting should accept a send id. @@ -23,14 +24,12 @@ export abstract class SendTokenService { // Returned error types should be discriminated union with a type that can be conditioned off for logic. - // Attempts to get a send access token for a specific send id. - // If the token is not found or is expired, it will request a new token from the server. - // As send access tokens can be protected by different credentials, the credentials must be passed in for those sends. - abstract getSendAccessToken: ( - sendId: string, - sendCredentials?: SendAccessCredentials, - ) => Promise; + // TODO: define return types. + // TODO: consider converting to observable. + abstract tryGetSendAccessToken: (sendId: string) => Promise; - // Private internal logic for getting the access token. - // abstract setSendAccessToken: (sendId: string, token: string) => Promise; + abstract getSendAccessTokenWithCredentials: ( + sendId: string, + sendAccessCredentials: SendAccessCredentials, + ) => Promise; } diff --git a/libs/common/src/auth/send-access/services/send-token.service.ts b/libs/common/src/auth/send-access/services/send-token.service.ts index 610718982f7..87ea511c882 100644 --- a/libs/common/src/auth/send-access/services/send-token.service.ts +++ b/libs/common/src/auth/send-access/services/send-token.service.ts @@ -1,15 +1,17 @@ import { GlobalStateProvider, KeyDefinition, SEND_ACCESS_DISK } from "../../../platform/state"; +// import { SendAccessTokenRequest } from "../../models/request/identity-token/send-access-token.request"; import { SendAccessCredentials, SendTokenService as SendTokenServiceAbstraction, } from "../abstractions/send-token.service"; +import { SendAccessToken } from "../models/send-access-token"; import { SendTokenApiService } from "./send-token-api.service"; // Will need to map sendId to access token // TODO: will need to build a better type for access token where it contains // the expires in and the token itself. -export const SEND_ACCESS_TOKEN_DICT = KeyDefinition.record( +export const SEND_ACCESS_TOKEN_DICT = KeyDefinition.record( SEND_ACCESS_DISK, "accessTokenDict", { @@ -23,9 +25,14 @@ export class SendTokenService implements SendTokenServiceAbstraction { private sendTokenApiService: SendTokenApiService, ) {} - async getSendAccessToken(sendId: string, sendCredentials?: SendAccessCredentials): Promise { + async getSendAccessToken( + sendId: string, + sendCredentials: SendAccessCredentials | undefined, + ): Promise { // TODO: check in storage for the access token and if it is expired. // If it is expired, we will need to request a new token from the server. // If it is not expired, we will return the token from storage. + // const request = new SendAccessTokenRequest(sendId, sendCredentials); + // const result = await this.sendTokenApiService.requestSendAccessToken(request); } }