nowy accessor

This commit is contained in:
Bartosz Chyży 2020-06-21 01:18:35 +02:00
parent d6a6b58a9d
commit c2e22ac73f
2 changed files with 32 additions and 12 deletions

View File

@ -12,8 +12,10 @@ namespace SafeMessageStorage.Services.MessageStorageService
Task<bool> SaveMessageAsync(Message message); Task<bool> SaveMessageAsync(Message message);
Task<Message> ReadMessageAsync(); Task<IEnumerable<Message>> ReadMessagesAsync();
Task<bool> DeleteMessageAsync(); Task<bool> DeleteMessageAsync(string messageId);
Task<bool> DeleteAllMessagesAsync();
} }
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -13,33 +14,50 @@ namespace SafeMessageStorage.Services.MessageStorageService
{ {
private IAuthorizationService _authorizationService; private IAuthorizationService _authorizationService;
private ISymmetricEncryptionEngine _encryptionEngine; private ISymmetricEncryptionEngine _encryptionEngine;
private readonly string _messageKey = "377f2486-d2c3-48a7-b821-45d7314479cd"; private readonly string _messagesKey = "377f2486-d2c3-48a7-b821-45d7314479cd";
public bool IsStorageEmpty => Xamarin.Essentials.SecureStorage.GetAsync(_messageKey).Result != null; public bool IsStorageEmpty => Xamarin.Essentials.SecureStorage.GetAsync(_messagesKey).Result != null;
public async Task<bool> SaveMessageAsync(Message message) public async Task<bool> SaveMessageAsync(Message message)
{ {
CheckIfAuthorized(); var messages = await ReadMessagesAsync();
var encrypyedMessage = _encryptionEngine.Encrypt(JsonConvert.SerializeObject(message), _authorizationService.AuthorizedUserHash); var messagesList = messages.ToList();
await Xamarin.Essentials.SecureStorage.SetAsync(_messageKey, encrypyedMessage); messagesList.Add(message);
var encrypyedMessage = _encryptionEngine.Encrypt(JsonConvert.SerializeObject(messagesList), _authorizationService.AuthorizedUserHash);
await Xamarin.Essentials.SecureStorage.SetAsync(_messagesKey, encrypyedMessage);
return true; return true;
} }
public async Task<Message> ReadMessageAsync() public async Task<IEnumerable<Message>> ReadMessagesAsync()
{ {
CheckIfAuthorized(); CheckIfAuthorized();
var encryptedMessage =await Xamarin.Essentials.SecureStorage.GetAsync(_messageKey); var encryptedMessage =await Xamarin.Essentials.SecureStorage.GetAsync(_messagesKey);
if (encryptedMessage == null) if (encryptedMessage == null)
return null; return null;
var decryptedMessage = _encryptionEngine.Decrypt(encryptedMessage, _authorizationService.AuthorizedUserHash); var decryptedMessage = _encryptionEngine.Decrypt(encryptedMessage, _authorizationService.AuthorizedUserHash);
return JsonConvert.DeserializeObject<Message>(decryptedMessage); return JsonConvert.DeserializeObject<List<Message>>(decryptedMessage);
} }
public async Task<bool> DeleteMessageAsync() public async Task<bool> DeleteMessageAsync(string messageId)
{
var messages = await ReadMessagesAsync();
var enumerable = messages.ToList();
if (enumerable.All(m => m.Id != messageId))
return false;
messages = enumerable.Where(m => m.Id != messageId);
var encrypyedMessage = _encryptionEngine.Encrypt(JsonConvert.SerializeObject(messages.ToList()), _authorizationService.AuthorizedUserHash);
await Xamarin.Essentials.SecureStorage.SetAsync(_messagesKey, encrypyedMessage);
return true;
}
public async Task<bool> DeleteAllMessagesAsync()
{ {
CheckIfAuthorized(); CheckIfAuthorized();
return Xamarin.Essentials.SecureStorage.Remove(_messageKey); return Xamarin.Essentials.SecureStorage.Remove(_messagesKey);
} }
private void CheckIfAuthorized() private void CheckIfAuthorized()