Bitwarden-app-android-iphon.../src/Core/Models/Domain/Attachment.cs

69 lines
2.0 KiB
C#
Raw Normal View History

2019-04-15 13:56:46 +02:00
using Bit.Core.Abstractions;
using Bit.Core.Models.Data;
2019-04-12 21:50:30 +02:00
using Bit.Core.Models.View;
2019-04-15 13:56:46 +02:00
using Bit.Core.Utilities;
2019-04-12 21:50:30 +02:00
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Bit.Core.Models.Domain
{
public class Attachment : Domain
{
2019-04-12 22:39:20 +02:00
private HashSet<string> _map = new HashSet<string>
{
"Id",
"Url",
"SizeName",
"FileName",
"Key"
};
2019-04-12 21:50:30 +02:00
public Attachment() { }
public Attachment(AttachmentData obj, bool alreadyEncrypted = false)
{
Size = obj.Size;
2019-04-12 22:39:20 +02:00
BuildDomainModel(this, obj, _map, alreadyEncrypted, new HashSet<string> { "Id", "Url", "SizeName" });
2019-04-12 21:50:30 +02:00
}
public string Id { get; set; }
public string Url { get; set; }
public string Size { get; set; }
public string SizeName { get; set; }
public CipherString Key { get; set; }
public CipherString FileName { get; set; }
public async Task<AttachmentView> DecryptAsync(string orgId)
{
var view = await DecryptObjAsync(new AttachmentView(this), this, new HashSet<string>
{
"FileName"
}, orgId);
2019-04-15 13:56:46 +02:00
if(Key != null)
{
var cryptoService = ServiceContainer.Resolve<ICryptoService>("cryptoService");
try
{
var orgKey = await cryptoService.GetOrgKeyAsync(orgId);
var decValue = await cryptoService.DecryptToBytesAsync(Key, orgKey);
view.Key = new SymmetricCryptoKey(decValue);
}
catch
{
// TODO: error?
}
}
2019-04-12 21:50:30 +02:00
return view;
}
public AttachmentData ToAttachmentData()
{
var a = new AttachmentData();
a.Size = Size;
2019-04-12 22:39:20 +02:00
BuildDataModel(this, a, _map, new HashSet<string> { "Id", "Url", "SizeName" });
2019-04-12 21:50:30 +02:00
return a;
}
}
}