1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-06 00:13:28 +00:00

[PM-23189] Add client managed token provider (#15408)

* Add client managed token provider

* Change token service to accept user id

* Resolve breaking changes in the SDK

* Fix tests

* Update sdk

* Fix type

* Fix types

* Fix cli

* Fix browser

* Add optional userId to refreshIdentityToken

* Fix merge issues

* Fix tests
This commit is contained in:
Oscar Hinton
2025-10-08 22:47:30 +02:00
committed by GitHub
parent 5f18f15c52
commit 095729d6fa
6 changed files with 26 additions and 7 deletions

View File

@@ -830,6 +830,7 @@ export default class MainBackground {
this.accountService,
this.kdfConfigService,
this.keyService,
this.apiService,
this.stateProvider,
this.configService,
);

View File

@@ -612,6 +612,7 @@ export class ServiceContainer {
this.accountService,
this.kdfConfigService,
this.keyService,
this.apiService,
this.stateProvider,
this.configService,
customUserAgent,

View File

@@ -1523,6 +1523,7 @@ const safeProviders: SafeProvider[] = [
AccountServiceAbstraction,
KdfConfigService,
KeyService,
ApiServiceAbstraction,
StateProvider,
ConfigService,
],

View File

@@ -163,7 +163,7 @@ export abstract class ApiService {
): Promise<
IdentityTokenResponse | IdentityTwoFactorResponse | IdentityDeviceVerificationResponse
>;
abstract refreshIdentityToken(): Promise<any>;
abstract refreshIdentityToken(userId?: UserId): Promise<any>;
abstract getProfile(): Promise<ProfileResponse>;
abstract getUserSubscription(): Promise<SubscriptionResponse>;

View File

@@ -8,11 +8,12 @@ import { KdfConfigService, KeyService, PBKDF2KdfConfig } from "@bitwarden/key-ma
import { BitwardenClient } from "@bitwarden/sdk-internal";
import {
ObservableTracker,
FakeAccountService,
FakeStateProvider,
mockAccountServiceWith,
ObservableTracker,
} from "../../../../spec";
import { ApiService } from "../../../abstractions/api.service";
import { AccountInfo } from "../../../auth/abstractions/account.service";
import { EncryptedString } from "../../../key-management/crypto/models/enc-string";
import { UserId } from "../../../types/guid";
@@ -46,6 +47,7 @@ describe("DefaultSdkService", () => {
let service!: DefaultSdkService;
let accountService!: FakeAccountService;
let fakeStateProvider!: FakeStateProvider;
let apiService!: MockProxy<ApiService>;
beforeEach(async () => {
await new TestSdkLoadService().loadAndInit();
@@ -55,6 +57,7 @@ describe("DefaultSdkService", () => {
platformUtilsService = mock<PlatformUtilsService>();
kdfConfigService = mock<KdfConfigService>();
keyService = mock<KeyService>();
apiService = mock<ApiService>();
const mockUserId = Utils.newGuid() as UserId;
accountService = mockAccountServiceWith(mockUserId);
fakeStateProvider = new FakeStateProvider(accountService);
@@ -72,6 +75,7 @@ describe("DefaultSdkService", () => {
accountService,
kdfConfigService,
keyService,
apiService,
fakeStateProvider,
configService,
);

View File

@@ -27,6 +27,7 @@ import {
UnsignedSharedKey,
} from "@bitwarden/sdk-internal";
import { ApiService } from "../../../abstractions/api.service";
import { AccountInfo, AccountService } from "../../../auth/abstractions/account.service";
import { DeviceType } from "../../../enums/device-type.enum";
import { EncryptedString, EncString } from "../../../key-management/crypto/models/enc-string";
@@ -43,7 +44,7 @@ import { StateProvider } from "../../state";
import { initializeState } from "./client-managed-state";
// A symbol that represents an overriden client that is explicitly set to undefined,
// A symbol that represents an overridden client that is explicitly set to undefined,
// blocking the creation of an internal client for that user.
const UnsetClient = Symbol("UnsetClient");
@@ -51,10 +52,17 @@ const UnsetClient = Symbol("UnsetClient");
* A token provider that exposes the access token to the SDK.
*/
class JsTokenProvider implements TokenProvider {
constructor() {}
constructor(
private apiService: ApiService,
private userId?: UserId,
) {}
async get_access_token(): Promise<string | undefined> {
return undefined;
if (this.userId == null) {
return undefined;
}
return await this.apiService.getActiveBearerToken(this.userId);
}
}
@@ -68,7 +76,10 @@ export class DefaultSdkService implements SdkService {
concatMap(async (env) => {
await SdkLoadService.Ready;
const settings = this.toSettings(env);
const client = await this.sdkClientFactory.createSdkClient(new JsTokenProvider(), settings);
const client = await this.sdkClientFactory.createSdkClient(
new JsTokenProvider(this.apiService),
settings,
);
await this.loadFeatureFlags(client);
return client;
}),
@@ -87,6 +98,7 @@ export class DefaultSdkService implements SdkService {
private accountService: AccountService,
private kdfConfigService: KdfConfigService,
private keyService: KeyService,
private apiService: ApiService,
private stateProvider: StateProvider,
private configService: ConfigService,
private userAgent: string | null = null,
@@ -173,7 +185,7 @@ export class DefaultSdkService implements SdkService {
const settings = this.toSettings(env);
const client = await this.sdkClientFactory.createSdkClient(
new JsTokenProvider(),
new JsTokenProvider(this.apiService, userId),
settings,
);