device user agent

This commit is contained in:
Kyle Spearrin 2019-09-04 11:52:32 -04:00
parent bdad5e4f0a
commit 2507f3301b
9 changed files with 53 additions and 17 deletions

View File

@ -40,7 +40,8 @@ namespace Bit.Droid
if(ServiceContainer.RegisteredServices.Count == 0)
{
RegisterLocalServices();
ServiceContainer.Init();
var deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
ServiceContainer.Init(deviceActionService.DeviceUserAgent);
if(App.Migration.MigrationHelpers.NeedsMigration())
{
var task = App.Migration.MigrationHelpers.PerformMigrationAsync();
@ -155,4 +156,4 @@ namespace Bit.Droid
await ServiceContainer.Resolve<IEnvironmentService>("environmentService").SetUrlsFromStorageAsync();
}
}
}
}

View File

@ -40,6 +40,7 @@ namespace Bit.Droid.Services
private ProgressDialog _progressDialog;
private bool _cameraPermissionsDenied;
private Toast _toast;
private string _userAgent;
public DeviceActionService(
IStorageService storageService,
@ -61,6 +62,19 @@ namespace Bit.Droid.Services
});
}
public string DeviceUserAgent
{
get
{
if(string.IsNullOrWhiteSpace(_userAgent))
{
_userAgent = $"Bitwarden_Mobile/{Xamarin.Essentials.AppInfo.VersionString} " +
$"(Android {Build.VERSION.Release}; SDK {Build.VERSION.Sdk}; Model {Build.Model})";
}
return _userAgent;
}
}
public DeviceType DeviceType => DeviceType.Android;
public void Toast(string text, bool longDuration = false)

View File

@ -6,6 +6,7 @@ namespace Bit.App.Abstractions
{
public interface IDeviceActionService
{
string DeviceUserAgent { get; }
DeviceType DeviceType { get; }
void Toast(string text, bool longDuration = false);
bool LaunchApp(string appName);

View File

@ -25,18 +25,22 @@ namespace Bit.Core.Services
private readonly ITokenService _tokenService;
private readonly IPlatformUtilsService _platformUtilsService;
private readonly Func<bool, Task> _logoutCallbackAsync;
private string _deviceType;
public ApiService(
ITokenService tokenService,
IPlatformUtilsService platformUtilsService,
Func<bool, Task> logoutCallbackAsync)
Func<bool, Task> logoutCallbackAsync,
string customUserAgent = null)
{
_tokenService = tokenService;
_platformUtilsService = platformUtilsService;
_logoutCallbackAsync = logoutCallbackAsync;
var device = _platformUtilsService.GetDevice();
_deviceType = device.ToString();
_httpClient.DefaultRequestHeaders.Add("Device-Type", device.ToString());
if(!string.IsNullOrWhiteSpace(customUserAgent))
{
_httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(customUserAgent);
}
}
public bool UrlsSet { get; private set; }
@ -86,7 +90,6 @@ namespace Bit.Core.Services
Content = new FormUrlEncodedContent(request.ToIdentityToken(_platformUtilsService.IdentityClientId))
};
requestMessage.Headers.Add("Accept", "application/json");
requestMessage.Headers.Add("Device-Type", _deviceType);
HttpResponseMessage response;
try
@ -304,7 +307,6 @@ namespace Bit.Core.Services
{
requestMessage.Method = HttpMethod.Post;
requestMessage.RequestUri = new Uri(string.Concat(EventsBaseUrl, "/collect"));
requestMessage.Headers.Add("Device-Type", _deviceType);
var authHeader = await GetActiveBearerTokenAsync();
requestMessage.Headers.Add("Authorization", string.Concat("Bearer ", authHeader));
requestMessage.Content = new StringContent(JsonConvert.SerializeObject(request, _jsonSettings),
@ -377,7 +379,6 @@ namespace Bit.Core.Services
}
}
requestMessage.Headers.Add("Device-Type", _deviceType);
if(authed)
{
var authHeader = await GetActiveBearerTokenAsync();
@ -432,7 +433,6 @@ namespace Bit.Core.Services
})
};
requestMessage.Headers.Add("Accept", "application/json");
requestMessage.Headers.Add("Device-Type", _deviceType);
HttpResponseMessage response;
try

View File

@ -11,7 +11,7 @@ namespace Bit.Core.Utilities
public static Dictionary<string, object> RegisteredServices { get; set; } = new Dictionary<string, object>();
public static bool Inited { get; set; }
public static void Init()
public static void Init(string customUserAgent = null)
{
if(Inited)
{
@ -31,7 +31,8 @@ namespace Bit.Core.Utilities
var cryptoFunctionService = new PclCryptoFunctionService(cryptoPrimitiveService);
var cryptoService = new CryptoService(storageService, secureStorageService, cryptoFunctionService);
var tokenService = new TokenService(storageService);
var apiService = new ApiService(tokenService, platformUtilsService, (bool expired) => Task.FromResult(0));
var apiService = new ApiService(tokenService, platformUtilsService, (bool expired) => Task.FromResult(0),
customUserAgent);
var appIdService = new AppIdService(storageService);
var userService = new UserService(storageService, tokenService);
var settingsService = new SettingsService(userService, storageService);

View File

@ -1,4 +1,5 @@
using AuthenticationServices;
using Bit.App.Abstractions;
using Bit.App.Resources;
using Bit.Core.Abstractions;
using Bit.Core.Utilities;
@ -265,7 +266,8 @@ namespace Bit.iOS.Autofill
ServiceContainer.Reset();
}
iOSCoreHelpers.RegisterLocalServices();
ServiceContainer.Init();
var deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
ServiceContainer.Init(deviceActionService.DeviceUserAgent);
if(!_initedHockeyApp)
{
iOSCoreHelpers.RegisterHockeyApp();

View File

@ -26,6 +26,7 @@ namespace Bit.iOS.Core.Services
private readonly IMessagingService _messagingService;
private Toast _toast;
private UIAlertController _progressAlert;
private string _userAgent;
public DeviceActionService(
IStorageService storageService,
@ -35,6 +36,19 @@ namespace Bit.iOS.Core.Services
_messagingService = messagingService;
}
public string DeviceUserAgent
{
get
{
if(string.IsNullOrWhiteSpace(_userAgent))
{
_userAgent = $"Bitwarden_Mobile/{Xamarin.Essentials.AppInfo.VersionString} " +
$"(iOS {UIDevice.CurrentDevice.SystemVersion}; Model {UIDevice.CurrentDevice.Model})";
}
return _userAgent;
}
}
public DeviceType DeviceType => DeviceType.iOS;
public bool LaunchApp(string appName)
@ -261,7 +275,7 @@ namespace Bit.iOS.Core.Services
public int SystemMajorVersion()
{
var versionParts = UIDevice.CurrentDevice.SystemVersion.Split('.');
if(versionParts.Length > 0 && int.TryParse(versionParts[0], out int version))
if(versionParts.Length > 0 && int.TryParse(versionParts[0], out var version))
{
return version;
}

View File

@ -1,4 +1,4 @@
using System;
using System;
using System.Diagnostics;
using Foundation;
using UIKit;
@ -12,6 +12,7 @@ using System.Collections.Generic;
using Bit.iOS.Core.Models;
using Bit.Core.Utilities;
using Bit.Core.Abstractions;
using Bit.App.Abstractions;
namespace Bit.iOS.Extension
{
@ -386,7 +387,8 @@ namespace Bit.iOS.Extension
ServiceContainer.Reset();
}
iOSCoreHelpers.RegisterLocalServices();
ServiceContainer.Init();
var deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
ServiceContainer.Init(deviceActionService.DeviceUserAgent);
if(!_initedHockeyApp)
{
iOSCoreHelpers.RegisterHockeyApp();
@ -408,4 +410,4 @@ namespace Bit.iOS.Extension
return userService.IsAuthenticatedAsync().GetAwaiter().GetResult();
}
}
}
}

View File

@ -287,7 +287,8 @@ namespace Bit.iOS
iOSCoreHelpers.RegisterLocalServices();
RegisterPush();
ServiceContainer.Init();
var deviceActionService = ServiceContainer.Resolve<IDeviceActionService>("deviceActionService");
ServiceContainer.Init(deviceActionService.DeviceUserAgent);
iOSCoreHelpers.RegisterHockeyApp();
_pushHandler = new iOSPushNotificationHandler(
ServiceContainer.Resolve<IPushNotificationListenerService>("pushNotificationListenerService"));