1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-26 12:25:20 +01:00

Add headers for client type and client version (#651)

This commit is contained in:
Oscar Hinton 2022-02-08 11:18:10 +01:00 committed by GitHub
parent 6b8508579f
commit 8130fce404
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 10 deletions

View File

@ -1,3 +1,4 @@
import { ClientType } from "../enums/clientType";
import { DeviceType } from "../enums/deviceType"; import { DeviceType } from "../enums/deviceType";
import { ThemeType } from "../enums/themeType"; import { ThemeType } from "../enums/themeType";
@ -6,9 +7,9 @@ interface ToastOptions {
} }
export abstract class PlatformUtilsService { export abstract class PlatformUtilsService {
identityClientId: string;
getDevice: () => DeviceType; getDevice: () => DeviceType;
getDeviceString: () => string; getDeviceString: () => string;
getClientType: () => ClientType;
isFirefox: () => boolean; isFirefox: () => boolean;
isChrome: () => boolean; isChrome: () => boolean;
isEdge: () => boolean; isEdge: () => boolean;

View File

@ -0,0 +1,8 @@
export enum ClientType {
Web = "web",
Browser = "browser",
Desktop = "desktop",
Mobile = "mobile",
Cli = "cli",
DirectoryConnector = "connector",
}

View File

@ -3,6 +3,7 @@ import { TokenRequest, TokenRequestTwoFactor } from "./tokenRequest";
import { CaptchaProtectedRequest } from "../captchaProtectedRequest"; import { CaptchaProtectedRequest } from "../captchaProtectedRequest";
import { DeviceRequest } from "../deviceRequest"; import { DeviceRequest } from "../deviceRequest";
import { ClientType } from "../../../enums/clientType";
import { Utils } from "../../../misc/utils"; import { Utils } from "../../../misc/utils";
export class PasswordTokenRequest extends TokenRequest implements CaptchaProtectedRequest { export class PasswordTokenRequest extends TokenRequest implements CaptchaProtectedRequest {
@ -16,7 +17,7 @@ export class PasswordTokenRequest extends TokenRequest implements CaptchaProtect
super(twoFactor, device); super(twoFactor, device);
} }
toIdentityToken(clientId: string) { toIdentityToken(clientId: ClientType) {
const obj = super.toIdentityToken(clientId); const obj = super.toIdentityToken(clientId);
obj.grant_type = "password"; obj.grant_type = "password";

View File

@ -1,3 +1,4 @@
import { ClientType } from "../enums/clientType";
import { DeviceType } from "../enums/deviceType"; import { DeviceType } from "../enums/deviceType";
import { PolicyType } from "../enums/policyType"; import { PolicyType } from "../enums/policyType";
@ -225,7 +226,7 @@ export class ApiService implements ApiServiceAbstraction {
const identityToken = const identityToken =
request instanceof ApiTokenRequest request instanceof ApiTokenRequest
? request.toIdentityToken() ? request.toIdentityToken()
: request.toIdentityToken(this.platformUtilsService.identityClientId); : request.toIdentityToken(this.platformUtilsService.getClientType());
const response = await this.fetch( const response = await this.fetch(
new Request(this.environmentService.getIdentityUrl() + "/connect/token", { new Request(this.environmentService.getIdentityUrl() + "/connect/token", {
@ -2205,11 +2206,16 @@ export class ApiService implements ApiServiceAbstraction {
return accessToken; return accessToken;
} }
fetch(request: Request): Promise<Response> { async fetch(request: Request): Promise<Response> {
if (request.method === "GET") { if (request.method === "GET") {
request.headers.set("Cache-Control", "no-store"); request.headers.set("Cache-Control", "no-store");
request.headers.set("Pragma", "no-cache"); request.headers.set("Pragma", "no-cache");
} }
request.headers.set("Bitwarden-Client-Name", this.platformUtilsService.getClientType());
request.headers.set(
"Bitwarden-Client-Version",
await this.platformUtilsService.getApplicationVersion()
);
return this.nativeFetch(request); return this.nativeFetch(request);
} }

View File

@ -2,6 +2,7 @@ import { clipboard, ipcRenderer, shell } from "electron";
import { isDev, isMacAppStore } from "../utils"; import { isDev, isMacAppStore } from "../utils";
import { ClientType } from "jslib-common/enums/clientType";
import { DeviceType } from "jslib-common/enums/deviceType"; import { DeviceType } from "jslib-common/enums/deviceType";
import { ThemeType } from "jslib-common/enums/themeType"; import { ThemeType } from "jslib-common/enums/themeType";
@ -11,8 +12,7 @@ import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.se
import { StateService } from "jslib-common/abstractions/state.service"; import { StateService } from "jslib-common/abstractions/state.service";
export class ElectronPlatformUtilsService implements PlatformUtilsService { export class ElectronPlatformUtilsService implements PlatformUtilsService {
identityClientId: string; private clientType: ClientType;
private deviceCache: DeviceType = null; private deviceCache: DeviceType = null;
constructor( constructor(
@ -21,7 +21,7 @@ export class ElectronPlatformUtilsService implements PlatformUtilsService {
private isDesktopApp: boolean, private isDesktopApp: boolean,
private stateService: StateService private stateService: StateService
) { ) {
this.identityClientId = isDesktopApp ? "desktop" : "connector"; this.clientType = isDesktopApp ? ClientType.Desktop : ClientType.DirectoryConnector;
} }
getDevice(): DeviceType { getDevice(): DeviceType {
@ -48,6 +48,10 @@ export class ElectronPlatformUtilsService implements PlatformUtilsService {
return device.replace("desktop", ""); return device.replace("desktop", "");
} }
getClientType() {
return this.clientType;
}
isFirefox(): boolean { isFirefox(): boolean {
return false; return false;
} }

View File

@ -1,5 +1,6 @@
import * as child_process from "child_process"; import * as child_process from "child_process";
import { ClientType } from "jslib-common/enums/clientType";
import { DeviceType } from "jslib-common/enums/deviceType"; import { DeviceType } from "jslib-common/enums/deviceType";
import { ThemeType } from "jslib-common/enums/themeType"; import { ThemeType } from "jslib-common/enums/themeType";
@ -9,12 +10,12 @@ import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.se
const open = require("open"); const open = require("open");
export class CliPlatformUtilsService implements PlatformUtilsService { export class CliPlatformUtilsService implements PlatformUtilsService {
identityClientId: string; clientType: ClientType;
private deviceCache: DeviceType = null; private deviceCache: DeviceType = null;
constructor(identityClientId: string, private packageJson: any) { constructor(clientType: ClientType, private packageJson: any) {
this.identityClientId = identityClientId; this.clientType = clientType;
} }
getDevice(): DeviceType { getDevice(): DeviceType {
@ -41,6 +42,10 @@ export class CliPlatformUtilsService implements PlatformUtilsService {
return device.replace("desktop", ""); return device.replace("desktop", "");
} }
getClientType() {
return this.clientType;
}
isFirefox() { isFirefox() {
return false; return false;
} }