1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-11-29 12:45:20 +01:00

Client & Version headers (#1757)

This commit is contained in:
Oscar Hinton 2022-02-08 17:43:40 +01:00 committed by GitHub
parent 10fafaf8c8
commit 427ff09af0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 6 deletions

View File

@ -116,7 +116,7 @@ namespace Bit.App.Pages
var redirectUri = "bitwarden://sso-callback"; var redirectUri = "bitwarden://sso-callback";
var url = _apiService.IdentityBaseUrl + "/connect/authorize?" + var url = _apiService.IdentityBaseUrl + "/connect/authorize?" +
"client_id=" + _platformUtilsService.IdentityClientId + "&" + "client_id=" + _platformUtilsService.GetClientType().GetString() + "&" +
"redirect_uri=" + Uri.EscapeDataString(redirectUri) + "&" + "redirect_uri=" + Uri.EscapeDataString(redirectUri) + "&" +
"response_type=code&scope=api%20offline_access&" + "response_type=code&scope=api%20offline_access&" +
"state=" + state + "&code_challenge=" + codeChallenge + "&" + "state=" + state + "&code_challenge=" + codeChallenge + "&" +

View File

@ -5,6 +5,7 @@ using Bit.App.Abstractions;
using Bit.App.Models; using Bit.App.Models;
using Bit.App.Resources; using Bit.App.Resources;
using Bit.Core.Abstractions; using Bit.Core.Abstractions;
using Bit.Core.Enums;
using Plugin.Fingerprint; using Plugin.Fingerprint;
using Plugin.Fingerprint.Abstractions; using Plugin.Fingerprint.Abstractions;
using Xamarin.Essentials; using Xamarin.Essentials;
@ -35,8 +36,6 @@ namespace Bit.App.Services
_broadcasterService = broadcasterService; _broadcasterService = broadcasterService;
} }
public string IdentityClientId => "mobile";
public void Init() public void Init()
{ {
_broadcasterService.Subscribe(nameof(MobilePlatformUtilsService), (message) => _broadcasterService.Subscribe(nameof(MobilePlatformUtilsService), (message) =>
@ -80,6 +79,11 @@ namespace Bit.App.Services
return DeviceInfo.Model; return DeviceInfo.Model;
} }
public ClientType GetClientType()
{
return ClientType.Mobile;
}
public bool IsViewOpen() public bool IsViewOpen()
{ {
return false; return false;

View File

@ -7,11 +7,10 @@ namespace Bit.Core.Abstractions
{ {
public interface IPlatformUtilsService public interface IPlatformUtilsService
{ {
string IdentityClientId { get; }
string GetApplicationVersion(); string GetApplicationVersion();
DeviceType GetDevice(); DeviceType GetDevice();
string GetDeviceString(); string GetDeviceString();
ClientType GetClientType();
bool IsDev(); bool IsDev();
bool IsSelfHost(); bool IsSelfHost();
bool IsViewOpen(); bool IsViewOpen();

View File

@ -0,0 +1,36 @@
namespace Bit.Core.Enums
{
public enum ClientType: byte
{
Web = 1,
Browser = 2,
Desktop = 3,
Mobile = 4,
Cli = 5,
DirectoryConnector = 6,
}
public static class ClientTypeExtensions
{
public static string GetString(this ClientType me)
{
switch (me)
{
case ClientType.Web:
return "web";
case ClientType.Browser:
return "browser";
case ClientType.Desktop:
return "desktop";
case ClientType.Mobile:
return "mobile";
case ClientType.Cli:
return "cli";
case ClientType.DirectoryConnector:
return "connector";
default:
return "";
}
}
}
}

View File

@ -1,4 +1,5 @@
using Bit.Core.Abstractions; using Bit.Core.Abstractions;
using Bit.Core.Enums;
using Bit.Core.Exceptions; using Bit.Core.Exceptions;
using Bit.Core.Models.Domain; using Bit.Core.Models.Domain;
using Bit.Core.Models.Request; using Bit.Core.Models.Request;
@ -37,6 +38,8 @@ namespace Bit.Core.Services
_logoutCallbackAsync = logoutCallbackAsync; _logoutCallbackAsync = logoutCallbackAsync;
var device = (int)_platformUtilsService.GetDevice(); var device = (int)_platformUtilsService.GetDevice();
_httpClient.DefaultRequestHeaders.Add("Device-Type", device.ToString()); _httpClient.DefaultRequestHeaders.Add("Device-Type", device.ToString());
_httpClient.DefaultRequestHeaders.Add("Bitwarden-Client-Name", _platformUtilsService.GetClientType().GetString());
_httpClient.DefaultRequestHeaders.Add("Bitwarden-Client-Version", _platformUtilsService.GetApplicationVersion());
if (!string.IsNullOrWhiteSpace(customUserAgent)) if (!string.IsNullOrWhiteSpace(customUserAgent))
{ {
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(customUserAgent); _httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(customUserAgent);
@ -87,7 +90,7 @@ namespace Bit.Core.Services
Version = new Version(1, 0), Version = new Version(1, 0),
RequestUri = new Uri(string.Concat(IdentityBaseUrl, "/connect/token")), RequestUri = new Uri(string.Concat(IdentityBaseUrl, "/connect/token")),
Method = HttpMethod.Post, Method = HttpMethod.Post,
Content = new FormUrlEncodedContent(request.ToIdentityToken(_platformUtilsService.IdentityClientId)) Content = new FormUrlEncodedContent(request.ToIdentityToken(_platformUtilsService.GetClientType().GetString()))
}; };
requestMessage.Headers.Add("Accept", "application/json"); requestMessage.Headers.Add("Accept", "application/json");
request.AlterIdentityTokenHeaders(requestMessage.Headers); request.AlterIdentityTokenHeaders(requestMessage.Headers);