2016-05-02 09:52:09 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Bit.App.Abstractions;
|
|
|
|
|
using Bit.App.Models.Api;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
using Plugin.Settings.Abstractions;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
|
|
|
|
namespace Bit.App.Services
|
|
|
|
|
{
|
|
|
|
|
public class AuthService : IAuthService
|
|
|
|
|
{
|
|
|
|
|
private const string TokenKey = "token";
|
2016-07-19 02:16:27 +03:00
|
|
|
|
private const string EmailKey = "email";
|
2016-05-03 00:50:16 +03:00
|
|
|
|
private const string UserIdKey = "userId";
|
2016-08-06 04:59:25 +03:00
|
|
|
|
private const string PreviousUserIdKey = "previousUserId";
|
2016-06-12 07:49:35 +03:00
|
|
|
|
private const string PinKey = "pin";
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
|
|
|
|
private readonly ISecureStorageService _secureStorage;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
private readonly ISettings _settings;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
private readonly ICryptoService _cryptoService;
|
2016-05-06 07:17:38 +03:00
|
|
|
|
private readonly IAuthApiRepository _authApiRepository;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-03 00:50:16 +03:00
|
|
|
|
private string _token;
|
2016-07-19 02:16:27 +03:00
|
|
|
|
private string _email;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
private string _userId;
|
2016-08-06 04:59:25 +03:00
|
|
|
|
private string _previousUserId;
|
2016-06-12 07:49:35 +03:00
|
|
|
|
private string _pin;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
|
2016-05-02 09:52:09 +03:00
|
|
|
|
public AuthService(
|
2016-05-03 00:50:16 +03:00
|
|
|
|
ISecureStorageService secureStorage,
|
|
|
|
|
ISettings settings,
|
2016-05-02 09:52:09 +03:00
|
|
|
|
ICryptoService cryptoService,
|
2016-05-06 07:17:38 +03:00
|
|
|
|
IAuthApiRepository authApiRepository)
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
|
|
|
|
_secureStorage = secureStorage;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
_settings = settings;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
_cryptoService = cryptoService;
|
2016-05-06 07:17:38 +03:00
|
|
|
|
_authApiRepository = authApiRepository;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Token
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-05-03 00:50:16 +03:00
|
|
|
|
if(_token != null)
|
|
|
|
|
{
|
|
|
|
|
return _token;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-02 09:52:09 +03:00
|
|
|
|
var tokenBytes = _secureStorage.Retrieve(TokenKey);
|
2016-05-03 00:50:16 +03:00
|
|
|
|
if(tokenBytes == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_token = Encoding.UTF8.GetString(tokenBytes, 0, tokenBytes.Length);
|
|
|
|
|
return _token;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if(value != null)
|
|
|
|
|
{
|
|
|
|
|
var tokenBytes = Encoding.UTF8.GetBytes(value);
|
|
|
|
|
_secureStorage.Store(TokenKey, tokenBytes);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_secureStorage.Delete(TokenKey);
|
2016-05-03 00:50:16 +03:00
|
|
|
|
}
|
2016-06-12 07:49:35 +03:00
|
|
|
|
|
|
|
|
|
_token = value;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string UserId
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-08-30 06:06:29 +03:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(_userId))
|
2016-05-03 00:50:16 +03:00
|
|
|
|
{
|
|
|
|
|
return _userId;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-30 06:06:29 +03:00
|
|
|
|
var userId = _settings.GetValueOrDefault(UserIdKey, string.Empty);
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(userId))
|
|
|
|
|
{
|
|
|
|
|
_userId = userId;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-03 00:50:16 +03:00
|
|
|
|
return _userId;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if(value != null)
|
|
|
|
|
{
|
|
|
|
|
_settings.AddOrUpdateValue(UserIdKey, value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-08-06 04:59:25 +03:00
|
|
|
|
PreviousUserId = _userId;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
_settings.Remove(UserIdKey);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
2016-06-12 07:49:35 +03:00
|
|
|
|
|
|
|
|
|
_userId = value;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-06 04:59:25 +03:00
|
|
|
|
public string PreviousUserId
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-08-30 06:06:29 +03:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(_previousUserId))
|
2016-08-06 04:59:25 +03:00
|
|
|
|
{
|
|
|
|
|
return _previousUserId;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-30 06:06:29 +03:00
|
|
|
|
var previousUserId = _settings.GetValueOrDefault(PreviousUserIdKey, string.Empty);
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(previousUserId))
|
|
|
|
|
{
|
|
|
|
|
_previousUserId = previousUserId;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-06 04:59:25 +03:00
|
|
|
|
return _previousUserId;
|
|
|
|
|
}
|
|
|
|
|
private set
|
|
|
|
|
{
|
|
|
|
|
if(value != null)
|
|
|
|
|
{
|
|
|
|
|
_settings.AddOrUpdateValue(PreviousUserIdKey, value);
|
|
|
|
|
_previousUserId = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool UserIdChanged => PreviousUserId != UserId;
|
|
|
|
|
|
2016-07-19 02:16:27 +03:00
|
|
|
|
public string Email
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-08-30 06:06:29 +03:00
|
|
|
|
if(!string.IsNullOrWhiteSpace(_email))
|
2016-07-19 02:16:27 +03:00
|
|
|
|
{
|
|
|
|
|
return _email;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-30 06:06:29 +03:00
|
|
|
|
var email = _settings.GetValueOrDefault(EmailKey, string.Empty);
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(email))
|
|
|
|
|
{
|
|
|
|
|
_email = email;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-19 02:16:27 +03:00
|
|
|
|
return _email;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if(value != null)
|
|
|
|
|
{
|
|
|
|
|
_settings.AddOrUpdateValue(EmailKey, value);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_settings.Remove(EmailKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_email = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-02 09:52:09 +03:00
|
|
|
|
public bool IsAuthenticated
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-08-30 06:06:29 +03:00
|
|
|
|
return _cryptoService.Key != null && !string.IsNullOrWhiteSpace(Token) && !string.IsNullOrWhiteSpace(UserId);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-23 09:17:11 +03:00
|
|
|
|
public bool IsAuthenticatedTwoFactor
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-08-30 06:06:29 +03:00
|
|
|
|
return _cryptoService.Key != null && !string.IsNullOrWhiteSpace(Token) && string.IsNullOrWhiteSpace(UserId);
|
2016-07-23 09:17:11 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-06-12 07:49:35 +03:00
|
|
|
|
public string PIN
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if(_pin != null)
|
|
|
|
|
{
|
|
|
|
|
return _pin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pinBytes = _secureStorage.Retrieve(PinKey);
|
|
|
|
|
if(pinBytes == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_pin = Encoding.UTF8.GetString(pinBytes, 0, pinBytes.Length);
|
|
|
|
|
return _pin;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if(value != null)
|
|
|
|
|
{
|
|
|
|
|
var pinBytes = Encoding.UTF8.GetBytes(value);
|
|
|
|
|
_secureStorage.Store(PinKey, pinBytes);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_secureStorage.Delete(PinKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_pin = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-03 00:50:16 +03:00
|
|
|
|
public void LogOut()
|
|
|
|
|
{
|
|
|
|
|
Token = null;
|
|
|
|
|
UserId = null;
|
2016-07-19 02:16:27 +03:00
|
|
|
|
Email = null;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
_cryptoService.Key = null;
|
2016-08-06 06:58:31 +03:00
|
|
|
|
_settings.Remove(Constants.FirstVaultLoad);
|
2016-05-03 00:50:16 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-02 09:52:09 +03:00
|
|
|
|
public async Task<ApiResult<TokenResponse>> TokenPostAsync(TokenRequest request)
|
|
|
|
|
{
|
2016-05-06 07:17:38 +03:00
|
|
|
|
// TODO: move more logic in here
|
|
|
|
|
return await _authApiRepository.PostTokenAsync(request);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
2016-07-23 09:17:11 +03:00
|
|
|
|
|
|
|
|
|
public async Task<ApiResult<TokenResponse>> TokenTwoFactorPostAsync(TokenTwoFactorRequest request)
|
|
|
|
|
{
|
|
|
|
|
// TODO: move more logic in here
|
|
|
|
|
return await _authApiRepository.PostTokenTwoFactorAsync(request);
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|