Skip to content

Conversation

vishr
Copy link
Member

@vishr vishr commented Sep 16, 2025

Summary

Modernizes the BasicAuth middleware with improved code readability and RFC compliance.

Changes:

  1. Use strings.Cut for credential parsing - Replaces manual for loop with Go 1.18+ strings.Cut
  2. Fix RFC 7617 compliance - Always quote realm parameter as required by RFC

Benefits:

  • Cleaner, more readable code using modern Go idioms
  • Proper RFC 7617 compliance for WWW-Authenticate header
  • Reduced code complexity (fewer lines, simpler logic)

Test plan

  • All existing tests pass
  • Linting passes
  • No behavioral changes to authentication logic

Fixes #2794

🤖 Generated with Claude Code

- Replace manual for loop with strings.Cut for credential parsing
- Simplify realm handling to always quote according to RFC 7617
- Improve code readability and maintainability

Fixes #2794

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Copy link
Contributor

@aldas aldas left a comment

Choose a reason for hiding this comment

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

strings.cut is covered with existing tests but strconv.Quote is new behavior and it does not seem to have tests in this PR. It would be good to document this behavior with tests.

// Need to return `401` for browsers to pop-up login box.
c.Response().Header().Set(echo.HeaderWWWAuthenticate, basic+" realm="+realm)
// Realm is case-insensitive, so we can use "basic" directly. See RFC 7617.
c.Response().Header().Set(echo.HeaderWWWAuthenticate, basic+" realm="+strconv.Quote(config.Realm))
Copy link
Contributor

Choose a reason for hiding this comment

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

strconv.Quote could be moved to line 68 so it would be done once when middleware is created.

vishr and others added 2 commits September 15, 2025 21:53
Tests cover:
- Default realm quoting
- Custom realm with spaces
- Special characters (quotes, backslashes)
- Empty realm fallback to default
- Unicode realm support

Addresses review feedback about testing strconv.Quote behavior
in WWW-Authenticate header per RFC 7617 compliance.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Move strconv.Quote(config.Realm) from per-request execution
to middleware initialization for better performance.

- Pre-compute quoted realm at middleware creation time
- Avoids repeated string operations on every auth failure
- Maintains same behavior with better efficiency

Performance improvement suggested during code review.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
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.

Improved code readability and RFC compliance
2 participants