store previous key and userid so we can determine if stored crypto is usable before a sync

This commit is contained in:
Kyle Spearrin 2016-08-05 21:59:25 -04:00
parent 2d0bfe1a92
commit d96a94b478
5 changed files with 63 additions and 1 deletions

View File

@ -9,6 +9,8 @@ namespace Bit.App.Abstractions
bool IsAuthenticatedTwoFactor { get; }
string Token { get; set; }
string UserId { get; set; }
string PreviousUserId { get; }
bool UserIdChanged { get; }
string Email { get; set; }
string PIN { get; set; }

View File

@ -6,6 +6,8 @@ namespace Bit.App.Abstractions
{
string Base64Key { get; }
byte[] Key { get; set; }
byte[] PreviousKey { get; }
bool KeyChanged { get; }
string Decrypt(CipherString encyptedValue);
CipherString Encrypt(string plaintextValue);

View File

@ -161,7 +161,11 @@ namespace Bit.App.Pages
var activatedButton = new Button
{
Text = "See Supported Apps",
Command = new Command(() => Device.OpenUri(new Uri("https://bitwarden.com"))),
Command = new Command(() =>
{
_googleAnalyticsService.TrackAppEvent("SeeSupportedApps");
Device.OpenUri(new Uri("https://bitwarden.com"));
}),
VerticalOptions = LayoutOptions.End,
HorizontalOptions = LayoutOptions.Fill,
Style = (Style)Application.Current.Resources["btn-primary"]

View File

@ -12,6 +12,7 @@ namespace Bit.App.Services
private const string TokenKey = "token";
private const string EmailKey = "email";
private const string UserIdKey = "userId";
private const string PreviousUserIdKey = "previousUserId";
private const string PinKey = "pin";
private readonly ISecureStorageService _secureStorage;
@ -22,6 +23,7 @@ namespace Bit.App.Services
private string _token;
private string _email;
private string _userId;
private string _previousUserId;
private string _pin;
public AuthService(
@ -90,6 +92,7 @@ namespace Bit.App.Services
}
else
{
PreviousUserId = _userId;
_settings.Remove(UserIdKey);
}
@ -97,6 +100,30 @@ namespace Bit.App.Services
}
}
public string PreviousUserId
{
get
{
if(_previousUserId != null)
{
return _previousUserId;
}
_previousUserId = _settings.GetValueOrDefault<string>(PreviousUserIdKey);
return _previousUserId;
}
private set
{
if(value != null)
{
_settings.AddOrUpdateValue(PreviousUserIdKey, value);
_previousUserId = value;
}
}
}
public bool UserIdChanged => PreviousUserId != UserId;
public string Email
{
get

View File

@ -4,18 +4,21 @@ using System.Text;
using Bit.App.Abstractions;
using Bit.App.Models;
using PCLCrypto;
using System.Linq;
namespace Bit.App.Services
{
public class CryptoService : ICryptoService
{
private const string KeyKey = "key";
private const string PreviousKeyKey = "previousKey";
private const int InitializationVectorSize = 16;
private readonly Random _random = new Random();
private readonly ISecureStorageService _secureStorage;
private readonly IKeyDerivationService _keyDerivationService;
private byte[] _key;
private byte[] _previousKey;
public CryptoService(
ISecureStorageService secureStorage,
@ -44,6 +47,7 @@ namespace Bit.App.Services
}
else
{
PreviousKey = _key;
_secureStorage.Delete(KeyKey);
_key = null;
}
@ -63,6 +67,29 @@ namespace Bit.App.Services
}
}
public byte[] PreviousKey
{
get
{
if(_previousKey == null)
{
_previousKey = _secureStorage.Retrieve(PreviousKeyKey);
}
return _previousKey;
}
private set
{
if(value != null)
{
_secureStorage.Store(PreviousKeyKey, value);
_previousKey = value;
}
}
}
public bool KeyChanged => !PreviousKey?.SequenceEqual(Key) ?? Key == null ? false : true;
public CipherString Encrypt(string plaintextValue)
{
if(Key == null)