| | 1 | | using System.Security.Claims; |
| | 2 | | using LGDXRobotCloud.API.Services.Identity; |
| | 3 | | using LGDXRobotCloud.Data.Models.Business.Administration; |
| | 4 | | using LGDXRobotCloud.Data.Models.Business.Identity; |
| | 5 | | using LGDXRobotCloud.Data.Models.DTOs.V1.Commands; |
| | 6 | | using LGDXRobotCloud.Data.Models.DTOs.V1.Requests; |
| | 7 | | using LGDXRobotCloud.Data.Models.DTOs.V1.Responses; |
| | 8 | | using LGDXRobotCloud.Utilities.Constants; |
| | 9 | | using Microsoft.AspNetCore.Authentication.JwtBearer; |
| | 10 | | using Microsoft.AspNetCore.Authorization; |
| | 11 | | using Microsoft.AspNetCore.Mvc; |
| | 12 | |
|
| | 13 | | namespace LGDXRobotCloud.API.Areas.Identity.Controllers; |
| | 14 | |
|
| | 15 | | [ApiController] |
| | 16 | | [Area("Identity")] |
| | 17 | | [Route("[area]/[controller]")] |
| | 18 | | [Authorize(AuthenticationSchemes = LgdxRobotCloudAuthenticationSchemes.ApiKeyOrCertificationScheme)] |
| | 19 | | [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] |
| 0 | 20 | | public sealed class UserController( |
| 0 | 21 | | IAuthService authService, |
| 0 | 22 | | ICurrentUserService currentUserService |
| 0 | 23 | | ) : ControllerBase |
| | 24 | | { |
| 0 | 25 | | private readonly IAuthService _authService = authService ?? throw new ArgumentNullException(nameof(authService)); |
| 0 | 26 | | private readonly ICurrentUserService _currentUserService = currentUserService ?? throw new ArgumentNullException(nameo |
| | 27 | |
|
| | 28 | | [HttpGet("")] |
| | 29 | | [ProducesResponseType(typeof(LgdxUserDto), StatusCodes.Status200OK)] |
| | 30 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 31 | | public async Task<ActionResult<LgdxUserDto>> GetUser() |
| 0 | 32 | | { |
| 0 | 33 | | var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; |
| 0 | 34 | | var user = await _currentUserService.GetUserAsync(userId!); |
| 0 | 35 | | return Ok(user.ToDto()); |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | [HttpPut("")] |
| | 39 | | [ProducesResponseType(typeof(LgdxUserDto), StatusCodes.Status204NoContent)] |
| | 40 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 41 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 42 | | public async Task<ActionResult> UpdateUser(LgdxUserUpdateDto lgdxUserUpdateDto) |
| 0 | 43 | | { |
| 0 | 44 | | var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; |
| 0 | 45 | | await _currentUserService.UpdateUserAsync(userId!, lgdxUserUpdateDto.ToBusinessModel()); |
| 0 | 46 | | return NoContent(); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | [HttpPost("Password")] |
| | 50 | | [ProducesResponseType(typeof(LgdxUserDto), StatusCodes.Status204NoContent)] |
| | 51 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 52 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 53 | | public async Task<ActionResult> UpdatePassword(UpdatePasswordRequestDto updatePasswordRequestDto) |
| 0 | 54 | | { |
| 0 | 55 | | var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; |
| 0 | 56 | | await _authService.UpdatePasswordAsync(userId!, updatePasswordRequestDto.ToBusinessModel()); |
| 0 | 57 | | return NoContent(); |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | [HttpPost("2FA/Initiate")] |
| | 61 | | [ProducesResponseType(typeof(InitiateTwoFactorResponseDto), StatusCodes.Status200OK)] |
| | 62 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 63 | | public async Task<ActionResult<InitiateTwoFactorResponseDto>> InitiateTwoFactor() |
| 0 | 64 | | { |
| 0 | 65 | | var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; |
| 0 | 66 | | var respond = await _currentUserService.InitiateTwoFactorAsync(userId!); |
| 0 | 67 | | return Ok(new InitiateTwoFactorResponseDto { |
| 0 | 68 | | SharedKey = respond |
| 0 | 69 | | }); |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | [HttpPost("2FA/Enable")] |
| | 73 | | [ProducesResponseType(typeof(EnableTwoFactorRespondDto), StatusCodes.Status200OK)] |
| | 74 | | [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] |
| | 75 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 76 | | public async Task<ActionResult<EnableTwoFactorRespondDto>> EnableTwoFactor(EnableTwoFactorRequestDto enableTwoFactorRe |
| 0 | 77 | | { |
| 0 | 78 | | var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; |
| 0 | 79 | | var respond = await _currentUserService.EnableTwoFactorAsync(userId!, enableTwoFactorRequestDto.TwoFactorCode); |
| 0 | 80 | | return Ok(new EnableTwoFactorRespondDto { |
| 0 | 81 | | RecoveryCodes = respond |
| 0 | 82 | | }); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | [HttpPost("2FA/Reset")] |
| | 86 | | [ProducesResponseType(typeof(ResetRecoveryCodesRespondDto), StatusCodes.Status200OK)] |
| | 87 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 88 | | public async Task<ActionResult<ResetRecoveryCodesRespondDto>> ResetRecoveryCodes() |
| 0 | 89 | | { |
| 0 | 90 | | var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; |
| 0 | 91 | | var respond = await _currentUserService.ResetRecoveryCodesAsync(userId!); |
| 0 | 92 | | return Ok(new ResetRecoveryCodesRespondDto { |
| 0 | 93 | | RecoveryCodes = respond |
| 0 | 94 | | }); |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | [HttpPost("2FA/Disable")] |
| | 98 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | 99 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | 100 | | public async Task<ActionResult> DisableTwoFactor() |
| 0 | 101 | | { |
| 0 | 102 | | var userId = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value; |
| 0 | 103 | | await _currentUserService.DisableTwoFactorAsync(userId!); |
| 0 | 104 | | return Ok(); |
| 0 | 105 | | } |
| | 106 | | } |