6 changed files with 4 additions and 380 deletions
@ -1,20 +0,0 @@
@@ -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."); |
||||
} |
||||
} |
||||
} |
||||
@ -1,163 +0,0 @@
@@ -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<LoginResponseModel> 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<ListResponseModel<LoginResponseModel>> 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<LoginResponseModel>(responses); |
||||
} |
||||
|
||||
[HttpGet("{id}/admin")] |
||||
public async Task<LoginResponseModel> 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<LoginResponseModel> 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<LoginResponseModel> 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<LoginResponseModel> 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<LoginResponseModel> 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); |
||||
} |
||||
} |
||||
} |
||||
@ -1,92 +0,0 @@
@@ -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<FieldDataModel> 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; |
||||
} |
||||
} |
||||
} |
||||
@ -1,62 +0,0 @@
@@ -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<AttachmentResponseModel> Attachments { get; set; } |
||||
public DateTime RevisionDate { get; set; } |
||||
} |
||||
} |
||||
Loading…
Reference in new issue