|
| 1 | +using Bit.Api.Auth.Models.Request.Accounts; |
| 2 | +using Bit.Api.Auth.Models.Request.Webauthn; |
| 3 | +using Bit.Api.Auth.Models.Response.WebAuthn; |
| 4 | +using Bit.Api.Models.Response; |
| 5 | +using Bit.Core; |
| 6 | +using Bit.Core.Auth.Models.Business.Tokenables; |
| 7 | +using Bit.Core.Auth.Repositories; |
| 8 | +using Bit.Core.Exceptions; |
| 9 | +using Bit.Core.Services; |
| 10 | +using Bit.Core.Tokens; |
| 11 | +using Bit.Core.Utilities; |
| 12 | +using Microsoft.AspNetCore.Authorization; |
| 13 | +using Microsoft.AspNetCore.Mvc; |
| 14 | + |
| 15 | +namespace Bit.Api.Auth.Controllers; |
| 16 | + |
| 17 | +[Route("webauthn")] |
| 18 | +[Authorize("Web")] |
| 19 | +[RequireFeature(FeatureFlagKeys.PasswordlessLogin)] |
| 20 | +public class WebAuthnController : Controller |
| 21 | +{ |
| 22 | + private readonly IUserService _userService; |
| 23 | + private readonly IWebAuthnCredentialRepository _credentialRepository; |
| 24 | + private readonly IDataProtectorTokenFactory<WebAuthnCredentialCreateOptionsTokenable> _createOptionsDataProtector; |
| 25 | + |
| 26 | + public WebAuthnController( |
| 27 | + IUserService userService, |
| 28 | + IWebAuthnCredentialRepository credentialRepository, |
| 29 | + IDataProtectorTokenFactory<WebAuthnCredentialCreateOptionsTokenable> createOptionsDataProtector) |
| 30 | + { |
| 31 | + _userService = userService; |
| 32 | + _credentialRepository = credentialRepository; |
| 33 | + _createOptionsDataProtector = createOptionsDataProtector; |
| 34 | + } |
| 35 | + |
| 36 | + [HttpGet("")] |
| 37 | + public async Task<ListResponseModel<WebAuthnCredentialResponseModel>> Get() |
| 38 | + { |
| 39 | + var user = await GetUserAsync(); |
| 40 | + var credentials = await _credentialRepository.GetManyByUserIdAsync(user.Id); |
| 41 | + |
| 42 | + return new ListResponseModel<WebAuthnCredentialResponseModel>(credentials.Select(c => new WebAuthnCredentialResponseModel(c))); |
| 43 | + } |
| 44 | + |
| 45 | + [HttpPost("options")] |
| 46 | + public async Task<WebAuthnCredentialCreateOptionsResponseModel> PostOptions([FromBody] SecretVerificationRequestModel model) |
| 47 | + { |
| 48 | + var user = await VerifyUserAsync(model); |
| 49 | + var options = await _userService.StartWebAuthnLoginRegistrationAsync(user); |
| 50 | + |
| 51 | + var tokenable = new WebAuthnCredentialCreateOptionsTokenable(user, options); |
| 52 | + var token = _createOptionsDataProtector.Protect(tokenable); |
| 53 | + |
| 54 | + return new WebAuthnCredentialCreateOptionsResponseModel |
| 55 | + { |
| 56 | + Options = options, |
| 57 | + Token = token |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + [HttpPost("")] |
| 62 | + public async Task Post([FromBody] WebAuthnCredentialRequestModel model) |
| 63 | + { |
| 64 | + var user = await GetUserAsync(); |
| 65 | + var tokenable = _createOptionsDataProtector.Unprotect(model.Token); |
| 66 | + if (!tokenable.TokenIsValid(user)) |
| 67 | + { |
| 68 | + throw new BadRequestException("The token associated with your request is expired. A valid token is required to continue."); |
| 69 | + } |
| 70 | + |
| 71 | + var success = await _userService.CompleteWebAuthLoginRegistrationAsync(user, model.Name, tokenable.Options, model.DeviceResponse); |
| 72 | + if (!success) |
| 73 | + { |
| 74 | + throw new BadRequestException("Unable to complete WebAuthn registration."); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + [HttpPost("{id}/delete")] |
| 79 | + public async Task Delete(Guid id, [FromBody] SecretVerificationRequestModel model) |
| 80 | + { |
| 81 | + var user = await VerifyUserAsync(model); |
| 82 | + var credential = await _credentialRepository.GetByIdAsync(id, user.Id); |
| 83 | + if (credential == null) |
| 84 | + { |
| 85 | + throw new NotFoundException("Credential not found."); |
| 86 | + } |
| 87 | + |
| 88 | + await _credentialRepository.DeleteAsync(credential); |
| 89 | + } |
| 90 | + |
| 91 | + private async Task<Core.Entities.User> GetUserAsync() |
| 92 | + { |
| 93 | + var user = await _userService.GetUserByPrincipalAsync(User); |
| 94 | + if (user == null) |
| 95 | + { |
| 96 | + throw new UnauthorizedAccessException(); |
| 97 | + } |
| 98 | + return user; |
| 99 | + } |
| 100 | + |
| 101 | + private async Task<Core.Entities.User> VerifyUserAsync(SecretVerificationRequestModel model) |
| 102 | + { |
| 103 | + var user = await GetUserAsync(); |
| 104 | + if (!await _userService.VerifySecretAsync(user, model.Secret)) |
| 105 | + { |
| 106 | + await Task.Delay(Constants.FailedSecretVerificationDelay); |
| 107 | + throw new BadRequestException(string.Empty, "User verification failed."); |
| 108 | + } |
| 109 | + |
| 110 | + return user; |
| 111 | + } |
| 112 | +} |
0 commit comments