From 8130fce40459d6597239eb283e1c8dfc28edb16f Mon Sep 17 00:00:00 2001 From: Oscar Hinton Date: Tue, 8 Feb 2022 11:18:10 +0100 Subject: [PATCH] Add headers for client type and client version (#651) --- common/src/abstractions/platformUtils.service.ts | 3 ++- common/src/enums/clientType.ts | 8 ++++++++ .../request/identityToken/passwordTokenRequest.ts | 3 ++- common/src/services/api.service.ts | 10 ++++++++-- .../src/services/electronPlatformUtils.service.ts | 10 +++++++--- node/src/cli/services/cliPlatformUtils.service.ts | 11 ++++++++--- 6 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 common/src/enums/clientType.ts diff --git a/common/src/abstractions/platformUtils.service.ts b/common/src/abstractions/platformUtils.service.ts index f5a6203dc0..dded8171e7 100644 --- a/common/src/abstractions/platformUtils.service.ts +++ b/common/src/abstractions/platformUtils.service.ts @@ -1,3 +1,4 @@ +import { ClientType } from "../enums/clientType"; import { DeviceType } from "../enums/deviceType"; import { ThemeType } from "../enums/themeType"; @@ -6,9 +7,9 @@ interface ToastOptions { } export abstract class PlatformUtilsService { - identityClientId: string; getDevice: () => DeviceType; getDeviceString: () => string; + getClientType: () => ClientType; isFirefox: () => boolean; isChrome: () => boolean; isEdge: () => boolean; diff --git a/common/src/enums/clientType.ts b/common/src/enums/clientType.ts new file mode 100644 index 0000000000..246769ebf2 --- /dev/null +++ b/common/src/enums/clientType.ts @@ -0,0 +1,8 @@ +export enum ClientType { + Web = "web", + Browser = "browser", + Desktop = "desktop", + Mobile = "mobile", + Cli = "cli", + DirectoryConnector = "connector", +} diff --git a/common/src/models/request/identityToken/passwordTokenRequest.ts b/common/src/models/request/identityToken/passwordTokenRequest.ts index a1d8466ba6..59f28ecd9f 100644 --- a/common/src/models/request/identityToken/passwordTokenRequest.ts +++ b/common/src/models/request/identityToken/passwordTokenRequest.ts @@ -3,6 +3,7 @@ import { TokenRequest, TokenRequestTwoFactor } from "./tokenRequest"; import { CaptchaProtectedRequest } from "../captchaProtectedRequest"; import { DeviceRequest } from "../deviceRequest"; +import { ClientType } from "../../../enums/clientType"; import { Utils } from "../../../misc/utils"; export class PasswordTokenRequest extends TokenRequest implements CaptchaProtectedRequest { @@ -16,7 +17,7 @@ export class PasswordTokenRequest extends TokenRequest implements CaptchaProtect super(twoFactor, device); } - toIdentityToken(clientId: string) { + toIdentityToken(clientId: ClientType) { const obj = super.toIdentityToken(clientId); obj.grant_type = "password"; diff --git a/common/src/services/api.service.ts b/common/src/services/api.service.ts index 4eda12e1c3..5c51e449b3 100644 --- a/common/src/services/api.service.ts +++ b/common/src/services/api.service.ts @@ -1,3 +1,4 @@ +import { ClientType } from "../enums/clientType"; import { DeviceType } from "../enums/deviceType"; import { PolicyType } from "../enums/policyType"; @@ -225,7 +226,7 @@ export class ApiService implements ApiServiceAbstraction { const identityToken = request instanceof ApiTokenRequest ? request.toIdentityToken() - : request.toIdentityToken(this.platformUtilsService.identityClientId); + : request.toIdentityToken(this.platformUtilsService.getClientType()); const response = await this.fetch( new Request(this.environmentService.getIdentityUrl() + "/connect/token", { @@ -2205,11 +2206,16 @@ export class ApiService implements ApiServiceAbstraction { return accessToken; } - fetch(request: Request): Promise { + async fetch(request: Request): Promise { if (request.method === "GET") { request.headers.set("Cache-Control", "no-store"); 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); } diff --git a/electron/src/services/electronPlatformUtils.service.ts b/electron/src/services/electronPlatformUtils.service.ts index 7ec81327b5..8e4632b6e1 100644 --- a/electron/src/services/electronPlatformUtils.service.ts +++ b/electron/src/services/electronPlatformUtils.service.ts @@ -2,6 +2,7 @@ import { clipboard, ipcRenderer, shell } from "electron"; import { isDev, isMacAppStore } from "../utils"; +import { ClientType } from "jslib-common/enums/clientType"; import { DeviceType } from "jslib-common/enums/deviceType"; 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"; export class ElectronPlatformUtilsService implements PlatformUtilsService { - identityClientId: string; - + private clientType: ClientType; private deviceCache: DeviceType = null; constructor( @@ -21,7 +21,7 @@ export class ElectronPlatformUtilsService implements PlatformUtilsService { private isDesktopApp: boolean, private stateService: StateService ) { - this.identityClientId = isDesktopApp ? "desktop" : "connector"; + this.clientType = isDesktopApp ? ClientType.Desktop : ClientType.DirectoryConnector; } getDevice(): DeviceType { @@ -48,6 +48,10 @@ export class ElectronPlatformUtilsService implements PlatformUtilsService { return device.replace("desktop", ""); } + getClientType() { + return this.clientType; + } + isFirefox(): boolean { return false; } diff --git a/node/src/cli/services/cliPlatformUtils.service.ts b/node/src/cli/services/cliPlatformUtils.service.ts index 7975d0647e..43350308e8 100644 --- a/node/src/cli/services/cliPlatformUtils.service.ts +++ b/node/src/cli/services/cliPlatformUtils.service.ts @@ -1,5 +1,6 @@ import * as child_process from "child_process"; +import { ClientType } from "jslib-common/enums/clientType"; import { DeviceType } from "jslib-common/enums/deviceType"; import { ThemeType } from "jslib-common/enums/themeType"; @@ -9,12 +10,12 @@ import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.se const open = require("open"); export class CliPlatformUtilsService implements PlatformUtilsService { - identityClientId: string; + clientType: ClientType; private deviceCache: DeviceType = null; - constructor(identityClientId: string, private packageJson: any) { - this.identityClientId = identityClientId; + constructor(clientType: ClientType, private packageJson: any) { + this.clientType = clientType; } getDevice(): DeviceType { @@ -41,6 +42,10 @@ export class CliPlatformUtilsService implements PlatformUtilsService { return device.replace("desktop", ""); } + getClientType() { + return this.clientType; + } + isFirefox() { return false; }