authorizationService
This commit is contained in:
parent
55f7522658
commit
ff98f1a5c1
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using SafeMessageStorage.Encryption.Hash.Providers;
|
||||
using SafeMessageStorage.Pages;
|
||||
using SafeMessageStorage.Services.AuthorizationService;
|
||||
using Xamarin.Forms;
|
||||
@ -11,7 +12,7 @@ namespace SafeMessageStorage
|
||||
public App()
|
||||
{
|
||||
InitializeComponent();
|
||||
MainPage = new NavigationPage(new AuthorizationPage(new AuthorizationServiceMOCK()));
|
||||
MainPage = new NavigationPage(new AuthorizationPage(new AuthorizationService(new Sha256HashProvider())));
|
||||
}
|
||||
|
||||
protected override void OnStart()
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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())
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -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())
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -10,7 +10,7 @@
|
||||
<StackLayout VerticalOptions="CenterAndExpand" Padding="40">
|
||||
<Label Text="Enter your security code"
|
||||
FontSize="23"/>
|
||||
<Entry IsPassword="True" Placeholder="Your hard code"/>
|
||||
<Entry IsPassword="True" Text="{Binding Password}" Placeholder="Your hard code"/>
|
||||
<Label x:Name="ErrorMessageLabel"
|
||||
TextColor="#d22"
|
||||
Margin="0,10" HorizontalOptions="Center"/>
|
||||
|
@ -11,6 +11,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Encryption\Symmetric\" />
|
||||
<Folder Include="Views\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -21,18 +21,18 @@ namespace SafeMessageStorage.ViewModels
|
||||
public event EventHandler AuthorizationSucceeded;
|
||||
public event EventHandler PasswordSetRequested;
|
||||
|
||||
private string _securityCode;
|
||||
public string SecurityCode
|
||||
private string _password;
|
||||
public string Password
|
||||
{
|
||||
get=>_securityCode;
|
||||
get=>_password;
|
||||
set
|
||||
{
|
||||
_securityCode = value;
|
||||
_password = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public ICommand AuthorizeCommand => new Command(async () => await Authorize(SecurityCode));
|
||||
public ICommand AuthorizeCommand => new Command(async () => await Authorize(Password));
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
@ -47,7 +47,7 @@ namespace SafeMessageStorage.ViewModels
|
||||
else
|
||||
{
|
||||
AuthorizationFailed?.Invoke(this, "Invalid code");
|
||||
SecurityCode = string.Empty;
|
||||
Password = string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user