1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-26 12:55:17 +01:00

add support for multiple uris on login model.

refactor cipher data models.
This commit is contained in:
Kyle Spearrin 2018-02-28 21:23:46 -05:00
parent 555e478ec3
commit 3563a85318
18 changed files with 409 additions and 313 deletions

View File

@ -0,0 +1,11 @@
namespace Bit.Core.Enums
{
public enum UriMatchType : byte
{
BaseDomain = 0,
FullHostname = 1,
FullUri = 2,
StartsWith = 3,
RegularExpression = 4
}
}

View File

@ -1,53 +0,0 @@
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

@ -0,0 +1,40 @@
using System.ComponentModel.DataAnnotations;
using Bit.Core.Models.Data;
using Bit.Core.Utilities;
namespace Bit.Core.Models.Api
{
public class CipherCardModel
{
public CipherCardModel() { }
public CipherCardModel(CipherCardData data)
{
CardholderName = data.CardholderName;
Brand = data.Brand;
Number = data.Number;
ExpMonth = data.ExpMonth;
ExpYear = data.ExpYear;
Code = data.Code;
}
[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; }
}
}

View File

@ -1,11 +0,0 @@
using System.Collections.Generic;
namespace Bit.Core.Models.Api
{
public abstract class CipherDataModel
{
public string Name { get; set; }
public string Notes { get; set; }
public IEnumerable<FieldDataModel> Fields { get; set; }
}
}

View File

@ -1,10 +1,18 @@
using System.ComponentModel.DataAnnotations;
using Bit.Core.Enums;
using Bit.Core.Models.Data;
namespace Bit.Core.Models.Api
{
public class FieldDataModel
public class CipherFieldModel
{
public CipherFieldModel(CipherFieldData data)
{
Type = data.Type;
Name = data.Name;
Value = data.Value;
}
public FieldType Type { get; set; }
[StringLength(1000)]
public string Name { get; set; }

View File

@ -0,0 +1,88 @@
using System.ComponentModel.DataAnnotations;
using Bit.Core.Models.Data;
using Bit.Core.Utilities;
namespace Bit.Core.Models.Api
{
public class CipherIdentityModel
{
public CipherIdentityModel() { }
public CipherIdentityModel(CipherIdentityData data)
{
Title = data.Title;
FirstName = data.FirstName;
MiddleName = data.MiddleName;
LastName = data.LastName;
Address1 = data.Address1;
Address2 = data.Address2;
Address3 = data.Address3;
City = data.City;
State = data.State;
PostalCode = data.PostalCode;
Country = data.Country;
Company = data.Company;
Email = data.Email;
Phone = data.Phone;
SSN = data.SSN;
Username = data.Username;
PassportNumber = data.PassportNumber;
LicenseNumber = data.LicenseNumber;
}
[EncryptedString]
[StringLength(1000)]
public string Title { get; set; }
[EncryptedString]
[StringLength(1000)]
public string FirstName { get; set; }
[EncryptedString]
[StringLength(1000)]
public string MiddleName { get; set; }
[EncryptedString]
[StringLength(1000)]
public string LastName { get; set; }
[EncryptedString]
[StringLength(1000)]
public string Address1 { get; set; }
[EncryptedString]
[StringLength(1000)]
public string Address2 { get; set; }
[EncryptedString]
[StringLength(1000)]
public string Address3 { get; set; }
[EncryptedString]
[StringLength(1000)]
public string City { get; set; }
[EncryptedString]
[StringLength(1000)]
public string State { get; set; }
[EncryptedString]
[StringLength(1000)]
public string PostalCode { get; set; }
[EncryptedString]
[StringLength(1000)]
public string Country { get; set; }
[EncryptedString]
[StringLength(1000)]
public string Company { get; set; }
[EncryptedString]
[StringLength(1000)]
public string Email { get; set; }
[EncryptedString]
[StringLength(1000)]
public string Phone { get; set; }
[EncryptedString]
[StringLength(1000)]
public string SSN { get; set; }
[EncryptedString]
[StringLength(1000)]
public string Username { get; set; }
[EncryptedString]
[StringLength(1000)]
public string PassportNumber { get; set; }
[EncryptedString]
[StringLength(1000)]
public string LicenseNumber { get; set; }
}
}

View File

@ -0,0 +1,68 @@
using System.ComponentModel.DataAnnotations;
using Bit.Core.Utilities;
using Bit.Core.Enums;
using System.Collections.Generic;
using System.Linq;
using Bit.Core.Models.Data;
namespace Bit.Core.Models.Api
{
public class CipherLoginModel
{
public CipherLoginModel(CipherLoginData data)
{
Uris = data.Uris.Select(u => new LoginApiUriModel(u));
Username = data.Username;
Password = data.Password;
Totp = data.Totp;
}
[EncryptedString]
[StringLength(10000)]
public string Uri
{
get => Uris?.FirstOrDefault()?.Uri;
set
{
if(Uris == null)
{
Uris = new List<LoginApiUriModel>();
}
Uris.Append(new LoginApiUriModel(value));
}
}
public IEnumerable<LoginApiUriModel> Uris { 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 LoginApiUriModel
{
public LoginApiUriModel() { }
public LoginApiUriModel(string uri)
{
Uri = uri;
MatchType = UriMatchType.BaseDomain;
}
public LoginApiUriModel(CipherLoginData.LoginDataUriModel uri)
{
Uri = uri.Uri;
MatchType = uri.MatchType;
}
[EncryptedString]
[StringLength(10000)]
public string Uri { get; set; }
public UriMatchType MatchType { get; set; }
}
}
}

View File

@ -0,0 +1,15 @@
using Bit.Core.Enums;
using Bit.Core.Models.Data;
namespace Bit.Core.Models.Api
{
public class CipherSecureNoteModel
{
public SecureNoteType Type { get; set; }
public CipherSecureNoteModel(CipherSecureNoteData data)
{
Type = data.Type;
}
}
}

View File

@ -1,47 +0,0 @@
using System;
using Bit.Core.Models.Table;
using Newtonsoft.Json;
namespace Bit.Core.Models.Api
{
public class LoginDataModel : CipherDataModel
{
public LoginDataModel() { }
public LoginDataModel(CipherRequestModel cipher)
{
Name = cipher.Name;
Notes = cipher.Notes;
Fields = cipher.Fields;
Uri = cipher.Login.Uri;
Username = cipher.Login.Username;
Password = cipher.Login.Password;
Totp = cipher.Login.Totp;
}
public LoginDataModel(Cipher cipher)
{
if(cipher.Type != Enums.CipherType.Login)
{
throw new ArgumentException("Cipher is not correct type.");
}
var data = JsonConvert.DeserializeObject<LoginDataModel>(cipher.Data);
Name = data.Name;
Notes = data.Notes;
Fields = data.Fields;
Uri = data.Uri;
Username = data.Username;
Password = data.Password;
Totp = data.Totp;
}
public string Uri { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Totp { get; set; }
}
}

View File

@ -7,6 +7,7 @@ using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using Core.Models.Data;
using Bit.Core.Models.Data;
namespace Bit.Core.Models.Api
{
@ -25,13 +26,13 @@ namespace Bit.Core.Models.Api
[EncryptedString]
[StringLength(10000)]
public string Notes { get; set; }
public IEnumerable<FieldDataModel> Fields { get; set; }
public IEnumerable<CipherFieldModel> Fields { get; set; }
public Dictionary<string, string> Attachments { get; set; }
public LoginType Login { get; set; }
public CardType Card { get; set; }
public IdentityType Identity { get; set; }
public SecureNoteType SecureNote { get; set; }
public CipherLoginModel Login { get; set; }
public CipherCardModel Card { get; set; }
public CipherIdentityModel Identity { get; set; }
public CipherSecureNoteModel SecureNote { get; set; }
public CipherDetails ToCipherDetails(Guid userId)
{
@ -59,19 +60,19 @@ namespace Bit.Core.Models.Api
switch(existingCipher.Type)
{
case CipherType.Login:
existingCipher.Data = JsonConvert.SerializeObject(new LoginDataModel(this),
existingCipher.Data = JsonConvert.SerializeObject(new CipherLoginData(this),
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
break;
case CipherType.Card:
existingCipher.Data = JsonConvert.SerializeObject(new CardDataModel(this),
existingCipher.Data = JsonConvert.SerializeObject(new CipherCardData(this),
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
break;
case CipherType.Identity:
existingCipher.Data = JsonConvert.SerializeObject(new IdentityDataModel(this),
existingCipher.Data = JsonConvert.SerializeObject(new CipherIdentityData(this),
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
break;
case CipherType.SecureNote:
existingCipher.Data = JsonConvert.SerializeObject(new SecureNoteDataModel(this),
existingCipher.Data = JsonConvert.SerializeObject(new CipherSecureNoteData(this),
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
break;
default:
@ -121,107 +122,6 @@ namespace Bit.Core.Models.Api
Edit = true
});
}
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 IdentityType
{
[EncryptedString]
[StringLength(1000)]
public string Title { get; set; }
[EncryptedString]
[StringLength(1000)]
public string FirstName { get; set; }
[EncryptedString]
[StringLength(1000)]
public string MiddleName { get; set; }
[EncryptedString]
[StringLength(1000)]
public string LastName { get; set; }
[EncryptedString]
[StringLength(1000)]
public string Address1 { get; set; }
[EncryptedString]
[StringLength(1000)]
public string Address2 { get; set; }
[EncryptedString]
[StringLength(1000)]
public string Address3 { get; set; }
[EncryptedString]
[StringLength(1000)]
public string City { get; set; }
[EncryptedString]
[StringLength(1000)]
public string State { get; set; }
[EncryptedString]
[StringLength(1000)]
public string PostalCode { get; set; }
[EncryptedString]
[StringLength(1000)]
public string Country { get; set; }
[EncryptedString]
[StringLength(1000)]
public string Company { get; set; }
[EncryptedString]
[StringLength(1000)]
public string Email { get; set; }
[EncryptedString]
[StringLength(1000)]
public string Phone { get; set; }
[EncryptedString]
[StringLength(1000)]
public string SSN { get; set; }
[EncryptedString]
[StringLength(1000)]
public string Username { get; set; }
[EncryptedString]
[StringLength(1000)]
public string PassportNumber { get; set; }
[EncryptedString]
[StringLength(1000)]
public string LicenseNumber { get; set; }
}
public class SecureNoteType
{
public Enums.SecureNoteType Type { get; set; }
}
}
public class CipherWithIdRequestModel : CipherRequestModel

View File

@ -3,6 +3,8 @@ using Core.Models.Data;
using System.Collections.Generic;
using Bit.Core.Models.Table;
using System.Linq;
using Newtonsoft.Json;
using Bit.Core.Models.Data;
namespace Bit.Core.Models.Api
{
@ -18,34 +20,58 @@ namespace Bit.Core.Models.Api
Id = cipher.Id.ToString();
Type = cipher.Type;
RevisionDate = cipher.RevisionDate;
OrganizationId = cipher.OrganizationId?.ToString();
Attachments = AttachmentResponseModel.FromCipher(cipher, globalSettings);
OrganizationUseTotp = orgUseTotp;
CipherData cipherData;
switch(cipher.Type)
{
case Enums.CipherType.Login:
Data = new LoginDataModel(cipher);
var loginData = JsonConvert.DeserializeObject<CipherLoginData>(cipher.Data);
cipherData = loginData;
Data = loginData;
Login = new CipherLoginModel(loginData);
break;
case Enums.CipherType.SecureNote:
Data = new SecureNoteDataModel(cipher);
var secureNoteData = JsonConvert.DeserializeObject<CipherSecureNoteData>(cipher.Data);
Data = secureNoteData;
cipherData = secureNoteData;
SecureNote = new CipherSecureNoteModel(secureNoteData);
break;
case Enums.CipherType.Card:
Data = new CardDataModel(cipher);
var cardData = JsonConvert.DeserializeObject<CipherCardData>(cipher.Data);
Data = cardData;
cipherData = cardData;
Card = new CipherCardModel(cardData);
break;
case Enums.CipherType.Identity:
Data = new IdentityDataModel(cipher);
var identityData = JsonConvert.DeserializeObject<CipherIdentityData>(cipher.Data);
Data = identityData;
cipherData = identityData;
Identity = new CipherIdentityModel(identityData);
break;
default:
throw new ArgumentException("Unsupported " + nameof(Type) + ".");
}
Name = cipherData.Name;
Notes = cipherData.Notes;
Fields = cipherData.Fields.Select(f => new CipherFieldModel(f));
RevisionDate = cipher.RevisionDate;
OrganizationId = cipher.OrganizationId?.ToString();
Attachments = AttachmentResponseModel.FromCipher(cipher, globalSettings);
OrganizationUseTotp = orgUseTotp;
}
public string Id { get; set; }
public string OrganizationId { get; set; }
public Enums.CipherType Type { get; set; }
public dynamic Data { get; set; }
public string Name { get; set; }
public string Notes { get; set; }
public CipherLoginModel Login { get; set; }
public CipherCardModel Card { get; set; }
public CipherIdentityModel Identity { get; set; }
public CipherSecureNoteModel SecureNote { get; set; }
public IEnumerable<CipherFieldModel> Fields { get; set; }
public IEnumerable<AttachmentResponseModel> Attachments { get; set; }
public bool OrganizationUseTotp { get; set; }
public DateTime RevisionDate { get; set; }

View File

@ -1,39 +0,0 @@
using System;
using Bit.Core.Enums;
using Bit.Core.Models.Table;
using Newtonsoft.Json;
namespace Bit.Core.Models.Api
{
public class SecureNoteDataModel : CipherDataModel
{
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)
{
throw new ArgumentException("Cipher is not correct type.");
}
var data = JsonConvert.DeserializeObject<SecureNoteDataModel>(cipher.Data);
Name = data.Name;
Notes = data.Notes;
Fields = data.Fields;
Type = data.Type;
}
public SecureNoteType Type { get; set; }
}
}

View File

@ -0,0 +1,27 @@
using Bit.Core.Models.Api;
namespace Bit.Core.Models.Data
{
public class CipherCardData : CipherData
{
public CipherCardData() { }
public CipherCardData(CipherRequestModel cipher)
: base(cipher)
{
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 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

@ -0,0 +1,22 @@
using System.Collections.Generic;
using System.Linq;
using Bit.Core.Models.Api;
namespace Bit.Core.Models.Data
{
public abstract class CipherData
{
public CipherData() { }
public CipherData(CipherRequestModel cipher)
{
Name = cipher.Name;
Notes = cipher.Notes;
Fields = cipher.Fields.Select(f => new CipherFieldData(f));
}
public string Name { get; set; }
public string Notes { get; set; }
public IEnumerable<CipherFieldData> Fields { get; set; }
}
}

View File

@ -0,0 +1,21 @@
using Bit.Core.Enums;
using Bit.Core.Models.Api;
namespace Bit.Core.Models.Data
{
public class CipherFieldData
{
public CipherFieldData() { }
public CipherFieldData(CipherFieldModel field)
{
Type = field.Type;
Name = field.Name;
Value = field.Value;
}
public FieldType Type { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
}

View File

@ -1,19 +1,14 @@
using System;
using Bit.Core.Models.Table;
using Newtonsoft.Json;
using Bit.Core.Models.Api;
namespace Bit.Core.Models.Api
namespace Bit.Core.Models.Data
{
public class IdentityDataModel : CipherDataModel
public class CipherIdentityData : CipherData
{
public IdentityDataModel() { }
public CipherIdentityData() { }
public IdentityDataModel(CipherRequestModel cipher)
public CipherIdentityData(CipherRequestModel cipher)
: base(cipher)
{
Name = cipher.Name;
Notes = cipher.Notes;
Fields = cipher.Fields;
Title = cipher.Identity.Title;
FirstName = cipher.Identity.FirstName;
MiddleName = cipher.Identity.MiddleName;
@ -34,39 +29,6 @@ namespace Bit.Core.Models.Api
LicenseNumber = cipher.Identity.LicenseNumber;
}
public IdentityDataModel(Cipher cipher)
{
if(cipher.Type != Enums.CipherType.Identity)
{
throw new ArgumentException("Cipher is not correct type.");
}
var data = JsonConvert.DeserializeObject<IdentityDataModel>(cipher.Data);
Name = data.Name;
Notes = data.Notes;
Fields = data.Fields;
Title = data.Title;
FirstName = data.FirstName;
MiddleName = data.MiddleName;
LastName = data.LastName;
Address1 = data.Address1;
Address2 = data.Address2;
Address3 = data.Address3;
City = data.City;
State = data.State;
PostalCode = data.PostalCode;
Country = data.Country;
Company = data.Company;
Email = data.Email;
Phone = data.Phone;
SSN = data.SSN;
Username = data.Username;
PassportNumber = data.PassportNumber;
LicenseNumber = data.LicenseNumber;
}
public string Title { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }

View File

@ -0,0 +1,40 @@
using System.Collections.Generic;
using System.Linq;
using Bit.Core.Enums;
using Bit.Core.Models.Api;
namespace Bit.Core.Models.Data
{
public class CipherLoginData : CipherData
{
public CipherLoginData() { }
public CipherLoginData(CipherRequestModel cipher)
: base(cipher)
{
Uris = cipher.Login.Uris?.Where(u => u != null).Select(u => new LoginDataUriModel(u));
Username = cipher.Login.Username;
Password = cipher.Login.Password;
Totp = cipher.Login.Totp;
}
public IEnumerable<LoginDataUriModel> Uris { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Totp { get; set; }
public class LoginDataUriModel
{
public LoginDataUriModel() { }
public LoginDataUriModel(CipherLoginModel.LoginApiUriModel uri)
{
Uri = uri.Uri;
MatchType = uri.MatchType;
}
public string Uri { get; set; }
public UriMatchType MatchType { get; set; }
}
}
}

View File

@ -0,0 +1,18 @@
using Bit.Core.Enums;
using Bit.Core.Models.Api;
namespace Bit.Core.Models.Data
{
public class CipherSecureNoteData : CipherData
{
public CipherSecureNoteData() { }
public CipherSecureNoteData(CipherRequestModel cipher)
: base(cipher)
{
Type = cipher.SecureNote.Type;
}
public SecureNoteType Type { get; set; }
}
}