-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[PM-24211]: 2FA Send Email Login validation should use AuthRequest.IsValidForAuthentication #6695
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 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d22b8c1
fix(two-factor-controller) [PM-24211]: Update send email validation tโฆ
enmande d32f5cf
refactor(login-features) [PM-24211]: Remove Core.LoginFeatures as no โฆ
enmande 1f85df1
feat(auth-request) [PM-24211]: Add tests for AuthRequest.IsValidForAuโฆ
enmande ca1a8c7
fix(two-factor-controller) [PM-24211]: Branching logic should return โฆ
enmande 09de3e8
chore(auth-request) [PM-24211]: Remove some old comments (solved-for).
enmande 427a0cf
fix(two-factor-controller) [PM-24211]: Update some comments (clarificโฆ
enmande 77234c1
fix(two-factor-controller) [PM-24211]: Rephrase a comment (accuracy).
enmande 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
14 changes: 0 additions & 14 deletions
14
src/Core/Auth/LoginFeatures/LoginServiceCollectionExtensions.cs
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
src/Core/Auth/LoginFeatures/PasswordlessLogin/Interfaces/IVerifyAuthRequest.cs
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
src/Core/Auth/LoginFeatures/PasswordlessLogin/VerifyAuthRequest.cs
This file was deleted.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| ๏ปฟusing Bit.Core.Auth.Entities; | ||
| using Bit.Core.Auth.Enums; | ||
| using Xunit; | ||
|
|
||
| namespace Bit.Core.Test.Auth.Entities; | ||
|
|
||
| public class AuthRequestTests | ||
| { | ||
| [Fact] | ||
| public void IsValidForAuthentication_WithValidRequest_ReturnsTrue() | ||
| { | ||
| // Arrange | ||
| var userId = Guid.NewGuid(); | ||
| var accessCode = "test-access-code"; | ||
| var authRequest = new AuthRequest | ||
| { | ||
| UserId = userId, | ||
| Type = AuthRequestType.AuthenticateAndUnlock, | ||
| ResponseDate = DateTime.UtcNow, | ||
| Approved = true, | ||
| CreationDate = DateTime.UtcNow, | ||
| AuthenticationDate = null, | ||
| AccessCode = accessCode | ||
| }; | ||
|
|
||
| // Act | ||
| var result = authRequest.IsValidForAuthentication(userId, accessCode); | ||
|
|
||
| // Assert | ||
| Assert.True(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsValidForAuthentication_WithWrongUserId_ReturnsFalse() | ||
| { | ||
| // Arrange | ||
| var userId = Guid.NewGuid(); | ||
| var differentUserId = Guid.NewGuid(); | ||
| var accessCode = "test-access-code"; | ||
| var authRequest = new AuthRequest | ||
| { | ||
| UserId = userId, | ||
| Type = AuthRequestType.AuthenticateAndUnlock, | ||
| ResponseDate = DateTime.UtcNow, | ||
| Approved = true, | ||
| CreationDate = DateTime.UtcNow, | ||
| AuthenticationDate = null, | ||
| AccessCode = accessCode | ||
| }; | ||
|
|
||
| // Act | ||
| var result = authRequest.IsValidForAuthentication(differentUserId, accessCode); | ||
|
|
||
| // Assert | ||
| Assert.False(result, "Auth request should not validate for a different user"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsValidForAuthentication_WithWrongAccessCode_ReturnsFalse() | ||
| { | ||
| // Arrange | ||
| var userId = Guid.NewGuid(); | ||
| var authRequest = new AuthRequest | ||
| { | ||
| UserId = userId, | ||
| Type = AuthRequestType.AuthenticateAndUnlock, | ||
| ResponseDate = DateTime.UtcNow, | ||
| Approved = true, | ||
| CreationDate = DateTime.UtcNow, | ||
| AuthenticationDate = null, | ||
| AccessCode = "correct-code" | ||
| }; | ||
|
|
||
| // Act | ||
| var result = authRequest.IsValidForAuthentication(userId, "wrong-code"); | ||
|
|
||
| // Assert | ||
| Assert.False(result); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsValidForAuthentication_WithoutResponseDate_ReturnsFalse() | ||
| { | ||
| // Arrange | ||
| var userId = Guid.NewGuid(); | ||
| var accessCode = "test-access-code"; | ||
| var authRequest = new AuthRequest | ||
| { | ||
| UserId = userId, | ||
| Type = AuthRequestType.AuthenticateAndUnlock, | ||
| ResponseDate = null, // Not responded to | ||
| Approved = true, | ||
| CreationDate = DateTime.UtcNow, | ||
| AuthenticationDate = null, | ||
| AccessCode = accessCode | ||
| }; | ||
|
|
||
| // Act | ||
| var result = authRequest.IsValidForAuthentication(userId, accessCode); | ||
|
|
||
| // Assert | ||
| Assert.False(result, "Unanswered auth requests should not be valid"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsValidForAuthentication_WithApprovedFalse_ReturnsFalse() | ||
| { | ||
| // Arrange | ||
| var userId = Guid.NewGuid(); | ||
| var accessCode = "test-access-code"; | ||
| var authRequest = new AuthRequest | ||
| { | ||
| UserId = userId, | ||
| Type = AuthRequestType.AuthenticateAndUnlock, | ||
| ResponseDate = DateTime.UtcNow, | ||
| Approved = false, // Denied | ||
| CreationDate = DateTime.UtcNow, | ||
| AuthenticationDate = null, | ||
| AccessCode = accessCode | ||
| }; | ||
|
|
||
| // Act | ||
| var result = authRequest.IsValidForAuthentication(userId, accessCode); | ||
|
|
||
| // Assert | ||
| Assert.False(result, "Denied auth requests should not be valid"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsValidForAuthentication_WithApprovedNull_ReturnsFalse() | ||
| { | ||
| // Arrange | ||
| var userId = Guid.NewGuid(); | ||
| var accessCode = "test-access-code"; | ||
| var authRequest = new AuthRequest | ||
| { | ||
| UserId = userId, | ||
| Type = AuthRequestType.AuthenticateAndUnlock, | ||
| ResponseDate = DateTime.UtcNow, | ||
| Approved = null, // Pending | ||
| CreationDate = DateTime.UtcNow, | ||
| AuthenticationDate = null, | ||
| AccessCode = accessCode | ||
| }; | ||
|
|
||
| // Act | ||
| var result = authRequest.IsValidForAuthentication(userId, accessCode); | ||
|
|
||
| // Assert | ||
| Assert.False(result, "Pending auth requests should not be valid"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsValidForAuthentication_WithExpiredRequest_ReturnsFalse() | ||
| { | ||
| // Arrange | ||
| var userId = Guid.NewGuid(); | ||
| var accessCode = "test-access-code"; | ||
| var authRequest = new AuthRequest | ||
| { | ||
| UserId = userId, | ||
| Type = AuthRequestType.AuthenticateAndUnlock, | ||
| ResponseDate = DateTime.UtcNow, | ||
| Approved = true, | ||
| CreationDate = DateTime.UtcNow.AddMinutes(-20), // Expired (15 min timeout) | ||
| AuthenticationDate = null, | ||
| AccessCode = accessCode | ||
| }; | ||
|
|
||
| // Act | ||
| var result = authRequest.IsValidForAuthentication(userId, accessCode); | ||
|
|
||
| // Assert | ||
| Assert.False(result, "Expired auth requests should not be valid"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsValidForAuthentication_WithWrongType_ReturnsFalse() | ||
| { | ||
| // Arrange | ||
| var userId = Guid.NewGuid(); | ||
| var accessCode = "test-access-code"; | ||
| var authRequest = new AuthRequest | ||
| { | ||
| UserId = userId, | ||
| Type = AuthRequestType.Unlock, // Wrong type | ||
| ResponseDate = DateTime.UtcNow, | ||
| Approved = true, | ||
| CreationDate = DateTime.UtcNow, | ||
| AuthenticationDate = null, | ||
| AccessCode = accessCode | ||
| }; | ||
|
|
||
| // Act | ||
| var result = authRequest.IsValidForAuthentication(userId, accessCode); | ||
|
|
||
| // Assert | ||
| Assert.False(result, "Only AuthenticateAndUnlock type should be valid"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IsValidForAuthentication_WithAlreadyUsed_ReturnsFalse() | ||
| { | ||
| // Arrange | ||
| var userId = Guid.NewGuid(); | ||
| var accessCode = "test-access-code"; | ||
| var authRequest = new AuthRequest | ||
| { | ||
| UserId = userId, | ||
| Type = AuthRequestType.AuthenticateAndUnlock, | ||
| ResponseDate = DateTime.UtcNow, | ||
| Approved = true, | ||
| CreationDate = DateTime.UtcNow, | ||
| AuthenticationDate = DateTime.UtcNow, // Already used | ||
| AccessCode = accessCode | ||
| }; | ||
|
|
||
| // Act | ||
| var result = authRequest.IsValidForAuthentication(userId, accessCode); | ||
|
|
||
| // Assert | ||
| Assert.False(result, "Auth requests should only be valid for one-time use"); | ||
| } | ||
| } |
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.