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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

899 lines
29 KiB
C#
Raw Normal View History

using Bit.Api.AdminConsole.Models.Response;
using Bit.Api.Auth.Models.Request.Accounts;
[PM-1188] Server owner auth migration (#2825) * [PM-1188] add sso project to auth * [PM-1188] move sso api models to auth * [PM-1188] fix sso api model namespace & imports * [PM-1188] move core files to auth * [PM-1188] fix core sso namespace & models * [PM-1188] move sso repository files to auth * [PM-1188] fix sso repo files namespace & imports * [PM-1188] move sso sql files to auth folder * [PM-1188] move sso test files to auth folders * [PM-1188] fix sso tests namespace & imports * [PM-1188] move auth api files to auth folder * [PM-1188] fix auth api files namespace & imports * [PM-1188] move auth core files to auth folder * [PM-1188] fix auth core files namespace & imports * [PM-1188] move auth email templates to auth folder * [PM-1188] move auth email folder back into shared directory * [PM-1188] fix auth email names * [PM-1188] move auth core models to auth folder * [PM-1188] fix auth model namespace & imports * [PM-1188] add entire Identity project to auth codeowners * [PM-1188] fix auth orm files namespace & imports * [PM-1188] move auth orm files to auth folder * [PM-1188] move auth sql files to auth folder * [PM-1188] move auth tests to auth folder * [PM-1188] fix auth test files namespace & imports * [PM-1188] move emergency access api files to auth folder * [PM-1188] fix emergencyaccess api files namespace & imports * [PM-1188] move emergency access core files to auth folder * [PM-1188] fix emergency access core files namespace & imports * [PM-1188] move emergency access orm files to auth folder * [PM-1188] fix emergency access orm files namespace & imports * [PM-1188] move emergency access sql files to auth folder * [PM-1188] move emergencyaccess test files to auth folder * [PM-1188] fix emergency access test files namespace & imports * [PM-1188] move captcha files to auth folder * [PM-1188] fix captcha files namespace & imports * [PM-1188] move auth admin files into auth folder * [PM-1188] fix admin auth files namespace & imports - configure mvc to look in auth folders for views * [PM-1188] remove extra imports and formatting * [PM-1188] fix ef auth model imports * [PM-1188] fix DatabaseContextModelSnapshot paths * [PM-1188] fix grant import in ef * [PM-1188] update sqlproj * [PM-1188] move missed sqlproj files * [PM-1188] move auth ef models out of auth folder * [PM-1188] fix auth ef models namespace * [PM-1188] remove auth ef models unused imports * [PM-1188] fix imports for auth ef models * [PM-1188] fix more ef model imports * [PM-1188] fix file encodings
2023-04-14 13:25:56 -04:00
using Bit.Api.Models.Request;
2021-12-14 15:05:07 +00:00
using Bit.Api.Models.Request.Accounts;
using Bit.Api.Models.Response;
using Bit.Api.Utilities;
using Bit.Core;
[PM-1188] Server owner auth migration (#2825) * [PM-1188] add sso project to auth * [PM-1188] move sso api models to auth * [PM-1188] fix sso api model namespace & imports * [PM-1188] move core files to auth * [PM-1188] fix core sso namespace & models * [PM-1188] move sso repository files to auth * [PM-1188] fix sso repo files namespace & imports * [PM-1188] move sso sql files to auth folder * [PM-1188] move sso test files to auth folders * [PM-1188] fix sso tests namespace & imports * [PM-1188] move auth api files to auth folder * [PM-1188] fix auth api files namespace & imports * [PM-1188] move auth core files to auth folder * [PM-1188] fix auth core files namespace & imports * [PM-1188] move auth email templates to auth folder * [PM-1188] move auth email folder back into shared directory * [PM-1188] fix auth email names * [PM-1188] move auth core models to auth folder * [PM-1188] fix auth model namespace & imports * [PM-1188] add entire Identity project to auth codeowners * [PM-1188] fix auth orm files namespace & imports * [PM-1188] move auth orm files to auth folder * [PM-1188] move auth sql files to auth folder * [PM-1188] move auth tests to auth folder * [PM-1188] fix auth test files namespace & imports * [PM-1188] move emergency access api files to auth folder * [PM-1188] fix emergencyaccess api files namespace & imports * [PM-1188] move emergency access core files to auth folder * [PM-1188] fix emergency access core files namespace & imports * [PM-1188] move emergency access orm files to auth folder * [PM-1188] fix emergency access orm files namespace & imports * [PM-1188] move emergency access sql files to auth folder * [PM-1188] move emergencyaccess test files to auth folder * [PM-1188] fix emergency access test files namespace & imports * [PM-1188] move captcha files to auth folder * [PM-1188] fix captcha files namespace & imports * [PM-1188] move auth admin files into auth folder * [PM-1188] fix admin auth files namespace & imports - configure mvc to look in auth folders for views * [PM-1188] remove extra imports and formatting * [PM-1188] fix ef auth model imports * [PM-1188] fix DatabaseContextModelSnapshot paths * [PM-1188] fix grant import in ef * [PM-1188] update sqlproj * [PM-1188] move missed sqlproj files * [PM-1188] move auth ef models out of auth folder * [PM-1188] fix auth ef models namespace * [PM-1188] remove auth ef models unused imports * [PM-1188] fix imports for auth ef models * [PM-1188] fix more ef model imports * [PM-1188] fix file encodings
2023-04-14 13:25:56 -04:00
using Bit.Core.Auth.Models.Api.Request.Accounts;
using Bit.Core.Auth.Models.Api.Response.Accounts;
using Bit.Core.Auth.Services;
using Bit.Core.Auth.Utilities;
using Bit.Core.Enums;
2021-06-30 09:35:26 +02:00
using Bit.Core.Enums.Provider;
using Bit.Core.Exceptions;
[AC-1070] Enforce master password policy on login (#2714) * [EC-1070] Add API endpoint to retrieve all policies for the current user The additional API endpoint is required to avoid forcing a full sync call before every login for master password policy enforcement on login. * [EC-1070] Add MasterPasswordPolicyData model * [EC-1070] Move PolicyResponseModel to Core project The response model is used by both the Identity and Api projects. * [EC-1070] Supply master password polices as a custom identity token response * [EC-1070] Include master password policies in 2FA token response * [EC-1070] Add response model to verify-password endpoint that includes master password policies * [AC-1070] Introduce MasterPasswordPolicyResponseModel * [AC-1070] Add policy service method to retrieve a user's master password policy * [AC-1070] User new policy service method - Update BaseRequestValidator - Update AccountsController for /verify-password endpoint - Update VerifyMasterPasswordResponseModel to accept MasterPasswordPolicyData * [AC-1070] Cleanup new policy service method - Use User object instead of Guid - Remove TODO message - Use `PolicyRepository.GetManyByTypeApplicableToUserIdAsync` instead of filtering locally * [AC-1070] Cleanup MasterPasswordPolicy models - Remove default values from both models - Add missing `RequireLower` - Fix mismatched properties in `CombineWith` method - Make properties nullable in response model * [AC-1070] Remove now un-used GET /policies endpoint * [AC-1070] Update policy service method to use GetManyByUserIdAsync * [AC-1070] Ensure existing value is not null before comparison * [AC-1070] Remove redundant VerifyMasterPasswordResponse model * [AC-1070] Fix service typo in constructor
2023-04-17 07:35:47 -07:00
using Bit.Core.Models.Api.Response;
2017-08-11 17:06:31 -04:00
using Bit.Core.Models.Business;
using Bit.Core.Models.Data;
using Bit.Core.Repositories;
using Bit.Core.Services;
using Bit.Core.Settings;
using Bit.Core.Tools.Entities;
using Bit.Core.Tools.Repositories;
using Bit.Core.Tools.Services;
using Bit.Core.Utilities;
using Bit.Core.Vault.Entities;
using Bit.Core.Vault.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
2015-12-08 22:57:38 -05:00
namespace Bit.Api.Controllers;
2022-08-29 16:06:55 -04:00
2015-12-08 22:57:38 -05:00
[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;
private readonly ICaptchaValidationService _captchaValidationService;
[AC-1070] Enforce master password policy on login (#2714) * [EC-1070] Add API endpoint to retrieve all policies for the current user The additional API endpoint is required to avoid forcing a full sync call before every login for master password policy enforcement on login. * [EC-1070] Add MasterPasswordPolicyData model * [EC-1070] Move PolicyResponseModel to Core project The response model is used by both the Identity and Api projects. * [EC-1070] Supply master password polices as a custom identity token response * [EC-1070] Include master password policies in 2FA token response * [EC-1070] Add response model to verify-password endpoint that includes master password policies * [AC-1070] Introduce MasterPasswordPolicyResponseModel * [AC-1070] Add policy service method to retrieve a user's master password policy * [AC-1070] User new policy service method - Update BaseRequestValidator - Update AccountsController for /verify-password endpoint - Update VerifyMasterPasswordResponseModel to accept MasterPasswordPolicyData * [AC-1070] Cleanup new policy service method - Use User object instead of Guid - Remove TODO message - Use `PolicyRepository.GetManyByTypeApplicableToUserIdAsync` instead of filtering locally * [AC-1070] Cleanup MasterPasswordPolicy models - Remove default values from both models - Add missing `RequireLower` - Fix mismatched properties in `CombineWith` method - Make properties nullable in response model * [AC-1070] Remove now un-used GET /policies endpoint * [AC-1070] Update policy service method to use GetManyByUserIdAsync * [AC-1070] Ensure existing value is not null before comparison * [AC-1070] Remove redundant VerifyMasterPasswordResponse model * [AC-1070] Fix service typo in constructor
2023-04-17 07:35:47 -07:00
private readonly IPolicyService _policyService;
2022-08-29 16:06:55 -04:00
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,
[AC-1070] Enforce master password policy on login (#2714) * [EC-1070] Add API endpoint to retrieve all policies for the current user The additional API endpoint is required to avoid forcing a full sync call before every login for master password policy enforcement on login. * [EC-1070] Add MasterPasswordPolicyData model * [EC-1070] Move PolicyResponseModel to Core project The response model is used by both the Identity and Api projects. * [EC-1070] Supply master password polices as a custom identity token response * [EC-1070] Include master password policies in 2FA token response * [EC-1070] Add response model to verify-password endpoint that includes master password policies * [AC-1070] Introduce MasterPasswordPolicyResponseModel * [AC-1070] Add policy service method to retrieve a user's master password policy * [AC-1070] User new policy service method - Update BaseRequestValidator - Update AccountsController for /verify-password endpoint - Update VerifyMasterPasswordResponseModel to accept MasterPasswordPolicyData * [AC-1070] Cleanup new policy service method - Use User object instead of Guid - Remove TODO message - Use `PolicyRepository.GetManyByTypeApplicableToUserIdAsync` instead of filtering locally * [AC-1070] Cleanup MasterPasswordPolicy models - Remove default values from both models - Add missing `RequireLower` - Fix mismatched properties in `CombineWith` method - Make properties nullable in response model * [AC-1070] Remove now un-used GET /policies endpoint * [AC-1070] Update policy service method to use GetManyByUserIdAsync * [AC-1070] Ensure existing value is not null before comparison * [AC-1070] Remove redundant VerifyMasterPasswordResponse model * [AC-1070] Fix service typo in constructor
2023-04-17 07:35:47 -07:00
ICaptchaValidationService captchaValidationService,
IPolicyService policyService)
2022-08-29 16:06:55 -04:00
{
_cipherRepository = cipherRepository;
_folderRepository = folderRepository;
_globalSettings = globalSettings;
2019-09-07 14:08:19 -04:00
_organizationService = organizationService;
_organizationUserRepository = organizationUserRepository;
_providerUserRepository = providerUserRepository;
_paymentService = paymentService;
_userRepository = userRepository;
_userService = userService;
_sendRepository = sendRepository;
_sendService = sendService;
_captchaValidationService = captchaValidationService;
[AC-1070] Enforce master password policy on login (#2714) * [EC-1070] Add API endpoint to retrieve all policies for the current user The additional API endpoint is required to avoid forcing a full sync call before every login for master password policy enforcement on login. * [EC-1070] Add MasterPasswordPolicyData model * [EC-1070] Move PolicyResponseModel to Core project The response model is used by both the Identity and Api projects. * [EC-1070] Supply master password polices as a custom identity token response * [EC-1070] Include master password policies in 2FA token response * [EC-1070] Add response model to verify-password endpoint that includes master password policies * [AC-1070] Introduce MasterPasswordPolicyResponseModel * [AC-1070] Add policy service method to retrieve a user's master password policy * [AC-1070] User new policy service method - Update BaseRequestValidator - Update AccountsController for /verify-password endpoint - Update VerifyMasterPasswordResponseModel to accept MasterPasswordPolicyData * [AC-1070] Cleanup new policy service method - Use User object instead of Guid - Remove TODO message - Use `PolicyRepository.GetManyByTypeApplicableToUserIdAsync` instead of filtering locally * [AC-1070] Cleanup MasterPasswordPolicy models - Remove default values from both models - Add missing `RequireLower` - Fix mismatched properties in `CombineWith` method - Make properties nullable in response model * [AC-1070] Remove now un-used GET /policies endpoint * [AC-1070] Update policy service method to use GetManyByUserIdAsync * [AC-1070] Ensure existing value is not null before comparison * [AC-1070] Remove redundant VerifyMasterPasswordResponse model * [AC-1070] Fix service typo in constructor
2023-04-17 07:35:47 -07:00
_policyService = policyService;
2015-12-08 22:57:38 -05:00
}
#region DEPRECATED (Moved to Identity Service)
[Obsolete("TDL-136 Moved to Identity (2022-01-12 cloud, 2022-09-19 self-hosted), left for backwards compatability with older clients.")]
[HttpPost("prelogin")]
[AllowAnonymous]
2015-12-08 22:57:38 -05:00
public async Task<PreloginResponseModel> PostPrelogin([FromBody] PreloginRequestModel model)
2022-08-29 16:06:55 -04:00
{
2015-12-08 22:57:38 -05:00
var kdfInformation = await _userRepository.GetKdfInformationByEmailAsync(model.Email);
if (kdfInformation == null)
2022-08-29 16:06:55 -04:00
{
2015-12-08 22:57:38 -05:00
kdfInformation = new UserKdfInformation
{
2017-06-02 13:17:46 -04:00
Kdf = KdfType.PBKDF2_SHA256,
KdfIterations = 100000,
};
2015-12-08 22:57:38 -05:00
}
return new PreloginResponseModel(kdfInformation);
2022-08-29 16:06:55 -04:00
}
2015-12-08 22:57:38 -05:00
[Obsolete("TDL-136 Moved to Identity (2022-01-12 cloud, 2022-09-19 self-hosted), left for backwards compatability with older clients.")]
2015-12-08 22:57:38 -05:00
[HttpPost("register")]
[AllowAnonymous]
[CaptchaProtected]
public async Task<RegisterResponseModel> PostRegister([FromBody] RegisterRequestModel model)
2022-08-29 16:06:55 -04:00
{
var user = model.ToUser();
var result = await _userService.RegisterUserAsync(user, model.MasterPasswordHash,
2015-12-08 22:57:38 -05:00
model.Token, model.OrganizationUserId);
if (result.Succeeded)
2015-12-08 22:57:38 -05:00
{
var captchaBypassToken = _captchaValidationService.GenerateCaptchaBypassToken(user);
return new RegisterResponseModel(captchaBypassToken);
2017-06-02 13:17:46 -04:00
}
foreach (var error in result.Errors.Where(e => e.Code != "DuplicateUserName"))
{
ModelState.AddModelError(string.Empty, error.Description);
}
2015-12-08 22:57:38 -05:00
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
2015-12-08 22:57:38 -05:00
#endregion
2022-08-29 14:53:16 -04:00
[HttpPost("password-hint")]
[AllowAnonymous]
public async Task PostPasswordHint([FromBody] PasswordHintRequestModel model)
2022-08-29 16:06:55 -04:00
{
await _userService.SendMasterPasswordHintAsync(model.Email);
2022-08-29 16:06:55 -04:00
}
[HttpPost("email-token")]
2017-07-05 15:35:46 -04:00
public async Task PostEmailToken([FromBody] EmailTokenRequestModel model)
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
throw new UnauthorizedAccessException();
2015-12-08 22:57:38 -05:00
}
if (user.UsesKeyConnector)
2015-12-08 22:57:38 -05:00
{
2017-06-02 13:17:46 -04:00
throw new BadRequestException("You cannot change your email when using Key Connector.");
}
if (!await _userService.CheckPasswordAsync(user, model.MasterPasswordHash))
{
await Task.Delay(2000);
throw new BadRequestException("MasterPasswordHash", "Invalid password.");
}
2017-05-31 09:54:32 -04:00
await _userService.InitiateEmailChangeAsync(user, model.NewEmail);
2015-12-08 22:57:38 -05:00
}
[HttpPost("email")]
2015-12-08 22:57:38 -05:00
public async Task PostEmail([FromBody] EmailRequestModel model)
2022-08-29 16:06:55 -04:00
{
2015-12-08 22:57:38 -05:00
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
2015-12-08 22:57:38 -05:00
throw new UnauthorizedAccessException();
}
if (user.UsesKeyConnector)
2022-08-29 16:06:55 -04:00
{
2015-12-08 22:57:38 -05:00
throw new BadRequestException("You cannot change your email when using Key Connector.");
}
2017-07-05 15:35:46 -04:00
2017-07-01 23:20:19 -04:00
var result = await _userService.ChangeEmailAsync(user, model.MasterPasswordHash, model.NewEmail,
2017-05-31 09:54:32 -04:00
model.NewMasterPasswordHash, model.Token, model.Key);
2017-07-01 23:20:19 -04:00
if (result.Succeeded)
{
return;
}
foreach (var error in result.Errors)
2022-08-29 16:06:55 -04:00
{
2017-07-01 23:20:19 -04:00
ModelState.AddModelError(string.Empty, error.Description);
}
await Task.Delay(2000);
2017-07-05 15:35:46 -04:00
throw new BadRequestException(ModelState);
2022-08-29 16:06:55 -04:00
}
2017-07-01 23:20:19 -04:00
[HttpPost("verify-email")]
2017-07-05 15:35:46 -04:00
public async Task PostVerifyEmail()
2022-08-29 16:06:55 -04:00
{
2017-07-05 15:35:46 -04:00
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2017-07-01 23:20:19 -04:00
{
throw new UnauthorizedAccessException();
2017-07-05 15:35:46 -04:00
}
await _userService.SendEmailVerificationAsync(user);
}
2017-07-01 23:20:19 -04:00
[HttpPost("verify-email-token")]
[AllowAnonymous]
2017-07-05 15:35:46 -04:00
public async Task PostVerifyEmailToken([FromBody] VerifyEmailRequestModel model)
2022-08-29 16:06:55 -04:00
{
2017-07-05 15:35:46 -04:00
var user = await _userService.GetUserByIdAsync(new Guid(model.UserId));
if (user == null)
2022-08-29 16:06:55 -04:00
{
2017-07-05 15:35:46 -04:00
throw new UnauthorizedAccessException();
2022-08-29 16:06:55 -04:00
}
2017-07-05 15:35:46 -04:00
var result = await _userService.ConfirmEmailAsync(user, model.Token);
if (result.Succeeded)
2022-08-29 16:06:55 -04:00
{
return;
2017-07-01 23:20:19 -04:00
}
2015-12-08 22:57:38 -05:00
foreach (var error in result.Errors)
2017-05-31 09:54:32 -04:00
{
2017-06-02 13:17:46 -04:00
ModelState.AddModelError(string.Empty, error.Description);
}
2017-05-31 09:54:32 -04:00
await Task.Delay(2000);
throw new BadRequestException(ModelState);
2017-05-31 09:54:32 -04:00
}
[HttpPost("password")]
public async Task PostPassword([FromBody] PasswordRequestModel model)
2022-08-29 16:06:55 -04:00
{
2017-05-31 09:54:32 -04:00
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
2017-05-31 09:54:32 -04:00
throw new UnauthorizedAccessException();
}
var result = await _userService.ChangePasswordAsync(user, model.MasterPasswordHash,
model.NewMasterPasswordHash, model.MasterPasswordHint, model.Key);
if (result.Succeeded)
2022-08-29 16:06:55 -04:00
{
return;
2017-05-31 09:54:32 -04:00
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
[HttpPost("set-password")]
public async Task PostSetPasswordAsync([FromBody] SetPasswordRequestModel model)
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
throw new UnauthorizedAccessException();
}
var result = await _userService.SetPasswordAsync(model.ToUser(user), model.MasterPasswordHash, model.Key,
model.OrgIdentifier);
if (result.Succeeded)
2022-08-29 16:06:55 -04:00
{
return;
}
2017-05-31 09:54:32 -04:00
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
throw new BadRequestException(ModelState);
}
[HttpPost("verify-password")]
[AC-1070] Enforce master password policy on login (#2714) * [EC-1070] Add API endpoint to retrieve all policies for the current user The additional API endpoint is required to avoid forcing a full sync call before every login for master password policy enforcement on login. * [EC-1070] Add MasterPasswordPolicyData model * [EC-1070] Move PolicyResponseModel to Core project The response model is used by both the Identity and Api projects. * [EC-1070] Supply master password polices as a custom identity token response * [EC-1070] Include master password policies in 2FA token response * [EC-1070] Add response model to verify-password endpoint that includes master password policies * [AC-1070] Introduce MasterPasswordPolicyResponseModel * [AC-1070] Add policy service method to retrieve a user's master password policy * [AC-1070] User new policy service method - Update BaseRequestValidator - Update AccountsController for /verify-password endpoint - Update VerifyMasterPasswordResponseModel to accept MasterPasswordPolicyData * [AC-1070] Cleanup new policy service method - Use User object instead of Guid - Remove TODO message - Use `PolicyRepository.GetManyByTypeApplicableToUserIdAsync` instead of filtering locally * [AC-1070] Cleanup MasterPasswordPolicy models - Remove default values from both models - Add missing `RequireLower` - Fix mismatched properties in `CombineWith` method - Make properties nullable in response model * [AC-1070] Remove now un-used GET /policies endpoint * [AC-1070] Update policy service method to use GetManyByUserIdAsync * [AC-1070] Ensure existing value is not null before comparison * [AC-1070] Remove redundant VerifyMasterPasswordResponse model * [AC-1070] Fix service typo in constructor
2023-04-17 07:35:47 -07:00
public async Task<MasterPasswordPolicyResponseModel> PostVerifyPassword([FromBody] SecretVerificationRequestModel model)
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
throw new UnauthorizedAccessException();
}
if (await _userService.CheckPasswordAsync(user, model.MasterPasswordHash))
{
[AC-1070] Enforce master password policy on login (#2714) * [EC-1070] Add API endpoint to retrieve all policies for the current user The additional API endpoint is required to avoid forcing a full sync call before every login for master password policy enforcement on login. * [EC-1070] Add MasterPasswordPolicyData model * [EC-1070] Move PolicyResponseModel to Core project The response model is used by both the Identity and Api projects. * [EC-1070] Supply master password polices as a custom identity token response * [EC-1070] Include master password policies in 2FA token response * [EC-1070] Add response model to verify-password endpoint that includes master password policies * [AC-1070] Introduce MasterPasswordPolicyResponseModel * [AC-1070] Add policy service method to retrieve a user's master password policy * [AC-1070] User new policy service method - Update BaseRequestValidator - Update AccountsController for /verify-password endpoint - Update VerifyMasterPasswordResponseModel to accept MasterPasswordPolicyData * [AC-1070] Cleanup new policy service method - Use User object instead of Guid - Remove TODO message - Use `PolicyRepository.GetManyByTypeApplicableToUserIdAsync` instead of filtering locally * [AC-1070] Cleanup MasterPasswordPolicy models - Remove default values from both models - Add missing `RequireLower` - Fix mismatched properties in `CombineWith` method - Make properties nullable in response model * [AC-1070] Remove now un-used GET /policies endpoint * [AC-1070] Update policy service method to use GetManyByUserIdAsync * [AC-1070] Ensure existing value is not null before comparison * [AC-1070] Remove redundant VerifyMasterPasswordResponse model * [AC-1070] Fix service typo in constructor
2023-04-17 07:35:47 -07:00
var policyData = await _policyService.GetMasterPasswordPolicyForUserAsync(user);
return new MasterPasswordPolicyResponseModel(policyData);
}
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)
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
throw new UnauthorizedAccessException();
}
var result = await _userService.SetKeyConnectorKeyAsync(model.ToUser(user), model.Key, model.OrgIdentifier);
if (result.Succeeded)
2022-08-29 16:06:55 -04:00
{
return;
}
foreach (var error in result.Errors)
2021-10-25 15:09:14 +02:00
{
ModelState.AddModelError(string.Empty, error.Description);
}
throw new BadRequestException(ModelState);
2021-10-25 15:09:14 +02:00
}
[HttpPost("convert-to-key-connector")]
public async Task PostConvertToKeyConnector()
2022-08-29 16:06:55 -04:00
{
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);
if (result.Succeeded)
2022-08-29 16:06:55 -04:00
{
return;
2021-10-25 15:09:14 +02:00
}
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)
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
throw new UnauthorizedAccessException();
}
var result = await _userService.ChangeKdfAsync(user, model.MasterPasswordHash,
model.NewMasterPasswordHash, model.Key, model.Kdf.Value, model.KdfIterations.Value, model.KdfMemory, model.KdfParallelism);
if (result.Succeeded)
2022-08-29 16:06:55 -04:00
{
return;
}
foreach (var error in result.Errors)
2015-12-08 22:57:38 -05:00
{
2017-06-02 13:17:46 -04:00
ModelState.AddModelError(string.Empty, error.Description);
}
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
2015-12-08 22:57:38 -05:00
[HttpPost("key")]
public async Task PostKey([FromBody] UpdateKeyRequestModel model)
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -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)));
}
var folders = new List<Folder>();
if (model.Folders.Any())
2022-08-29 16:06:55 -04:00
{
2017-05-31 09:54:32 -04:00
var existingFolders = await _folderRepository.GetManyByUserIdAsync(user.Id);
folders.AddRange(existingFolders
.Join(model.Folders, f => f.Id, f => f.Id, (existing, f) => f.ToFolder(existing)));
2022-08-29 16:06:55 -04:00
}
2015-12-08 22:57:38 -05:00
var sends = new List<Send>();
if (model.Sends?.Any() == true)
2015-12-08 22:57:38 -05:00
{
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)));
2015-12-08 22:57:38 -05:00
}
var result = await _userService.UpdateKeyAsync(
2015-12-08 22:57:38 -05:00
user,
model.MasterPasswordHash,
model.Key,
model.PrivateKey,
ciphers,
2022-08-29 16:06:55 -04:00
folders,
2015-12-08 22:57:38 -05:00
sends);
if (result.Succeeded)
2022-08-29 16:06:55 -04:00
{
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)
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
throw new UnauthorizedAccessException();
2015-12-08 22:57:38 -05:00
}
2017-03-06 20:51:13 -05:00
var result = await _userService.RefreshSecurityStampAsync(user, model.Secret);
if (result.Succeeded)
{
2017-07-14 09:05:15 -04:00
return;
2017-06-02 13:17:46 -04:00
}
foreach (var error in result.Errors)
2022-08-29 14:53:16 -04:00
{
ModelState.AddModelError(string.Empty, error.Description);
}
await Task.Delay(2000);
throw new BadRequestException(ModelState);
}
2015-12-08 22:57:38 -05:00
[HttpGet("profile")]
public async Task<ProfileResponseModel> GetProfile()
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
throw new UnauthorizedAccessException();
}
var organizationUserDetails = await _organizationUserRepository.GetManyDetailsByUserAsync(user.Id,
2017-06-02 13:17:46 -04:00
OrganizationUserStatusType.Confirmed);
var providerUserDetails = await _providerUserRepository.GetManyDetailsByUserAsync(user.Id,
ProviderUserStatusType.Confirmed);
var providerUserOrganizationDetails =
2017-06-02 13:17:46 -04:00
await _providerUserRepository.GetManyOrganizationDetailsByUserAsync(user.Id,
ProviderUserStatusType.Confirmed);
var response = new ProfileResponseModel(user, organizationUserDetails, providerUserDetails,
providerUserOrganizationDetails, await _userService.TwoFactorIsEnabledAsync(user), await _userService.HasPremiumFromOrganization(user));
return response;
}
2015-12-08 22:57:38 -05:00
[HttpGet("organizations")]
public async Task<ListResponseModel<ProfileOrganizationResponseModel>> GetOrganizations()
2022-08-29 16:06:55 -04:00
{
2017-06-02 13:17:46 -04:00
var userId = _userService.GetProperUserId(User);
var organizationUserDetails = await _organizationUserRepository.GetManyDetailsByUserAsync(userId.Value,
2017-03-25 21:53:32 -04:00
OrganizationUserStatusType.Confirmed);
2017-06-02 13:17:46 -04:00
var responseData = organizationUserDetails.Select(o => new ProfileOrganizationResponseModel(o));
return new ListResponseModel<ProfileOrganizationResponseModel>(responseData);
}
[HttpPut("profile")]
[HttpPost("profile")]
public async Task<ProfileResponseModel> PutProfile([FromBody] UpdateProfileRequestModel model)
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 14:53:16 -04:00
{
throw new UnauthorizedAccessException();
2022-08-29 14:53:16 -04:00
}
await _userService.SaveUserAsync(model.ToUser(user));
var response = new ProfileResponseModel(user, null, null, null, await _userService.TwoFactorIsEnabledAsync(user), await _userService.HasPremiumFromOrganization(user));
2017-06-02 13:17:46 -04:00
return response;
}
[HttpPut("avatar")]
[HttpPost("avatar")]
public async Task<ProfileResponseModel> PutAvatar([FromBody] UpdateAvatarRequestModel model)
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
{
throw new UnauthorizedAccessException();
}
await _userService.SaveUserAsync(model.ToUser(user), true);
var response = new ProfileResponseModel(user, null, null, null, await _userService.TwoFactorIsEnabledAsync(user), await _userService.HasPremiumFromOrganization(user));
return response;
}
[HttpGet("revision-date")]
public async Task<long?> GetAccountRevisionDate()
2022-08-29 16:06:55 -04:00
{
2015-12-27 00:14:56 -05:00
var userId = _userService.GetProperUserId(User);
long? revisionDate = null;
if (userId.HasValue)
2022-08-29 16:06:55 -04:00
{
2015-12-27 00:14:56 -05:00
var date = await _userService.GetAccountRevisionDateByIdAsync(userId.Value);
revisionDate = CoreHelpers.ToEpocMilliseconds(date);
}
2017-08-09 10:53:42 -04:00
return revisionDate;
2022-08-29 16:06:55 -04:00
}
[HttpPost("keys")]
2015-12-27 00:14:56 -05:00
public async Task<KeysResponseModel> PostKeys([FromBody] KeysRequestModel model)
2022-08-29 16:06:55 -04:00
{
2015-12-27 00:14:56 -05:00
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 14:53:16 -04:00
{
2015-12-27 00:14:56 -05:00
throw new UnauthorizedAccessException();
}
2017-07-06 14:55:58 -04:00
2017-08-09 10:53:42 -04:00
await _userService.SaveUserAsync(model.ToUser(user));
return new KeysResponseModel(user);
}
2022-08-29 14:53:16 -04:00
2015-12-27 00:14:56 -05:00
[HttpGet("keys")]
2017-08-09 10:53:42 -04:00
public async Task<KeysResponseModel> GetKeys()
2022-08-29 16:06:55 -04:00
{
2017-08-09 10:53:42 -04:00
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
2017-08-09 10:53:42 -04:00
throw new UnauthorizedAccessException();
}
return new KeysResponseModel(user);
2022-08-29 16:06:55 -04:00
}
2017-08-09 10:53:42 -04:00
[HttpDelete]
2019-09-19 08:53:33 -04:00
[HttpPost("delete")]
2017-08-09 10:53:42 -04:00
public async Task Delete([FromBody] SecretVerificationRequestModel model)
2022-08-29 16:06:55 -04:00
{
2017-08-09 10:53:42 -04:00
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2017-08-09 10:53:42 -04:00
{
throw new UnauthorizedAccessException();
}
2019-09-19 09:23:48 -04:00
if (!await _userService.VerifySecretAsync(user, model.Secret))
2022-08-29 14:53:16 -04:00
{
2017-07-05 15:35:46 -04:00
ModelState.AddModelError(string.Empty, "User verification failed.");
await Task.Delay(2000);
}
2017-08-09 10:53:42 -04:00
else
2022-08-29 16:06:55 -04:00
{
2017-08-09 10:53:42 -04:00
var result = await _userService.DeleteAsync(user);
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);
}
2019-09-19 08:53:33 -04:00
}
throw new BadRequestException(ModelState);
2022-08-29 16:06:55 -04:00
}
2017-08-09 10:53:42 -04:00
[AllowAnonymous]
[HttpPost("delete-recover")]
2015-12-08 22:57:38 -05:00
public async Task PostDeleteRecover([FromBody] DeleteRecoverRequestModel model)
2022-08-29 16:06:55 -04:00
{
await _userService.SendDeleteConfirmationAsync(model.Email);
2022-08-29 16:06:55 -04:00
}
2017-08-09 10:53:42 -04:00
[HttpPost("delete-recover-token")]
[AllowAnonymous]
public async Task PostDeleteRecoverToken([FromBody] VerifyDeleteRecoverRequestModel model)
2022-08-29 16:06:55 -04:00
{
2017-07-05 15:35:46 -04:00
var user = await _userService.GetUserByIdAsync(new Guid(model.UserId));
if (user == null)
2017-07-06 14:55:58 -04:00
{
throw new UnauthorizedAccessException();
2017-08-11 17:06:31 -04:00
}
var result = await _userService.DeleteAsync(user, model.Token);
if (result.Succeeded)
2022-08-29 14:53:16 -04:00
{
return;
2017-08-11 17:06:31 -04:00
}
foreach (var error in result.Errors)
2022-08-29 16:06:55 -04:00
{
ModelState.AddModelError(string.Empty, error.Description);
2017-07-06 14:55:58 -04:00
}
await Task.Delay(2000);
throw new BadRequestException(ModelState);
2019-02-18 15:40:47 -05:00
}
[HttpPost("iap-check")]
public async Task PostIapCheck([FromBody] IapCheckRequestModel model)
2022-08-29 16:06:55 -04:00
{
2019-02-18 15:40:47 -05:00
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
2019-02-18 15:40:47 -05:00
throw new UnauthorizedAccessException();
}
2019-09-19 08:53:33 -04:00
await _userService.IapCheckAsync(user, model.PaymentMethodType.Value);
2022-08-29 16:06:55 -04:00
}
2019-02-18 15:40:47 -05:00
[HttpPost("premium")]
public async Task<PaymentResponseModel> PostPremium(PremiumRequestModel model)
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
{
2019-02-18 15:40:47 -05:00
throw new UnauthorizedAccessException();
2022-08-29 14:53:16 -04:00
}
2017-07-06 14:55:58 -04:00
var valid = model.Validate(_globalSettings);
UserLicense license = null;
if (valid && _globalSettings.SelfHosted)
{
2017-07-06 14:55:58 -04:00
license = await ApiHelpers.ReadJsonFileFromBody<UserLicense>(HttpContext, model.License);
}
if (!valid && !_globalSettings.SelfHosted && string.IsNullOrWhiteSpace(model.Country))
2022-08-29 14:53:16 -04:00
{
throw new BadRequestException("Country is required.");
2017-07-06 14:55:58 -04:00
}
if (!valid || (_globalSettings.SelfHosted && license == null))
2017-07-06 14:55:58 -04:00
{
throw new BadRequestException("Invalid license.");
2022-08-29 16:06:55 -04:00
}
var result = await _userService.SignUpPremiumAsync(user, model.PaymentToken,
model.PaymentMethodType.Value, model.AdditionalStorageGb.GetValueOrDefault(0), license,
new TaxInfo
{
2017-07-06 14:55:58 -04:00
BillingAddressCountry = model.Country,
BillingAddressPostalCode = model.PostalCode,
2022-08-29 16:06:55 -04:00
});
2017-07-06 14:55:58 -04:00
var profile = new ProfileResponseModel(user, null, null, null, await _userService.TwoFactorIsEnabledAsync(user), await _userService.HasPremiumFromOrganization(user));
return new PaymentResponseModel
2022-08-29 16:06:55 -04:00
{
2017-07-06 14:55:58 -04:00
UserProfile = profile,
2019-02-18 15:40:47 -05:00
PaymentIntentClientSecret = result.Item2,
2017-07-06 14:55:58 -04:00
Success = result.Item1
2022-08-29 16:06:55 -04:00
};
2017-07-06 14:55:58 -04:00
}
[HttpGet("subscription")]
public async Task<SubscriptionResponseModel> GetSubscription()
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
{
throw new UnauthorizedAccessException();
}
if (!_globalSettings.SelfHosted && user.Gateway != null)
2022-08-29 16:06:55 -04:00
{
var subscriptionInfo = await _paymentService.GetSubscriptionAsync(user);
var license = await _userService.GenerateLicenseAsync(user, subscriptionInfo);
return new SubscriptionResponseModel(user, subscriptionInfo, license);
2022-08-29 16:06:55 -04:00
}
else if (!_globalSettings.SelfHosted)
2022-08-29 16:06:55 -04:00
{
var license = await _userService.GenerateLicenseAsync(user);
return new SubscriptionResponseModel(user, license);
2022-08-29 16:06:55 -04:00
}
else
{
return new SubscriptionResponseModel(user);
2017-07-06 14:55:58 -04:00
}
2022-08-29 16:06:55 -04:00
}
2017-07-06 14:55:58 -04:00
[HttpPost("payment")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PostPayment([FromBody] PaymentRequestModel model)
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
{
throw new UnauthorizedAccessException();
}
2017-08-14 20:57:45 -04:00
await _userService.ReplacePaymentMethodAsync(user, model.PaymentToken, model.PaymentMethodType.Value,
new TaxInfo
{
BillingAddressCountry = model.Country,
BillingAddressPostalCode = model.PostalCode,
});
}
[HttpPost("storage")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task<PaymentResponseModel> PostStorage([FromBody] StorageRequestModel model)
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
throw new UnauthorizedAccessException();
}
var result = await _userService.AdjustStorageAsync(user, model.StorageGbAdjustment.Value);
return new PaymentResponseModel
2017-07-06 14:55:58 -04:00
{
Success = true,
PaymentIntentClientSecret = result
};
}
[HttpPost("license")]
[SelfHosted(SelfHostedOnly = true)]
2018-12-31 13:34:02 -05:00
public async Task PostLicense(LicenseRequestModel model)
2022-08-29 16:06:55 -04:00
{
2018-12-31 13:34:02 -05:00
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
2019-02-18 15:40:47 -05:00
throw new UnauthorizedAccessException();
2017-07-06 14:55:58 -04:00
}
var license = await ApiHelpers.ReadJsonFileFromBody<UserLicense>(HttpContext, model.License);
if (license == null)
2017-07-06 14:55:58 -04:00
{
2019-02-18 15:40:47 -05:00
throw new BadRequestException("Invalid license");
2022-08-29 14:53:16 -04:00
}
await _userService.UpdateLicenseAsync(user, license);
2022-08-29 16:06:55 -04:00
}
2020-06-08 17:40:18 -04:00
[HttpPost("cancel-premium")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PostCancel()
2022-08-29 16:06:55 -04:00
{
2020-06-08 17:40:18 -04:00
var user = await _userService.GetUserByPrincipalAsync(User);
2017-07-06 14:55:58 -04:00
if (user == null)
2022-08-29 14:53:16 -04:00
{
2020-06-08 17:40:18 -04:00
throw new UnauthorizedAccessException();
2022-08-29 16:06:55 -04:00
}
2017-06-02 13:17:46 -04:00
await _userService.CancelPremiumAsync(user);
2022-08-29 14:53:16 -04:00
}
2020-06-08 17:40:18 -04:00
[HttpPost("reinstate-premium")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PostReinstate()
2022-08-29 16:06:55 -04:00
{
2020-06-08 17:40:18 -04:00
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
2020-06-08 17:40:18 -04:00
throw new UnauthorizedAccessException();
}
await _userService.ReinstatePremiumAsync(user);
2022-08-29 16:06:55 -04:00
}
[HttpGet("tax")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task<TaxInfoResponseModel> GetTaxInfo()
2022-08-29 16:06:55 -04:00
{
2020-06-08 17:40:18 -04:00
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 14:53:16 -04:00
{
throw new UnauthorizedAccessException();
}
var taxInfo = await _paymentService.GetTaxInfoAsync(user);
return new TaxInfoResponseModel(taxInfo);
2022-08-29 16:06:55 -04:00
}
[HttpPut("tax")]
[SelfHosted(NotSelfHostedOnly = true)]
public async Task PutTaxInfo([FromBody] TaxInfoUpdateRequestModel model)
2022-08-29 16:06:55 -04:00
{
2020-06-08 17:40:18 -04:00
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
throw new UnauthorizedAccessException();
2020-06-08 17:40:18 -04:00
}
var taxInfo = new TaxInfo
{
BillingAddressPostalCode = model.PostalCode,
2020-06-08 17:40:18 -04:00
BillingAddressCountry = model.Country,
2022-08-29 16:06:55 -04:00
};
await _paymentService.SaveTaxInfoAsync(user, taxInfo);
}
[HttpDelete("sso/{organizationId}")]
public async Task DeleteSsoUser(string organizationId)
2022-08-29 16:06:55 -04:00
{
var userId = _userService.GetProperUserId(User);
if (!userId.HasValue)
2022-08-29 16:06:55 -04:00
{
throw new NotFoundException();
}
await _organizationService.DeleteSsoUserAsync(userId.Value, new Guid(organizationId));
2022-08-29 16:06:55 -04:00
}
[HttpGet("sso/user-identifier")]
public async Task<string> GetSsoUserIdentifier()
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
var token = await _userService.GenerateSignInTokenAsync(user, TokenPurposes.LinkSso);
var userIdentifier = $"{user.Id},{token}";
return userIdentifier;
2022-08-29 16:06:55 -04:00
}
[HttpPost("api-key")]
public async Task<ApiKeyResponseModel> ApiKey([FromBody] SecretVerificationRequestModel model)
2022-08-29 16:06:55 -04:00
{
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)
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
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)
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
if (user == null)
2022-08-29 16:06:55 -04:00
{
throw new UnauthorizedAccessException();
}
2021-12-16 15:35:09 +01:00
var result = await _userService.UpdateTempPasswordAsync(user, model.NewMasterPasswordHash, model.Key, model.MasterPasswordHint);
if (result.Succeeded)
{
return;
}
foreach (var error in result.Errors)
2022-08-29 16:06:55 -04:00
{
ModelState.AddModelError(string.Empty, error.Description);
}
throw new BadRequestException(ModelState);
}
[HttpPost("request-otp")]
public async Task PostRequestOTP()
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
await _userService.SendOTPAsync(user);
}
[HttpPost("verify-otp")]
public async Task VerifyOTP([FromBody] VerifyOTPRequestModel model)
2022-08-29 16:06:55 -04:00
{
var user = await _userService.GetUserByPrincipalAsync(User);
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
}
}