revert old bearer code. use bearer scheme again

This commit is contained in:
Kyle Spearrin 2017-08-10 10:16:58 -04:00
parent f231565163
commit 0eb68ec461
6 changed files with 3 additions and 116 deletions

View file

@ -6,8 +6,6 @@ namespace Bit.App.Abstractions
{ {
string Token { get; set; } string Token { get; set; }
string RefreshToken { get; set; } string RefreshToken { get; set; }
[Obsolete("Old auth scheme")]
string AuthBearer { get; set; }
string GetTwoFactorToken(string email); string GetTwoFactorToken(string email);
void SetTwoFactorToken(string email, string token); void SetTwoFactorToken(string email, string token);
DateTime TokenExpiration { get; } DateTime TokenExpiration { get; }

View file

@ -10,8 +10,6 @@ namespace Bit.App.Models.Api
public string MasterPasswordHash { get; set; } public string MasterPasswordHash { get; set; }
public string Token { get; set; } public string Token { get; set; }
public TwoFactorProviderType? Provider { get; set; } public TwoFactorProviderType? Provider { get; set; }
[Obsolete]
public string OldAuthBearer { get; set; }
public DeviceRequest Device { get; set; } public DeviceRequest Device { get; set; }
public bool Remember { get; set; } public bool Remember { get; set; }
@ -26,11 +24,6 @@ namespace Bit.App.Models.Api
{ "client_id", "mobile" } { "client_id", "mobile" }
}; };
if(OldAuthBearer != null)
{
dict.Add("OldAuthBearer", OldAuthBearer);
}
if(Device != null) if(Device != null)
{ {
dict.Add("DeviceType", Device.Type.ToString()); dict.Add("DeviceType", Device.Type.ToString());

View file

@ -7,7 +7,6 @@ using Newtonsoft.Json;
using Plugin.Connectivity.Abstractions; using Plugin.Connectivity.Abstractions;
using Bit.App.Abstractions; using Bit.App.Abstractions;
using System.Net; using System.Net;
using XLabs.Ioc;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
namespace Bit.App.Repositories namespace Bit.App.Repositories
@ -48,54 +47,7 @@ namespace Bit.App.Repositories
private async Task<T> HandleTokenStateAsync<T>(Func<T> success, Func<T> webException, private async Task<T> HandleTokenStateAsync<T>(Func<T> success, Func<T> webException,
Func<HttpResponseMessage, Task<T>> error) Func<HttpResponseMessage, Task<T>> error)
{ {
if(!string.IsNullOrWhiteSpace(TokenService.AuthBearer) && string.IsNullOrWhiteSpace(TokenService.Token)) if(TokenService.TokenNeedsRefresh && !string.IsNullOrWhiteSpace(TokenService.RefreshToken))
{
// Migrate from old auth bearer to new access token
var deviceInfoService = Resolver.Resolve<IDeviceInfoService>();
var appIdService = Resolver.Resolve<IAppIdService>();
using(var client = HttpService.IdentityClient)
{
var requestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri(client.BaseAddress, "connect/token"),
Content = new FormUrlEncodedContent(new TokenRequest
{
Email = "abcdefgh",
MasterPasswordHash = "abcdefgh",
OldAuthBearer = TokenService.AuthBearer,
Device = new DeviceRequest(appIdService, deviceInfoService)
}.ToIdentityTokenRequest())
};
try
{
var response = await client.SendAsync(requestMessage).ConfigureAwait(false);
if(!response.IsSuccessStatusCode)
{
if(response.StatusCode == HttpStatusCode.BadRequest)
{
response.StatusCode = HttpStatusCode.Unauthorized;
}
return await error.Invoke(response).ConfigureAwait(false);
}
var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var tokenResponse = JsonConvert.DeserializeObject<TokenResponse>(responseContent);
TokenService.Token = tokenResponse.AccessToken;
TokenService.RefreshToken = tokenResponse.RefreshToken;
TokenService.AuthBearer = null;
}
catch
{
return webException.Invoke();
}
}
}
else if(TokenService.TokenNeedsRefresh && !string.IsNullOrWhiteSpace(TokenService.RefreshToken))
{ {
using(var client = HttpService.IdentityClient) using(var client = HttpService.IdentityClient)
{ {
@ -135,10 +87,6 @@ namespace Bit.App.Repositories
} }
} }
} }
else if(!string.IsNullOrWhiteSpace(TokenService.AuthBearer))
{
TokenService.AuthBearer = null;
}
return success.Invoke(); return success.Invoke();
} }

View file

@ -150,8 +150,7 @@ namespace Bit.App.Services
get get
{ {
return _cryptoService.Key != null && return _cryptoService.Key != null &&
(!string.IsNullOrWhiteSpace(_tokenService.Token) || !string.IsNullOrWhiteSpace(_tokenService.Token) &&
!string.IsNullOrWhiteSpace(_tokenService.AuthBearer)) &&
!string.IsNullOrWhiteSpace(UserId); !string.IsNullOrWhiteSpace(UserId);
} }
} }
@ -199,7 +198,6 @@ namespace Bit.App.Services
{ {
_tokenService.Token = null; _tokenService.Token = null;
_tokenService.RefreshToken = null; _tokenService.RefreshToken = null;
_tokenService.AuthBearer = null;
UserId = null; UserId = null;
Email = null; Email = null;
_cryptoService.ClearKeys(); _cryptoService.ClearKeys();

View file

@ -11,14 +11,12 @@ namespace Bit.App.Services
private const string TokenKey = "accessToken"; private const string TokenKey = "accessToken";
private const string RefreshTokenKey = "refreshToken"; private const string RefreshTokenKey = "refreshToken";
private const string TwoFactorTokenKeyFormat = "twoFactorToken_{0}"; private const string TwoFactorTokenKeyFormat = "twoFactorToken_{0}";
private const string AuthBearerKey = "token";
private readonly ISecureStorageService _secureStorage; private readonly ISecureStorageService _secureStorage;
private string _token; private string _token;
private dynamic _decodedToken; private dynamic _decodedToken;
private string _refreshToken; private string _refreshToken;
private string _authBearer;
public TokenService(ISecureStorageService secureStorage) public TokenService(ISecureStorageService secureStorage)
{ {
@ -54,7 +52,6 @@ namespace Bit.App.Services
{ {
_secureStorage.Delete(TokenKey); _secureStorage.Delete(TokenKey);
RefreshToken = null; RefreshToken = null;
AuthBearer = null;
} }
_decodedToken = null; _decodedToken = null;
@ -132,40 +129,6 @@ namespace Bit.App.Services
} }
} }
public string AuthBearer
{
get
{
if(_authBearer != null)
{
return _authBearer;
}
var tokenBytes = _secureStorage.Retrieve(AuthBearerKey);
if(tokenBytes == null)
{
return null;
}
_authBearer = Encoding.UTF8.GetString(tokenBytes, 0, tokenBytes.Length);
return _authBearer;
}
set
{
if(value != null)
{
var tokenBytes = Encoding.UTF8.GetBytes(value);
_secureStorage.Store(AuthBearerKey, tokenBytes);
}
else
{
_secureStorage.Delete(AuthBearerKey);
}
_authBearer = value;
}
}
public string GetTwoFactorToken(string email) public string GetTwoFactorToken(string email)
{ {
var emailEncoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(email)); var emailEncoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(email));

View file

@ -15,20 +15,7 @@ namespace Bit.App
if(!string.IsNullOrWhiteSpace(tokenService.Token)) if(!string.IsNullOrWhiteSpace(tokenService.Token))
{ {
var httpService = Resolver.Resolve<IHttpService>(); Headers.Add("Authorization", $"Bearer {tokenService.Token}");
var bearerString = "Bearer";
var tokenIssuer = tokenService.TokenIssuer;
if(tokenIssuer == httpService.ApiClient.BaseAddress.OriginalString)
{
bearerString = string.Concat(bearerString, "2");
}
else if(tokenIssuer == httpService.IdentityClient.BaseAddress.OriginalString)
{
bearerString = string.Concat(bearerString, "3");
}
Headers.Add("Authorization", $"{bearerString} {tokenService.Token}");
} }
if(!string.IsNullOrWhiteSpace(appIdService.AppId)) if(!string.IsNullOrWhiteSpace(appIdService.AppId))
{ {