diff --git a/Govor.API/Common/Extensions/ConfigurationProgramExtensions.cs b/Govor.API/Common/Extensions/ConfigurationProgramExtensions.cs index 8233328..4795c58 100644 --- a/Govor.API/Common/Extensions/ConfigurationProgramExtensions.cs +++ b/Govor.API/Common/Extensions/ConfigurationProgramExtensions.cs @@ -89,6 +89,7 @@ public static class ConfigurationProgramExtensions services.AddScoped(); services.AddScoped(); + services.AddScoped(); } public static void AddRepositories(this IServiceCollection services) diff --git a/Govor.API/appsettings.json b/Govor.API/appsettings.json index d01d102..7bdb15b 100644 --- a/Govor.API/appsettings.json +++ b/Govor.API/appsettings.json @@ -12,7 +12,7 @@ "AllowedHosts": "govor-team-govor-88b3.twc1.net;localhost;localhost:7155", "JwtAccessOption": { "SecretKey": "Q89eY7zP7C4+TqLmHF4kw9xkF1E8Ru4Zpg+up9wFt9g=", - "Minutes": 25 + "Minutes": 5 }, "JwtRefreshOption": { "RefreshTokenLifetimeDays": 30 diff --git a/Govor.Application/Services/UserSessions/UserSessionReader.cs b/Govor.Application/Services/UserSessions/UserSessionReader.cs index 32c9600..cd93052 100644 --- a/Govor.Application/Services/UserSessions/UserSessionReader.cs +++ b/Govor.Application/Services/UserSessions/UserSessionReader.cs @@ -23,11 +23,12 @@ public class UserSessionReader : IUserSessionReader { _logger.LogInformation($"Getting all sessions for user {userId}"); var sessions = await _repository.GetByUserIdAsync(userId); - return sessions; + + return sessions.Where(f => !f.IsRevoked).ToList() ?? []; } catch (NotFoundByKeyException ex) { - _logger.LogWarning("The user has no active sessions."); + _logger.LogWarning("The user has no sessions."); return []; } } diff --git a/Govor.Application/Services/UserSessions/UserSessionRevoker.cs b/Govor.Application/Services/UserSessions/UserSessionRevoker.cs new file mode 100644 index 0000000..6eb6450 --- /dev/null +++ b/Govor.Application/Services/UserSessions/UserSessionRevoker.cs @@ -0,0 +1,63 @@ +using Govor.Application.Interfaces.UserSession; +using Govor.Core.Repositories.UserSessionsRepository; +using Govor.Data.Repositories.Exceptions; +using Microsoft.Extensions.Logging; + +namespace Govor.Application.Services.UserSessions; + +public class UserSessionRevoker : IUserSessionRevoker +{ + private readonly IUserSessionsRepository _sessionsRepository; + private readonly ILogger _logger; + + public UserSessionRevoker(IUserSessionsRepository sessionsRepository, ILogger logger) + { + _sessionsRepository = sessionsRepository; + _logger = logger; + } + + public async Task CloseSessionByIdAsync(Guid sessionId, Guid userId) + { + try + { + var session = await _sessionsRepository.GetByIdAsync(sessionId); + + if (session.UserId != userId) + { + _logger.LogWarning("User {userId} does not belong to this session {sessionId}", userId, sessionId); + throw new UnauthorizedAccessException($"User {userId} does not belong to this session {sessionId}"); + } + + if(session.IsRevoked) return; + + session.IsRevoked = true; + await _sessionsRepository.UpdateAsync(session); + } + catch (NotFoundByKeyException ex) + { + _logger.LogWarning("User {userId} tried close unreal session!", userId); + throw new NotFoundException("Session not found", ex); + } + } + + public async Task CloseAllSessionsAsync(Guid userId) + { + try + { + var sessions = await _sessionsRepository.GetByUserIdAsync(userId); + + foreach (var session in sessions) + { + if(session.IsRevoked) continue; + + session.IsRevoked = true; + await _sessionsRepository.UpdateAsync(session); + } + } + catch (NotFoundByKeyException ex) + { + _logger.LogWarning("User {userId} has not sessions", userId); + throw new NotFoundException("Session not found", ex); + } + } +} \ No newline at end of file