-
Notifications
You must be signed in to change notification settings - Fork 69
security: add authentication middleware to validate API credentials #407
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
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,89 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "ccsync_backend/utils" | ||
| "encoding/json" | ||
| "io" | ||
| "net/http" | ||
|
|
||
| "github.com/gorilla/sessions" | ||
| ) | ||
|
|
||
| // credentialsPayload represents the common credential fields in request bodies | ||
| type credentialsPayload struct { | ||
| Email string `json:"email"` | ||
| EncryptionSecret string `json:"encryptionSecret"` | ||
| UUID string `json:"UUID"` | ||
| } | ||
|
|
||
| // AuthMiddleware validates that the user is authenticated and that request body | ||
| // credentials match the session credentials to prevent unauthorized access. | ||
| func AuthMiddleware(store *sessions.CookieStore) func(http.Handler) http.Handler { | ||
| return func(next http.Handler) http.Handler { | ||
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| // Get session | ||
| session, err := store.Get(r, "session-name") | ||
| if err != nil { | ||
| utils.Logger.Warnf("Auth middleware: failed to get session: %v", err) | ||
| http.Error(w, "Authentication required", http.StatusUnauthorized) | ||
| return | ||
| } | ||
|
|
||
| // Check if user is authenticated | ||
| userInfo, ok := session.Values["user"].(map[string]interface{}) | ||
| if !ok || userInfo == nil { | ||
| http.Error(w, "Authentication required", http.StatusUnauthorized) | ||
| return | ||
| } | ||
|
|
||
| // Extract session credentials | ||
| sessionEmail, _ := userInfo["email"].(string) | ||
| sessionUUID, _ := userInfo["uuid"].(string) | ||
| sessionSecret, _ := userInfo["encryption_secret"].(string) | ||
|
|
||
| if sessionEmail == "" || sessionUUID == "" || sessionSecret == "" { | ||
| utils.Logger.Warnf("Auth middleware: incomplete session credentials") | ||
| http.Error(w, "Authentication required", http.StatusUnauthorized) | ||
| return | ||
| } | ||
|
|
||
| // For POST requests with JSON body, validate credentials match session | ||
| if r.Method == http.MethodPost && r.Body != nil { | ||
| // Read the body | ||
| bodyBytes, err := io.ReadAll(r.Body) | ||
| if err != nil { | ||
| http.Error(w, "Failed to read request body", http.StatusBadRequest) | ||
| return | ||
| } | ||
| // Restore the body for the next handler | ||
| r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) | ||
|
|
||
| // Parse credentials from body | ||
| var creds credentialsPayload | ||
| if err := json.Unmarshal(bodyBytes, &creds); err == nil { | ||
| // If credentials are provided in the body, validate they match session | ||
| if creds.Email != "" || creds.UUID != "" || creds.EncryptionSecret != "" { | ||
| if creds.Email != sessionEmail { | ||
| utils.Logger.Warnf("Auth middleware: email mismatch - session=%s, request=%s", sessionEmail, creds.Email) | ||
| http.Error(w, "Credential mismatch: email does not match session", http.StatusForbidden) | ||
| return | ||
| } | ||
| if creds.UUID != sessionUUID { | ||
| utils.Logger.Warnf("Auth middleware: UUID mismatch - session=%s, request=%s", sessionUUID, creds.UUID) | ||
| http.Error(w, "Credential mismatch: UUID does not match session", http.StatusForbidden) | ||
| return | ||
| } | ||
| if creds.EncryptionSecret != sessionSecret { | ||
| utils.Logger.Warnf("Auth middleware: encryption secret mismatch for user %s", sessionEmail) | ||
| http.Error(w, "Credential mismatch: encryption secret does not match session", http.StatusForbidden) | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| next.ServeHTTP(w, r) | ||
| }) | ||
| } | ||
| } | ||
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.
seems to work, code is clear as well, but the current approach probably breaks non-browser clients like Postman, or the Flutter App.
The Auth creds validation for the Backend might work for Frontend but can break for Taskwarrior Flutter App, probably we should open an issue there then, to Remove CCSync backend as a way of sync, then replace it with Taskchampion.
The app uses use Taskchampion directly (now), so probably can make it use the CCSync generated creds + the deployed Taskchampion link, bypassing the backend. Can go ahead with this i guess if it works