api: add user's monograph passwords key (#41)

This commit is contained in:
01zulfi
2025-08-18 11:45:42 +05:00
committed by GitHub
parent 8df70c81fc
commit 6d6342dbff
6 changed files with 52 additions and 7 deletions

View File

@@ -74,8 +74,12 @@ namespace Notesnook.API.Controllers
var userId = User.FindFirstValue("sub");
try
{
if (user.AttachmentsKey != null)
await UserService.SetUserAttachmentsKeyAsync(userId, user.AttachmentsKey);
var keys = new UserKeys
{
AttachmentsKey = user.AttachmentsKey,
MonographPasswordsKey = user.MonographPasswordsKey
};
await UserService.SetUserKeysAsync(userId, keys);
return Ok();
}
catch (Exception ex)

View File

@@ -17,10 +17,9 @@ You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System.Threading;
using System.Threading.Tasks;
using Notesnook.API.Models;
using Notesnook.API.Models.Responses;
using Streetwriters.Common.Interfaces;
namespace Notesnook.API.Interfaces
{
@@ -31,6 +30,6 @@ namespace Notesnook.API.Interfaces
Task DeleteUserAsync(string userId, string jti, string password);
Task<bool> ResetUserAsync(string userId, bool removeAttachments);
Task<UserResponse> GetUserAsync(string userId);
Task SetUserAttachmentsKeyAsync(string userId, IEncrypted key);
Task SetUserKeysAsync(string userId, UserKeys keys);
}
}

View File

@@ -12,6 +12,9 @@ namespace Notesnook.API.Models.Responses
[JsonPropertyName("attachmentsKey")]
public EncryptedData AttachmentsKey { get; set; }
[JsonPropertyName("monographPasswordsKey")]
public EncryptedData MonographPasswordsKey { get; set; }
[JsonPropertyName("subscription")]
public ISubscription Subscription { get; set; }

View File

@@ -0,0 +1,27 @@
/*
This file is part of the Notesnook Sync Server project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the Affero GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Affero GNU General Public License for more details.
You should have received a copy of the Affero GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Notesnook.API.Models
{
public class UserKeys
{
public EncryptedData AttachmentsKey { get; set; }
public EncryptedData MonographPasswordsKey { get; set; }
}
}

View File

@@ -34,6 +34,7 @@ namespace Notesnook.API.Models
public string Salt { get; set; }
public EncryptedData VaultKey { get; set; }
public EncryptedData AttachmentsKey { get; set; }
public EncryptedData MonographPasswordsKey { get; set; }
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]

View File

@@ -128,6 +128,7 @@ namespace Notesnook.API.Services
MFA = user.MFA,
PhoneNumber = user.PhoneNumber,
AttachmentsKey = userSettings.AttachmentsKey,
MonographPasswordsKey = userSettings.MonographPasswordsKey,
Salt = userSettings.Salt,
Subscription = subscription,
Success = true,
@@ -135,10 +136,19 @@ namespace Notesnook.API.Services
};
}
public async Task SetUserAttachmentsKeyAsync(string userId, IEncrypted key)
public async Task SetUserKeysAsync(string userId, UserKeys keys)
{
var userSettings = await Repositories.UsersSettings.FindOneAsync((u) => u.UserId == userId) ?? throw new Exception("User not found.");
userSettings.AttachmentsKey = (EncryptedData)key;
if (keys.AttachmentsKey != null)
{
userSettings.AttachmentsKey = keys.AttachmentsKey;
}
if (keys.MonographPasswordsKey != null)
{
userSettings.MonographPasswordsKey = keys.MonographPasswordsKey;
}
await Repositories.UsersSettings.UpdateAsync(userSettings.Id, userSettings);
}
@@ -226,6 +236,7 @@ namespace Notesnook.API.Services
var userSettings = await Repositories.UsersSettings.FindOneAsync((s) => s.UserId == userId);
userSettings.AttachmentsKey = null;
userSettings.MonographPasswordsKey = null;
userSettings.VaultKey = null;
userSettings.LastSynced = 0;