1
0
mirror of https://github.com/bitwarden/server.git synced 2025-01-08 19:47:44 +01:00
bitwarden-server/src/Api/Controllers/CiphersController.cs

435 lines
18 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Bit.Core.Repositories;
using Microsoft.AspNetCore.Authorization;
2017-03-09 03:55:08 +01:00
using Bit.Core.Models.Api;
using Bit.Core.Exceptions;
using Bit.Core.Services;
2017-04-12 18:42:00 +02:00
using Bit.Core;
using Bit.Api.Utilities;
2017-07-10 20:30:12 +02:00
using Bit.Core.Utilities;
using Core.Models.Data;
using System.Collections.Generic;
namespace Bit.Api.Controllers
{
[Route("ciphers")]
[Authorize("Application")]
public class CiphersController : Controller
{
private readonly ICipherRepository _cipherRepository;
2017-04-27 15:19:30 +02:00
private readonly ICollectionCipherRepository _collectionCipherRepository;
private readonly ICipherService _cipherService;
private readonly IUserService _userService;
2017-04-12 18:42:00 +02:00
private readonly CurrentContext _currentContext;
2017-07-01 05:01:41 +02:00
private readonly GlobalSettings _globalSettings;
public CiphersController(
ICipherRepository cipherRepository,
2017-04-27 15:19:30 +02:00
ICollectionCipherRepository collectionCipherRepository,
ICipherService cipherService,
2017-04-12 18:42:00 +02:00
IUserService userService,
2017-07-01 05:01:41 +02:00
CurrentContext currentContext,
GlobalSettings globalSettings)
{
_cipherRepository = cipherRepository;
2017-04-27 15:19:30 +02:00
_collectionCipherRepository = collectionCipherRepository;
_cipherService = cipherService;
_userService = userService;
2017-04-12 18:42:00 +02:00
_currentContext = currentContext;
2017-07-01 05:01:41 +02:00
_globalSettings = globalSettings;
}
[HttpGet("{id}")]
2017-03-01 04:51:29 +01:00
public async Task<CipherResponseModel> Get(string id)
{
2017-02-18 07:17:09 +01:00
var userId = _userService.GetProperUserId(User).Value;
2017-03-01 04:51:29 +01:00
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
if(cipher == null)
{
throw new NotFoundException();
}
2017-07-01 05:01:41 +02:00
return new CipherResponseModel(cipher, _globalSettings);
}
[HttpGet("{id}/admin")]
public async Task<CipherResponseModel> GetAdmin(string id)
{
var cipher = await _cipherRepository.GetDetailsByIdAsync(new Guid(id));
if(cipher == null || !cipher.OrganizationId.HasValue ||
!_currentContext.OrganizationAdmin(cipher.OrganizationId.Value))
{
throw new NotFoundException();
}
return new CipherResponseModel(cipher, _globalSettings);
}
2017-04-04 23:22:47 +02:00
[HttpGet("{id}/full-details")]
2017-05-07 05:23:01 +02:00
[HttpGet("{id}/details")]
public async Task<CipherDetailsResponseModel> GetDetails(string id)
2017-04-04 23:22:47 +02:00
{
var userId = _userService.GetProperUserId(User).Value;
var cipherId = new Guid(id);
2017-05-07 05:23:01 +02:00
var cipher = await _cipherRepository.GetByIdAsync(cipherId, userId);
2017-04-04 23:22:47 +02:00
if(cipher == null)
{
throw new NotFoundException();
}
2017-04-27 15:19:30 +02:00
var collectionCiphers = await _collectionCipherRepository.GetManyByUserIdCipherIdAsync(userId, cipherId);
2017-07-01 05:01:41 +02:00
return new CipherDetailsResponseModel(cipher, _globalSettings, collectionCiphers);
2017-04-04 23:22:47 +02:00
}
[HttpGet("")]
public async Task<ListResponseModel<CipherResponseModel>> Get([FromQuery]Core.Enums.CipherType? type = null)
{
2017-02-18 07:17:09 +01:00
var userId = _userService.GetProperUserId(User).Value;
IEnumerable<CipherDetails> ciphers;
if(type.HasValue)
{
2017-10-09 17:23:20 +02:00
ciphers = await _cipherRepository.GetManyByTypeAndUserIdAsync(type.Value, userId);
}
else
{
ciphers = await _cipherRepository.GetManyByUserIdAsync(userId);
}
2017-07-01 05:01:41 +02:00
var responses = ciphers.Select(c => new CipherResponseModel(c, _globalSettings)).ToList();
2017-03-01 04:51:29 +01:00
return new ListResponseModel<CipherResponseModel>(responses);
}
[HttpPost("")]
public async Task<CipherResponseModel> Post([FromBody]CipherRequestModel model)
{
var userId = _userService.GetProperUserId(User).Value;
var cipher = model.ToCipherDetails(userId);
await _cipherService.SaveDetailsAsync(cipher, userId);
var response = new CipherResponseModel(cipher, _globalSettings);
return response;
}
[HttpPost("admin")]
public async Task<CipherMiniResponseModel> PostAdmin([FromBody]CipherRequestModel model)
{
var cipher = model.ToOrganizationCipher();
if(!_currentContext.OrganizationAdmin(cipher.OrganizationId.Value))
{
throw new NotFoundException();
}
var userId = _userService.GetProperUserId(User).Value;
await _cipherService.SaveAsync(cipher, userId, true);
var response = new CipherMiniResponseModel(cipher, _globalSettings, false);
return response;
}
[HttpPut("{id}")]
[HttpPost("{id}")]
public async Task<CipherResponseModel> Put(string id, [FromBody]CipherRequestModel model)
{
var userId = _userService.GetProperUserId(User).Value;
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
if(cipher == null)
{
throw new NotFoundException();
}
var modelOrgId = string.IsNullOrWhiteSpace(model.OrganizationId) ? (Guid?)null : new Guid(model.OrganizationId);
if(cipher.OrganizationId != modelOrgId)
{
throw new BadRequestException("Organization mismatch. Re-sync if you recently shared this item, " +
"then try again.");
}
await _cipherService.SaveDetailsAsync(model.ToCipherDetails(cipher), userId);
var response = new CipherResponseModel(cipher, _globalSettings);
return response;
}
[HttpPut("{id}/admin")]
[HttpPost("{id}/admin")]
public async Task<CipherMiniResponseModel> PutAdmin(string id, [FromBody]CipherRequestModel model)
{
var userId = _userService.GetProperUserId(User).Value;
var cipher = await _cipherRepository.GetDetailsByIdAsync(new Guid(id));
if(cipher == null || !cipher.OrganizationId.HasValue ||
!_currentContext.OrganizationAdmin(cipher.OrganizationId.Value))
{
throw new NotFoundException();
}
// object cannot be a descendant of CipherDetails, so let's clone it.
var cipherClone = CoreHelpers.CloneObject(model.ToCipher(cipher));
await _cipherService.SaveAsync(cipherClone, userId, true);
var response = new CipherMiniResponseModel(cipherClone, _globalSettings, cipher.OrganizationUseTotp);
return response;
}
2017-04-04 23:22:47 +02:00
[HttpGet("details")]
2017-04-27 15:19:30 +02:00
public async Task<ListResponseModel<CipherDetailsResponseModel>> GetCollections()
2017-03-23 22:43:12 +01:00
{
var userId = _userService.GetProperUserId(User).Value;
2017-04-27 15:19:30 +02:00
var ciphers = await _cipherRepository.GetManyByUserIdHasCollectionsAsync(userId);
2017-04-27 15:19:30 +02:00
var collectionCiphers = await _collectionCipherRepository.GetManyByUserIdAsync(userId);
2017-04-27 15:39:21 +02:00
var collectionCiphersGroupDict = collectionCiphers.GroupBy(c => c.CipherId).ToDictionary(s => s.Key);
2017-07-01 05:01:41 +02:00
var responses = ciphers.Select(c => new CipherDetailsResponseModel(c, _globalSettings, collectionCiphersGroupDict));
2017-03-23 22:43:12 +01:00
return new ListResponseModel<CipherDetailsResponseModel>(responses);
}
2017-04-17 23:01:23 +02:00
[HttpGet("organization-details")]
2017-04-27 15:19:30 +02:00
public async Task<ListResponseModel<CipherMiniDetailsResponseModel>> GetOrganizationCollections(string organizationId)
2017-04-17 23:01:23 +02:00
{
var userId = _userService.GetProperUserId(User).Value;
var orgIdGuid = new Guid(organizationId);
if(!_currentContext.OrganizationAdmin(orgIdGuid))
{
throw new NotFoundException();
}
var ciphers = await _cipherRepository.GetManyByOrganizationIdAsync(orgIdGuid);
2017-04-27 15:19:30 +02:00
var collectionCiphers = await _collectionCipherRepository.GetManyByOrganizationIdAsync(orgIdGuid);
2017-04-27 15:39:21 +02:00
var collectionCiphersGroupDict = collectionCiphers.GroupBy(c => c.CipherId).ToDictionary(s => s.Key);
2017-04-17 23:01:23 +02:00
2017-07-01 05:01:41 +02:00
var responses = ciphers.Select(c => new CipherMiniDetailsResponseModel(c, _globalSettings,
collectionCiphersGroupDict));
2017-04-17 23:01:23 +02:00
return new ListResponseModel<CipherMiniDetailsResponseModel>(responses);
}
[HttpPost("import")]
public async Task PostImport([FromBody]ImportCiphersRequestModel model)
{
2017-10-09 22:58:37 +02:00
if(model.Ciphers.Count() > 5000 || model.FolderRelationships.Count() > 5000 || model.Folders.Count() > 200)
{
throw new BadRequestException("You cannot import this much data at once.");
}
var userId = _userService.GetProperUserId(User).Value;
2017-05-07 05:45:29 +02:00
var folders = model.Folders.Select(f => f.ToFolder(userId)).ToList();
2017-09-28 19:11:56 +02:00
var ciphers = model.Ciphers.Select(c => c.ToCipherDetails(userId)).ToList();
2017-06-13 15:12:00 +02:00
await _cipherService.ImportCiphersAsync(folders, ciphers, model.FolderRelationships);
}
2017-09-06 15:06:13 +02:00
[HttpPost("import-organization")]
public async Task PostImport([FromQuery]string organizationId, [FromBody]ImportOrganizationCiphersRequestModel model)
{
2017-10-09 22:58:37 +02:00
if(model.Ciphers.Count() > 5000 || model.CollectionRelationships.Count() > 5000 || model.Collections.Count() > 200)
{
throw new BadRequestException("You cannot import this much data at once.");
}
2017-09-06 15:06:13 +02:00
var orgId = new Guid(organizationId);
if(!_currentContext.OrganizationAdmin(orgId))
{
throw new NotFoundException();
}
var userId = _userService.GetProperUserId(User).Value;
2017-09-06 15:06:13 +02:00
var collections = model.Collections.Select(c => c.ToCollection(orgId)).ToList();
2017-09-28 19:11:56 +02:00
var ciphers = model.Ciphers.Select(l => l.ToOrganizationCipherDetails(orgId)).ToList();
await _cipherService.ImportCiphersAsync(collections, ciphers, model.CollectionRelationships, userId);
}
[HttpPut("{id}/partial")]
[HttpPost("{id}/partial")]
public async Task PutPartial(string id, [FromBody]CipherPartialRequestModel model)
{
var userId = _userService.GetProperUserId(User).Value;
var folderId = string.IsNullOrWhiteSpace(model.FolderId) ? null : (Guid?)new Guid(model.FolderId);
await _cipherRepository.UpdatePartialAsync(new Guid(id), userId, folderId, model.Favorite);
}
2017-04-12 18:42:00 +02:00
[HttpPut("{id}/share")]
[HttpPost("{id}/share")]
public async Task PutShare(string id, [FromBody]CipherShareRequestModel model)
{
var userId = _userService.GetProperUserId(User).Value;
2017-07-10 20:30:12 +02:00
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id));
2017-04-12 18:42:00 +02:00
if(cipher == null || cipher.UserId != userId ||
!_currentContext.OrganizationUser(new Guid(model.Cipher.OrganizationId)))
{
throw new NotFoundException();
}
2017-07-10 20:30:12 +02:00
var original = CoreHelpers.CloneObject(cipher);
await _cipherService.ShareAsync(original, model.Cipher.ToCipher(cipher), new Guid(model.Cipher.OrganizationId),
2017-04-27 15:39:21 +02:00
model.CollectionIds.Select(c => new Guid(c)), userId);
}
2017-04-27 15:19:30 +02:00
[HttpPut("{id}/collections")]
[HttpPost("{id}/collections")]
public async Task PutCollections(string id, [FromBody]CipherCollectionsRequestModel model)
2017-04-12 18:42:00 +02:00
{
var userId = _userService.GetProperUserId(User).Value;
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
if(cipher == null || !cipher.OrganizationId.HasValue ||
!_currentContext.OrganizationUser(cipher.OrganizationId.Value))
{
throw new NotFoundException();
}
2017-04-27 15:39:21 +02:00
await _cipherService.SaveCollectionsAsync(cipher, model.CollectionIds.Select(c => new Guid(c)), userId, false);
2017-04-18 05:12:48 +02:00
}
2017-04-27 15:19:30 +02:00
[HttpPut("{id}/collections-admin")]
[HttpPost("{id}/collections-admin")]
public async Task PutCollectionsAdmin(string id, [FromBody]CipherCollectionsRequestModel model)
2017-04-18 05:12:48 +02:00
{
var userId = _userService.GetProperUserId(User).Value;
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id));
if(cipher == null || !cipher.OrganizationId.HasValue ||
!_currentContext.OrganizationAdmin(cipher.OrganizationId.Value))
{
throw new NotFoundException();
}
2017-04-27 15:39:21 +02:00
await _cipherService.SaveCollectionsAsync(cipher, model.CollectionIds.Select(c => new Guid(c)), userId, true);
2017-04-12 18:42:00 +02:00
}
[HttpDelete("{id}")]
[HttpPost("{id}/delete")]
public async Task Delete(string id)
{
2017-03-22 02:13:20 +01:00
var userId = _userService.GetProperUserId(User).Value;
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
if(cipher == null)
{
throw new NotFoundException();
}
await _cipherService.DeleteAsync(cipher, userId);
}
2017-04-19 22:00:47 +02:00
[HttpDelete("{id}/admin")]
[HttpPost("{id}/delete-admin")]
public async Task DeleteAdmin(string id)
{
var userId = _userService.GetProperUserId(User).Value;
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id));
if(cipher == null || !cipher.OrganizationId.HasValue ||
!_currentContext.OrganizationAdmin(cipher.OrganizationId.Value))
{
throw new NotFoundException();
}
await _cipherService.DeleteAsync(cipher, userId, true);
}
2017-06-09 06:30:59 +02:00
[HttpDelete("")]
[HttpPost("delete")]
public async Task DeleteMany([FromBody]CipherBulkDeleteRequestModel model)
{
2017-10-09 22:58:37 +02:00
if(model.Ids.Count() > 200)
{
throw new BadRequestException("You can only delete up to 200 items at a time.");
}
2017-06-09 06:30:59 +02:00
var userId = _userService.GetProperUserId(User).Value;
await _cipherService.DeleteManyAsync(model.Ids.Select(i => new Guid(i)), userId);
}
[HttpPut("move")]
[HttpPost("move")]
public async Task MoveMany([FromBody]CipherBulkMoveRequestModel model)
{
2017-10-09 22:58:37 +02:00
if(model.Ids.Count() > 200)
{
throw new BadRequestException("You can only move up to 200 items at a time.");
}
2017-06-09 06:30:59 +02:00
var userId = _userService.GetProperUserId(User).Value;
await _cipherService.MoveManyAsync(model.Ids.Select(i => new Guid(i)),
string.IsNullOrWhiteSpace(model.FolderId) ? (Guid?)null : new Guid(model.FolderId), userId);
2017-06-09 06:30:59 +02:00
}
2017-10-26 03:26:09 +02:00
[HttpPost("purge")]
public async Task PostPurge()
{
var userId = _userService.GetProperUserId(User).Value;
await _cipherRepository.DeleteByUserIdAsync(userId);
}
[HttpPost("{id}/attachment")]
[DisableFormValueModelBinding]
public async Task<CipherResponseModel> PostAttachment(string id)
{
2017-07-10 20:30:12 +02:00
ValidateAttachment();
var idGuid = new Guid(id);
var userId = _userService.GetProperUserId(User).Value;
var cipher = await _cipherRepository.GetByIdAsync(idGuid, userId);
if(cipher == null)
{
2017-07-10 20:30:12 +02:00
throw new NotFoundException();
}
2017-07-10 20:30:12 +02:00
await Request.GetFileAsync(async (stream, fileName) =>
2017-07-01 05:01:41 +02:00
{
2017-07-10 20:30:12 +02:00
await _cipherService.CreateAttachmentAsync(cipher, stream, fileName,
Request.ContentLength.GetValueOrDefault(0), userId);
});
return new CipherResponseModel(cipher, _globalSettings);
2017-07-10 20:30:12 +02:00
}
[HttpPost("{id}/attachment/{attachmentId}/share")]
[DisableFormValueModelBinding]
public async Task PostAttachmentShare(string id, string attachmentId, Guid organizationId)
{
ValidateAttachment();
2017-07-01 05:01:41 +02:00
var userId = _userService.GetProperUserId(User).Value;
2017-07-10 20:30:12 +02:00
var cipher = await _cipherRepository.GetByIdAsync(new Guid(id));
if(cipher == null || cipher.UserId != userId || !_currentContext.OrganizationUser(organizationId))
{
throw new NotFoundException();
}
await Request.GetFileAsync(async (stream, fileName) =>
{
2017-07-10 20:30:12 +02:00
await _cipherService.CreateAttachmentShareAsync(cipher, stream, fileName,
Request.ContentLength.GetValueOrDefault(0), attachmentId, organizationId);
});
}
[HttpDelete("{id}/attachment/{attachmentId}")]
[HttpPost("{id}/attachment/{attachmentId}/delete")]
public async Task DeleteAttachment(string id, string attachmentId)
{
var idGuid = new Guid(id);
var userId = _userService.GetProperUserId(User).Value;
var cipher = await _cipherRepository.GetByIdAsync(idGuid, userId);
if(cipher == null)
{
throw new NotFoundException();
}
2017-07-07 17:07:22 +02:00
await _cipherService.DeleteAttachmentAsync(cipher, attachmentId, userId, false);
}
2017-07-10 20:30:12 +02:00
private void ValidateAttachment()
{
if(!Request?.ContentType.Contains("multipart/") ?? true)
{
throw new BadRequestException("Invalid content.");
}
if(Request.ContentLength > 105906176) // 101 MB, give em' 1 extra MB for cushion
{
throw new BadRequestException("Max file size is 100 MB.");
}
}
}
}