Skip to content

Refactor code structure and fix APIBaseUrl naming#24

Open
marcschaeferger wants to merge 1 commit into
dev-marcfrom
improvements
Open

Refactor code structure and fix APIBaseUrl naming#24
marcschaeferger wants to merge 1 commit into
dev-marcfrom
improvements

Conversation

@marcschaeferger

Copy link
Copy Markdown
Member

Description

This pull request refactors and improves the badger.go middleware, focusing on code clarity, maintainability, and correctness. The main changes include extracting and modularizing logic for session verification and exchange, introducing better error handling, and standardizing naming conventions. Several helper functions were added, and the code now uses a shared HTTP client with timeouts. There are also improvements to security and test coverage.

Refactoring and modularization:

  • Extracted session verification and exchange logic into dedicated helper functions (handleSessionExchange, buildOriginalURL, buildVerifyBody, handleVerifyResponse, etc.), making the main handler (ServeHTTP) much clearer and easier to maintain. [1] [2] [3]
  • Added a shared http.Client with a timeout to the Badger struct for all outbound HTTP requests.

Error handling and security improvements:

  • Standardized error messages and HTTP status codes, and improved error handling throughout the middleware. [1] [2]
  • Improved escaping for redirect URLs in HTML and JavaScript contexts in renderRedirectPage, reducing possible XSS vectors. [1] [2]

Naming and API consistency:

  • Renamed APIBaseUrl to APIBaseURL throughout the codebase for consistency, including in struct fields, variables, and test cases. [1] [2] [3] [4]
  • Updated UserId to UserID in all relevant types.

Configuration and initialization:

  • Added a validateConfig function to check for required configuration fields when forward auth is enabled, improving startup validation.
  • Refactored trusted IP parsing into a dedicated parseTrustedIPs method, improving clarity and error handling.

Testing:

  • Updated test cases to use the new field names and ensure coverage for the refactored configuration validation. [1] [2]

Note:

PR Titel and Description generated by AI (Copilot). Code Changes 90% human + 10% AI based on golangci linting and coderabbit review

Signed-off-by: Marc Schäfer <git@marcschaeferger.de>
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.

1 participant