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;
|
2017-09-20 23:52:45 -04:00
|
|
|
|
using Core.Models.Data;
|
2018-02-28 21:23:46 -05:00
|
|
|
|
using Bit.Core.Models.Data;
|
2018-03-01 09:29:49 -05:00
|
|
|
|
using Newtonsoft.Json.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; }
|
|
|
|
|
|
|
2015-12-30 21:40:19 -05:00
|
|
|
|
[StringLength(36)]
|
2017-03-21 00:04:39 -04:00
|
|
|
|
public string OrganizationId { get; set; }
|
2017-09-20 23:52:45 -04:00
|
|
|
|
public string FolderId { get; set; }
|
|
|
|
|
|
public bool Favorite { get; set; }
|
2015-12-08 22:57:38 -05:00
|
|
|
|
[Required]
|
|
|
|
|
|
[EncryptedString]
|
2017-07-27 14:08:39 -04:00
|
|
|
|
[StringLength(1000)]
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
[EncryptedString]
|
2016-10-13 18:45:33 -04:00
|
|
|
|
[StringLength(10000)]
|
2017-09-21 10:52:23 -04:00
|
|
|
|
public string Notes { get; set; }
|
2018-02-28 21:23:46 -05:00
|
|
|
|
public IEnumerable<CipherFieldModel> Fields { get; set; }
|
2018-07-27 17:49:27 -04:00
|
|
|
|
public IEnumerable<CipherPasswordHistoryModel> PasswordHistory { get; set; }
|
2017-09-21 10:52:23 -04:00
|
|
|
|
public Dictionary<string, string> Attachments { get; set; }
|
|
|
|
|
|
|
2018-02-28 21:23:46 -05:00
|
|
|
|
public CipherLoginModel Login { get; set; }
|
|
|
|
|
|
public CipherCardModel Card { get; set; }
|
|
|
|
|
|
public CipherIdentityModel Identity { get; set; }
|
|
|
|
|
|
public CipherSecureNoteModel SecureNote { get; set; }
|
2017-09-21 10:52:23 -04:00
|
|
|
|
|
2017-12-04 16:44:17 -05:00
|
|
|
|
public CipherDetails ToCipherDetails(Guid userId)
|
2017-09-20 23:52:45 -04:00
|
|
|
|
{
|
|
|
|
|
|
var cipher = new CipherDetails
|
|
|
|
|
|
{
|
|
|
|
|
|
Type = Type,
|
|
|
|
|
|
UserId = string.IsNullOrWhiteSpace(OrganizationId) ? (Guid?)userId : null,
|
2017-12-04 16:43:18 -05:00
|
|
|
|
OrganizationId = null,
|
2017-09-20 23:52:45 -04:00
|
|
|
|
Edit = true
|
|
|
|
|
|
};
|
|
|
|
|
|
ToCipherDetails(cipher);
|
|
|
|
|
|
return cipher;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public CipherDetails ToCipherDetails(CipherDetails existingCipher)
|
|
|
|
|
|
{
|
|
|
|
|
|
existingCipher.FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId);
|
|
|
|
|
|
existingCipher.Favorite = Favorite;
|
|
|
|
|
|
ToCipher(existingCipher);
|
|
|
|
|
|
return existingCipher;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Cipher ToCipher(Cipher existingCipher)
|
2017-03-21 00:04:39 -04:00
|
|
|
|
{
|
|
|
|
|
|
switch(existingCipher.Type)
|
2015-12-08 22:57:38 -05:00
|
|
|
|
{
|
2017-01-02 21:52:13 -05:00
|
|
|
|
case CipherType.Login:
|
2018-03-01 09:29:49 -05:00
|
|
|
|
var loginObj = JObject.FromObject(new CipherLoginData(this),
|
|
|
|
|
|
new JsonSerializer { NullValueHandling = NullValueHandling.Ignore });
|
|
|
|
|
|
loginObj[nameof(CipherLoginData.Uri)]?.Parent?.Remove();
|
|
|
|
|
|
existingCipher.Data = loginObj.ToString(Formatting.None);
|
2016-06-08 22:00:31 -04:00
|
|
|
|
break;
|
2017-09-21 10:52:23 -04:00
|
|
|
|
case CipherType.Card:
|
2018-02-28 21:23:46 -05:00
|
|
|
|
existingCipher.Data = JsonConvert.SerializeObject(new CipherCardData(this),
|
2017-09-21 10:52:23 -04:00
|
|
|
|
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
|
|
|
|
|
|
break;
|
2017-10-06 15:47:31 -04:00
|
|
|
|
case CipherType.Identity:
|
2018-02-28 21:23:46 -05:00
|
|
|
|
existingCipher.Data = JsonConvert.SerializeObject(new CipherIdentityData(this),
|
2017-10-06 15:47:31 -04:00
|
|
|
|
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
|
|
|
|
|
|
break;
|
2017-09-21 10:52:23 -04:00
|
|
|
|
case CipherType.SecureNote:
|
2018-02-28 21:23:46 -05:00
|
|
|
|
existingCipher.Data = JsonConvert.SerializeObject(new CipherSecureNoteData(this),
|
2017-09-21 10:52:23 -04:00
|
|
|
|
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
|
|
|
|
|
|
break;
|
2016-06-08 22:00:31 -04:00
|
|
|
|
default:
|
2017-10-06 15:47:31 -04:00
|
|
|
|
throw new ArgumentException("Unsupported type: " + nameof(Type) + ".");
|
2015-12-08 22:57:38 -05:00
|
|
|
|
}
|
2016-06-08 22:00:31 -04:00
|
|
|
|
|
2017-09-13 16:54:23 -04:00
|
|
|
|
if((Attachments?.Count ?? 0) == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return existingCipher;
|
|
|
|
|
|
}
|
2017-07-10 14:30:12 -04:00
|
|
|
|
|
|
|
|
|
|
var attachments = existingCipher.GetAttachments();
|
2017-09-13 16:54:23 -04:00
|
|
|
|
if((attachments?.Count ?? 0) == 0)
|
2017-07-10 14:30:12 -04:00
|
|
|
|
{
|
2017-09-13 16:54:23 -04:00
|
|
|
|
return existingCipher;
|
|
|
|
|
|
}
|
2017-07-10 14:30:12 -04:00
|
|
|
|
|
2017-09-13 16:54:23 -04:00
|
|
|
|
foreach(var attachment in attachments.Where(a => Attachments.ContainsKey(a.Key)))
|
|
|
|
|
|
{
|
|
|
|
|
|
attachment.Value.FileName = Attachments[attachment.Key];
|
2017-07-10 14:30:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-09-13 16:54:23 -04:00
|
|
|
|
existingCipher.SetAttachments(attachments);
|
2017-07-10 14:30:12 -04:00
|
|
|
|
return existingCipher;
|
|
|
|
|
|
}
|
2017-09-20 23:52:45 -04:00
|
|
|
|
|
|
|
|
|
|
public Cipher ToOrganizationCipher()
|
|
|
|
|
|
{
|
|
|
|
|
|
if(string.IsNullOrWhiteSpace(OrganizationId))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentNullException(nameof(OrganizationId));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ToCipher(new Cipher
|
|
|
|
|
|
{
|
|
|
|
|
|
Type = Type,
|
|
|
|
|
|
OrganizationId = new Guid(OrganizationId)
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2017-09-21 10:52:23 -04:00
|
|
|
|
|
2017-09-28 13:11:56 -04:00
|
|
|
|
public CipherDetails ToOrganizationCipherDetails(Guid orgId)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ToCipherDetails(new CipherDetails
|
|
|
|
|
|
{
|
|
|
|
|
|
Type = Type,
|
|
|
|
|
|
OrganizationId = orgId,
|
|
|
|
|
|
Edit = true
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2017-09-20 23:52:45 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class CipherWithIdRequestModel : CipherRequestModel
|
|
|
|
|
|
{
|
|
|
|
|
|
[Required]
|
|
|
|
|
|
[StringLength(36)]
|
|
|
|
|
|
public string Id { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public Cipher ToCipher(Guid userId)
|
|
|
|
|
|
{
|
2017-09-27 21:55:39 -04:00
|
|
|
|
var cipher = ToCipherDetails(userId);
|
|
|
|
|
|
cipher.Id = new Guid(Id);
|
|
|
|
|
|
return cipher;
|
2017-09-20 23:52:45 -04:00
|
|
|
|
}
|
2017-07-10 14:30:12 -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-04-27 09:19:30 -04:00
|
|
|
|
public IEnumerable<string> CollectionIds { get; set; }
|
2017-03-21 00:04:39 -04:00
|
|
|
|
[Required]
|
2017-09-13 16:54:23 -04:00
|
|
|
|
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) });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-27 09:19:30 -04:00
|
|
|
|
if(!CollectionIds?.Any() ?? false)
|
2017-04-12 12:42:00 -04:00
|
|
|
|
{
|
2017-04-27 09:19:30 -04:00
|
|
|
|
yield return new ValidationResult("You must select at least one collection.",
|
|
|
|
|
|
new string[] { nameof(CollectionIds) });
|
2017-04-12 12:42:00 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-27 09:19:30 -04:00
|
|
|
|
public class CipherCollectionsRequestModel
|
2017-04-12 12:42:00 -04:00
|
|
|
|
{
|
|
|
|
|
|
[Required]
|
2017-04-27 09:19:30 -04:00
|
|
|
|
public IEnumerable<string> CollectionIds { get; set; }
|
2017-03-21 00:04:39 -04:00
|
|
|
|
}
|
2017-06-09 00:30:59 -04:00
|
|
|
|
|
|
|
|
|
|
public class CipherBulkDeleteRequestModel
|
|
|
|
|
|
{
|
|
|
|
|
|
[Required]
|
|
|
|
|
|
public IEnumerable<string> Ids { get; set; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class CipherBulkMoveRequestModel
|
|
|
|
|
|
{
|
|
|
|
|
|
[Required]
|
|
|
|
|
|
public IEnumerable<string> Ids { get; set; }
|
|
|
|
|
|
public string FolderId { get; set; }
|
|
|
|
|
|
}
|
2018-06-13 14:03:44 -04:00
|
|
|
|
|
|
|
|
|
|
public class CipherBulkShareRequestModel
|
|
|
|
|
|
{
|
|
|
|
|
|
[Required]
|
|
|
|
|
|
public IEnumerable<string> CollectionIds { get; set; }
|
|
|
|
|
|
[Required]
|
|
|
|
|
|
public IEnumerable<CipherWithIdRequestModel> Ciphers { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!Ciphers?.Any() ?? false)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new ValidationResult("You must select at least one cipher.",
|
|
|
|
|
|
new string[] { nameof(Ciphers) });
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var allHaveIds = true;
|
|
|
|
|
|
var organizationIds = new HashSet<string>();
|
|
|
|
|
|
foreach(var c in Ciphers)
|
|
|
|
|
|
{
|
|
|
|
|
|
organizationIds.Add(c.OrganizationId);
|
|
|
|
|
|
if(allHaveIds)
|
|
|
|
|
|
{
|
|
|
|
|
|
allHaveIds = !(string.IsNullOrWhiteSpace(c.Id) || string.IsNullOrWhiteSpace(c.OrganizationId));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(!allHaveIds)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new ValidationResult("All Ciphers must have an Id and OrganizationId.",
|
|
|
|
|
|
new string[] { nameof(Ciphers) });
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(organizationIds.Count != 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new ValidationResult("All ciphers must be for the same organization.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(!CollectionIds?.Any() ?? false)
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return new ValidationResult("You must select at least one collection.",
|
|
|
|
|
|
new string[] { nameof(CollectionIds) });
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-12-08 22:57:38 -05:00
|
|
|
|
}
|