Repository navigation
fix(api): harden auth/session surface (#243) #334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8579504
fix(api): harden auth/session surface (#243)
thomasluizon 5658611
fix(api): address review — inline refresh-token format gate + revoke-…
thomasluizon b2b72e0
Merge remote-tracking branch 'origin/main' into fix/auth-session-hard…
thomasluizon 89d2dab
fix(api): gate refresh rate-limit partition on a real session lookup …
thomasluizon fc6d09a
Merge remote-tracking branch 'origin/main' into fix/auth-session-hard…
thomasluizon 9f1fa62
fix(api): make logout-all concurrency-resilient + scope refresh parti…
thomasluizon 8f7c094
Merge remote-tracking branch 'origin/main' into fix/auth-session-hard…
thomasluizon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/Orbit.Application/Auth/Commands/LogoutAllSessionsCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using MediatR; | ||
| using Orbit.Domain.Common; | ||
| using Orbit.Domain.Interfaces; | ||
|
|
||
| namespace Orbit.Application.Auth.Commands; | ||
|
|
||
| public record LogoutAllSessionsCommand(Guid UserId) : IRequest<Result>; | ||
|
|
||
| public class LogoutAllSessionsCommandHandler(IAuthSessionService authSessionService) | ||
| : IRequestHandler<LogoutAllSessionsCommand, Result> | ||
| { | ||
| public Task<Result> Handle(LogoutAllSessionsCommand request, CancellationToken cancellationToken) | ||
| { | ||
| return authSessionService.RevokeAllSessionsAsync(request.UserId, cancellationToken); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/Orbit.Application/Auth/Validators/LogoutAllSessionsCommandValidator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| using FluentValidation; | ||
| using Orbit.Application.Auth.Commands; | ||
|
|
||
| namespace Orbit.Application.Auth.Validators; | ||
|
|
||
| public class LogoutAllSessionsCommandValidator : AbstractValidator<LogoutAllSessionsCommand> | ||
| { | ||
| public LogoutAllSessionsCommandValidator() | ||
| { | ||
| RuleFor(x => x.UserId) | ||
| .NotEmpty(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/Orbit.Application/Auth/Validators/RefreshTokenRules.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| using FluentValidation; | ||
|
|
||
| namespace Orbit.Application.Auth.Validators; | ||
|
|
||
| public static class RefreshTokenRules | ||
| { | ||
| public const int TokenLength = 128; | ||
| public const string TokenPattern = "^[0-9A-F]+$"; | ||
|
|
||
| public static void AddRefreshTokenRules<T>(IRuleBuilder<T, string> rule) | ||
| { | ||
| rule | ||
| .NotEmpty() | ||
| .Length(TokenLength) | ||
| .Matches(TokenPattern) | ||
| .WithMessage("Refresh token format is invalid."); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
tests/Orbit.Application.Tests/Commands/Auth/LogoutAllSessionsCommandHandlerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| using FluentAssertions; | ||
| using NSubstitute; | ||
| using Orbit.Application.Auth.Commands; | ||
| using Orbit.Domain.Common; | ||
| using Orbit.Domain.Interfaces; | ||
|
|
||
| namespace Orbit.Application.Tests.Commands.Auth; | ||
|
|
||
| public class LogoutAllSessionsCommandHandlerTests | ||
| { | ||
| private readonly IAuthSessionService _authSessionService = Substitute.For<IAuthSessionService>(); | ||
| private readonly LogoutAllSessionsCommandHandler _handler; | ||
|
|
||
| public LogoutAllSessionsCommandHandlerTests() | ||
| { | ||
| _handler = new LogoutAllSessionsCommandHandler(_authSessionService); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Handle_RevokesEverySessionForTheUser() | ||
| { | ||
| var userId = Guid.NewGuid(); | ||
| _authSessionService.RevokeAllSessionsAsync(userId, Arg.Any<CancellationToken>()) | ||
| .Returns(Result.Success()); | ||
|
|
||
| var result = await _handler.Handle(new LogoutAllSessionsCommand(userId), CancellationToken.None); | ||
|
|
||
| result.IsSuccess.Should().BeTrue(); | ||
| await _authSessionService.Received(1).RevokeAllSessionsAsync(userId, Arg.Any<CancellationToken>()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task Handle_PropagatesFailure() | ||
| { | ||
| var userId = Guid.NewGuid(); | ||
| _authSessionService.RevokeAllSessionsAsync(userId, Arg.Any<CancellationToken>()) | ||
| .Returns(Result.Failure("User ID is required.", "USER_ID_REQUIRED")); | ||
|
|
||
| var result = await _handler.Handle(new LogoutAllSessionsCommand(userId), CancellationToken.None); | ||
|
|
||
| result.IsFailure.Should().BeTrue(); | ||
| result.ErrorCode.Should().Be("USER_ID_REQUIRED"); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
tests/Orbit.Application.Tests/Validators/LogoutAllSessionsCommandValidatorTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| using FluentAssertions; | ||
| using Orbit.Application.Auth.Commands; | ||
| using Orbit.Application.Auth.Validators; | ||
|
|
||
| namespace Orbit.Application.Tests.Validators; | ||
|
|
||
| public class LogoutAllSessionsCommandValidatorTests | ||
| { | ||
| private readonly LogoutAllSessionsCommandValidator _validator = new(); | ||
|
|
||
| [Fact] | ||
| public void Validate_WithUserId_Passes() | ||
| { | ||
| var result = _validator.Validate(new LogoutAllSessionsCommand(Guid.NewGuid())); | ||
| result.IsValid.Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Validate_EmptyUserId_Fails() | ||
| { | ||
| var result = _validator.Validate(new LogoutAllSessionsCommand(Guid.Empty)); | ||
| result.IsValid.Should().BeFalse(); | ||
| result.Errors.Should().ContainSingle() | ||
| .Which.PropertyName.Should().Be("UserId"); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.