1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-22 05:25:10 -05:00
bitwarden/src/Api/Models/Request/CipherRequestModel.cs

62 lines
1.9 KiB
C#

using System;
using System.ComponentModel.DataAnnotations;
using Bit.Api.Utilities;
using Bit.Core.Domains;
using Bit.Core.Enums;
using Newtonsoft.Json;
namespace Bit.Api.Models
{
public class CipherRequestModel
{
public CipherType Type { get; set; }
[Required]
[StringLength(36)]
public string Id { get; set; }
[StringLength(36)]
public string FolderId { get; set; }
[Required]
[EncryptedString]
[StringLength(300)]
public string Name { get; set; }
[EncryptedString]
[StringLength(10000)]
public string Uri { get; set; }
[EncryptedString]
[StringLength(300)]
public string Username { get; set; }
[EncryptedString]
[StringLength(300)]
public string Password { get; set; }
[EncryptedString]
[StringLength(10000)]
public string Notes { get; set; }
public virtual Cipher ToCipher(Guid userId)
{
var cipher = new Cipher
{
Id = new Guid(Id),
UserId = userId,
FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId),
Type = Type
};
switch(Type)
{
case CipherType.Folder:
cipher.Data = JsonConvert.SerializeObject(new FolderDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
break;
case CipherType.Login:
cipher.Data = JsonConvert.SerializeObject(new LoginDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
break;
default:
throw new ArgumentException("Unsupported " + nameof(Type) + ".");
}
return cipher;
}
}
}