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;
|
2016-06-29 01:15:37 -04:00
|
|
|
|
using Bit.Core.Services;
|
2017-04-27 14:50:22 -04:00
|
|
|
|
using Bit.Core;
|
2017-04-28 16:06:25 -04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using Core.Models.Data;
|
|
|
|
|
|
using Bit.Core.Models.Table;
|
2015-12-08 22:57:38 -05:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Controllers
|
|
|
|
|
|
{
|
2017-01-02 21:52:13 -05:00
|
|
|
|
[Route("logins")]
|
2017-01-09 22:20:34 -05:00
|
|
|
|
// "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
|
|
|
|
{
|
2016-05-21 17:16:22 -04:00
|
|
|
|
private readonly ICipherRepository _cipherRepository;
|
2017-04-28 16:06:25 -04:00
|
|
|
|
private readonly IFolderRepository _folderRepository;
|
2016-06-29 01:15:37 -04:00
|
|
|
|
private readonly ICipherService _cipherService;
|
2017-01-24 22:46:54 -05:00
|
|
|
|
private readonly IUserService _userService;
|
2017-04-27 14:50:22 -04:00
|
|
|
|
private readonly CurrentContext _currentContext;
|
2015-12-08 22:57:38 -05:00
|
|
|
|
|
2017-01-02 21:52:13 -05:00
|
|
|
|
public LoginsController(
|
2016-05-21 17:16:22 -04:00
|
|
|
|
ICipherRepository cipherRepository,
|
2017-04-28 16:06:25 -04:00
|
|
|
|
IFolderRepository folderRepository,
|
2016-06-29 01:15:37 -04:00
|
|
|
|
ICipherService cipherService,
|
2017-04-27 14:50:22 -04:00
|
|
|
|
IUserService userService,
|
|
|
|
|
|
CurrentContext currentContext)
|
2015-12-08 22:57:38 -05:00
|
|
|
|
{
|
2016-05-21 17:16:22 -04:00
|
|
|
|
_cipherRepository = cipherRepository;
|
2017-04-28 16:06:25 -04:00
|
|
|
|
_folderRepository = folderRepository;
|
2016-06-29 01:15:37 -04:00
|
|
|
|
_cipherService = cipherService;
|
2017-01-24 22:46:54 -05:00
|
|
|
|
_userService = userService;
|
2017-04-27 14:50:22 -04:00
|
|
|
|
_currentContext = currentContext;
|
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-05-06 23:23:01 -04:00
|
|
|
|
var response = new LoginResponseModel(login);
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
var login = await _cipherRepository.GetByIdAsync(new Guid(id));
|
|
|
|
|
|
if(login == null || !login.OrganizationId.HasValue ||
|
|
|
|
|
|
!_currentContext.OrganizationAdmin(login.OrganizationId.Value))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var response = new LoginResponseModel(login);
|
|
|
|
|
|
return response;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-08 22:57:38 -05:00
|
|
|
|
[HttpGet("")]
|
2017-04-28 16:06:25 -04:00
|
|
|
|
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-02-28 22:51:29 -05:00
|
|
|
|
var logins = await _cipherRepository.GetManyByTypeAndUserIdAsync(Core.Enums.CipherType.Login,
|
2017-02-18 01:17:09 -05:00
|
|
|
|
userId);
|
2017-04-27 09:39:21 -04:00
|
|
|
|
var responses = logins.Select(l => new LoginResponseModel(l)).ToList();
|
2017-04-28 16:06:25 -04:00
|
|
|
|
await ExpandManyAsync(logins, responses, expand, userId);
|
2017-02-28 22:51:29 -05:00
|
|
|
|
return new ListResponseModel<LoginResponseModel>(responses);
|
2015-12-08 22:57:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost("")]
|
2017-03-18 11:58:02 -04:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
var response = new LoginResponseModel(login);
|
|
|
|
|
|
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-03-18 11:58:02 -04:00
|
|
|
|
var response = new LoginResponseModel(login);
|
2015-12-08 22:57:38 -05:00
|
|
|
|
return response;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPut("{id}")]
|
2016-07-13 21:43:48 -04:00
|
|
|
|
[HttpPost("{id}")]
|
2017-03-18 11:58:02 -04:00
|
|
|
|
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;
|
2017-03-24 16:15:50 -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-04-27 14:50:22 -04:00
|
|
|
|
await _cipherService.SaveDetailsAsync(model.ToCipherDetails(login), userId);
|
|
|
|
|
|
|
|
|
|
|
|
var response = new LoginResponseModel(login);
|
|
|
|
|
|
return response;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPut("{id}/admin")]
|
|
|
|
|
|
[HttpPost("{id}/admin")]
|
|
|
|
|
|
public async Task<LoginResponseModel> PutAdmin(string id, [FromBody]LoginRequestModel model)
|
|
|
|
|
|
{
|
|
|
|
|
|
var login = await _cipherRepository.GetByIdAsync(new Guid(id));
|
|
|
|
|
|
if(login == null || !login.OrganizationId.HasValue ||
|
|
|
|
|
|
!_currentContext.OrganizationAdmin(login.OrganizationId.Value))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var userId = _userService.GetProperUserId(User).Value;
|
|
|
|
|
|
await _cipherService.SaveAsync(model.ToCipher(login), userId, true);
|
2015-12-08 22:57:38 -05:00
|
|
|
|
|
2017-03-18 11:58:02 -04:00
|
|
|
|
var response = new LoginResponseModel(login);
|
2015-12-08 22:57:38 -05:00
|
|
|
|
return response;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpDelete("{id}")]
|
2016-07-13 21:43:48 -04:00
|
|
|
|
[HttpPost("{id}/delete")]
|
2015-12-08 22:57:38 -05:00
|
|
|
|
public async Task Delete(string id)
|
|
|
|
|
|
{
|
2017-03-24 09:27:15 -04: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-03-24 09:27:15 -04:00
|
|
|
|
await _cipherService.DeleteAsync(login, userId);
|
2015-12-08 22:57:38 -05:00
|
|
|
|
}
|
2017-04-28 16:06:25 -04:00
|
|
|
|
|
|
|
|
|
|
[Obsolete]
|
|
|
|
|
|
private async Task ExpandManyAsync(IEnumerable<CipherDetails> logins, ICollection<LoginResponseModel> responses,
|
|
|
|
|
|
string[] expand, Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(expand == null || expand.Count() == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(expand.Any(e => e.ToLower() == "folder"))
|
|
|
|
|
|
{
|
|
|
|
|
|
var folders = await _folderRepository.GetManyByUserIdAsync(userId);
|
|
|
|
|
|
if(folders != null && folders.Count() > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach(var response in responses)
|
|
|
|
|
|
{
|
|
|
|
|
|
var login = logins.SingleOrDefault(s => s.Id.ToString() == response.Id);
|
|
|
|
|
|
if(login == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var folder = folders.SingleOrDefault(f => f.Id == login.FolderId);
|
|
|
|
|
|
if(folder == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response.Folder = new FolderResponseModel(folder);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2015-12-08 22:57:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|