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

309 lines
10 KiB
C#
Raw Normal View History

2015-12-08 22:57:38 -05:00
using System;
using System.Threading.Tasks;
2016-05-19 19:10:24 -04:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
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;
2016-05-19 19:10:24 -04:00
using Microsoft.AspNetCore.Identity;
2017-03-08 21:45:08 -05:00
using Bit.Core.Models.Table;
2015-12-08 22:57:38 -05:00
using Bit.Core.Enums;
using System.Linq;
2017-03-02 21:51:03 -05:00
using Bit.Core.Repositories;
using System.Collections.Generic;
2015-12-08 22:57:38 -05:00
namespace Bit.Api.Controllers
{
[Route("accounts")]
[Authorize("Application")]
public class AccountsController : Controller
{
private readonly IUserService _userService;
private readonly ICipherService _cipherService;
2017-03-06 20:51:13 -05:00
private readonly IOrganizationUserRepository _organizationUserRepository;
2015-12-08 22:57:38 -05:00
private readonly UserManager<User> _userManager;
public AccountsController(
IUserService userService,
ICipherService cipherService,
2017-03-06 20:51:13 -05:00
IOrganizationUserRepository organizationUserRepository,
UserManager<User> userManager)
2015-12-08 22:57:38 -05:00
{
_userService = userService;
_cipherService = cipherService;
2017-03-06 20:51:13 -05:00
_organizationUserRepository = organizationUserRepository;
2015-12-08 22:57:38 -05:00
_userManager = userManager;
}
[HttpPost("register")]
[AllowAnonymous]
public async Task PostRegister([FromBody]RegisterRequestModel model)
{
var result = await _userService.RegisterUserAsync(model.ToUser(), model.MasterPasswordHash);
2015-12-08 22:57:38 -05:00
if(result.Succeeded)
{
return;
}
foreach(var error in result.Errors.Where(e => e.Code != "DuplicateUserName"))
2015-12-08 22:57:38 -05:00
{
ModelState.AddModelError(string.Empty, error.Description);
}
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
[HttpPost("password-hint")]
[AllowAnonymous]
public async Task PostPasswordHint([FromBody]PasswordHintRequestModel model)
{
await _userService.SendMasterPasswordHintAsync(model.Email);
}
[HttpPost("email-token")]
public async Task PostEmailToken([FromBody]EmailTokenRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
2017-06-02 13:17:46 -04:00
if(user == null)
{
throw new UnauthorizedAccessException();
}
if(!await _userManager.CheckPasswordAsync(user, model.MasterPasswordHash))
2015-12-08 22:57:38 -05:00
{
await Task.Delay(2000);
throw new BadRequestException("MasterPasswordHash", "Invalid password.");
}
await _userService.InitiateEmailChangeAsync(user, model.NewEmail);
2015-12-08 22:57:38 -05:00
}
[HttpPut("email")]
[HttpPost("email")]
2017-05-03 10:12:13 -04:00
public async Task PutEmail([FromBody]EmailRequestModel model)
2015-12-08 22:57:38 -05:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
2017-06-02 13:17:46 -04:00
if(user == null)
{
throw new UnauthorizedAccessException();
}
2017-05-31 09:54:32 -04:00
var result = await _userService.ChangeEmailAsync(user, model.MasterPasswordHash, model.NewEmail,
model.NewMasterPasswordHash, model.Token, model.Key);
2015-12-08 22:57:38 -05:00
if(result.Succeeded)
{
return;
}
foreach(var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
[HttpPut("password")]
[HttpPost("password")]
2017-05-03 10:12:13 -04:00
public async Task PutPassword([FromBody]PasswordRequestModel model)
2017-05-31 09:54:32 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
2017-06-02 13:17:46 -04:00
if(user == null)
{
throw new UnauthorizedAccessException();
}
2017-05-31 09:54:32 -04:00
var result = await _userService.ChangePasswordAsync(user, model.MasterPasswordHash,
model.NewMasterPasswordHash, model.Key);
if(result.Succeeded)
{
return;
}
foreach(var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
[HttpPut("key")]
[HttpPost("key")]
public async Task PutKey([FromBody]UpdateKeyRequestModel model)
2015-12-08 22:57:38 -05:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
2017-06-02 13:17:46 -04:00
if(user == null)
{
throw new UnauthorizedAccessException();
}
2015-12-08 22:57:38 -05:00
// NOTE: It is assumed that the eventual repository call will make sure the updated
// ciphers belong to user making this call. Therefore, no check is done here.
2017-05-31 09:54:32 -04:00
var ciphers = model.Ciphers.Select(c => c.ToCipher(user.Id));
var folders = model.Folders.Select(c => c.ToFolder(user.Id));
2017-05-31 09:54:32 -04:00
var result = await _userService.UpdateKeyAsync(
user,
2015-12-08 22:57:38 -05:00
model.MasterPasswordHash,
2017-05-31 09:54:32 -04:00
model.Key,
model.PrivateKey,
2017-04-17 14:53:07 -04:00
ciphers,
2017-05-31 09:54:32 -04:00
folders);
2015-12-08 22:57:38 -05:00
if(result.Succeeded)
{
return;
}
foreach(var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
[HttpPut("security-stamp")]
[HttpPost("security-stamp")]
2015-12-08 22:57:38 -05:00
public async Task PutSecurityStamp([FromBody]SecurityStampRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
2017-06-02 13:17:46 -04:00
if(user == null)
{
throw new UnauthorizedAccessException();
}
var result = await _userService.RefreshSecurityStampAsync(user, model.MasterPasswordHash);
2015-12-08 22:57:38 -05:00
if(result.Succeeded)
{
return;
}
foreach(var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
[HttpGet("profile")]
public async Task<ProfileResponseModel> GetProfile()
2015-12-08 22:57:38 -05:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
2017-06-02 13:17:46 -04:00
if(user == null)
{
throw new UnauthorizedAccessException();
}
2017-03-25 21:53:32 -04:00
var organizationUserDetails = await _organizationUserRepository.GetManyDetailsByUserAsync(user.Id,
OrganizationUserStatusType.Confirmed);
2017-03-06 20:51:13 -05:00
var response = new ProfileResponseModel(user, organizationUserDetails);
return response;
2015-12-08 22:57:38 -05:00
}
2017-03-06 20:51:13 -05:00
[HttpGet("organizations")]
public async Task<ListResponseModel<ProfileOrganizationResponseModel>> GetOrganizations()
{
var userId = _userService.GetProperUserId(User);
2017-03-25 21:53:32 -04:00
var organizationUserDetails = await _organizationUserRepository.GetManyDetailsByUserAsync(userId.Value,
OrganizationUserStatusType.Confirmed);
2017-03-06 20:51:13 -05:00
var responseData = organizationUserDetails.Select(o => new ProfileOrganizationResponseModel(o));
return new ListResponseModel<ProfileOrganizationResponseModel>(responseData);
}
2015-12-08 22:57:38 -05:00
[HttpPut("profile")]
[HttpPost("profile")]
2015-12-08 22:57:38 -05:00
public async Task<ProfileResponseModel> PutProfile([FromBody]UpdateProfileRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
2017-06-02 13:17:46 -04:00
if(user == null)
{
throw new UnauthorizedAccessException();
}
await _userService.SaveUserAsync(model.ToUser(user));
2017-03-02 21:51:03 -05:00
var response = new ProfileResponseModel(user, null);
2015-12-08 22:57:38 -05:00
return response;
}
[HttpGet("revision-date")]
public async Task<long?> GetAccountRevisionDate()
{
var userId = _userService.GetProperUserId(User);
long? revisionDate = null;
if(userId.HasValue)
{
var date = await _userService.GetAccountRevisionDateByIdAsync(userId.Value);
revisionDate = Core.Utilities.CoreHelpers.ToEpocMilliseconds(date);
}
return revisionDate;
}
[HttpPut("keys")]
[HttpPost("keys")]
public async Task<KeysResponseModel> PutKeys([FromBody]KeysRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
2017-06-02 13:17:46 -04:00
if(user == null)
{
throw new UnauthorizedAccessException();
}
await _userService.SaveUserAsync(model.ToUser(user));
return new KeysResponseModel(user);
}
[HttpGet("keys")]
public async Task<KeysResponseModel> GetKeys()
{
var user = await _userService.GetUserByPrincipalAsync(User);
2017-06-02 13:17:46 -04:00
if(user == null)
{
throw new UnauthorizedAccessException();
}
return new KeysResponseModel(user);
}
2015-12-27 00:14:56 -05:00
[HttpPost("delete")]
public async Task PostDelete([FromBody]DeleteAccountRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
2017-06-02 13:17:46 -04:00
if(user == null)
{
throw new UnauthorizedAccessException();
}
2015-12-27 00:14:56 -05:00
if(!await _userManager.CheckPasswordAsync(user, model.MasterPasswordHash))
{
ModelState.AddModelError("MasterPasswordHash", "Invalid password.");
await Task.Delay(2000);
}
else
{
var result = await _userService.DeleteAsync(user);
if(result.Succeeded)
{
return;
}
foreach(var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
throw new BadRequestException(ModelState);
}
2015-12-08 22:57:38 -05:00
}
}