1
0
mirror of https://github.com/bitwarden/server.git synced 2025-05-28 06:44:50 -05:00

refactor api models for other cipher types

This commit is contained in:
Kyle Spearrin 2017-09-21 10:52:23 -04:00
parent 12650a0ada
commit c58135bac5
8 changed files with 151 additions and 12 deletions

View File

@ -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.");
}

View File

@ -5,6 +5,7 @@
// Folder is deprecated
Folder = 0,
Login = 1,
SecureNote = 2
SecureNote = 2,
Card = 3
}
}

View File

@ -3,7 +3,7 @@
public enum FieldType : byte
{
Text = 0,
Password = 1,
Hidden = 1,
Boolean = 2
}
}

View File

@ -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<CardDataModel>(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; }
}
}

View File

@ -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)

View File

@ -24,21 +24,30 @@ namespace Bit.Core.Models.Api
public string Name { get; set; }
[EncryptedString]
[StringLength(10000)]
public string Notes { get; set; }
public IEnumerable<FieldDataModel> Fields { get; set; }
public Dictionary<string, string> 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<FieldDataModel> Fields { get; set; }
public Dictionary<string, string> 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

View File

@ -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) + ".");
}

View File

@ -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)