mirror of
https://github.com/bitwarden/browser
synced 2026-02-07 04:03:29 +00:00
PM-20532 - TODO cleanup
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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<SendAccessToken | SendTokenApiRetrievalError>;
|
||||
) => Promise<SendAccessToken | SendTokenApiError>;
|
||||
}
|
||||
|
||||
@@ -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<string, SendTokenApiRetrievalError> = {
|
||||
const INVALID_REQUEST_ERROR_MAPPING: Record<string, SendTokenApiError> = {
|
||||
"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<string, SendTokenApiRetrievalError> = {
|
||||
const INVALID_GRANT_ERROR_MAPPING: Record<string, SendTokenApiError> = {
|
||||
"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<SendAccessToken | SendTokenApiRetrievalError> {
|
||||
): Promise<SendAccessToken | SendTokenApiError> {
|
||||
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": {
|
||||
|
||||
@@ -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<SendAccessToken, stri
|
||||
);
|
||||
|
||||
type CredentialsRequiredApiError = Extract<
|
||||
SendTokenApiRetrievalError,
|
||||
SendTokenApiError,
|
||||
"password-required" | "email-and-otp-required" | "unknown-error"
|
||||
>;
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user