-
Notifications
You must be signed in to change notification settings - Fork 62
Fix authorization middleware #344
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
92cf7b0
Implement core authentication middleware for session validation
Hell1213 addf847
Add credential validation helpers for request authorization
Hell1213 5bfcde3
Secure task endpoints with session-based authorization
Hell1213 fdf3e84
Integrate authentication middleware and complete authorization implem…
Hell1213 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package controllers | ||
|
|
||
| import ( | ||
| "errors" | ||
| "net/http" | ||
| ) | ||
|
|
||
| func ValidateUserCredentials(r *http.Request, requestEmail, requestUUID string) error { | ||
| userInfo, ok := r.Context().Value("user").(map[string]interface{}) | ||
| if !ok { | ||
| return errors.New("user context not found") | ||
| } | ||
|
|
||
| sessionEmail, emailOk := userInfo["email"].(string) | ||
| sessionUUID, uuidOk := userInfo["uuid"].(string) | ||
|
|
||
| if !emailOk || !uuidOk { | ||
| return errors.New("invalid user session data") | ||
| } | ||
|
|
||
| if sessionEmail != requestEmail || sessionUUID != requestUUID { | ||
| return errors.New("credentials do not match authenticated user") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func GetSessionCredentials(r *http.Request) (email, uuid, encryptionSecret string, err error) { | ||
| userInfo, ok := r.Context().Value("user").(map[string]interface{}) | ||
| if !ok { | ||
| return "", "", "", errors.New("user context not found") | ||
| } | ||
|
|
||
| email, emailOk := userInfo["email"].(string) | ||
| uuid, uuidOk := userInfo["uuid"].(string) | ||
| encryptionSecret, secretOk := userInfo["encryption_secret"].(string) | ||
|
|
||
| if !emailOk || !uuidOk || !secretOk { | ||
| return "", "", "", errors.New("incomplete user session data") | ||
| } | ||
|
|
||
| return email, uuid, encryptionSecret, nil | ||
| } |
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added tests for helper functions |
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,167 @@ | ||
| package controllers | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http/httptest" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestValidateUserCredentials_MatchingCredentials(t *testing.T) { | ||
| req := httptest.NewRequest("GET", "/test", nil) | ||
| ctx := context.WithValue(req.Context(), "user", map[string]interface{}{ | ||
| "email": "test@example.com", | ||
| "uuid": "test-uuid-123", | ||
| "encryption_secret": "test-secret", | ||
| }) | ||
| req = req.WithContext(ctx) | ||
|
|
||
| err := ValidateUserCredentials(req, "test@example.com", "test-uuid-123") | ||
| if err != nil { | ||
| t.Errorf("Expected no error, got %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateUserCredentials_MismatchedEmail(t *testing.T) { | ||
| req := httptest.NewRequest("GET", "/test", nil) | ||
| ctx := context.WithValue(req.Context(), "user", map[string]interface{}{ | ||
| "email": "test@example.com", | ||
| "uuid": "test-uuid-123", | ||
| "encryption_secret": "test-secret", | ||
| }) | ||
| req = req.WithContext(ctx) | ||
|
|
||
| err := ValidateUserCredentials(req, "wrong@example.com", "test-uuid-123") | ||
| if err == nil { | ||
| t.Error("Expected error for mismatched email") | ||
| } | ||
| if err.Error() != "credentials do not match authenticated user" { | ||
| t.Errorf("Expected specific error message, got %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateUserCredentials_MismatchedUUID(t *testing.T) { | ||
| req := httptest.NewRequest("GET", "/test", nil) | ||
| ctx := context.WithValue(req.Context(), "user", map[string]interface{}{ | ||
| "email": "test@example.com", | ||
| "uuid": "test-uuid-123", | ||
| "encryption_secret": "test-secret", | ||
| }) | ||
| req = req.WithContext(ctx) | ||
|
|
||
| err := ValidateUserCredentials(req, "test@example.com", "wrong-uuid") | ||
| if err == nil { | ||
| t.Error("Expected error for mismatched UUID") | ||
| } | ||
| if err.Error() != "credentials do not match authenticated user" { | ||
| t.Errorf("Expected specific error message, got %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateUserCredentials_NoContext(t *testing.T) { | ||
| req := httptest.NewRequest("GET", "/test", nil) | ||
|
|
||
| err := ValidateUserCredentials(req, "test@example.com", "test-uuid-123") | ||
| if err == nil { | ||
| t.Error("Expected error for missing context") | ||
| } | ||
| if err.Error() != "user context not found" { | ||
| t.Errorf("Expected 'user context not found', got %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestValidateUserCredentials_InvalidSessionData(t *testing.T) { | ||
| req := httptest.NewRequest("GET", "/test", nil) | ||
| ctx := context.WithValue(req.Context(), "user", map[string]interface{}{ | ||
| "email": 12345, | ||
| "uuid": "test-uuid-123", | ||
| }) | ||
| req = req.WithContext(ctx) | ||
|
|
||
| err := ValidateUserCredentials(req, "test@example.com", "test-uuid-123") | ||
| if err == nil { | ||
| t.Error("Expected error for invalid session data") | ||
| } | ||
| if err.Error() != "invalid user session data" { | ||
| t.Errorf("Expected 'invalid user session data', got %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestGetSessionCredentials_ValidSession(t *testing.T) { | ||
| req := httptest.NewRequest("GET", "/test", nil) | ||
| ctx := context.WithValue(req.Context(), "user", map[string]interface{}{ | ||
| "email": "test@example.com", | ||
| "uuid": "test-uuid-123", | ||
| "encryption_secret": "test-secret-456", | ||
| }) | ||
| req = req.WithContext(ctx) | ||
|
|
||
| email, uuid, secret, err := GetSessionCredentials(req) | ||
| if err != nil { | ||
| t.Errorf("Expected no error, got %v", err) | ||
| } | ||
| if email != "test@example.com" { | ||
| t.Errorf("Expected email test@example.com, got %s", email) | ||
| } | ||
| if uuid != "test-uuid-123" { | ||
| t.Errorf("Expected uuid test-uuid-123, got %s", uuid) | ||
| } | ||
| if secret != "test-secret-456" { | ||
| t.Errorf("Expected secret test-secret-456, got %s", secret) | ||
| } | ||
| } | ||
|
|
||
| func TestGetSessionCredentials_NoContext(t *testing.T) { | ||
| req := httptest.NewRequest("GET", "/test", nil) | ||
|
|
||
| email, uuid, secret, err := GetSessionCredentials(req) | ||
| if err == nil { | ||
| t.Error("Expected error for missing context") | ||
| } | ||
| if err.Error() != "user context not found" { | ||
| t.Errorf("Expected 'user context not found', got %v", err) | ||
| } | ||
| if email != "" || uuid != "" || secret != "" { | ||
| t.Error("Expected empty strings for credentials") | ||
| } | ||
| } | ||
|
|
||
| func TestGetSessionCredentials_IncompleteData(t *testing.T) { | ||
| req := httptest.NewRequest("GET", "/test", nil) | ||
| ctx := context.WithValue(req.Context(), "user", map[string]interface{}{ | ||
| "email": "test@example.com", | ||
| "uuid": "test-uuid-123", | ||
| }) | ||
| req = req.WithContext(ctx) | ||
|
|
||
| email, uuid, secret, err := GetSessionCredentials(req) | ||
| if err == nil { | ||
| t.Error("Expected error for incomplete session data") | ||
| } | ||
| if err.Error() != "incomplete user session data" { | ||
| t.Errorf("Expected 'incomplete user session data', got %v", err) | ||
| } | ||
| if email != "" || uuid != "" || secret != "" { | ||
| t.Error("Expected empty strings for credentials") | ||
| } | ||
| } | ||
|
|
||
| func TestGetSessionCredentials_InvalidDataTypes(t *testing.T) { | ||
| req := httptest.NewRequest("GET", "/test", nil) | ||
| ctx := context.WithValue(req.Context(), "user", map[string]interface{}{ | ||
| "email": 12345, | ||
| "uuid": true, | ||
| "encryption_secret": []string{"invalid"}, | ||
| }) | ||
| req = req.WithContext(ctx) | ||
|
|
||
| email, uuid, secret, err := GetSessionCredentials(req) | ||
| if err == nil { | ||
| t.Error("Expected error for invalid data types") | ||
| } | ||
| if err.Error() != "incomplete user session data" { | ||
| t.Errorf("Expected 'incomplete user session data', got %v", err) | ||
| } | ||
| if email != "" || uuid != "" || secret != "" { | ||
| t.Error("Expected empty strings for credentials") | ||
| } | ||
| } |
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using session creds for bulk operations |
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
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.
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.
implemented creds validation helpers