refactor: return next step from security

This commit is contained in:
Polianin Nikita 2024-11-02 01:06:58 +03:00
parent 61dc0a8bc4
commit 3811d879ab
2 changed files with 5 additions and 4 deletions

View File

@ -7,6 +7,7 @@ using Mirea.Api.Dto.Common;
using Mirea.Api.Dto.Requests; using Mirea.Api.Dto.Requests;
using Mirea.Api.Endpoint.Common.Attributes; using Mirea.Api.Endpoint.Common.Attributes;
using Mirea.Api.Endpoint.Common.Exceptions; using Mirea.Api.Endpoint.Common.Exceptions;
using Mirea.Api.Endpoint.Common.MapperDto;
using Mirea.Api.Endpoint.Common.Services; using Mirea.Api.Endpoint.Common.Services;
using Mirea.Api.Endpoint.Configuration.Model; using Mirea.Api.Endpoint.Configuration.Model;
using Mirea.Api.Security.Common.Domain; using Mirea.Api.Security.Common.Domain;
@ -50,7 +51,7 @@ public class AuthController(IOptionsSnapshot<Admin> user, AuthService auth, Pass
}, },
HttpContext, request.Password); HttpContext, request.Password);
return Ok(tokenResult ? TwoFactorAuthentication.None : TwoFactorAuthentication.TotpRequired); return Ok(tokenResult.ConvertToDto());
} }
[HttpGet("Login")] [HttpGet("Login")]

View File

@ -121,7 +121,7 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I
return true; return true;
} }
public async Task<bool> LoginAsync(CookieOptionsParameters cookieOptions, User user, HttpContext context, string password, CancellationToken cancellation = default) public async Task<TwoFactorAuthenticator> LoginAsync(CookieOptionsParameters cookieOptions, User user, HttpContext context, string password, CancellationToken cancellation = default)
{ {
var requestContext = new RequestContextInfo(context, cookieOptions); var requestContext = new RequestContextInfo(context, cookieOptions);
@ -130,7 +130,7 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I
if (user.TwoFactorAuthenticator == TwoFactorAuthenticator.None) if (user.TwoFactorAuthenticator == TwoFactorAuthenticator.None)
{ {
await GenerateAuthTokensAsync(cookieOptions, context, requestContext, user.Id.ToString(), cancellation); await GenerateAuthTokensAsync(cookieOptions, context, requestContext, user.Id.ToString(), cancellation);
return true; return TwoFactorAuthenticator.None;
} }
var firstAuthToken = new FirstAuthToken(requestContext) var firstAuthToken = new FirstAuthToken(requestContext)
@ -142,7 +142,7 @@ public class AuthService(ICacheService cache, IAccessToken accessTokenService, I
await cache.SetAsync(GetFirstAuthCacheKey(requestContext.Fingerprint), firstAuthToken, absoluteExpirationRelativeToNow: LifetimeFirstAuth, cancellationToken: cancellation); await cache.SetAsync(GetFirstAuthCacheKey(requestContext.Fingerprint), firstAuthToken, absoluteExpirationRelativeToNow: LifetimeFirstAuth, cancellationToken: cancellation);
return false; return user.TwoFactorAuthenticator;
} }
public async Task RefreshTokenAsync(CookieOptionsParameters cookieOptions, HttpContext context, CancellationToken cancellation = default) public async Task RefreshTokenAsync(CookieOptionsParameters cookieOptions, HttpContext context, CancellationToken cancellation = default)