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

884 lines
30 KiB
C#
Raw Normal View History

using Bit.Api.Utilities;
using Bit.Core;
using Bit.Core.Enums;
using Bit.Core.Exceptions;
using Bit.Core.Models.Api;
using Bit.Core.Models.Api.Request.Accounts;
2017-08-11 17:06:31 -04:00
using Bit.Core.Models.Business;
using Bit.Core.Models.Data;
using Bit.Core.Models.Table;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Core.Settings;
using Bit.Core.Utilities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
2021-06-30 09:35:26 +02:00
using Bit.Core.Enums.Provider;
2015-12-08 22:57:38 -05:00
namespace Bit.Api.Controllers
{
[Route("accounts")]
[Authorize("Application")]
public class AccountsController : Controller
{
private readonly GlobalSettings _globalSettings;
private readonly ICipherRepository _cipherRepository;
private readonly IFolderRepository _folderRepository;
private readonly IOrganizationService _organizationService;
2017-03-06 20:51:13 -05:00
private readonly IOrganizationUserRepository _organizationUserRepository;
2021-06-30 09:35:26 +02:00
private readonly IProviderUserRepository _providerUserRepository;
2019-02-08 23:53:09 -05:00
private readonly IPaymentService _paymentService;
private readonly IUserRepository _userRepository;
private readonly IUserService _userService;
private readonly ISendRepository _sendRepository;
private readonly ISendService _sendService;
2015-12-08 22:57:38 -05:00
public AccountsController(
GlobalSettings globalSettings,
ICipherRepository cipherRepository,
IFolderRepository folderRepository,
IOrganizationService organizationService,
2017-03-06 20:51:13 -05:00
IOrganizationUserRepository organizationUserRepository,
2021-06-30 09:35:26 +02:00
IProviderUserRepository providerUserRepository,
2019-02-08 23:53:09 -05:00
IPaymentService paymentService,
IUserRepository userRepository,
IUserService userService,
ISendRepository sendRepository,
ISendService sendService)
2015-12-08 22:57:38 -05:00
{
_cipherRepository = cipherRepository;
_folderRepository = folderRepository;
_globalSettings = globalSettings;
_organizationService = organizationService;
2017-03-06 20:51:13 -05:00
_organizationUserRepository = organizationUserRepository;
2021-06-30 09:35:26 +02:00
_providerUserRepository = providerUserRepository;
2019-02-08 23:53:09 -05:00
_paymentService = paymentService;
_userRepository = userRepository;
_userService = userService;
_sendRepository = sendRepository;
_sendService = sendService;
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)
{
2019-09-07 14:08:19 -04:00
kdfInformation = new UserKdfInformation
{
Kdf = KdfType.PBKDF2_SHA256,
KdfIterations = 100000
};
}
return new PreloginResponseModel(kdfInformation);
}
2015-12-08 22:57:38 -05:00
[HttpPost("register")]
[AllowAnonymous]
[CaptchaProtected]
2015-12-08 22:57:38 -05:00
public async Task PostRegister([FromBody]RegisterRequestModel model)
{
var result = await _userService.RegisterUserAsync(model.ToUser(), model.MasterPasswordHash,
model.Token, model.OrganizationUserId);
if (result.Succeeded)
2015-12-08 22:57:38 -05:00
{
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);
if (user == null)
2017-06-02 13:17:46 -04:00
{
throw new UnauthorizedAccessException();
}
if (user.UsesKeyConnector)
{
throw new BadRequestException("You cannot change your email when using Key Connector.");
}
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);
if (user == null)
2017-06-02 13:17:46 -04:00
{
throw new UnauthorizedAccessException();
}
if (user.UsesKeyConnector)
{
throw new BadRequestException("You cannot change your email when using Key Connector.");
}
2017-05-31 09:54:32 -04:00
var result = await _userService.ChangeEmailAsync(user, model.MasterPasswordHash, model.NewEmail,
model.NewMasterPasswordHash, model.Token, model.Key);
if (result.Succeeded)
2015-12-08 22:57:38 -05:00
{
return;
}
foreach (var error in result.Errors)
2015-12-08 22:57:38 -05:00
{
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)
2017-07-01 23:20:19 -04:00
{
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));
if (user == null)
2017-07-01 23:20:19 -04:00
{
throw new UnauthorizedAccessException();
}
2017-07-05 15:35:46 -04:00
var result = await _userService.ConfirmEmailAsync(user, model.Token);
if (result.Succeeded)
2017-07-05 15:35:46 -04:00
{
return;
}
foreach (var error in result.Errors)
2017-07-05 15:35:46 -04:00
{
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);
if (user == null)
2017-06-02 13:17:46 -04:00
{
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)
2017-05-31 09:54:32 -04:00
{
return;
}
foreach (var error in result.Errors)
2017-05-31 09:54:32 -04:00
{
ModelState.AddModelError(string.Empty, error.Description);
}
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
[HttpPost("set-password")]
public async Task PostSetPasswordAsync([FromBody]SetPasswordRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
{
throw new UnauthorizedAccessException();
}
var result = await _userService.SetPasswordAsync(model.ToUser(user), model.MasterPasswordHash, model.Key,
model.OrgIdentifier);
if (result.Succeeded)
{
return;
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
throw new BadRequestException(ModelState);
}
2017-05-31 09:54:32 -04:00
[HttpPost("verify-password")]
public async Task PostVerifyPassword([FromBody]SecretVerificationRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
{
throw new UnauthorizedAccessException();
}
if (await _userService.CheckPasswordAsync(user, model.MasterPasswordHash))
{
return;
}
ModelState.AddModelError(nameof(model.MasterPasswordHash), "Invalid password.");
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
[HttpPost("set-key-connector-key")]
public async Task PostSetKeyConnectorKeyAsync([FromBody]SetKeyConnectorKeyRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
{
throw new UnauthorizedAccessException();
}
var result = await _userService.SetKeyConnectorKeyAsync(model.ToUser(user), model.Key, model.OrgIdentifier);
if (result.Succeeded)
{
return;
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
throw new BadRequestException(ModelState);
}
[HttpPost("convert-to-key-connector")]
public async Task PostConvertToKeyConnector()
2021-10-25 15:09:14 +02:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
{
throw new UnauthorizedAccessException();
}
var result = await _userService.ConvertToKeyConnectorAsync(user);
2021-10-25 15:09:14 +02:00
if (result.Succeeded)
{
return;
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
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);
if (user == null)
2017-06-02 13:17:46 -04:00
{
throw new UnauthorizedAccessException();
}
var ciphers = new List<Cipher>();
if (model.Ciphers.Any())
{
var existingCiphers = await _cipherRepository.GetManyByUserIdAsync(user.Id);
ciphers.AddRange(existingCiphers
.Join(model.Ciphers, c => c.Id, c => c.Id, (existing, c) => c.ToCipher(existing)));
}
2015-12-08 22:57:38 -05:00
var folders = new List<Folder>();
if (model.Folders.Any())
{
var existingFolders = await _folderRepository.GetManyByUserIdAsync(user.Id);
folders.AddRange(existingFolders
.Join(model.Folders, f => f.Id, f => f.Id, (existing, f) => f.ToFolder(existing)));
}
var sends = new List<Send>();
if (model.Sends?.Any() == true)
{
var existingSends = await _sendRepository.GetManyByUserIdAsync(user.Id);
sends.AddRange(existingSends
.Join(model.Sends, s => s.Id, s => s.Id, (existing, s) => s.ToSend(existing, _sendService)));
}
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,
folders,
sends);
2015-12-08 22:57:38 -05:00
if (result.Succeeded)
2015-12-08 22:57:38 -05:00
{
return;
}
foreach (var error in result.Errors)
2015-12-08 22:57:38 -05:00
{
ModelState.AddModelError(string.Empty, error.Description);
}
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
[HttpPost("security-stamp")]
public async Task PostSecurityStamp([FromBody]SecretVerificationRequestModel model)
2015-12-08 22:57:38 -05:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2017-06-02 13:17:46 -04:00
{
throw new UnauthorizedAccessException();
}
var result = await _userService.RefreshSecurityStampAsync(user, model.Secret);
if (result.Succeeded)
2015-12-08 22:57:38 -05:00
{
return;
}
foreach (var error in result.Errors)
2015-12-08 22:57:38 -05:00
{
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);
if (user == null)
2017-06-02 13:17:46 -04:00
{
throw new UnauthorizedAccessException();
}
2017-03-25 21:53:32 -04:00
var organizationUserDetails = await _organizationUserRepository.GetManyDetailsByUserAsync(user.Id,
OrganizationUserStatusType.Confirmed);
2021-06-30 09:35:26 +02:00
var providerUserDetails = await _providerUserRepository.GetManyDetailsByUserAsync(user.Id,
ProviderUserStatusType.Confirmed);
var providerUserOrganizationDetails =
await _providerUserRepository.GetManyOrganizationDetailsByUserAsync(user.Id,
ProviderUserStatusType.Confirmed);
2021-06-30 09:35:26 +02:00
var response = new ProfileResponseModel(user, organizationUserDetails, providerUserDetails,
providerUserOrganizationDetails, 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);
if (user == null)
2017-06-02 13:17:46 -04:00
{
throw new UnauthorizedAccessException();
}
await _userService.SaveUserAsync(model.ToUser(user));
var response = new ProfileResponseModel(user, null, null, 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);
if (user == null)
2017-06-02 13:17:46 -04:00
{
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);
if (user == null)
2017-06-02 13:17:46 -04:00
{
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")]
public async Task Delete([FromBody]SecretVerificationRequestModel model)
2015-12-27 00:14:56 -05:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2017-06-02 13:17:46 -04:00
{
throw new UnauthorizedAccessException();
}
if (!await _userService.VerifySecretAsync(user, model.Secret))
2015-12-27 00:14:56 -05:00
{
ModelState.AddModelError(string.Empty, "User verification failed.");
2015-12-27 00:14:56 -05:00
await Task.Delay(2000);
}
else
{
var result = await _userService.DeleteAsync(user);
if (result.Succeeded)
2015-12-27 00:14:56 -05:00
{
return;
}
foreach (var error in result.Errors)
2015-12-27 00:14:56 -05:00
{
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)
2017-08-09 10:53:42 -04:00
{
throw new UnauthorizedAccessException();
}
var result = await _userService.DeleteAsync(user, model.Token);
if (result.Succeeded)
2017-08-09 10:53:42 -04:00
{
return;
}
foreach (var error in result.Errors)
2017-08-09 10:53:42 -04:00
{
ModelState.AddModelError(string.Empty, error.Description);
}
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
2019-09-19 08:53:33 -04:00
[HttpPost("iap-check")]
2019-09-19 09:23:48 -04:00
public async Task PostIapCheck([FromBody]IapCheckRequestModel model)
2019-09-19 08:53:33 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2019-09-19 08:53:33 -04:00
{
throw new UnauthorizedAccessException();
}
await _userService.IapCheckAsync(user, model.PaymentMethodType.Value);
}
2017-07-06 14:55:58 -04:00
[HttpPost("premium")]
public async Task<PaymentResponseModel> PostPremium(PremiumRequestModel model)
2017-07-06 14:55:58 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2017-07-06 14:55:58 -04:00
{
throw new UnauthorizedAccessException();
}
2017-08-11 17:06:31 -04:00
var valid = model.Validate(_globalSettings);
UserLicense license = null;
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 && string.IsNullOrWhiteSpace(model.Country))
{
throw new BadRequestException("Country is required.");
}
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.");
}
var result = await _userService.SignUpPremiumAsync(user, model.PaymentToken,
model.PaymentMethodType.Value, model.AdditionalStorageGb.GetValueOrDefault(0), license,
new TaxInfo
{
BillingAddressCountry = model.Country,
BillingAddressPostalCode = model.PostalCode,
});
var profile = new ProfileResponseModel(user, null, null, null, await _userService.TwoFactorIsEnabledAsync(user));
return new PaymentResponseModel
{
UserProfile = profile,
PaymentIntentClientSecret = result.Item2,
Success = result.Item1
};
2017-07-06 14:55:58 -04:00
}
[HttpGet("billing")]
2019-02-18 15:40:47 -05:00
[SelfHosted(NotSelfHostedOnly = true)]
2017-07-06 14:55:58 -04:00
public async Task<BillingResponseModel> GetBilling()
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2017-07-06 14:55:58 -04:00
{
throw new UnauthorizedAccessException();
}
2019-02-18 15:40:47 -05:00
var billingInfo = await _paymentService.GetBillingAsync(user);
return new BillingResponseModel(billingInfo);
}
[HttpGet("subscription")]
public async Task<SubscriptionResponseModel> GetSubscription()
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2019-02-18 15:40:47 -05:00
{
throw new UnauthorizedAccessException();
}
if (!_globalSettings.SelfHosted && user.Gateway != null)
2017-07-06 14:55:58 -04:00
{
2019-02-18 15:40:47 -05:00
var subscriptionInfo = await _paymentService.GetSubscriptionAsync(user);
var license = await _userService.GenerateLicenseAsync(user, subscriptionInfo);
return new SubscriptionResponseModel(user, subscriptionInfo, license);
}
else if (!_globalSettings.SelfHosted)
{
2017-09-18 17:57:37 -04:00
var license = await _userService.GenerateLicenseAsync(user);
2019-02-18 15:40:47 -05:00
return new SubscriptionResponseModel(user, license);
2017-07-06 14:55:58 -04:00
}
else
{
2019-02-18 15:40:47 -05:00
return new SubscriptionResponseModel(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)
2017-07-06 14:55:58 -04:00
{
throw new UnauthorizedAccessException();
}
await _userService.ReplacePaymentMethodAsync(user, model.PaymentToken, model.PaymentMethodType.Value,
new TaxInfo
{
BillingAddressCountry = model.Country,
BillingAddressPostalCode = model.PostalCode,
});
2017-07-06 14:55:58 -04:00
}
[HttpPost("storage")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task<PaymentResponseModel> PostStorage([FromBody]StorageRequestModel model)
2017-07-06 14:55:58 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2017-07-06 14:55:58 -04:00
{
throw new UnauthorizedAccessException();
}
var result = await _userService.AdjustStorageAsync(user, model.StorageGbAdjustment.Value);
return new PaymentResponseModel
{
Success = true,
PaymentIntentClientSecret = result
};
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)
2017-07-06 14:55:58 -04:00
{
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)
2017-07-06 14:55:58 -04:00
{
throw new UnauthorizedAccessException();
}
await _userService.ReinstatePremiumAsync(user);
}
2020-06-08 17:40:18 -04:00
[HttpGet("tax")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task<TaxInfoResponseModel> GetTaxInfo()
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
{
throw new UnauthorizedAccessException();
}
var taxInfo = await _paymentService.GetTaxInfoAsync(user);
return new TaxInfoResponseModel(taxInfo);
}
2020-06-08 17:40:18 -04:00
[HttpPut("tax")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PutTaxInfo([FromBody]TaxInfoUpdateRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
{
throw new UnauthorizedAccessException();
}
var taxInfo = new TaxInfo
{
BillingAddressPostalCode = model.PostalCode,
BillingAddressCountry = model.Country,
};
await _paymentService.SaveTaxInfoAsync(user, taxInfo);
}
[HttpDelete("sso/{organizationId}")]
public async Task DeleteSsoUser(string organizationId)
{
var userId = _userService.GetProperUserId(User);
if (!userId.HasValue)
{
throw new NotFoundException();
}
await _organizationService.DeleteSsoUserAsync(userId.Value, new Guid(organizationId));
}
[HttpGet("sso/user-identifier")]
public async Task<string> GetSsoUserIdentifier()
{
var user = await _userService.GetUserByPrincipalAsync(User);
var token = await _userService.GenerateSignInTokenAsync(user, TokenPurposes.LinkSso);
var userIdentifier = $"{user.Id},{token}";
return userIdentifier;
}
[HttpPost("api-key")]
public async Task<ApiKeyResponseModel> ApiKey([FromBody]SecretVerificationRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
{
throw new UnauthorizedAccessException();
}
if (!await _userService.VerifySecretAsync(user, model.Secret))
{
await Task.Delay(2000);
throw new BadRequestException(string.Empty, "User verification failed.");
}
return new ApiKeyResponseModel(user);
}
[HttpPost("rotate-api-key")]
public async Task<ApiKeyResponseModel> RotateApiKey([FromBody]SecretVerificationRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
{
throw new UnauthorizedAccessException();
}
if (!await _userService.VerifySecretAsync(user, model.Secret))
{
await Task.Delay(2000);
throw new BadRequestException(string.Empty, "User verification failed.");
}
await _userService.RotateApiKeyAsync(user);
var response = new ApiKeyResponseModel(user);
return response;
}
[HttpPut("update-temp-password")]
public async Task PutUpdateTempPasswordAsync([FromBody]UpdateTempPasswordRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
{
throw new UnauthorizedAccessException();
}
var result = await _userService.UpdateTempPasswordAsync(user, model.NewMasterPasswordHash, model.Key, model.MasterPasswordHint);
if (result.Succeeded)
{
return;
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
throw new BadRequestException(ModelState);
}
[HttpPost("request-otp")]
public async Task PostRequestOTP()
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user is not { UsesKeyConnector: true })
{
throw new UnauthorizedAccessException();
}
await _userService.SendOTPAsync(user);
}
[HttpPost("verify-otp")]
public async Task VerifyOTP([FromBody]VerifyOTPRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user is not { UsesKeyConnector: true })
{
throw new UnauthorizedAccessException();
}
if (!await _userService.VerifyOTPAsync(user, model.OTP))
{
await Task.Delay(2000);
throw new BadRequestException("Token", "Invalid token");
}
}
2015-12-08 22:57:38 -05:00
}
}