2015-12-08 22:57:38 -05:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
2017-03-08 21:55:08 -05:00
|
|
|
|
using Bit.Core.Utilities;
|
2017-03-08 21:45:08 -05:00
|
|
|
|
using Bit.Core.Models.Table;
|
2015-12-08 22:57:38 -05:00
|
|
|
|
using Bit.Core.Enums;
|
2016-05-21 17:16:22 -04:00
|
|
|
|
using Newtonsoft.Json;
|
2017-03-21 00:04:39 -04:00
|
|
|
|
using System.Collections.Generic;
|
2017-04-04 22:07:30 -04:00
|
|
|
|
using System.Linq;
|
2015-12-08 22:57:38 -05:00
|
|
|
|
|
2017-03-08 21:55:08 -05:00
|
|
|
|
namespace Bit.Core.Models.Api
|
2015-12-08 22:57:38 -05:00
|
|
|
|
{
|
2016-06-08 22:00:31 -04:00
|
|
|
|
public class CipherRequestModel
|
2015-12-08 22:57:38 -05:00
|
|
|
|
{
|
|
|
|
|
|
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)]
|
2017-03-21 00:04:39 -04:00
|
|
|
|
public string OrganizationId { get; set; }
|
2015-12-08 22:57:38 -05:00
|
|
|
|
[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]
|
2016-10-13 18:45:33 -04:00
|
|
|
|
[StringLength(10000)]
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public string Uri { get; set; }
|
|
|
|
|
|
[EncryptedString]
|
2016-10-13 18:45:33 -04:00
|
|
|
|
[StringLength(300)]
|
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]
|
2016-10-13 18:45:33 -04:00
|
|
|
|
[StringLength(10000)]
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public string Notes { get; set; }
|
|
|
|
|
|
|
2017-01-24 22:46:54 -05:00
|
|
|
|
public virtual Cipher ToCipher(Guid userId)
|
2015-12-08 22:57:38 -05:00
|
|
|
|
{
|
2017-03-21 00:04:39 -04:00
|
|
|
|
return ToCipher(new Cipher
|
2015-12-08 22:57:38 -05:00
|
|
|
|
{
|
2016-05-21 17:16:22 -04:00
|
|
|
|
Id = new Guid(Id),
|
2017-03-21 00:04:39 -04:00
|
|
|
|
UserId = string.IsNullOrWhiteSpace(OrganizationId) ? (Guid?)userId : null,
|
2016-06-08 22:00:31 -04:00
|
|
|
|
Type = Type
|
2017-03-21 00:04:39 -04:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Cipher ToCipher(Cipher existingCipher)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch(existingCipher.Type)
|
2015-12-08 22:57:38 -05:00
|
|
|
|
{
|
2017-01-02 21:52:13 -05:00
|
|
|
|
case CipherType.Login:
|
2017-04-04 22:07:30 -04:00
|
|
|
|
existingCipher.Data = JsonConvert.SerializeObject(new LoginDataModel(this),
|
|
|
|
|
|
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
|
2016-06-08 22:00:31 -04:00
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new ArgumentException("Unsupported " + nameof(Type) + ".");
|
2015-12-08 22:57:38 -05:00
|
|
|
|
}
|
2016-06-08 22:00:31 -04:00
|
|
|
|
|
2017-03-21 00:04:39 -04:00
|
|
|
|
return existingCipher;
|
2015-12-08 22:57:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-03-21 00:04:39 -04:00
|
|
|
|
|
2017-04-12 12:42:00 -04:00
|
|
|
|
public class CipherShareRequestModel : IValidatableObject
|
2017-03-21 00:04:39 -04:00
|
|
|
|
{
|
2017-04-04 22:07:30 -04:00
|
|
|
|
[Required]
|
2017-03-21 00:04:39 -04:00
|
|
|
|
public IEnumerable<string> SubvaultIds { get; set; }
|
|
|
|
|
|
[Required]
|
|
|
|
|
|
public CipherRequestModel Cipher { get; set; }
|
2017-04-04 22:07:30 -04:00
|
|
|
|
|
2017-04-12 12:42:00 -04:00
|
|
|
|
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(Cipher.OrganizationId))
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new ValidationResult("Cipher OrganizationId is required.",
|
|
|
|
|
|
new string[] { nameof(Cipher.OrganizationId) });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(!SubvaultIds?.Any() ?? false)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new ValidationResult("You must select at least one subvault.",
|
|
|
|
|
|
new string[] { nameof(SubvaultIds) });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class CipherSubvaultsRequestModel : IValidatableObject
|
|
|
|
|
|
{
|
|
|
|
|
|
[Required]
|
|
|
|
|
|
public IEnumerable<string> SubvaultIds { get; set; }
|
|
|
|
|
|
|
2017-04-04 22:07:30 -04:00
|
|
|
|
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!SubvaultIds?.Any() ?? false)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new ValidationResult("You must select at least one subvault.",
|
|
|
|
|
|
new string[] { nameof(SubvaultIds) });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-03-21 00:04:39 -04:00
|
|
|
|
}
|
2015-12-08 22:57:38 -05:00
|
|
|
|
}
|