From 9c18e7995ed62228e59e729609ffd31cd98c44c1 Mon Sep 17 00:00:00 2001 From: Jared Snider Date: Thu, 5 Jun 2025 17:43:38 -0400 Subject: [PATCH] PM-20532 - TODO cleanup --- .../identity-token/send-access-token.request.ts | 1 + .../abstractions/send-token-api.service.ts | 17 +++++++---------- .../services/send-token-api.service.ts | 14 ++++++-------- .../send-access/services/send-token.service.ts | 12 +++++------- 4 files changed, 19 insertions(+), 25 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 5c6cec20a2c..07371194ab3 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 @@ -37,6 +37,7 @@ export class SendAccessTokenRequest { /** * Builds the payload to send to /connect/token */ + // TODO: add tests for this method toIdentityTokenPayload(): SendAccessTokenPayload { const base: SendAccessTokenPayloadBase = { client_id: SendAccessTokenRequest.CLIENT_ID, diff --git a/libs/common/src/auth/send-access/abstractions/send-token-api.service.ts b/libs/common/src/auth/send-access/abstractions/send-token-api.service.ts index c0ca0e6d7c4..ec4fc78a520 100644 --- a/libs/common/src/auth/send-access/abstractions/send-token-api.service.ts +++ b/libs/common/src/auth/send-access/abstractions/send-token-api.service.ts @@ -1,21 +1,18 @@ import { SendAccessTokenRequest } from "../../models/request/identity-token/send-access-token.request"; import { SendAccessToken } from "../models/send-access-token"; -import { SendTokenApiRetrievalError } from "../services/send-token-api.service"; +import { SendTokenApiError } from "../services/send-token-api.service"; /** * Abstract class for the SendTokenApiService. * Communicates with Identity to obtain send access tokens. */ export abstract class SendTokenApiService { - // TODO: add return type for requestSendAccessToken and error scenarios - // Returns a valid send access token or several error types (use discriminated union): - // RequiresPassword - // RequiresEmailOtp - // InvalidCredentials - // ExpiredRequiredPassword // these will live at higher level in SendTokenService - // ExpiredRequiredEmailOtp - + /** + * Requests a send access token from Identity server. + * @param request The request object containing the necessary parameters to obtain the access token. + * @returns A promise that resolves to a SendAccessToken or a SendTokenApiError. + */ abstract requestSendAccessToken: ( request: SendAccessTokenRequest, - ) => Promise; + ) => Promise; } diff --git a/libs/common/src/auth/send-access/services/send-token-api.service.ts b/libs/common/src/auth/send-access/services/send-token-api.service.ts index bfecca66c82..742c4cc710a 100644 --- a/libs/common/src/auth/send-access/services/send-token-api.service.ts +++ b/libs/common/src/auth/send-access/services/send-token-api.service.ts @@ -6,7 +6,7 @@ import { SendAccessTokenRequest } from "../../models/request/identity-token/send import { SendTokenApiService as SendTokenApiServiceAbstraction } from "../abstractions/send-token-api.service"; import { SendAccessToken } from "../models/send-access-token"; -export type SendTokenApiRetrievalError = +export type SendTokenApiError = | "invalid-request" | "send-id-required" | "password-hash-required" @@ -17,18 +17,19 @@ export type SendTokenApiRetrievalError = | "json-parse-error" | "unknown-error"; -const INVALID_REQUEST_ERROR_MAPPING: Record = { +const INVALID_REQUEST_ERROR_MAPPING: Record = { "send_id is required.": "send-id-required", "Password hash is required.": "password-hash-required", "": "invalid-request", // This is a catch-all for any null/undefined invalid request error descriptions }; -const INVALID_GRANT_ERROR_MAPPING: Record = { +const INVALID_GRANT_ERROR_MAPPING: Record = { "Password hash invalid.": "invalid-password-hash", "Invalid OTP.": "invalid-otp", "": "invalid-grant", // This is a catch-all for any null/undefined invalid grant error descriptions }; +// TODO: add tests for this service. export class SendTokenApiService implements SendTokenApiServiceAbstraction { constructor( private environmentService: EnvironmentService, @@ -37,7 +38,7 @@ export class SendTokenApiService implements SendTokenApiServiceAbstraction { async requestSendAccessToken( request: SendAccessTokenRequest, - ): Promise { + ): Promise { const payload = request.toIdentityTokenPayload(); const headers = new Headers({ @@ -81,10 +82,7 @@ export class SendTokenApiService implements SendTokenApiServiceAbstraction { return "unknown-error"; } - private mapTokenResponseToError( - error: string, - errorDescription?: string, - ): SendTokenApiRetrievalError { + private mapTokenResponseToError(error: string, errorDescription?: string): SendTokenApiError { const errorDescKey = errorDescription ?? ""; switch (error) { case "invalid_request": { 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 cdc09ca5b2e..5435d8495ab 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 @@ -19,7 +19,7 @@ import { } from "../abstractions/send-token.service"; import { SendAccessToken } from "../models/send-access-token"; -import { SendTokenApiRetrievalError } from "./send-token-api.service"; +import { SendTokenApiError } from "./send-token-api.service"; // TODO: add JSDocs // TODO: add tests for this service. @@ -34,12 +34,12 @@ export const SEND_ACCESS_TOKEN_DICT = KeyDefinition.record; function isCredentialsRequiredApiError( - error: SendTokenApiRetrievalError, + error: SendTokenApiError, ): error is CredentialsRequiredApiError { return ( error === "password-hash-required" || @@ -51,13 +51,11 @@ function isCredentialsRequiredApiError( export type TryGetSendAccessTokenError = "expired" | CredentialsRequiredApiError; export type GetSendAcccessTokenError = Extract< - SendTokenApiRetrievalError, + SendTokenApiError, "invalid-password" | "invalid-otp" | "unknown-error" >; -function isGetSendAccessTokenError( - error: SendTokenApiRetrievalError, -): error is GetSendAcccessTokenError { +function isGetSendAccessTokenError(error: SendTokenApiError): error is GetSendAcccessTokenError { return error === "invalid-password-hash" || error === "invalid-otp" || error === "unknown-error"; }