diff --git a/Notesnook.API/Controllers/UsersController.cs b/Notesnook.API/Controllers/UsersController.cs index a15c856..05ca526 100644 --- a/Notesnook.API/Controllers/UsersController.cs +++ b/Notesnook.API/Controllers/UsersController.cs @@ -127,6 +127,24 @@ namespace Notesnook.API.Controllers } } + + [HttpGet("verifier")] + public async Task GetEncryptionVerifier() + { + var userId = User.GetUserId(); + try + { + var response = await UserService.GetEncryptionVerifier(userId); + if (response == null) return NotFound(); + return Ok(response); + } + catch (Exception ex) + { + logger.LogError(ex, "Failed to get encryption verifier for user id: {UserId}", userId); + return BadRequest(new { error = ex.Message }); + } + } + [HttpPost("reset")] public async Task Reset([FromForm] bool removeAttachments) { diff --git a/Notesnook.API/Interfaces/IUserService.cs b/Notesnook.API/Interfaces/IUserService.cs index 795cfb2..3ca8452 100644 --- a/Notesnook.API/Interfaces/IUserService.cs +++ b/Notesnook.API/Interfaces/IUserService.cs @@ -31,6 +31,7 @@ namespace Notesnook.API.Interfaces Task DeleteUserAsync(string userId, string? jti, string password); Task ResetUserAsync(string userId, bool removeAttachments); Task GetUserAsync(string userId); + Task GetEncryptionVerifier(string userId); Task SetUserKeysAsync(string userId, UserKeys keys); } } \ No newline at end of file diff --git a/Notesnook.API/Repositories/SyncItemsRepository.cs b/Notesnook.API/Repositories/SyncItemsRepository.cs index d26d40d..c8205b6 100644 --- a/Notesnook.API/Repositories/SyncItemsRepository.cs +++ b/Notesnook.API/Repositories/SyncItemsRepository.cs @@ -36,6 +36,7 @@ using Streetwriters.Common; using Streetwriters.Data.DbContexts; using Streetwriters.Data.Interfaces; using Streetwriters.Data.Repositories; +using AspNetCore.Identity.Mongo.Mongo; namespace Notesnook.API.Repositories { diff --git a/Notesnook.API/Services/UserService.cs b/Notesnook.API/Services/UserService.cs index 0226258..9002cf9 100644 --- a/Notesnook.API/Services/UserService.cs +++ b/Notesnook.API/Services/UserService.cs @@ -30,6 +30,7 @@ using Notesnook.API.Helpers; using Notesnook.API.Interfaces; using Notesnook.API.Models; using Notesnook.API.Models.Responses; +using Notesnook.API.Repositories; using Streetwriters.Common; using Streetwriters.Common.Accessors; using Streetwriters.Common.Enums; @@ -192,6 +193,26 @@ namespace Notesnook.API.Services await Repositories.UsersSettings.UpdateAsync(userSettings.Id, userSettings); } + public async Task GetEncryptionVerifier(string userId) + { + SyncItemsRepository[] repositories = [Repositories.Notes, Repositories.Notebooks, Repositories.Shortcuts, Repositories.Contents, Repositories.Settings, Repositories.LegacySettings, Repositories.Attachments, Repositories.Reminders, Repositories.Relations, Repositories.Colors, Repositories.Tags, Repositories.Vaults, Repositories.InboxItemsHistory]; + foreach (var repo in repositories) + { + var item = await repo.FindOneAsync((s) => s.UserId == userId && (s.KeyVersion == null || s.KeyVersion == 0)); + if (item != null) + { + return new EncryptedData + { + Cipher = item.Cipher, + IV = item.IV, + Salt = "", + Length = item.Length + }; + } + } + return null; + } + public async Task DeleteUserAsync(string userId) { logger.LogInformation("Deleting user {UserId}", userId);