1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-09 05:00:10 +00:00

PM-20532 - WIP on SendTokenApiService

This commit is contained in:
Jared Snider
2025-05-19 18:52:38 -04:00
parent 98371dd36f
commit c1f141f7db
5 changed files with 49 additions and 11 deletions

View File

@@ -147,6 +147,7 @@ export abstract class ApiService {
IdentityTokenResponse | IdentityTwoFactorResponse | IdentityDeviceVerificationResponse
>;
refreshIdentityToken: () => Promise<any>;
getCredentials: () => Promise<RequestCredentials | undefined>;
getProfile: () => Promise<ProfileResponse>;
getUserSubscription: () => Promise<SubscriptionResponse>;

View File

@@ -6,7 +6,6 @@ import { DeviceRequest } from "./device.request";
export type SendAccessTokenPasswordPayload = { password: string };
export type SendAccessTokenEmailOtpPayload = { email: string; otp: string };
// If truly anonymous, you get no extra fields:
export type SendAccessTokenAnonymousPayload = object; // empty object
export interface SendAccessTokenPayloadBase {
@@ -15,12 +14,6 @@ export interface SendAccessTokenPayloadBase {
scope: Scope;
send_id: string;
// TODO: ask if we need device information on server + device claims added in server validator
// device info
// device_type: this.device.type,
// device_identifier: this.device.identifier,
// device_name: this.device.name,
}
// Payload is the base + only 1 set of 3 credentials.

View File

@@ -0,0 +1,10 @@
import { SendAccessTokenRequest } from "../../models/request/identity-token/send-access-token.request";
/**
* 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
abstract requestSendAccessToken: (request: SendAccessTokenRequest) => Promise<unknown>;
}

View File

@@ -1,5 +1,39 @@
import { SendAccessTokenRequest } from "../../models/request/identity-token/send-access-token.request";
import { firstValueFrom } from "rxjs";
export abstract class SendTokenApiService {
abstract requestSendAccessToken: (request: SendAccessTokenRequest) => Promise<unknown>;
import { ApiService } from "../../../abstractions/api.service";
import { EnvironmentService } from "../../../platform/abstractions/environment.service";
import { SendAccessTokenRequest } from "../../models/request/identity-token/send-access-token.request";
import { SendTokenApiService as SendTokenApiServiceAbstraction } from "../abstractions/send-token-api.service";
export class SendTokenApiService implements SendTokenApiServiceAbstraction {
constructor(
private environmentService: EnvironmentService,
private apiService: ApiService,
) {}
// TODO: talk with Justin about needing to use httpOperations or not.
async requestSendAccessToken(request: SendAccessTokenRequest): Promise<void> {
const payload = request.toIdentityTokenPayload();
const headers = new Headers({
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
Accept: "application/json",
});
const credentials = await this.apiService.getCredentials();
const env = await firstValueFrom(this.environmentService.environment$);
const req = new Request(env.getIdentityUrl() + "/connect/token", {
method: "POST",
body: new URLSearchParams(payload as any),
headers: headers,
credentials: credentials,
cache: "no-store",
});
await this.apiService.fetch(req);
// TODO: add processing.
}
}

View File

@@ -1910,7 +1910,7 @@ export class ApiService implements ApiServiceAbstraction {
.join("&");
}
private async getCredentials(): Promise<RequestCredentials> {
async getCredentials(): Promise<RequestCredentials> {
const env = await firstValueFrom(this.environmentService.environment$);
if (!this.isWebClient || env.hasBaseUrl()) {
return "include";