You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.1 KiB
70 lines
2.1 KiB
using System.ComponentModel.DataAnnotations; |
|
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 string? Notes { 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; |
|
} |
|
|
|
await cipherRepository.CreateAsync(cipher); |
|
|
|
return new SceneResult<Result>( |
|
result: new Result |
|
{ |
|
CipherId = cipher.Id |
|
}, |
|
mangleMap: manglerService.GetMangleMap()); |
|
} |
|
}
|
|
|