authorizationService

This commit is contained in:
Bartosz Chyży 2020-06-07 14:49:02 +02:00
parent 55f7522658
commit ff98f1a5c1
9 changed files with 150 additions and 8 deletions

View File

@ -1,4 +1,5 @@
using System; using System;
using SafeMessageStorage.Encryption.Hash.Providers;
using SafeMessageStorage.Pages; using SafeMessageStorage.Pages;
using SafeMessageStorage.Services.AuthorizationService; using SafeMessageStorage.Services.AuthorizationService;
using Xamarin.Forms; using Xamarin.Forms;
@ -11,7 +12,7 @@ namespace SafeMessageStorage
public App() public App()
{ {
InitializeComponent(); InitializeComponent();
MainPage = new NavigationPage(new AuthorizationPage(new AuthorizationServiceMOCK())); MainPage = new NavigationPage(new AuthorizationPage(new AuthorizationService(new Sha256HashProvider())));
} }
protected override void OnStart() protected override void OnStart()

View File

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace SafeMessageStorage.Encryption.Hash
{
public abstract class HashProviderBase : IHashProvider
{
private readonly HashAlgorithm _hashAlgorithm;
protected HashProviderBase(HashAlgorithm hashAlgorithm)
{
_hashAlgorithm = hashAlgorithm;
}
public virtual string GetHashString(string input)
{
return GetHashString(Encoding.UTF8.GetBytes(input));
}
public virtual string GetHashString(byte[] bytes)
{
using (_hashAlgorithm)
{
return GetHashBytes(bytes)
.Select(b =>
b.ToString("x2"))
.Aggregate((a, b) =>
a + b);
}
}
public virtual byte[] GetHashBytes(string input)
{
return GetHashBytes(Encoding.UTF8.GetBytes(input));
}
public virtual byte[] GetHashBytes(byte[] bytes)
{
using (_hashAlgorithm)
{
return _hashAlgorithm.ComputeHash(bytes);
}
}
}
}

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace SafeMessageStorage.Encryption.Hash
{
public interface IHashProvider
{
string GetHashString(string input);
string GetHashString(byte[] bytes);
byte[] GetHashBytes(string input);
byte[] GetHashBytes(byte[] bytes);
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace SafeMessageStorage.Encryption.Hash.Providers
{
public class Sha256HashProvider : HashProviderBase
{
public Sha256HashProvider() : base(new SHA256CryptoServiceProvider())
{
}
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace SafeMessageStorage.Encryption.Hash.Providers
{
public class Sha512HashProvider : HashProviderBase
{
public Sha512HashProvider() : base(new SHA512CryptoServiceProvider())
{
}
}
}

View File

@ -10,7 +10,7 @@
<StackLayout VerticalOptions="CenterAndExpand" Padding="40"> <StackLayout VerticalOptions="CenterAndExpand" Padding="40">
<Label Text="Enter your security code" <Label Text="Enter your security code"
FontSize="23"/> FontSize="23"/>
<Entry IsPassword="True" Placeholder="Your hard code"/> <Entry IsPassword="True" Text="{Binding Password}" Placeholder="Your hard code"/>
<Label x:Name="ErrorMessageLabel" <Label x:Name="ErrorMessageLabel"
TextColor="#d22" TextColor="#d22"
Margin="0,10" HorizontalOptions="Center"/> Margin="0,10" HorizontalOptions="Center"/>

View File

@ -11,6 +11,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Encryption\Symmetric\" />
<Folder Include="Views\" /> <Folder Include="Views\" />
</ItemGroup> </ItemGroup>

View File

@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using SafeMessageStorage.Encryption.Hash;
namespace SafeMessageStorage.Services.AuthorizationService
{
public class AuthorizationService : IAuthorizationService
{
private readonly string _passwordKey = "passwordGUID";
private readonly IHashProvider _hashProvider;
public AuthorizationService(IHashProvider hashProvider)
{
_hashProvider = hashProvider;
}
public bool IsAuthorized { get; private set; }
public bool IsPasswordSet => Xamarin.Essentials.SecureStorage.GetAsync(_passwordKey).Result != null;
public async Task<bool> Authorize(string password)
{
var keyChainPassword = await Xamarin.Essentials.SecureStorage.GetAsync(_passwordKey);
var result = keyChainPassword?.Equals(_hashProvider.GetHashString(password)) ?? false;
IsAuthorized = result;
return result;
}
public Task<bool> Deauthorize()
{
IsAuthorized = false;
return Task.FromResult(true);
}
public async Task<bool> ChangePassword(string currentPassword, string newPassword)
{
var keyChainPassword = await Xamarin.Essentials.SecureStorage.GetAsync(_passwordKey);
var passwordMatch = keyChainPassword?.Equals(_hashProvider.GetHashString(currentPassword)) ?? true;
if (!passwordMatch)
return false;
await Xamarin.Essentials.SecureStorage.SetAsync(_passwordKey, _hashProvider.GetHashString(newPassword));
return true;
}
}
}

View File

@ -21,18 +21,18 @@ namespace SafeMessageStorage.ViewModels
public event EventHandler AuthorizationSucceeded; public event EventHandler AuthorizationSucceeded;
public event EventHandler PasswordSetRequested; public event EventHandler PasswordSetRequested;
private string _securityCode; private string _password;
public string SecurityCode public string Password
{ {
get=>_securityCode; get=>_password;
set set
{ {
_securityCode = value; _password = value;
OnPropertyChanged(); OnPropertyChanged();
} }
} }
public ICommand AuthorizeCommand => new Command(async () => await Authorize(SecurityCode)); public ICommand AuthorizeCommand => new Command(async () => await Authorize(Password));
public void Initialize() public void Initialize()
{ {
@ -47,7 +47,7 @@ namespace SafeMessageStorage.ViewModels
else else
{ {
AuthorizationFailed?.Invoke(this, "Invalid code"); AuthorizationFailed?.Invoke(this, "Invalid code");
SecurityCode = string.Empty; Password = string.Empty;
} }
} }
} }