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

567 lines
19 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;
using Bit.Core.Enums;
using System.Linq;
2017-03-02 21:51:03 -05:00
using Bit.Core.Repositories;
2017-07-06 14:55:58 -04:00
using Bit.Core.Utilities;
using Bit.Core;
2017-08-11 17:06:31 -04:00
using Bit.Core.Models.Business;
using Bit.Api.Utilities;
using Bit.Core.Models.Table;
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 IUserRepository _userRepository;
private readonly ICipherRepository _cipherRepository;
private readonly IFolderRepository _folderRepository;
private readonly ICipherService _cipherService;
2017-03-06 20:51:13 -05:00
private readonly IOrganizationUserRepository _organizationUserRepository;
2017-08-11 22:55:25 -04:00
private readonly ILicensingService _licenseService;
private readonly GlobalSettings _globalSettings;
2015-12-08 22:57:38 -05:00
public AccountsController(
IUserService userService,
IUserRepository userRepository,
ICipherRepository cipherRepository,
IFolderRepository folderRepository,
ICipherService cipherService,
2017-03-06 20:51:13 -05:00
IOrganizationUserRepository organizationUserRepository,
2017-08-11 22:55:25 -04:00
ILicensingService licenseService,
GlobalSettings globalSettings)
2015-12-08 22:57:38 -05:00
{
_userService = userService;
_userRepository = userRepository;
_cipherRepository = cipherRepository;
_folderRepository = folderRepository;
_cipherService = cipherService;
2017-03-06 20:51:13 -05:00
_organizationUserRepository = organizationUserRepository;
2017-08-11 22:55:25 -04:00
_licenseService = licenseService;
_globalSettings = globalSettings;
2015-12-08 22:57:38 -05:00
}
[HttpPost("prelogin")]
[AllowAnonymous]
public async Task<PreloginResponseModel> PostPrelogin([FromBody]PreloginRequestModel model)
{
var kdfInformation = await _userRepository.GetKdfInformationByEmailAsync(model.Email);
if(kdfInformation == null)
{
throw new NotFoundException();
}
return new PreloginResponseModel(kdfInformation);
}
2015-12-08 22:57:38 -05:00
[HttpPost("register")]
[AllowAnonymous]
public async Task PostRegister([FromBody]RegisterRequestModel model)
{
var result = await _userService.RegisterUserAsync(model.ToUser(), model.MasterPasswordHash,
model.Token, model.OrganizationUserId);
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();
}
2018-04-17 08:10:17 -04:00
if(!await _userService.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
}
[HttpPost("email")]
public async Task PostEmail([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);
}
2017-07-05 15:35:46 -04:00
2017-07-01 23:20:19 -04:00
[HttpPost("verify-email")]
public async Task PostVerifyEmail()
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
await _userService.SendEmailVerificationAsync(user);
}
[HttpPost("verify-email-token")]
[AllowAnonymous]
2017-07-05 15:35:46 -04:00
public async Task PostVerifyEmailToken([FromBody]VerifyEmailRequestModel model)
2017-07-01 23:20:19 -04:00
{
2017-07-05 15:35:46 -04:00
var user = await _userService.GetUserByIdAsync(new Guid(model.UserId));
2017-07-01 23:20:19 -04:00
if(user == null)
{
throw new UnauthorizedAccessException();
}
2017-07-05 15:35:46 -04:00
var result = await _userService.ConfirmEmailAsync(user, model.Token);
if(result.Succeeded)
{
return;
}
foreach(var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
await Task.Delay(2000);
throw new BadRequestException(ModelState);
2017-07-01 23:20:19 -04:00
}
2015-12-08 22:57:38 -05:00
[HttpPost("password")]
public async Task PostPassword([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);
}
[HttpPost("kdf")]
public async Task PostKdf([FromBody]KdfRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
var result = await _userService.ChangeKdfAsync(user, model.MasterPasswordHash,
model.NewMasterPasswordHash, model.Key, model.Kdf.Value, model.KdfIterations.Value);
if(result.Succeeded)
{
return;
}
foreach(var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
2017-05-31 09:54:32 -04:00
[HttpPost("key")]
public async Task PostKey([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();
}
var existingCiphers = await _cipherRepository.GetManyByUserIdAsync(user.Id);
var ciphersDict = model.Ciphers?.ToDictionary(c => c.Id.Value);
var ciphers = new List<Cipher>();
if(existingCiphers.Any() && ciphersDict != null)
{
foreach(var cipher in existingCiphers.Where(c => ciphersDict.ContainsKey(c.Id)))
{
ciphers.Add(ciphersDict[cipher.Id].ToCipher(cipher));
}
}
2015-12-08 22:57:38 -05:00
var existingFolders = await _folderRepository.GetManyByUserIdAsync(user.Id);
var foldersDict = model.Folders?.ToDictionary(f => f.Id);
var folders = new List<Folder>();
if(existingFolders.Any() && foldersDict != null)
{
foreach(var folder in existingFolders.Where(f => foldersDict.ContainsKey(f.Id)))
{
folders.Add(foldersDict[folder.Id].ToFolder(folder));
}
}
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);
}
[HttpPost("security-stamp")]
public async Task PostSecurityStamp([FromBody]SecurityStampRequestModel 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();
}
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);
2018-08-28 17:40:08 -04:00
var response = new ProfileResponseModel(user, organizationUserDetails,
await _userService.TwoFactorIsEnabledAsync(user));
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));
var response = new ProfileResponseModel(user, null, await _userService.TwoFactorIsEnabledAsync(user));
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);
2017-07-14 09:05:15 -04:00
revisionDate = CoreHelpers.ToEpocMilliseconds(date);
}
return revisionDate;
}
[HttpPost("keys")]
public async Task<KeysResponseModel> PostKeys([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);
}
2018-07-20 13:09:50 -04:00
[HttpDelete]
2015-12-27 00:14:56 -05:00
[HttpPost("delete")]
2018-07-20 13:09:50 -04:00
public async Task Delete([FromBody]DeleteAccountRequestModel model)
2015-12-27 00:14:56 -05:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
2017-06-02 13:17:46 -04:00
if(user == null)
{
throw new UnauthorizedAccessException();
}
2018-04-17 08:10:17 -04:00
if(!await _userService.CheckPasswordAsync(user, model.MasterPasswordHash))
2015-12-27 00:14:56 -05:00
{
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);
}
2017-07-06 14:55:58 -04:00
2017-08-09 10:53:42 -04:00
[AllowAnonymous]
[HttpPost("delete-recover")]
public async Task PostDeleteRecover([FromBody]DeleteRecoverRequestModel model)
{
await _userService.SendDeleteConfirmationAsync(model.Email);
}
[HttpPost("delete-recover-token")]
[AllowAnonymous]
public async Task PostDeleteRecoverToken([FromBody]VerifyDeleteRecoverRequestModel model)
{
var user = await _userService.GetUserByIdAsync(new Guid(model.UserId));
if(user == null)
{
throw new UnauthorizedAccessException();
}
var result = await _userService.DeleteAsync(user, model.Token);
if(result.Succeeded)
{
return;
}
foreach(var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
2017-07-06 14:55:58 -04:00
[HttpPost("premium")]
2017-08-11 17:06:31 -04:00
public async Task<ProfileResponseModel> PostPremium(PremiumRequestModel model)
2017-07-06 14:55:58 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
2017-08-11 17:06:31 -04:00
var valid = model.Validate(_globalSettings);
UserLicense license = null;
2017-08-14 20:57:45 -04:00
if(valid && _globalSettings.SelfHosted)
2017-08-11 17:06:31 -04:00
{
2017-08-14 20:57:45 -04:00
license = await ApiHelpers.ReadJsonFileFromBody<UserLicense>(HttpContext, model.License);
2017-08-11 17:06:31 -04:00
}
if(!valid || (_globalSettings.SelfHosted && license == null))
2017-08-11 17:06:31 -04:00
{
throw new BadRequestException("Invalid license.");
}
await _userService.SignUpPremiumAsync(user, model.PaymentToken,
model.AdditionalStorageGb.GetValueOrDefault(0), license);
return new ProfileResponseModel(user, null, await _userService.TwoFactorIsEnabledAsync(user));
2017-07-06 14:55:58 -04:00
}
[HttpGet("billing")]
public async Task<BillingResponseModel> GetBilling()
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
if(!_globalSettings.SelfHosted && user.Gateway != null)
2017-07-06 14:55:58 -04:00
{
var paymentService = user.GetPaymentService(_globalSettings);
var billingInfo = await paymentService.GetBillingAsync(user);
var license = await _userService.GenerateLicenseAsync(user, billingInfo);
return new BillingResponseModel(user, billingInfo, license);
}
else if(!_globalSettings.SelfHosted)
{
2017-09-18 17:57:37 -04:00
var license = await _userService.GenerateLicenseAsync(user);
return new BillingResponseModel(user, license);
2017-07-06 14:55:58 -04:00
}
else
{
return new BillingResponseModel(user);
}
2017-07-06 14:55:58 -04:00
}
[HttpPost("payment")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PostPayment([FromBody]PaymentRequestModel model)
2017-07-06 14:55:58 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
await _userService.ReplacePaymentMethodAsync(user, model.PaymentToken);
}
[HttpPost("storage")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PostStorage([FromBody]StorageRequestModel model)
2017-07-06 14:55:58 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
2017-07-11 10:59:59 -04:00
await _userService.AdjustStorageAsync(user, model.StorageGbAdjustment.Value);
2017-07-06 14:55:58 -04:00
}
[HttpPost("license")]
[SelfHosted(SelfHostedOnly = true)]
public async Task PostLicense(LicenseRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
2017-08-14 20:57:45 -04:00
var license = await ApiHelpers.ReadJsonFileFromBody<UserLicense>(HttpContext, model.License);
if(license == null)
{
throw new BadRequestException("Invalid license");
}
await _userService.UpdateLicenseAsync(user, license);
}
2017-07-06 14:55:58 -04:00
[HttpPost("cancel-premium")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PostCancel()
2017-07-06 14:55:58 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
2018-12-31 13:34:02 -05:00
await _userService.CancelPremiumAsync(user);
2017-07-06 14:55:58 -04:00
}
[HttpPost("reinstate-premium")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PostReinstate()
2017-07-06 14:55:58 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if(user == null)
{
throw new UnauthorizedAccessException();
}
await _userService.ReinstatePremiumAsync(user);
}
2015-12-08 22:57:38 -05:00
}
}