Skip to content
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

Issue - 996 | Refactor BasicAuthMiddleware to reduce cognitive complexity #1203

Open
wants to merge 18 commits into
base: development
Choose a base branch
from

Conversation

nidhey27
Copy link

@nidhey27 nidhey27 commented Nov 14, 2024

Issue

Closes: #996

Description:

  • Extracted parseBasicAuth to handle authorization header parsing
  • Added respondUnauthorized for consistent unauthorized responses
  • Moved credential validation logic to validateCredentials
  • Simplified main middleware function for improved readability and maintainability

Checklist:

  • I have formatted my code using goimport and golangci-lint.
  • All new code is covered by unit tests.
  • This PR does not decrease the overall code coverage.
  • I have reviewed the code comments and documentation for clarity.

Signed-off-by: nidhey27 <nidhey60@gmail.com>
@nidhey27 nidhey27 force-pushed the refactor/basic-auth-middleware branch from 03ec4c9 to b2d176c Compare November 14, 2024 07:55
@Umang01-hash
Copy link
Contributor

@nidhey27 your PR has some failing tests as well as code quality issues. Please resolve them

@nidhey27
Copy link
Author

nidhey27 commented Nov 14, 2024

@Umang01-hash sure I will have a look at it. Also a bit busy today with my day job so I will try to close this by coming weekend.

Signed-off-by: nidhey27 <nidhey60@gmail.com>
@nidhey27 nidhey27 force-pushed the refactor/basic-auth-middleware branch from ffe2bcd to 7637fdf Compare November 14, 2024 12:26
middleware(handler).ServeHTTP(rr, req)

assert.Equal(t, http.StatusUnauthorized, rr.Code)
assert.Contains(t, rr.Body.String(), "Unauthorized: Authorization header missing")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test condition is failing as, on missing header the checks respond with - "Invalid or missing header"

@nidhey27
Copy link
Author

@Umang01-hash @vipul-rawat Were the recent changes you made intentional or accidental?

@Umang01-hash
Copy link
Contributor

@nidhey27 We haven't committed anything intentionally. I think you pulled from development and all our recent commits there got merged.


// respondUnauthorized sends a 401 Unauthorized response with a given message.
func respondUnauthorized(w http.ResponseWriter, message string) {
http.Error(w, "Unauthorized: "+message, http.StatusUnauthorized)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function with single line in its body may not be required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor BasicAuthMiddleware
6 participants