-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PM-2572] Add new cipher encryption on attachments without key when moving cipher to an org #3238
base: main
Are you sure you want to change the base?
Changes from 4 commits
4332e7a
354aec0
25e394e
265c95b
890eff7
df9fcd1
c40eb9d
290266c
485aca0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -571,28 +571,33 @@ public async Task SaveWithServerAsync(Cipher cipher) | |
await UpsertAsync(data); | ||
} | ||
|
||
public async Task ShareWithServerAsync(CipherView cipher, string organizationId, HashSet<string> collectionIds) | ||
public async Task ShareWithServerAsync(CipherView cipherView, string organizationId, HashSet<string> collectionIds) | ||
{ | ||
var attachmentTasks = new List<Task>(); | ||
if (cipher.Attachments != null) | ||
Cipher cipher = null; | ||
//If the cipher doesn't have a key, we update it | ||
if(cipherView.Key == null) | ||
{ | ||
foreach (var attachment in cipher.Attachments) | ||
cipher = await EncryptAsync(cipherView); | ||
await UpdateAndUpsertAsync(async () => await _apiService.PutCipherAsync(cipherView.Id, new CipherRequest(cipher))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⛏️ You can omit the |
||
cipher = await GetAsync(cipherView.Id); | ||
cipherView = await cipher.DecryptAsync(); | ||
} | ||
if (cipherView.Attachments != null) | ||
{ | ||
foreach (var attachment in cipherView.Attachments) | ||
{ | ||
if (attachment.Key == null) | ||
{ | ||
attachmentTasks.Add(ShareAttachmentWithServerAsync(attachment, cipher.Id, organizationId)); | ||
attachmentTasks.Add(ShareAttachmentWithServerAsync(attachment, cipherView.Id, organizationId)); | ||
} | ||
} | ||
} | ||
await Task.WhenAll(attachmentTasks); | ||
cipher.OrganizationId = organizationId; | ||
cipher.CollectionIds = collectionIds; | ||
var encCipher = await EncryptAsync(cipher); | ||
var request = new CipherShareRequest(encCipher); | ||
var response = await _apiService.PutShareCipherAsync(cipher.Id, request); | ||
var userId = await _stateService.GetActiveUserIdAsync(); | ||
var data = new CipherData(response, userId, collectionIds); | ||
await UpsertAsync(data); | ||
cipherView.OrganizationId = organizationId; | ||
cipherView.CollectionIds = collectionIds; | ||
cipher = await EncryptAsync(cipherView); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎨 You can move encrypting into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't do that because I need the cipher to create two different requests, CipherRequest and CipherShareRequest. I could add in the api call but I think there's already too much happening in one line of code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see, I think it's fine to pass the |
||
await UpdateAndUpsertAsync(async () => await _apiService.PutShareCipherAsync(cipherView.Id, new CipherShareRequest(cipher)), collectionIds); | ||
} | ||
|
||
public async Task<Cipher> SaveAttachmentRawWithServerAsync(Cipher cipher, CipherView cipherView, string filename, byte[] data) | ||
|
@@ -1355,6 +1360,13 @@ await _stateService.GetDisableAutoTotpCopyAsync() == true) | |
} | ||
} | ||
|
||
private async Task UpdateAndUpsertAsync(Func<Task<CipherResponse>> func, HashSet<string> collectionIds = null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⛏️ I'd rename the |
||
{ | ||
var response = await func(); | ||
var data = new CipherData(response, await _stateService.GetActiveUserIdAsync(), collectionIds); | ||
await UpsertAsync(data); | ||
} | ||
|
||
private class CipherLocaleComparer : IComparer<CipherView> | ||
{ | ||
private readonly II18nService _i18nService; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎨 I think we could improve this checking if we should use cipher key encryption by calling
ShouldUseCipherKeyEncryptionAsync
, so we save a sever call if that's not the case.