Files
server/src/Api/Controllers/LoginsController.cs

164 lines
6.2 KiB
C#
Raw Normal View History

2015-12-08 22:57:38 -05:00
using System;
using System.Linq;
using System.Threading.Tasks;
2016-05-19 19:10:24 -04:00
using Microsoft.AspNetCore.Mvc;
2015-12-08 22:57:38 -05:00
using Bit.Core.Repositories;
2016-05-19 19:10:24 -04:00
using Microsoft.AspNetCore.Authorization;
2017-03-08 21:55:08 -05:00
using Bit.Core.Models.Api;
2015-12-08 22:57:38 -05:00
using Bit.Core.Exceptions;
using Bit.Core.Services;
2017-04-27 14:50:22 -04:00
using Bit.Core;
2015-12-08 22:57:38 -05:00
namespace Bit.Api.Controllers
{
2017-01-02 21:52:13 -05:00
[Route("logins")]
// "sites" route is deprecated
2015-12-08 22:57:38 -05:00
[Route("sites")]
[Authorize("Application")]
2017-01-02 21:52:13 -05:00
public class LoginsController : Controller
2015-12-08 22:57:38 -05:00
{
private readonly ICipherRepository _cipherRepository;
private readonly ICipherService _cipherService;
private readonly IUserService _userService;
2017-04-27 14:50:22 -04:00
private readonly CurrentContext _currentContext;
2017-06-30 23:01:41 -04:00
private readonly GlobalSettings _globalSettings;
2015-12-08 22:57:38 -05:00
2017-01-02 21:52:13 -05:00
public LoginsController(
ICipherRepository cipherRepository,
ICipherService cipherService,
2017-04-27 14:50:22 -04:00
IUserService userService,
2017-06-30 23:01:41 -04:00
CurrentContext currentContext,
GlobalSettings globalSettings)
2015-12-08 22:57:38 -05:00
{
_cipherRepository = cipherRepository;
_cipherService = cipherService;
_userService = userService;
2017-04-27 14:50:22 -04:00
_currentContext = currentContext;
2017-06-30 23:01:41 -04:00
_globalSettings = globalSettings;
2015-12-08 22:57:38 -05:00
}
[HttpGet("{id}")]
2017-05-06 23:23:01 -04:00
public async Task<LoginResponseModel> Get(string id)
2015-12-08 22:57:38 -05:00
{
2017-02-18 01:17:09 -05:00
var userId = _userService.GetProperUserId(User).Value;
2017-05-06 23:23:01 -04:00
var login = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
2017-01-02 21:52:13 -05:00
if(login == null || login.Type != Core.Enums.CipherType.Login)
2015-12-08 22:57:38 -05:00
{
throw new NotFoundException();
}
2017-06-30 23:01:41 -04:00
var response = new LoginResponseModel(login, _globalSettings);
2015-12-08 22:57:38 -05:00
return response;
}
2017-04-27 14:50:22 -04:00
[HttpGet("{id}/admin")]
public async Task<LoginResponseModel> GetAdmin(string id)
{
2017-07-07 14:08:30 -04:00
var userId = _userService.GetProperUserId(User).Value;
var login = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
2017-04-27 14:50:22 -04:00
if(login == null || !login.OrganizationId.HasValue ||
!_currentContext.OrganizationAdmin(login.OrganizationId.Value))
{
throw new NotFoundException();
}
2017-07-07 14:08:30 -04:00
var response = new LoginResponseModel(login, _globalSettings, login.OrganizationUseTotp);
2017-04-27 14:50:22 -04:00
return response;
}
2015-12-08 22:57:38 -05:00
[HttpGet("")]
public async Task<ListResponseModel<LoginResponseModel>> Get(string[] expand = null)
2015-12-08 22:57:38 -05:00
{
2017-02-18 01:17:09 -05:00
var userId = _userService.GetProperUserId(User).Value;
2017-06-13 09:12:00 -04:00
var logins = await _cipherRepository.GetManyByTypeAndUserIdAsync(Core.Enums.CipherType.Login, userId);
2017-06-30 23:01:41 -04:00
var responses = logins.Select(l => new LoginResponseModel(l, _globalSettings)).ToList();
2017-02-28 22:51:29 -05:00
return new ListResponseModel<LoginResponseModel>(responses);
2015-12-08 22:57:38 -05:00
}
[HttpPost("")]
public async Task<LoginResponseModel> Post([FromBody]LoginRequestModel model)
2015-12-08 22:57:38 -05:00
{
2017-02-18 01:17:09 -05:00
var userId = _userService.GetProperUserId(User).Value;
2017-03-18 23:41:46 -04:00
var login = model.ToCipherDetails(userId);
2017-04-27 14:50:22 -04:00
await _cipherService.SaveDetailsAsync(login, userId);
2017-06-30 23:01:41 -04:00
var response = new LoginResponseModel(login, _globalSettings);
2017-04-27 14:50:22 -04:00
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);
2015-12-08 22:57:38 -05:00
2017-07-07 14:08:30 -04:00
var response = new LoginResponseModel(login, _globalSettings, false);
2015-12-08 22:57:38 -05:00
return response;
}
[HttpPut("{id}")]
[HttpPost("{id}")]
public async Task<LoginResponseModel> Put(string id, [FromBody]LoginRequestModel model)
2015-12-08 22:57:38 -05:00
{
2017-02-18 01:17:09 -05:00
var userId = _userService.GetProperUserId(User).Value;
var login = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
2017-01-02 21:52:13 -05:00
if(login == null || login.Type != Core.Enums.CipherType.Login)
2015-12-08 22:57:38 -05:00
{
throw new NotFoundException();
}
2017-05-07 00:08:23 -04:00
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.");
}
2017-04-27 14:50:22 -04:00
await _cipherService.SaveDetailsAsync(model.ToCipherDetails(login), userId);
2017-06-30 23:01:41 -04:00
var response = new LoginResponseModel(login, _globalSettings);
2017-04-27 14:50:22 -04:00
return response;
}
[HttpPut("{id}/admin")]
[HttpPost("{id}/admin")]
public async Task<LoginResponseModel> PutAdmin(string id, [FromBody]LoginRequestModel model)
{
2017-07-07 14:08:30 -04:00
var userId = _userService.GetProperUserId(User).Value;
var login = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
2017-04-27 14:50:22 -04:00
if(login == null || !login.OrganizationId.HasValue ||
!_currentContext.OrganizationAdmin(login.OrganizationId.Value))
{
throw new NotFoundException();
}
2017-07-11 14:43:43 -04:00
// 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);
2015-12-08 22:57:38 -05:00
2017-07-11 14:43:43 -04:00
var response = new LoginResponseModel(cipher, _globalSettings, login.OrganizationUseTotp);
2015-12-08 22:57:38 -05:00
return response;
}
[HttpDelete("{id}")]
[HttpPost("{id}/delete")]
2015-12-08 22:57:38 -05:00
public async Task Delete(string id)
{
var userId = _userService.GetProperUserId(User).Value;
var login = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
2017-01-02 21:52:13 -05:00
if(login == null || login.Type != Core.Enums.CipherType.Login)
2015-12-08 22:57:38 -05:00
{
throw new NotFoundException();
}
await _cipherService.DeleteAsync(login, userId);
2015-12-08 22:57:38 -05:00
}
}
}