mirror of
https://github.com/bitwarden/server.git
synced 2026-06-01 01:55:55 -05:00
88 lines
2.7 KiB
C#
88 lines
2.7 KiB
C#
using System.ComponentModel.DataAnnotations;
|
|
using System.Text.Json;
|
|
using Bit.Core.Repositories;
|
|
using Bit.Core.Vault.Enums;
|
|
using Bit.Core.Vault.Repositories;
|
|
using Bit.Seeder.Factories;
|
|
using Bit.Seeder.Models;
|
|
using Bit.Seeder.Services;
|
|
|
|
namespace Bit.Seeder.Scenes;
|
|
|
|
public class UserSshKeyCipherScene(IUserRepository userRepository, ICipherRepository cipherRepository, IManglerService manglerService) : IScene<UserSshKeyCipherScene.Request, UserSshKeyCipherScene.Result>
|
|
{
|
|
public class Request
|
|
{
|
|
[Required]
|
|
public required Guid UserId { get; set; }
|
|
[Required]
|
|
public required string UserKeyB64 { get; set; }
|
|
[Required]
|
|
public required string Name { get; set; }
|
|
public string? PrivateKey { get; set; }
|
|
public string? PublicKey { get; set; }
|
|
public string? Fingerprint { get; set; }
|
|
public bool Reprompt { get; set; }
|
|
public bool Favorite { get; set; }
|
|
public string? Notes { get; set; }
|
|
public Guid? FolderId { get; set; }
|
|
}
|
|
|
|
public class Result
|
|
{
|
|
public required Guid CipherId { get; init; }
|
|
}
|
|
|
|
public async Task<SceneResult<Result>> SeedAsync(Request request)
|
|
{
|
|
var user = await userRepository.GetByIdAsync(request.UserId);
|
|
if (user == null)
|
|
{
|
|
throw new Exception($"User with ID {request.UserId} not found.");
|
|
}
|
|
|
|
var sshKey = new SshKeyViewDto
|
|
{
|
|
PrivateKey = request.PrivateKey,
|
|
PublicKey = request.PublicKey,
|
|
Fingerprint = request.Fingerprint
|
|
};
|
|
var cipher = SshKeyCipherSeeder.Create(new CipherSeed
|
|
{
|
|
Type = CipherType.SSHKey,
|
|
Name = request.Name,
|
|
Notes = request.Notes,
|
|
EncryptionKey = request.UserKeyB64,
|
|
UserId = request.UserId,
|
|
SshKey = sshKey
|
|
});
|
|
if (request.Reprompt)
|
|
{
|
|
cipher.Reprompt = CipherRepromptType.Password;
|
|
}
|
|
if (request.Favorite)
|
|
{
|
|
cipher.Favorites = JsonSerializer.Serialize(new Dictionary<string, bool>
|
|
{
|
|
{ request.UserId.ToString().ToUpperInvariant(), true}
|
|
});
|
|
}
|
|
if (request.FolderId.HasValue)
|
|
{
|
|
cipher.Folders = CipherComposer.BuildFoldersJson(new Dictionary<Guid, Guid>
|
|
{
|
|
{ request.UserId, request.FolderId.Value }
|
|
});
|
|
}
|
|
|
|
await cipherRepository.CreateAsync(cipher);
|
|
|
|
return new SceneResult<Result>(
|
|
result: new Result
|
|
{
|
|
CipherId = cipher.Id
|
|
},
|
|
mangleMap: manglerService.GetMangleMap());
|
|
}
|
|
}
|