mirror of
https://github.com/bitwarden/server.git
synced 2025-05-29 15:24:51 -05:00
refactor api models for other cipher types
This commit is contained in:
parent
12650a0ada
commit
c58135bac5
@ -144,7 +144,7 @@ namespace Bit.Api.Controllers
|
|||||||
var modelOrgId = string.IsNullOrWhiteSpace(model.OrganizationId) ? (Guid?)null : new Guid(model.OrganizationId);
|
var modelOrgId = string.IsNullOrWhiteSpace(model.OrganizationId) ? (Guid?)null : new Guid(model.OrganizationId);
|
||||||
if(cipher.OrganizationId != modelOrgId)
|
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.");
|
"then try again.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
// Folder is deprecated
|
// Folder is deprecated
|
||||||
Folder = 0,
|
Folder = 0,
|
||||||
Login = 1,
|
Login = 1,
|
||||||
SecureNote = 2
|
SecureNote = 2,
|
||||||
|
Card = 3
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
public enum FieldType : byte
|
public enum FieldType : byte
|
||||||
{
|
{
|
||||||
Text = 0,
|
Text = 0,
|
||||||
Password = 1,
|
Hidden = 1,
|
||||||
Boolean = 2
|
Boolean = 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
53
src/Core/Models/Api/CardDataModel.cs
Normal file
53
src/Core/Models/Api/CardDataModel.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
@ -26,10 +26,20 @@ namespace Bit.Core.Models.Api
|
|||||||
Notes = cipher.Notes;
|
Notes = cipher.Notes;
|
||||||
Fields = cipher.Fields;
|
Fields = cipher.Fields;
|
||||||
|
|
||||||
Uri = cipher.Uri;
|
if(cipher.Login == null)
|
||||||
Username = cipher.Username;
|
{
|
||||||
Password = cipher.Password;
|
Uri = cipher.Uri;
|
||||||
Totp = cipher.Totp;
|
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)
|
public LoginDataModel(Cipher cipher)
|
||||||
|
@ -24,21 +24,30 @@ namespace Bit.Core.Models.Api
|
|||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
[EncryptedString]
|
[EncryptedString]
|
||||||
[StringLength(10000)]
|
[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; }
|
public string Uri { get; set; }
|
||||||
|
[Obsolete("Use Login property")]
|
||||||
[EncryptedString]
|
[EncryptedString]
|
||||||
[StringLength(1000)]
|
[StringLength(1000)]
|
||||||
public string Username { get; set; }
|
public string Username { get; set; }
|
||||||
|
[Obsolete("Use Login property")]
|
||||||
[EncryptedString]
|
[EncryptedString]
|
||||||
[StringLength(1000)]
|
[StringLength(1000)]
|
||||||
public string Password { get; set; }
|
public string Password { get; set; }
|
||||||
[EncryptedString]
|
[Obsolete("Use Login property")]
|
||||||
[StringLength(10000)]
|
|
||||||
public string Notes { get; set; }
|
|
||||||
[EncryptedString]
|
[EncryptedString]
|
||||||
[StringLength(1000)]
|
[StringLength(1000)]
|
||||||
public string Totp { get; set; }
|
public string Totp { get; set; }
|
||||||
public IEnumerable<FieldDataModel> Fields { get; set; }
|
|
||||||
public Dictionary<string, string> Attachments { get; set; }
|
|
||||||
|
|
||||||
public CipherDetails ToCipherDetails(Guid userId)
|
public CipherDetails ToCipherDetails(Guid userId)
|
||||||
{
|
{
|
||||||
@ -69,6 +78,14 @@ namespace Bit.Core.Models.Api
|
|||||||
existingCipher.Data = JsonConvert.SerializeObject(new LoginDataModel(this),
|
existingCipher.Data = JsonConvert.SerializeObject(new LoginDataModel(this),
|
||||||
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
|
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
|
||||||
break;
|
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:
|
default:
|
||||||
throw new ArgumentException("Unsupported " + nameof(Type) + ".");
|
throw new ArgumentException("Unsupported " + nameof(Type) + ".");
|
||||||
}
|
}
|
||||||
@ -116,6 +133,49 @@ namespace Bit.Core.Models.Api
|
|||||||
OrganizationId = new Guid(OrganizationId)
|
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
|
public class CipherWithIdRequestModel : CipherRequestModel
|
||||||
|
@ -28,6 +28,12 @@ namespace Bit.Core.Models.Api
|
|||||||
case Enums.CipherType.Login:
|
case Enums.CipherType.Login:
|
||||||
Data = new LoginDataModel(cipher);
|
Data = new LoginDataModel(cipher);
|
||||||
break;
|
break;
|
||||||
|
case Enums.CipherType.SecureNote:
|
||||||
|
Data = new SecureNoteDataModel(cipher);
|
||||||
|
break;
|
||||||
|
case Enums.CipherType.Card:
|
||||||
|
Data = new CardDataModel(cipher);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
throw new ArgumentException("Unsupported " + nameof(Type) + ".");
|
throw new ArgumentException("Unsupported " + nameof(Type) + ".");
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,15 @@ namespace Bit.Core.Models.Api
|
|||||||
{
|
{
|
||||||
public SecureNoteDataModel() { }
|
public SecureNoteDataModel() { }
|
||||||
|
|
||||||
|
public SecureNoteDataModel(CipherRequestModel cipher)
|
||||||
|
{
|
||||||
|
Name = cipher.Name;
|
||||||
|
Notes = cipher.Notes;
|
||||||
|
Fields = cipher.Fields;
|
||||||
|
|
||||||
|
Type = cipher.SecureNote.Type;
|
||||||
|
}
|
||||||
|
|
||||||
public SecureNoteDataModel(Cipher cipher)
|
public SecureNoteDataModel(Cipher cipher)
|
||||||
{
|
{
|
||||||
if(cipher.Type != CipherType.SecureNote)
|
if(cipher.Type != CipherType.SecureNote)
|
Loading…
x
Reference in New Issue
Block a user