-
Notifications
You must be signed in to change notification settings - Fork 0
feat: implement authentication token source configuration and middlew… #105
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d211f2d
feat: implement authentication token source configuration and middlew…
Suhaibinator 4fb5145
feat: implement auth token extraction logic for header and cookie sou…
Suhaibinator 20dedab
feat: add warning for unconfigured auth token cookie name
Suhaibinator 4fed79a
test: add unit tests for auth token configuration and extraction logic
Suhaibinator b688ad9
test: add unit tests for RouteOverrides rate limit and auth token checks
Suhaibinator 456f5d1
docs: update authentication and configuration documentation to includ…
Suhaibinator 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package common | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestRouteOverridesHasRateLimit(t *testing.T) { | ||
| overrides := RouteOverrides{} | ||
| if overrides.HasRateLimit() { | ||
| t.Fatal("expected HasRateLimit to be false when rate limit is nil") | ||
| } | ||
|
|
||
| overrides.RateLimit = &RateLimitConfig[any, any]{} | ||
| if !overrides.HasRateLimit() { | ||
| t.Fatal("expected HasRateLimit to be true when rate limit is set") | ||
| } | ||
| } | ||
|
|
||
| func TestRouteOverridesHasAuthToken(t *testing.T) { | ||
| overrides := RouteOverrides{} | ||
| if overrides.HasAuthToken() { | ||
| t.Fatal("expected HasAuthToken to be false when auth token is nil") | ||
| } | ||
|
|
||
| overrides.AuthToken = &AuthTokenConfig{} | ||
| if !overrides.HasAuthToken() { | ||
| t.Fatal("expected HasAuthToken to be true when auth token is set") | ||
| } | ||
| } |
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 |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import ( | |
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/Suhaibinator/SRouter/pkg/common" | ||
| // Keep middleware alias if needed for other types | ||
| "github.com/Suhaibinator/SRouter/pkg/router/internal/mocks" // Use centralized mocks | ||
| "github.com/Suhaibinator/SRouter/pkg/scontext" // Added scontext import | ||
|
|
@@ -313,6 +314,47 @@ func TestAuthRequiredMiddlewareWithUserObject(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestAuthRequiredMiddlewareWithCookieSource(t *testing.T) { | ||
| logger := zap.NewNop() | ||
| r := NewRouter(RouterConfig{Logger: logger}, mocks.MockAuthFunction, mocks.MockUserIDFromUser) | ||
|
|
||
| handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| userID, ok := scontext.GetUserIDFromRequest[string, string](r) | ||
| if !ok { | ||
| t.Error("Expected user ID to be in context") | ||
| } | ||
| if userID != "user123" { | ||
| t.Errorf("Expected user ID %q, got %q", "user123", userID) | ||
| } | ||
| w.WriteHeader(http.StatusOK) | ||
| _, _ = w.Write([]byte("OK")) | ||
| }) | ||
|
|
||
| authTokenConfig := common.AuthTokenConfig{ | ||
| Source: common.AuthTokenSourceCookie, | ||
| CookieName: "auth_token", | ||
| } | ||
| wrappedHandler := r.authRequiredMiddlewareWithConfig(authTokenConfig)(handler) | ||
|
|
||
| // Test with valid auth cookie | ||
| req, _ := http.NewRequest("GET", "/test", nil) | ||
| req.AddCookie(&http.Cookie{Name: "auth_token", Value: "valid-token"}) | ||
| rr := httptest.NewRecorder() | ||
| wrappedHandler.ServeHTTP(rr, req) | ||
| if rr.Code != http.StatusOK { | ||
| t.Errorf("Expected status code %d, got %d", http.StatusOK, rr.Code) | ||
| } | ||
|
|
||
| // Test with valid Authorization header but no cookie (should not fallback) | ||
| req, _ = http.NewRequest("GET", "/test", nil) | ||
| req.Header.Set("Authorization", "Bearer valid-token") | ||
| rr = httptest.NewRecorder() | ||
| wrappedHandler.ServeHTTP(rr, req) | ||
| if rr.Code != http.StatusUnauthorized { | ||
| t.Errorf("Expected status code %d, got %d", http.StatusUnauthorized, rr.Code) | ||
| } | ||
| } | ||
|
Comment on lines
+317
to
+356
|
||
|
|
||
| // TestAuthRequiredMiddlewareWithTraceID tests the authRequiredMiddleware function with trace ID | ||
| // (from auth_required_middleware_test.go) | ||
| func TestAuthRequiredMiddlewareWithTraceID(t *testing.T) { | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The documentation for CookieName should clarify that it's required when Source is AuthTokenSourceCookie. Consider updating the comment to: "CookieName is used when Source is AuthTokenSourceCookie. Required when using cookie-based authentication."