mirror of
https://github.com/bitwarden/android.git
synced 2024-10-31 23:25:45 +03:00
revert old bearer code. use bearer scheme again
This commit is contained in:
parent
f231565163
commit
0eb68ec461
6 changed files with 3 additions and 116 deletions
|
@ -6,8 +6,6 @@ namespace Bit.App.Abstractions
|
|||
{
|
||||
string Token { get; set; }
|
||||
string RefreshToken { get; set; }
|
||||
[Obsolete("Old auth scheme")]
|
||||
string AuthBearer { get; set; }
|
||||
string GetTwoFactorToken(string email);
|
||||
void SetTwoFactorToken(string email, string token);
|
||||
DateTime TokenExpiration { get; }
|
||||
|
|
|
@ -10,8 +10,6 @@ namespace Bit.App.Models.Api
|
|||
public string MasterPasswordHash { get; set; }
|
||||
public string Token { get; set; }
|
||||
public TwoFactorProviderType? Provider { get; set; }
|
||||
[Obsolete]
|
||||
public string OldAuthBearer { get; set; }
|
||||
public DeviceRequest Device { get; set; }
|
||||
public bool Remember { get; set; }
|
||||
|
||||
|
@ -26,11 +24,6 @@ namespace Bit.App.Models.Api
|
|||
{ "client_id", "mobile" }
|
||||
};
|
||||
|
||||
if(OldAuthBearer != null)
|
||||
{
|
||||
dict.Add("OldAuthBearer", OldAuthBearer);
|
||||
}
|
||||
|
||||
if(Device != null)
|
||||
{
|
||||
dict.Add("DeviceType", Device.Type.ToString());
|
||||
|
|
|
@ -7,7 +7,6 @@ using Newtonsoft.Json;
|
|||
using Plugin.Connectivity.Abstractions;
|
||||
using Bit.App.Abstractions;
|
||||
using System.Net;
|
||||
using XLabs.Ioc;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Bit.App.Repositories
|
||||
|
@ -48,54 +47,7 @@ namespace Bit.App.Repositories
|
|||
private async Task<T> HandleTokenStateAsync<T>(Func<T> success, Func<T> webException,
|
||||
Func<HttpResponseMessage, Task<T>> error)
|
||||
{
|
||||
if(!string.IsNullOrWhiteSpace(TokenService.AuthBearer) && string.IsNullOrWhiteSpace(TokenService.Token))
|
||||
{
|
||||
// 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))
|
||||
if(TokenService.TokenNeedsRefresh && !string.IsNullOrWhiteSpace(TokenService.RefreshToken))
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
|
|
@ -150,8 +150,7 @@ namespace Bit.App.Services
|
|||
get
|
||||
{
|
||||
return _cryptoService.Key != null &&
|
||||
(!string.IsNullOrWhiteSpace(_tokenService.Token) ||
|
||||
!string.IsNullOrWhiteSpace(_tokenService.AuthBearer)) &&
|
||||
!string.IsNullOrWhiteSpace(_tokenService.Token) &&
|
||||
!string.IsNullOrWhiteSpace(UserId);
|
||||
}
|
||||
}
|
||||
|
@ -199,7 +198,6 @@ namespace Bit.App.Services
|
|||
{
|
||||
_tokenService.Token = null;
|
||||
_tokenService.RefreshToken = null;
|
||||
_tokenService.AuthBearer = null;
|
||||
UserId = null;
|
||||
Email = null;
|
||||
_cryptoService.ClearKeys();
|
||||
|
|
|
@ -11,14 +11,12 @@ namespace Bit.App.Services
|
|||
private const string TokenKey = "accessToken";
|
||||
private const string RefreshTokenKey = "refreshToken";
|
||||
private const string TwoFactorTokenKeyFormat = "twoFactorToken_{0}";
|
||||
private const string AuthBearerKey = "token";
|
||||
|
||||
private readonly ISecureStorageService _secureStorage;
|
||||
|
||||
private string _token;
|
||||
private dynamic _decodedToken;
|
||||
private string _refreshToken;
|
||||
private string _authBearer;
|
||||
|
||||
public TokenService(ISecureStorageService secureStorage)
|
||||
{
|
||||
|
@ -54,7 +52,6 @@ namespace Bit.App.Services
|
|||
{
|
||||
_secureStorage.Delete(TokenKey);
|
||||
RefreshToken = null;
|
||||
AuthBearer = 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)
|
||||
{
|
||||
var emailEncoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(email));
|
||||
|
|
|
@ -15,20 +15,7 @@ namespace Bit.App
|
|||
|
||||
if(!string.IsNullOrWhiteSpace(tokenService.Token))
|
||||
{
|
||||
var httpService = Resolver.Resolve<IHttpService>();
|
||||
|
||||
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}");
|
||||
Headers.Add("Authorization", $"Bearer {tokenService.Token}");
|
||||
}
|
||||
if(!string.IsNullOrWhiteSpace(appIdService.AppId))
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue