1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 08:13:42 +00:00

allow custom user agent string

This commit is contained in:
Kyle Spearrin
2019-10-07 10:02:18 -04:00
parent 83d6b2449c
commit 9f2d9c0a91

View File

@@ -117,7 +117,7 @@ export class ApiService implements ApiServiceAbstraction {
private usingBaseUrl = false; private usingBaseUrl = false;
constructor(private tokenService: TokenService, private platformUtilsService: PlatformUtilsService, constructor(private tokenService: TokenService, private platformUtilsService: PlatformUtilsService,
private logoutCallback: (expired: boolean) => Promise<void>) { private logoutCallback: (expired: boolean) => Promise<void>, private customUserAgent: string = null) {
this.device = platformUtilsService.getDevice(); this.device = platformUtilsService.getDevice();
this.deviceType = this.device.toString(); this.deviceType = this.device.toString();
this.isWebClient = this.device === DeviceType.IEBrowser || this.device === DeviceType.ChromeBrowser || this.isWebClient = this.device === DeviceType.IEBrowser || this.device === DeviceType.ChromeBrowser ||
@@ -158,15 +158,19 @@ export class ApiService implements ApiServiceAbstraction {
// Auth APIs // Auth APIs
async postIdentityToken(request: TokenRequest): Promise<IdentityTokenResponse | IdentityTwoFactorResponse> { async postIdentityToken(request: TokenRequest): Promise<IdentityTokenResponse | IdentityTwoFactorResponse> {
const headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
'Accept': 'application/json',
'Device-Type': this.deviceType,
});
if (this.customUserAgent != null) {
headers.set('User-Agent', this.customUserAgent);
}
const response = await this.fetch(new Request(this.identityBaseUrl + '/connect/token', { const response = await this.fetch(new Request(this.identityBaseUrl + '/connect/token', {
body: this.qsStringify(request.toIdentityToken(this.platformUtilsService.identityClientId)), body: this.qsStringify(request.toIdentityToken(this.platformUtilsService.identityClientId)),
credentials: this.getCredentials(), credentials: this.getCredentials(),
cache: 'no-cache', cache: 'no-cache',
headers: new Headers({ headers: headers,
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
'Accept': 'application/json',
'Device-Type': this.deviceType,
}),
method: 'POST', method: 'POST',
})); }));
@@ -860,6 +864,9 @@ export class ApiService implements ApiServiceAbstraction {
'Authorization': 'Bearer ' + authHeader, 'Authorization': 'Bearer ' + authHeader,
'Content-Type': 'application/json; charset=utf-8', 'Content-Type': 'application/json; charset=utf-8',
}); });
if (this.customUserAgent != null) {
headers.set('User-Agent', this.customUserAgent);
}
const response = await this.fetch(new Request(this.eventsBaseUrl + '/collect', { const response = await this.fetch(new Request(this.eventsBaseUrl + '/collect', {
cache: 'no-cache', cache: 'no-cache',
credentials: this.getCredentials(), credentials: this.getCredentials(),
@@ -926,6 +933,9 @@ export class ApiService implements ApiServiceAbstraction {
const headers = new Headers({ const headers = new Headers({
'Device-Type': this.deviceType, 'Device-Type': this.deviceType,
}); });
if (this.customUserAgent != null) {
headers.set('User-Agent', this.customUserAgent);
}
const requestInit: RequestInit = { const requestInit: RequestInit = {
cache: 'no-cache', cache: 'no-cache',
@@ -985,6 +995,14 @@ export class ApiService implements ApiServiceAbstraction {
if (refreshToken == null || refreshToken === '') { if (refreshToken == null || refreshToken === '') {
throw new Error(); throw new Error();
} }
const headers = new Headers({
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
'Accept': 'application/json',
'Device-Type': this.deviceType,
});
if (this.customUserAgent != null) {
headers.set('User-Agent', this.customUserAgent);
}
const decodedToken = this.tokenService.decodeToken(); const decodedToken = this.tokenService.decodeToken();
const response = await this.fetch(new Request(this.identityBaseUrl + '/connect/token', { const response = await this.fetch(new Request(this.identityBaseUrl + '/connect/token', {
@@ -995,11 +1013,7 @@ export class ApiService implements ApiServiceAbstraction {
}), }),
cache: 'no-cache', cache: 'no-cache',
credentials: this.getCredentials(), credentials: this.getCredentials(),
headers: new Headers({ headers: headers,
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
'Accept': 'application/json',
'Device-Type': this.deviceType,
}),
method: 'POST', method: 'POST',
})); }));