2015-12-08 22:57:38 -05:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
|
using Bit.Api.Utilities;
|
|
|
|
|
|
using Bit.Core.Domains;
|
|
|
|
|
|
using Bit.Core.Enums;
|
2016-05-21 17:16:22 -04:00
|
|
|
|
using Newtonsoft.Json;
|
2015-12-08 22:57:38 -05:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Models
|
|
|
|
|
|
{
|
|
|
|
|
|
public class CipherRequestModel : IValidatableObject
|
|
|
|
|
|
{
|
|
|
|
|
|
public CipherType Type { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
[Required]
|
2015-12-30 21:40:19 -05:00
|
|
|
|
[StringLength(36)]
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public string Id { get; set; }
|
2015-12-30 21:40:19 -05:00
|
|
|
|
[StringLength(36)]
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public string FolderId { get; set; }
|
|
|
|
|
|
[Required]
|
|
|
|
|
|
[EncryptedString]
|
2015-12-30 21:40:19 -05:00
|
|
|
|
[StringLength(300)]
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
[EncryptedString]
|
2015-12-30 21:40:19 -05:00
|
|
|
|
[StringLength(5000)]
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public string Uri { get; set; }
|
|
|
|
|
|
[EncryptedString]
|
2015-12-30 21:40:19 -05:00
|
|
|
|
[StringLength(200)]
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public string Username { get; set; }
|
|
|
|
|
|
[EncryptedString]
|
2015-12-30 21:40:19 -05:00
|
|
|
|
[StringLength(300)]
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public string Password { get; set; }
|
|
|
|
|
|
[EncryptedString]
|
2015-12-30 21:40:19 -05:00
|
|
|
|
[StringLength(5000)]
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public string Notes { get; set; }
|
|
|
|
|
|
|
2016-05-21 17:16:22 -04:00
|
|
|
|
public virtual Cipher ToCipher(string userId = null)
|
2015-12-08 22:57:38 -05:00
|
|
|
|
{
|
2016-05-21 17:16:22 -04:00
|
|
|
|
return new Cipher
|
2015-12-08 22:57:38 -05:00
|
|
|
|
{
|
2016-05-21 17:16:22 -04:00
|
|
|
|
Id = new Guid(Id),
|
|
|
|
|
|
UserId = new Guid(userId),
|
|
|
|
|
|
FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId),
|
|
|
|
|
|
Type = Type,
|
|
|
|
|
|
Data = JsonConvert.SerializeObject(new CipherDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore })
|
2015-12-08 22:57:38 -05:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(Type == CipherType.Site)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(Uri))
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new ValidationResult("Uri is required for a site cypher.", new[] { "Uri" });
|
|
|
|
|
|
}
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(Password))
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new ValidationResult("Password is required for a site cypher.", new[] { "Password" });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|