From 91e1c5878c271673daebfbb72345cd36d82dc99c Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 6 Oct 2017 15:29:36 -0400 Subject: [PATCH] remove deprecated code --- src/Api/Controllers/AuthController.cs | 20 --- src/Api/Controllers/LoginsController.cs | 163 ------------------ src/Core/Models/Api/LoginDataModel.cs | 30 +--- .../Models/Api/Request/CipherRequestModel.cs | 17 -- .../Models/Api/Request/LoginRequestModel.cs | 92 ---------- .../Models/Api/Response/LoginResponseModel.cs | 62 ------- 6 files changed, 4 insertions(+), 380 deletions(-) delete mode 100644 src/Api/Controllers/AuthController.cs delete mode 100644 src/Api/Controllers/LoginsController.cs delete mode 100644 src/Core/Models/Api/Request/LoginRequestModel.cs delete mode 100644 src/Core/Models/Api/Response/LoginResponseModel.cs diff --git a/src/Api/Controllers/AuthController.cs b/src/Api/Controllers/AuthController.cs deleted file mode 100644 index b5e33da6d7..0000000000 --- a/src/Api/Controllers/AuthController.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Authorization; -using Bit.Core.Exceptions; - -namespace Bit.Api.Controllers -{ - [Obsolete] - [Route("auth")] - public class AuthController : Controller - { - [HttpPost("token")] - [AllowAnonymous] - public IActionResult PostToken() - { - throw new BadRequestException("You are using an outdated version of bitwarden that is no longer supported. " + - "Please update your app first and try again."); - } - } -} diff --git a/src/Api/Controllers/LoginsController.cs b/src/Api/Controllers/LoginsController.cs deleted file mode 100644 index 1d9a363556..0000000000 --- a/src/Api/Controllers/LoginsController.cs +++ /dev/null @@ -1,163 +0,0 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; -using Bit.Core.Repositories; -using Microsoft.AspNetCore.Authorization; -using Bit.Core.Models.Api; -using Bit.Core.Exceptions; -using Bit.Core.Services; -using Bit.Core; - -namespace Bit.Api.Controllers -{ - [Obsolete("Use ciphers API instead.")] - [Route("logins")] - // "sites" route is deprecated - [Route("sites")] - [Authorize("Application")] - public class LoginsController : Controller - { - private readonly ICipherRepository _cipherRepository; - private readonly ICipherService _cipherService; - private readonly IUserService _userService; - private readonly CurrentContext _currentContext; - private readonly GlobalSettings _globalSettings; - - public LoginsController( - ICipherRepository cipherRepository, - ICipherService cipherService, - IUserService userService, - CurrentContext currentContext, - GlobalSettings globalSettings) - { - _cipherRepository = cipherRepository; - _cipherService = cipherService; - _userService = userService; - _currentContext = currentContext; - _globalSettings = globalSettings; - } - - [HttpGet("{id}")] - public async Task Get(string id) - { - var userId = _userService.GetProperUserId(User).Value; - var login = await _cipherRepository.GetByIdAsync(new Guid(id), userId); - if(login == null || login.Type != Core.Enums.CipherType.Login) - { - throw new NotFoundException(); - } - - var response = new LoginResponseModel(login, _globalSettings); - return response; - } - - [HttpGet("")] - public async Task> Get(string[] expand = null) - { - var userId = _userService.GetProperUserId(User).Value; - var logins = await _cipherRepository.GetManyByTypeAndUserIdAsync(Core.Enums.CipherType.Login, userId); - var responses = logins.Select(l => new LoginResponseModel(l, _globalSettings)).ToList(); - return new ListResponseModel(responses); - } - - [HttpGet("{id}/admin")] - public async Task GetAdmin(string id) - { - var login = await _cipherRepository.GetDetailsByIdAsync(new Guid(id)); - if(login == null || !login.OrganizationId.HasValue || - !_currentContext.OrganizationAdmin(login.OrganizationId.Value)) - { - throw new NotFoundException(); - } - - var response = new LoginResponseModel(login, _globalSettings, login.OrganizationUseTotp); - return response; - } - - [HttpPost("")] - public async Task Post([FromBody]LoginRequestModel model) - { - var userId = _userService.GetProperUserId(User).Value; - var login = model.ToCipherDetails(userId); - await _cipherService.SaveDetailsAsync(login, userId); - - var response = new LoginResponseModel(login, _globalSettings); - return response; - } - - [HttpPost("admin")] - public async Task PostAdmin([FromBody]LoginRequestModel model) - { - var login = model.ToOrganizationCipher(); - if(!_currentContext.OrganizationAdmin(login.OrganizationId.Value)) - { - throw new NotFoundException(); - } - - var userId = _userService.GetProperUserId(User).Value; - await _cipherService.SaveAsync(login, userId, true); - - var response = new LoginResponseModel(login, _globalSettings, false); - return response; - } - - [HttpPut("{id}")] - [HttpPost("{id}")] - public async Task Put(string id, [FromBody]LoginRequestModel model) - { - var userId = _userService.GetProperUserId(User).Value; - var login = await _cipherRepository.GetByIdAsync(new Guid(id), userId); - if(login == null || login.Type != Core.Enums.CipherType.Login) - { - throw new NotFoundException(); - } - - var modelOrgId = string.IsNullOrWhiteSpace(model.OrganizationId) ? (Guid?)null : new Guid(model.OrganizationId); - if(login.OrganizationId != modelOrgId) - { - throw new BadRequestException("Organization mismatch. Re-sync if you recently shared this login, " + - "then try again."); - } - - await _cipherService.SaveDetailsAsync(model.ToCipherDetails(login), userId); - - var response = new LoginResponseModel(login, _globalSettings); - return response; - } - - [HttpPut("{id}/admin")] - [HttpPost("{id}/admin")] - public async Task PutAdmin(string id, [FromBody]LoginRequestModel model) - { - var userId = _userService.GetProperUserId(User).Value; - var login = await _cipherRepository.GetDetailsByIdAsync(new Guid(id)); - if(login == null || !login.OrganizationId.HasValue || - !_currentContext.OrganizationAdmin(login.OrganizationId.Value)) - { - throw new NotFoundException(); - } - - // object cannot be a descendant of CipherDetails, so let's clone it. - var cipher = Core.Utilities.CoreHelpers.CloneObject(model.ToCipher(login)); - await _cipherService.SaveAsync(cipher, userId, true); - - var response = new LoginResponseModel(cipher, _globalSettings, login.OrganizationUseTotp); - return response; - } - - [HttpDelete("{id}")] - [HttpPost("{id}/delete")] - public async Task Delete(string id) - { - var userId = _userService.GetProperUserId(User).Value; - var login = await _cipherRepository.GetByIdAsync(new Guid(id), userId); - if(login == null || login.Type != Core.Enums.CipherType.Login) - { - throw new NotFoundException(); - } - - await _cipherService.DeleteAsync(login, userId); - } - } -} diff --git a/src/Core/Models/Api/LoginDataModel.cs b/src/Core/Models/Api/LoginDataModel.cs index c5f7af3b4a..354adeeb60 100644 --- a/src/Core/Models/Api/LoginDataModel.cs +++ b/src/Core/Models/Api/LoginDataModel.cs @@ -8,38 +8,16 @@ namespace Bit.Core.Models.Api { public LoginDataModel() { } - public LoginDataModel(LoginRequestModel login) - { - Name = login.Name; - Notes = login.Notes; - Fields = login.Fields; - - Uri = login.Uri; - Username = login.Username; - Password = login.Password; - Totp = login.Totp; - } - public LoginDataModel(CipherRequestModel cipher) { Name = cipher.Name; Notes = cipher.Notes; Fields = cipher.Fields; - 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; - } + 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 1f93c63949..cf5e045c74 100644 --- a/src/Core/Models/Api/Request/CipherRequestModel.cs +++ b/src/Core/Models/Api/Request/CipherRequestModel.cs @@ -32,23 +32,6 @@ namespace Bit.Core.Models.Api 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; } - [Obsolete("Use Login property")] - [EncryptedString] - [StringLength(1000)] - public string Totp { get; set; } - public CipherDetails ToCipherDetails(Guid userId) { var cipher = new CipherDetails diff --git a/src/Core/Models/Api/Request/LoginRequestModel.cs b/src/Core/Models/Api/Request/LoginRequestModel.cs deleted file mode 100644 index b5b87c0293..0000000000 --- a/src/Core/Models/Api/Request/LoginRequestModel.cs +++ /dev/null @@ -1,92 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; -using Bit.Core.Utilities; -using Newtonsoft.Json; -using Core.Models.Data; -using Bit.Core.Models.Table; -using System.Collections.Generic; - -namespace Bit.Core.Models.Api -{ - public class LoginRequestModel - { - [StringLength(36)] - public string OrganizationId { get; set; } - [StringLength(36)] - public string FolderId { get; set; } - public bool Favorite { get; set; } - [Required] - [EncryptedString] - [StringLength(1000)] - public string Name { get; set; } - [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(10000)] - public string Notes { get; set; } - [EncryptedString] - [StringLength(1000)] - public string Totp { get; set; } - public IEnumerable Fields { get; set; } - - public CipherDetails ToCipherDetails(Guid userId) - { - return ToCipherDetails(new CipherDetails - { - UserId = string.IsNullOrWhiteSpace(OrganizationId) ? (Guid?)userId : null, - OrganizationId = string.IsNullOrWhiteSpace(OrganizationId) ? (Guid?)null : new Guid(OrganizationId), - Edit = true - }); - } - - public CipherDetails ToOrganizationCipherDetails(Guid orgId) - { - return ToCipherDetails(new CipherDetails - { - OrganizationId = orgId, - Edit = true - }); - } - - public Cipher ToOrganizationCipher() - { - if(string.IsNullOrWhiteSpace(OrganizationId)) - { - throw new ArgumentNullException(nameof(OrganizationId)); - } - - return ToCipher(new Cipher - { - OrganizationId = new Guid(OrganizationId) - }); - } - - public CipherDetails ToCipherDetails(CipherDetails existingLogin) - { - existingLogin.FolderId = string.IsNullOrWhiteSpace(FolderId) ? null : (Guid?)new Guid(FolderId); - existingLogin.Favorite = Favorite; - - existingLogin.Data = JsonConvert.SerializeObject(new LoginDataModel(this), - new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); - existingLogin.Type = Enums.CipherType.Login; - - return existingLogin; - } - - public Cipher ToCipher(Cipher existingLogin) - { - existingLogin.Data = JsonConvert.SerializeObject(new LoginDataModel(this), - new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); - existingLogin.Type = Enums.CipherType.Login; - - return existingLogin; - } - } -} diff --git a/src/Core/Models/Api/Response/LoginResponseModel.cs b/src/Core/Models/Api/Response/LoginResponseModel.cs deleted file mode 100644 index 5bfe27327a..0000000000 --- a/src/Core/Models/Api/Response/LoginResponseModel.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; -using Core.Models.Data; -using Bit.Core.Models.Table; -using System.Collections.Generic; - -namespace Bit.Core.Models.Api -{ - public class LoginResponseModel : ResponseModel - { - public LoginResponseModel(Cipher cipher, GlobalSettings globalSettings, bool orgUseTotp, string obj = "login") - : base(obj) - { - if(cipher == null) - { - throw new ArgumentNullException(nameof(cipher)); - } - - if(cipher.Type != Enums.CipherType.Login) - { - throw new ArgumentException(nameof(cipher.Type)); - } - - var data = new LoginDataModel(cipher); - - Id = cipher.Id.ToString(); - OrganizationId = cipher.OrganizationId?.ToString(); - Name = data.Name; - Uri = data.Uri; - Username = data.Username; - Password = data.Password; - Notes = data.Notes; - Totp = data.Totp; - RevisionDate = cipher.RevisionDate; - Edit = true; - OrganizationUseTotp = orgUseTotp; - Attachments = AttachmentResponseModel.FromCipher(cipher, globalSettings); - } - - public LoginResponseModel(CipherDetails cipher, GlobalSettings globalSettings, string obj = "login") - : this(cipher as Cipher, globalSettings, cipher.OrganizationUseTotp, obj) - { - FolderId = cipher.FolderId?.ToString(); - Favorite = cipher.Favorite; - Edit = cipher.Edit; - } - - public string Id { get; set; } - public string OrganizationId { get; set; } - public string FolderId { get; set; } - public bool Favorite { get; set; } - public bool Edit { get; set; } - public bool OrganizationUseTotp { 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; } - public string Totp { get; set; } - public IEnumerable Attachments { get; set; } - public DateTime RevisionDate { get; set; } - } -}