1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 08:02:49 -05:00

initial commit of source

This commit is contained in:
Kyle Spearrin
2015-12-08 22:57:38 -05:00
commit 437b971003
87 changed files with 3819 additions and 0 deletions

View File

@ -0,0 +1,19 @@
using System.ComponentModel.DataAnnotations;
namespace Bit.Api.Models
{
public class EmailRequestModel
{
[Required]
[EmailAddress]
public string NewEmail { get; set; }
[Required]
public string MasterPasswordHash { get; set; }
[Required]
public string NewMasterPasswordHash { get; set; }
[Required]
public string Token { get; set; }
[Required]
public CipherRequestModel[] Ciphers { get; set; }
}
}

View File

@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;
namespace Bit.Api.Models
{
public class EmailTokenRequestModel
{
[Required]
[EmailAddress]
public string NewEmail { get; set; }
[Required]
public string MasterPasswordHash { get; set; }
}
}

View File

@ -0,0 +1,11 @@
using System.ComponentModel.DataAnnotations;
namespace Bit.Api.Models
{
public class PasswordHintRequestModel
{
[Required]
[EmailAddress]
public string Email { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using System.ComponentModel.DataAnnotations;
namespace Bit.Api.Models
{
public class PasswordRequestModel
{
[Required]
public string MasterPasswordHash { get; set; }
[Required]
public string NewMasterPasswordHash { get; set; }
[Required]
public CipherRequestModel[] Ciphers { get; set; }
}
}

View File

@ -0,0 +1,29 @@
using System.ComponentModel.DataAnnotations;
using Bit.Core.Domains;
namespace Bit.Api.Models
{
public class RegisterRequestModel
{
[Required]
public string Token { get; set; }
[Required]
public string Name { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
public string MasterPasswordHash { get; set; }
public string MasterPasswordHint { get; set; }
public User ToUser()
{
return new User
{
Name = Name,
Email = Email,
MasterPasswordHint = MasterPasswordHint
};
}
}
}

View File

@ -0,0 +1,11 @@
using System.ComponentModel.DataAnnotations;
namespace Bit.Api.Models
{
public class RegisterTokenRequestModel
{
[Required]
[EmailAddress]
public string Email { get; set; }
}
}

View File

@ -0,0 +1,10 @@
using System.ComponentModel.DataAnnotations;
namespace Bit.Api.Models
{
public class SecurityStampRequestModel
{
[Required]
public string MasterPasswordHash { get; set; }
}
}

View File

@ -0,0 +1,24 @@
using System.ComponentModel.DataAnnotations;
using Bit.Core.Domains;
namespace Bit.Api.Models
{
public class UpdateProfileRequestModel
{
[Required]
public string Name { get; set; }
public string MasterPasswordHint { get; set; }
[Required]
[RegularExpression("^[a-z]{2}-[A-Z]{2}$")]
public string Culture { get; set; }
public User ToUser(User existingUser)
{
existingUser.Name = Name;
existingUser.MasterPasswordHint = string.IsNullOrWhiteSpace(MasterPasswordHint) ? null : MasterPasswordHint;
existingUser.Culture = Culture;
return existingUser;
}
}
}

View File

@ -0,0 +1,22 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Bit.Api.Models
{
public class UpdateTwoFactorRequestModel : IValidatableObject
{
[Required]
public string MasterPasswordHash { get; set; }
[Required]
public bool? Enabled { get; set; }
public string Token { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if(Enabled.HasValue && Enabled.Value && string.IsNullOrWhiteSpace(Token))
{
yield return new ValidationResult("Token is required.", new[] { "Token" });
}
}
}
}

View File

@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;
namespace Bit.Api.Models
{
public class AuthTokenRequestModel
{
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
public string MasterPasswordHash { get; set; }
}
}

View File

@ -0,0 +1,12 @@
using System.ComponentModel.DataAnnotations;
namespace Bit.Api.Models
{
public class AuthTokenTwoFactorRequestModel
{
[Required]
public string Code { get; set; }
[Required]
public string Provider { get; set; }
}
}

View File

@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Bit.Api.Utilities;
using Bit.Core.Domains;
using System.Linq;
using Bit.Core.Enums;
namespace Bit.Api.Models
{
public class CipherRequestModel : IValidatableObject
{
public CipherType Type { get; set; }
[Required]
public string Id { get; set; }
public string FolderId { get; set; }
[Required]
[EncryptedString]
public string Name { get; set; }
[EncryptedString]
public string Uri { get; set; }
[EncryptedString]
public string Username { get; set; }
[EncryptedString]
public string Password { get; set; }
[EncryptedString]
public string Notes { get; set; }
public virtual Site ToSite(string userId = null)
{
return new Site
{
Id = Id,
UserId = userId,
FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : FolderId,
Name = Name,
Uri = Uri,
Username = Username,
Password = Password,
Notes = string.IsNullOrWhiteSpace(Notes) ? null : Notes
};
}
public Folder ToFolder(string userId = null)
{
return new Folder
{
Id = Id,
UserId = userId,
Name = Name
};
}
public static IEnumerable<dynamic> ToDynamicCiphers(CipherRequestModel[] models, string userId)
{
var sites = models.Where(m => m.Type == CipherType.Site).Select(m => m.ToSite(userId)).ToList();
var folders = models.Where(m => m.Type == CipherType.Folder).Select(m => m.ToFolder(userId)).ToList();
var ciphers = new List<dynamic>();
ciphers.AddRange(sites);
ciphers.AddRange(folders);
return ciphers;
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if(Type == CipherType.Site)
{
if(string.IsNullOrWhiteSpace(Uri))
{
yield return new ValidationResult("Uri is required for a site cypher.", new[] { "Uri" });
}
if(string.IsNullOrWhiteSpace(Username))
{
yield return new ValidationResult("Username is required for a site cypher.", new[] { "Username" });
}
if(string.IsNullOrWhiteSpace(Password))
{
yield return new ValidationResult("Password is required for a site cypher.", new[] { "Password" });
}
}
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.ComponentModel.DataAnnotations;
using Bit.Api.Utilities;
using Bit.Core.Domains;
namespace Bit.Api.Models
{
public class FolderRequestModel
{
[Required]
[EncryptedString]
public string Name { get; set; }
public Folder ToFolder(string userId = null)
{
return new Folder
{
UserId = userId,
Name = Name
};
}
public Folder ToFolder(Folder existingFolder)
{
existingFolder.Name = Name;
return existingFolder;
}
}
}

View File

@ -0,0 +1,52 @@
using System;
using System.ComponentModel.DataAnnotations;
using Bit.Api.Utilities;
using Bit.Core.Domains;
namespace Bit.Api.Models
{
public class SiteRequestModel
{
public string FolderId { get; set; }
[Required]
[EncryptedString]
public string Name { get; set; }
[Required]
[EncryptedString]
public string Uri { get; set; }
[Required]
[EncryptedString]
public string Username { get; set; }
[Required]
[EncryptedString]
public string Password { get; set; }
[EncryptedString]
public string Notes { get; set; }
public Site ToSite(string userId = null)
{
return new Site
{
UserId = userId,
FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : FolderId,
Name = Name,
Uri = Uri,
Username = Username,
Password = Password,
Notes = string.IsNullOrWhiteSpace(Notes) ? null : Notes
};
}
public Site ToSite(Site existingSite)
{
existingSite.FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : FolderId;
existingSite.Name = Name;
existingSite.Uri = Uri;
existingSite.Username = Username;
existingSite.Password = Password;
existingSite.Notes = string.IsNullOrWhiteSpace(Notes) ? null : Notes;
return existingSite;
}
}
}

View File

@ -0,0 +1,18 @@
using System;
using Bit.Core.Domains;
namespace Bit.Api.Models
{
public class AuthTokenResponseModel : ResponseModel
{
public AuthTokenResponseModel(string token, User user = null)
: base("authToken")
{
Token = token;
Profile = user == null ? null : new ProfileResponseModel(user);
}
public string Token { get; set; }
public ProfileResponseModel Profile { get; set; }
}
}

View File

@ -0,0 +1,51 @@
using System.Linq;
using System.Collections.Generic;
using Microsoft.AspNet.Mvc.ModelBinding;
namespace Bit.Api.Models.Response
{
public class ErrorResponseModel : ResponseModel
{
public ErrorResponseModel()
: base("error")
{ }
public ErrorResponseModel(ModelStateDictionary modelState)
: this()
{
Message = "The model state is invalid.";
ValidationErrors = new Dictionary<string, IEnumerable<string>>();
var keys = modelState.Keys.ToList();
var values = modelState.Values.ToList();
for(var i = 0; i < values.Count; i++)
{
var value = values[i];
if(keys.Count <= i)
{
// Keys not available for some reason.
break;
}
var key = keys[i];
if(value.ValidationState != ModelValidationState.Invalid || value.Errors.Count == 0)
{
continue;
}
var errors = value.Errors.Select(e => e.ErrorMessage);
ValidationErrors.Add(key, errors);
}
}
public string Message { get; set; }
public Dictionary<string, IEnumerable<string>> ValidationErrors { get; set; }
// For use in development environments.
public string ExceptionMessage { get; set; }
public string ExceptionStackTrace { get; set; }
public string InnerExceptionMessage { get; set; }
}
}

View File

@ -0,0 +1,23 @@
using System;
using Bit.Core.Domains;
namespace Bit.Api.Models
{
public class FolderResponseModel : ResponseModel
{
public FolderResponseModel(Folder folder)
: base("folder")
{
if(folder == null)
{
throw new ArgumentNullException(nameof(folder));
}
Id = folder.Id;
Name = folder.Name;
}
public string Id { get; set; }
public string Name { get; set; }
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
namespace Bit.Api.Models
{
public class ListResponseModel<T> : ResponseModel where T : ResponseModel
{
public ListResponseModel(IEnumerable<T> data)
: base("list")
{
Data = data;
}
public IEnumerable<T> Data { get; set; }
}
}

View File

@ -0,0 +1,31 @@
using System;
using Bit.Core.Domains;
namespace Bit.Api.Models
{
public class ProfileResponseModel : ResponseModel
{
public ProfileResponseModel(User user)
: base("profile")
{
if(user == null)
{
throw new ArgumentNullException(nameof(user));
}
Id = user.Id;
Name = user.Name;
Email = user.Email;
MasterPasswordHint = string.IsNullOrWhiteSpace(user.MasterPasswordHint) ? null : user.MasterPasswordHint;
Culture = user.Culture;
TwoFactorEnabled = user.TwoFactorEnabled;
}
public string Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string MasterPasswordHint { get; set; }
public string Culture { get; set; }
public bool TwoFactorEnabled { get; set; }
}
}

View File

@ -0,0 +1,19 @@
using System;
namespace Bit.Api.Models
{
public abstract class ResponseModel
{
public ResponseModel(string obj)
{
if(string.IsNullOrWhiteSpace(obj))
{
throw new ArgumentNullException(nameof(obj));
}
Object = obj;
}
public string Object { get; private set; }
}
}

View File

@ -0,0 +1,36 @@
using System;
using Bit.Core.Domains;
namespace Bit.Api.Models
{
public class SiteResponseModel : ResponseModel
{
public SiteResponseModel(Site site)
: base("site")
{
if(site == null)
{
throw new ArgumentNullException(nameof(site));
}
Id = site.Id;
FolderId = string.IsNullOrWhiteSpace(site.FolderId) ? null : site.FolderId;
Name = site.Name;
Uri = site.Uri;
Username = site.Username;
Password = site.Password;
Notes = site.Notes;
}
public string Id { get; set; }
public string FolderId { get; set; }
public string Name { get; set; }
public string Uri { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Notes { get; set; }
// Expandables
public FolderResponseModel Folder { get; set; }
}
}

View File

@ -0,0 +1,26 @@
using System;
using Bit.Core.Domains;
using Bit.Core.Enums;
namespace Bit.Api.Models
{
public class TwoFactorResponseModel : ResponseModel
{
public TwoFactorResponseModel(User user)
: base("twoFactor")
{
if(user == null)
{
throw new ArgumentNullException(nameof(user));
}
TwoFactorEnabled = user.TwoFactorEnabled;
AuthenticatorKey = user.AuthenticatorKey;
TwoFactorProvider = user.TwoFactorProvider;
}
public bool TwoFactorEnabled { get; set; }
public TwoFactorProvider? TwoFactorProvider { get; set; }
public string AuthenticatorKey { get; set; }
}
}