From c58135bac550a6441826cb68fea09ee52ba9149a Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Thu, 21 Sep 2017 10:52:23 -0400 Subject: [PATCH] refactor api models for other cipher types --- src/Api/Controllers/CiphersController.cs | 2 +- src/Core/Enums/CipherType.cs | 3 +- src/Core/Enums/FieldType.cs | 2 +- src/Core/Models/Api/CardDataModel.cs | 53 ++++++++++++++ src/Core/Models/Api/LoginDataModel.cs | 18 +++-- .../Models/Api/Request/CipherRequestModel.cs | 70 +++++++++++++++++-- .../Api/Response/CipherResponseModel.cs | 6 ++ ...ureNoteModel.cs => SecureNoteDataModel.cs} | 9 +++ 8 files changed, 151 insertions(+), 12 deletions(-) create mode 100644 src/Core/Models/Api/CardDataModel.cs rename src/Core/Models/Api/{SecureNoteModel.cs => SecureNoteDataModel.cs} (76%) diff --git a/src/Api/Controllers/CiphersController.cs b/src/Api/Controllers/CiphersController.cs index ad71540e94..0c91e8c898 100644 --- a/src/Api/Controllers/CiphersController.cs +++ b/src/Api/Controllers/CiphersController.cs @@ -144,7 +144,7 @@ namespace Bit.Api.Controllers var modelOrgId = string.IsNullOrWhiteSpace(model.OrganizationId) ? (Guid?)null : new Guid(model.OrganizationId); if(cipher.OrganizationId != modelOrgId) { - throw new BadRequestException("Organization mismatch. Re-sync if you recently shared this login, " + + throw new BadRequestException("Organization mismatch. Re-sync if you recently shared this item, " + "then try again."); } diff --git a/src/Core/Enums/CipherType.cs b/src/Core/Enums/CipherType.cs index 2fc8c22127..c44c011398 100644 --- a/src/Core/Enums/CipherType.cs +++ b/src/Core/Enums/CipherType.cs @@ -5,6 +5,7 @@ // Folder is deprecated Folder = 0, Login = 1, - SecureNote = 2 + SecureNote = 2, + Card = 3 } } diff --git a/src/Core/Enums/FieldType.cs b/src/Core/Enums/FieldType.cs index 074a58d492..bf30d4f124 100644 --- a/src/Core/Enums/FieldType.cs +++ b/src/Core/Enums/FieldType.cs @@ -3,7 +3,7 @@ public enum FieldType : byte { Text = 0, - Password = 1, + Hidden = 1, Boolean = 2 } } diff --git a/src/Core/Models/Api/CardDataModel.cs b/src/Core/Models/Api/CardDataModel.cs new file mode 100644 index 0000000000..b45e056036 --- /dev/null +++ b/src/Core/Models/Api/CardDataModel.cs @@ -0,0 +1,53 @@ +using System; +using Bit.Core.Models.Table; +using Newtonsoft.Json; + +namespace Bit.Core.Models.Api +{ + public class CardDataModel : CipherDataModel + { + public CardDataModel() { } + + public CardDataModel(CipherRequestModel cipher) + { + Name = cipher.Name; + Notes = cipher.Notes; + Fields = cipher.Fields; + + CardholderName = cipher.Card.CardholderName; + Brand = cipher.Card.Brand; + Number = cipher.Card.Number; + ExpMonth = cipher.Card.ExpMonth; + ExpYear = cipher.Card.ExpYear; + Code = cipher.Card.Code; + } + + public CardDataModel(Cipher cipher) + { + if(cipher.Type != Enums.CipherType.Card) + { + throw new ArgumentException("Cipher is not correct type."); + } + + var data = JsonConvert.DeserializeObject(cipher.Data); + + Name = data.Name; + Notes = data.Notes; + Fields = data.Fields; + + CardholderName = data.CardholderName; + Brand = data.Brand; + Number = data.Number; + ExpMonth = data.ExpMonth; + ExpYear = data.ExpYear; + Code = data.Code; + } + + public string CardholderName { get; set; } + public string Brand { get; set; } + public string Number { get; set; } + public string ExpMonth { get; set; } + public string ExpYear { get; set; } + public string Code { get; set; } + } +} diff --git a/src/Core/Models/Api/LoginDataModel.cs b/src/Core/Models/Api/LoginDataModel.cs index e08b67a722..c5f7af3b4a 100644 --- a/src/Core/Models/Api/LoginDataModel.cs +++ b/src/Core/Models/Api/LoginDataModel.cs @@ -26,10 +26,20 @@ namespace Bit.Core.Models.Api Notes = cipher.Notes; Fields = cipher.Fields; - Uri = cipher.Uri; - Username = cipher.Username; - Password = cipher.Password; - Totp = cipher.Totp; + if(cipher.Login == null) + { + Uri = cipher.Uri; + Username = cipher.Username; + Password = cipher.Password; + Totp = cipher.Totp; + } + else + { + Uri = cipher.Login.Uri; + Username = cipher.Login.Username; + Password = cipher.Login.Password; + Totp = cipher.Login.Totp; + } } public LoginDataModel(Cipher cipher) diff --git a/src/Core/Models/Api/Request/CipherRequestModel.cs b/src/Core/Models/Api/Request/CipherRequestModel.cs index c9c8ff8b28..b6ced71488 100644 --- a/src/Core/Models/Api/Request/CipherRequestModel.cs +++ b/src/Core/Models/Api/Request/CipherRequestModel.cs @@ -24,21 +24,30 @@ namespace Bit.Core.Models.Api public string Name { get; set; } [EncryptedString] [StringLength(10000)] + public string Notes { get; set; } + public IEnumerable Fields { get; set; } + public Dictionary Attachments { get; set; } + + public LoginType Login { get; set; } + public CardType Card { get; set; } + public SecureNoteType SecureNote { get; set; } + + [Obsolete("Use Login property")] + [EncryptedString] + [StringLength(10000)] public string Uri { get; set; } + [Obsolete("Use Login property")] [EncryptedString] [StringLength(1000)] public string Username { get; set; } + [Obsolete("Use Login property")] [EncryptedString] [StringLength(1000)] public string Password { get; set; } - [EncryptedString] - [StringLength(10000)] - public string Notes { get; set; } + [Obsolete("Use Login property")] [EncryptedString] [StringLength(1000)] public string Totp { get; set; } - public IEnumerable Fields { get; set; } - public Dictionary Attachments { get; set; } public CipherDetails ToCipherDetails(Guid userId) { @@ -69,6 +78,14 @@ namespace Bit.Core.Models.Api existingCipher.Data = JsonConvert.SerializeObject(new LoginDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); break; + case CipherType.Card: + existingCipher.Data = JsonConvert.SerializeObject(new CardDataModel(this), + new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); + break; + case CipherType.SecureNote: + existingCipher.Data = JsonConvert.SerializeObject(new SecureNoteDataModel(this), + new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); + break; default: throw new ArgumentException("Unsupported " + nameof(Type) + "."); } @@ -116,6 +133,49 @@ namespace Bit.Core.Models.Api OrganizationId = new Guid(OrganizationId) }); } + + public class LoginType + { + [EncryptedString] + [StringLength(10000)] + public string Uri { get; set; } + [EncryptedString] + [StringLength(1000)] + public string Username { get; set; } + [EncryptedString] + [StringLength(1000)] + public string Password { get; set; } + [EncryptedString] + [StringLength(1000)] + public string Totp { get; set; } + } + + public class CardType + { + [EncryptedString] + [StringLength(1000)] + public string CardholderName { get; set; } + [EncryptedString] + [StringLength(1000)] + public string Brand { get; set; } + [EncryptedString] + [StringLength(1000)] + public string Number { get; set; } + [EncryptedString] + [StringLength(1000)] + public string ExpMonth { get; set; } + [EncryptedString] + [StringLength(1000)] + public string ExpYear { get; set; } + [EncryptedString] + [StringLength(1000)] + public string Code { get; set; } + } + + public class SecureNoteType + { + public Enums.SecureNoteType Type { get; set; } + } } public class CipherWithIdRequestModel : CipherRequestModel diff --git a/src/Core/Models/Api/Response/CipherResponseModel.cs b/src/Core/Models/Api/Response/CipherResponseModel.cs index 96452d9edf..1c64b3e5ed 100644 --- a/src/Core/Models/Api/Response/CipherResponseModel.cs +++ b/src/Core/Models/Api/Response/CipherResponseModel.cs @@ -28,6 +28,12 @@ namespace Bit.Core.Models.Api case Enums.CipherType.Login: Data = new LoginDataModel(cipher); break; + case Enums.CipherType.SecureNote: + Data = new SecureNoteDataModel(cipher); + break; + case Enums.CipherType.Card: + Data = new CardDataModel(cipher); + break; default: throw new ArgumentException("Unsupported " + nameof(Type) + "."); } diff --git a/src/Core/Models/Api/SecureNoteModel.cs b/src/Core/Models/Api/SecureNoteDataModel.cs similarity index 76% rename from src/Core/Models/Api/SecureNoteModel.cs rename to src/Core/Models/Api/SecureNoteDataModel.cs index 63ad6b720a..e3f66f31b9 100644 --- a/src/Core/Models/Api/SecureNoteModel.cs +++ b/src/Core/Models/Api/SecureNoteDataModel.cs @@ -9,6 +9,15 @@ namespace Bit.Core.Models.Api { public SecureNoteDataModel() { } + public SecureNoteDataModel(CipherRequestModel cipher) + { + Name = cipher.Name; + Notes = cipher.Notes; + Fields = cipher.Fields; + + Type = cipher.SecureNote.Type; + } + public SecureNoteDataModel(Cipher cipher) { if(cipher.Type != CipherType.SecureNote)