| | 1 | | using LGDXRobotCloud.API.Exceptions; |
| | 2 | | using LGDXRobotCloud.Data.Entities; |
| | 3 | | using LGDXRobotCloud.Data.Models.Business.Administration; |
| | 4 | | using LGDXRobotCloud.Data.Models.Business.Identity; |
| | 5 | | using Microsoft.AspNetCore.Identity; |
| | 6 | |
|
| | 7 | | namespace LGDXRobotCloud.API.Services.Identity; |
| | 8 | |
|
| | 9 | | public interface ICurrentUserService |
| | 10 | | { |
| | 11 | | Task<LgdxUserBusinessModel> GetUserAsync(string userId); |
| | 12 | | Task<bool> UpdateUserAsync(string userId, LgdxUserUpdateBusinessModel lgdxUserBusinessModel); |
| | 13 | | Task<string> InitiateTwoFactorAsync(string userId); |
| | 14 | | Task<List<string>> EnableTwoFactorAsync(string userId, string twoFactorCode); |
| | 15 | | Task<List<string>> ResetRecoveryCodesAsync(string userId); |
| | 16 | | Task<bool> DisableTwoFactorAsync(string userId); |
| | 17 | | } |
| | 18 | |
|
| 14 | 19 | | public class CurrentUserService( |
| 14 | 20 | | UserManager<LgdxUser> userManager |
| 14 | 21 | | ) : ICurrentUserService |
| | 22 | | { |
| 14 | 23 | | private readonly UserManager<LgdxUser> _userManager = userManager ?? throw new ArgumentNullException(nameof(userManage |
| | 24 | |
|
| | 25 | | public async Task<LgdxUserBusinessModel> GetUserAsync(string userId) |
| 2 | 26 | | { |
| 2 | 27 | | var user = await _userManager.FindByIdAsync(userId) |
| 2 | 28 | | ?? throw new LgdxNotFound404Exception(); |
| | 29 | |
|
| 1 | 30 | | return new LgdxUserBusinessModel |
| 1 | 31 | | { |
| 1 | 32 | | Id = Guid.Parse(user.Id!), |
| 1 | 33 | | Name = user.Name ?? string.Empty, |
| 1 | 34 | | UserName = user.UserName ?? string.Empty, |
| 1 | 35 | | Email = user.Email ?? string.Empty, |
| 1 | 36 | | Roles = await _userManager.GetRolesAsync(user), |
| 1 | 37 | | TwoFactorEnabled = user.TwoFactorEnabled, |
| 1 | 38 | | AccessFailedCount = user.AccessFailedCount |
| 1 | 39 | | }; |
| 1 | 40 | | } |
| | 41 | |
|
| | 42 | | public async Task<bool> UpdateUserAsync(string userId, LgdxUserUpdateBusinessModel lgdxUserBusinessModel) |
| 3 | 43 | | { |
| 3 | 44 | | var user = await _userManager.FindByIdAsync(userId) |
| 3 | 45 | | ?? throw new LgdxNotFound404Exception(); |
| | 46 | |
|
| 2 | 47 | | user.Name = lgdxUserBusinessModel.Name; |
| 2 | 48 | | user.Email = lgdxUserBusinessModel.Email; |
| 2 | 49 | | var result = await _userManager.UpdateAsync(user); |
| 2 | 50 | | if (!result.Succeeded) |
| 1 | 51 | | { |
| 1 | 52 | | throw new LgdxIdentity400Expection(result.Errors); |
| | 53 | | } |
| | 54 | |
|
| 1 | 55 | | return true; |
| 1 | 56 | | } |
| | 57 | |
|
| | 58 | | public async Task<string> InitiateTwoFactorAsync(string userId) |
| 1 | 59 | | { |
| 1 | 60 | | var user = await _userManager.FindByIdAsync(userId) |
| 1 | 61 | | ?? throw new LgdxNotFound404Exception(); |
| | 62 | |
|
| 1 | 63 | | await _userManager.SetTwoFactorEnabledAsync(user, false); |
| 1 | 64 | | var key = await _userManager.GetAuthenticatorKeyAsync(user); |
| 1 | 65 | | if (string.IsNullOrEmpty(key)) |
| 0 | 66 | | { |
| 0 | 67 | | await _userManager.ResetAuthenticatorKeyAsync(user); |
| 0 | 68 | | key = await _userManager.GetAuthenticatorKeyAsync(user); |
| | 69 | |
|
| 0 | 70 | | if (string.IsNullOrEmpty(key)) |
| 0 | 71 | | { |
| 0 | 72 | | throw new NotSupportedException("The user manager must produce an authenticator key after reset."); |
| | 73 | | } |
| 0 | 74 | | } |
| 1 | 75 | | return key; |
| 1 | 76 | | } |
| | 77 | |
|
| | 78 | | public async Task<List<string>> EnableTwoFactorAsync(string userId, string twoFactorCode) |
| 4 | 79 | | { |
| 4 | 80 | | var user = await _userManager.FindByIdAsync(userId) |
| 4 | 81 | | ?? throw new LgdxNotFound404Exception(); |
| | 82 | |
|
| 3 | 83 | | if (string.IsNullOrEmpty(twoFactorCode)) |
| 1 | 84 | | { |
| 1 | 85 | | throw new LgdxValidation400Expection("RequiresTwoFactor", "The 2FA code is required."); |
| | 86 | | } |
| 2 | 87 | | if (!await _userManager.VerifyTwoFactorTokenAsync(user, _userManager.Options.Tokens.AuthenticatorTokenProvider, twoF |
| 1 | 88 | | { |
| 1 | 89 | | throw new LgdxValidation400Expection("InvalidTwoFactorCode", "The 2FA code is invalid."); |
| | 90 | | } |
| 1 | 91 | | await _userManager.SetTwoFactorEnabledAsync(user, true); |
| | 92 | |
|
| 1 | 93 | | var recoveryCodesEnumerable = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10); |
| 1 | 94 | | return recoveryCodesEnumerable?.ToList() ?? []; |
| 1 | 95 | | } |
| | 96 | |
|
| | 97 | | public async Task<List<string>> ResetRecoveryCodesAsync(string userId) |
| 2 | 98 | | { |
| 2 | 99 | | var user = await _userManager.FindByIdAsync(userId) |
| 2 | 100 | | ?? throw new LgdxNotFound404Exception(); |
| | 101 | |
|
| 1 | 102 | | var recoveryCodesEnumerable = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10); |
| 1 | 103 | | return recoveryCodesEnumerable?.ToList() ?? []; |
| 1 | 104 | | } |
| | 105 | |
|
| | 106 | | public async Task<bool> DisableTwoFactorAsync(string userId) |
| 2 | 107 | | { |
| 2 | 108 | | var user = await _userManager.FindByIdAsync(userId) |
| 2 | 109 | | ?? throw new LgdxNotFound404Exception(); |
| | 110 | |
|
| 1 | 111 | | await _userManager.ResetAuthenticatorKeyAsync(user); |
| 1 | 112 | | await _userManager.SetTwoFactorEnabledAsync(user, false); |
| 1 | 113 | | return true; |
| 1 | 114 | | } |
| | 115 | | } |