bitwarden-android/src/Core/Services/TokenService.cs

241 lines
7.1 KiB
C#
Raw Normal View History

2019-04-09 17:35:21 +03:00
using Bit.Core.Abstractions;
using Bit.Core.Utilities;
using Newtonsoft.Json.Linq;
using System;
using System.Text;
using System.Threading.Tasks;
namespace Bit.Core.Services
{
public class TokenService : ITokenService
{
private readonly IStorageService _storageService;
private string _token;
private JObject _decodedToken;
private string _refreshToken;
private const string Keys_AccessToken = "accessToken";
private const string Keys_RefreshToken = "refreshToken";
private const string Keys_TwoFactorTokenFormat = "twoFactorToken_{0}";
public TokenService(IStorageService storageService)
{
_storageService = storageService;
}
public async Task SetTokensAsync(string accessToken, string refreshToken)
{
await Task.WhenAll(
SetTokenAsync(accessToken),
SetRefreshTokenAsync(refreshToken));
}
public async Task SetTokenAsync(string token)
{
_token = token;
_decodedToken = null;
[Auto Logout] Final review of feature (#932) * Initial commit of LockService name refactor (#831) * [Auto-Logout] Update Service layer logic (#835) * Initial commit of service logic update * Added default value for action * Updated ToggleTokensAsync conditional * Removed unused variables, updated action conditional * Initial commit: lockOption/lock refactor app layer (#840) * [Auto-Logout] Settings Refactor - Application Layer Part 2 (#844) * Initial commit of app layer part 2 * Updated biometrics position * Reverted resource name refactor * LockOptions refactor revert * Updated method casing :: Removed VaultTimeout prefix for timeouts * Fixed dupe string resource (#854) * Updated dependency to use VaultTimeoutService (#896) * [Auto Logout] Xamarin Forms in AutoFill flow (iOS) (#902) * fix typo in PINRequireMasterPasswordRestart (#900) * initial commit for xf usage in autofill * Fixed databinding for hint button * Updated Two Factor page launch - removed unused imports * First pass at broadcast/messenger implentation for autofill * setting theme in extension using theme manager * extension app resources * App resources from main app * fix ref to twoFactorPage * apply resources to page * load empty app for sytling in extension * move ios renderers to ios core * static ref to resources and GetResourceColor helper * fix method ref * move application.current.resources refs to helper * switch login page alerts to device action dialogs * run on main thread * showDialog with device action service * abstract action sheet to device action service * add support for yubikey * add yubikey iimages to extension * support close button action * add support to action extension * remove empty lines Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> * [Auto Logout] Update lock option to be default value (#929) * Initial commit - make lock action default * Removed extra whitespace Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> Co-authored-by: Kyle Spearrin <kspearrin@users.noreply.github.com>
2020-05-29 19:26:36 +03:00
if (await SkipTokenStorage())
{
// If we have a vault timeout and the action is log out, don't store token
return;
}
2019-04-09 17:35:21 +03:00
await _storageService.SaveAsync(Keys_AccessToken, token);
}
public async Task<string> GetTokenAsync()
{
if (_token != null)
2019-04-09 17:35:21 +03:00
{
return _token;
}
_token = await _storageService.GetAsync<string>(Keys_AccessToken);
return _token;
}
public async Task SetRefreshTokenAsync(string refreshToken)
{
_refreshToken = refreshToken;
[Auto Logout] Final review of feature (#932) * Initial commit of LockService name refactor (#831) * [Auto-Logout] Update Service layer logic (#835) * Initial commit of service logic update * Added default value for action * Updated ToggleTokensAsync conditional * Removed unused variables, updated action conditional * Initial commit: lockOption/lock refactor app layer (#840) * [Auto-Logout] Settings Refactor - Application Layer Part 2 (#844) * Initial commit of app layer part 2 * Updated biometrics position * Reverted resource name refactor * LockOptions refactor revert * Updated method casing :: Removed VaultTimeout prefix for timeouts * Fixed dupe string resource (#854) * Updated dependency to use VaultTimeoutService (#896) * [Auto Logout] Xamarin Forms in AutoFill flow (iOS) (#902) * fix typo in PINRequireMasterPasswordRestart (#900) * initial commit for xf usage in autofill * Fixed databinding for hint button * Updated Two Factor page launch - removed unused imports * First pass at broadcast/messenger implentation for autofill * setting theme in extension using theme manager * extension app resources * App resources from main app * fix ref to twoFactorPage * apply resources to page * load empty app for sytling in extension * move ios renderers to ios core * static ref to resources and GetResourceColor helper * fix method ref * move application.current.resources refs to helper * switch login page alerts to device action dialogs * run on main thread * showDialog with device action service * abstract action sheet to device action service * add support for yubikey * add yubikey iimages to extension * support close button action * add support to action extension * remove empty lines Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> * [Auto Logout] Update lock option to be default value (#929) * Initial commit - make lock action default * Removed extra whitespace Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> Co-authored-by: Kyle Spearrin <kspearrin@users.noreply.github.com>
2020-05-29 19:26:36 +03:00
if (await SkipTokenStorage())
{
// If we have a vault timeout and the action is log out, don't store token
return;
}
2019-04-09 17:35:21 +03:00
await _storageService.SaveAsync(Keys_RefreshToken, refreshToken);
}
public async Task<string> GetRefreshTokenAsync()
{
if (_refreshToken != null)
2019-04-09 17:35:21 +03:00
{
return _refreshToken;
}
_refreshToken = await _storageService.GetAsync<string>(Keys_RefreshToken);
return _refreshToken;
}
[Auto Logout] Final review of feature (#932) * Initial commit of LockService name refactor (#831) * [Auto-Logout] Update Service layer logic (#835) * Initial commit of service logic update * Added default value for action * Updated ToggleTokensAsync conditional * Removed unused variables, updated action conditional * Initial commit: lockOption/lock refactor app layer (#840) * [Auto-Logout] Settings Refactor - Application Layer Part 2 (#844) * Initial commit of app layer part 2 * Updated biometrics position * Reverted resource name refactor * LockOptions refactor revert * Updated method casing :: Removed VaultTimeout prefix for timeouts * Fixed dupe string resource (#854) * Updated dependency to use VaultTimeoutService (#896) * [Auto Logout] Xamarin Forms in AutoFill flow (iOS) (#902) * fix typo in PINRequireMasterPasswordRestart (#900) * initial commit for xf usage in autofill * Fixed databinding for hint button * Updated Two Factor page launch - removed unused imports * First pass at broadcast/messenger implentation for autofill * setting theme in extension using theme manager * extension app resources * App resources from main app * fix ref to twoFactorPage * apply resources to page * load empty app for sytling in extension * move ios renderers to ios core * static ref to resources and GetResourceColor helper * fix method ref * move application.current.resources refs to helper * switch login page alerts to device action dialogs * run on main thread * showDialog with device action service * abstract action sheet to device action service * add support for yubikey * add yubikey iimages to extension * support close button action * add support to action extension * remove empty lines Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> * [Auto Logout] Update lock option to be default value (#929) * Initial commit - make lock action default * Removed extra whitespace Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> Co-authored-by: Kyle Spearrin <kspearrin@users.noreply.github.com>
2020-05-29 19:26:36 +03:00
public async Task ToggleTokensAsync()
{
var token = await GetTokenAsync();
var refreshToken = await GetRefreshTokenAsync();
if (await SkipTokenStorage())
{
await ClearTokenAsync();
_token = token;
_refreshToken = refreshToken;
return;
}
await SetTokenAsync(token);
await SetRefreshTokenAsync(refreshToken);
}
2019-04-09 17:35:21 +03:00
public async Task SetTwoFactorTokenAsync(string token, string email)
{
await _storageService.SaveAsync(string.Format(Keys_TwoFactorTokenFormat, email), token);
}
public async Task<string> GetTwoFactorTokenAsync(string email)
{
return await _storageService.GetAsync<string>(string.Format(Keys_TwoFactorTokenFormat, email));
}
public async Task ClearTwoFactorTokenAsync(string email)
{
await _storageService.RemoveAsync(string.Format(Keys_TwoFactorTokenFormat, email));
}
public async Task ClearTokenAsync()
{
_token = null;
_decodedToken = null;
_refreshToken = null;
await Task.WhenAll(
_storageService.RemoveAsync(Keys_AccessToken),
_storageService.RemoveAsync(Keys_RefreshToken));
}
public JObject DecodeToken()
{
if (_decodedToken != null)
2019-04-09 17:35:21 +03:00
{
return _decodedToken;
}
if (_token == null)
2019-04-09 17:35:21 +03:00
{
throw new InvalidOperationException("Token not found.");
}
var parts = _token.Split('.');
if (parts.Length != 3)
2019-04-09 17:35:21 +03:00
{
throw new InvalidOperationException("JWT must have 3 parts.");
}
var decodedBytes = CoreHelpers.Base64UrlDecode(parts[1]);
if (decodedBytes == null || decodedBytes.Length < 1)
2019-04-09 17:35:21 +03:00
{
throw new InvalidOperationException("Cannot decode the token.");
}
_decodedToken = JObject.Parse(Encoding.UTF8.GetString(decodedBytes));
return _decodedToken;
}
public DateTime? GetTokenExpirationDate()
{
var decoded = DecodeToken();
if (decoded?["exp"] == null)
2019-04-09 17:35:21 +03:00
{
return null;
}
return CoreHelpers.Epoc.AddSeconds(Convert.ToDouble(decoded["exp"].Value<long>()));
}
public int TokenSecondsRemaining()
{
var d = GetTokenExpirationDate();
if (d == null)
2019-04-09 17:35:21 +03:00
{
return 0;
}
var timeRemaining = d.Value - DateTime.UtcNow;
return (int)timeRemaining.TotalSeconds;
}
public bool TokenNeedsRefresh(int minutes = 5)
{
var sRemaining = TokenSecondsRemaining();
return sRemaining < (60 * minutes);
}
public string GetUserId()
{
var decoded = DecodeToken();
if (decoded?["sub"] == null)
2019-04-09 17:35:21 +03:00
{
throw new Exception("No user id found.");
}
return decoded["sub"].Value<string>();
}
public string GetEmail()
{
var decoded = DecodeToken();
if (decoded?["email"] == null)
2019-04-09 17:35:21 +03:00
{
throw new Exception("No email found.");
}
return decoded["email"].Value<string>();
}
public bool GetEmailVerified()
{
var decoded = DecodeToken();
if (decoded?["email_verified"] == null)
2019-04-09 17:35:21 +03:00
{
throw new Exception("No email verification found.");
}
return decoded["email_verified"].Value<bool>();
}
public string GetName()
{
var decoded = DecodeToken();
if (decoded?["name"] == null)
2019-04-09 17:35:21 +03:00
{
return null;
}
return decoded["name"].Value<string>();
}
public bool GetPremium()
{
var decoded = DecodeToken();
if (decoded?["premium"] == null)
2019-04-09 17:35:21 +03:00
{
return false;
}
return decoded["premium"].Value<bool>();
}
public string GetIssuer()
{
var decoded = DecodeToken();
if (decoded?["iss"] == null)
2019-04-09 17:35:21 +03:00
{
throw new Exception("No issuer found.");
}
return decoded["iss"].Value<string>();
}
[Auto Logout] Final review of feature (#932) * Initial commit of LockService name refactor (#831) * [Auto-Logout] Update Service layer logic (#835) * Initial commit of service logic update * Added default value for action * Updated ToggleTokensAsync conditional * Removed unused variables, updated action conditional * Initial commit: lockOption/lock refactor app layer (#840) * [Auto-Logout] Settings Refactor - Application Layer Part 2 (#844) * Initial commit of app layer part 2 * Updated biometrics position * Reverted resource name refactor * LockOptions refactor revert * Updated method casing :: Removed VaultTimeout prefix for timeouts * Fixed dupe string resource (#854) * Updated dependency to use VaultTimeoutService (#896) * [Auto Logout] Xamarin Forms in AutoFill flow (iOS) (#902) * fix typo in PINRequireMasterPasswordRestart (#900) * initial commit for xf usage in autofill * Fixed databinding for hint button * Updated Two Factor page launch - removed unused imports * First pass at broadcast/messenger implentation for autofill * setting theme in extension using theme manager * extension app resources * App resources from main app * fix ref to twoFactorPage * apply resources to page * load empty app for sytling in extension * move ios renderers to ios core * static ref to resources and GetResourceColor helper * fix method ref * move application.current.resources refs to helper * switch login page alerts to device action dialogs * run on main thread * showDialog with device action service * abstract action sheet to device action service * add support for yubikey * add yubikey iimages to extension * support close button action * add support to action extension * remove empty lines Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> * [Auto Logout] Update lock option to be default value (#929) * Initial commit - make lock action default * Removed extra whitespace Co-authored-by: Jonas Kittner <54631600+theendlessriver13@users.noreply.github.com> Co-authored-by: Kyle Spearrin <kyle.spearrin@gmail.com> Co-authored-by: Kyle Spearrin <kspearrin@users.noreply.github.com>
2020-05-29 19:26:36 +03:00
private async Task<bool> SkipTokenStorage()
{
var timeout = await _storageService.GetAsync<int?>(Constants.VaultTimeoutKey);
var action = await _storageService.GetAsync<string>(Constants.VaultTimeoutActionKey);
return timeout.HasValue && action == "logOut";
}
2019-04-09 17:35:21 +03:00
}
}