user specific remember two factor

This commit is contained in:
Kyle Spearrin 2017-06-27 16:45:12 -04:00
parent 37428c01dd
commit 2f0ca6f7c0
4 changed files with 28 additions and 28 deletions

View file

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

View file

@ -48,7 +48,7 @@ namespace Bit.App.Repositories
var errorResponse = JObject.Parse(responseContent); var errorResponse = JObject.Parse(responseContent);
if(errorResponse["TwoFactorProviders2"] != null) if(errorResponse["TwoFactorProviders2"] != null)
{ {
TokenService.TwoFactorToken = null; TokenService.SetTwoFactorToken(requestObj.Email, null);
return ApiResult<TokenResponse>.Success(new TokenResponse return ApiResult<TokenResponse>.Success(new TokenResponse
{ {

View file

@ -222,7 +222,7 @@ namespace Bit.App.Services
Device = new DeviceRequest(_appIdService, _deviceInfoService) Device = new DeviceRequest(_appIdService, _deviceInfoService)
}; };
var twoFactorToken = _tokenService.TwoFactorToken; var twoFactorToken = _tokenService.GetTwoFactorToken(normalizedEmail);
if(!string.IsNullOrWhiteSpace(twoFactorToken)) if(!string.IsNullOrWhiteSpace(twoFactorToken))
{ {
request.Token = twoFactorToken; request.Token = twoFactorToken;
@ -281,11 +281,6 @@ namespace Bit.App.Services
private async Task ProcessLoginSuccessAsync(SymmetricCryptoKey key, TokenResponse response) private async Task ProcessLoginSuccessAsync(SymmetricCryptoKey key, TokenResponse response)
{ {
if(!string.IsNullOrWhiteSpace(response.TwoFactorToken))
{
_tokenService.TwoFactorToken = response.TwoFactorToken;
}
if(response.Key != null) if(response.Key != null)
{ {
_cryptoService.SetEncKey(new CipherString(response.Key)); _cryptoService.SetEncKey(new CipherString(response.Key));
@ -311,6 +306,11 @@ namespace Bit.App.Services
_cryptoService.SetOrgKeys(profile.Result); _cryptoService.SetOrgKeys(profile.Result);
} }
} }
if(!string.IsNullOrWhiteSpace(response.TwoFactorToken))
{
_tokenService.SetTwoFactorToken(_tokenService.TokenEmail, response.TwoFactorToken);
}
} }
} }
} }

View file

@ -9,7 +9,7 @@ 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 TwoFactorTokenKey = "twoFactorToken"; private const string TwoFactorTokenKeyFormat = "twoFactorToken_{0}";
private const string AuthBearerKey = "token"; private const string AuthBearerKey = "token";
private readonly ISecureStorageService _secureStorage; private readonly ISecureStorageService _secureStorage;
@ -166,11 +166,9 @@ namespace Bit.App.Services
} }
} }
public string TwoFactorToken public string GetTwoFactorToken(string email)
{ {
get var tokenBytes = _secureStorage.Retrieve(string.Format(TwoFactorTokenKeyFormat, email));
{
var tokenBytes = _secureStorage.Retrieve(TwoFactorTokenKey);
if(tokenBytes == null) if(tokenBytes == null)
{ {
return null; return null;
@ -178,17 +176,18 @@ namespace Bit.App.Services
return Encoding.UTF8.GetString(tokenBytes, 0, tokenBytes.Length); return Encoding.UTF8.GetString(tokenBytes, 0, tokenBytes.Length);
} }
set
public void SetTwoFactorToken(string email, string token)
{ {
if(value != null) var key = string.Format(TwoFactorTokenKeyFormat, email);
if(token != null)
{ {
var tokenBytes = Encoding.UTF8.GetBytes(value); var tokenBytes = Encoding.UTF8.GetBytes(token);
_secureStorage.Store(TwoFactorTokenKey, tokenBytes); _secureStorage.Store(key, tokenBytes);
} }
else else
{ {
_secureStorage.Delete(TwoFactorTokenKey); _secureStorage.Delete(key);
}
} }
} }