Skip to content

refactor: deduplicate cache, pagination, and OAuth error helpers#101

Merged
appleboy merged 2 commits intomainfrom
worktree-mellow-riding-pelican
Mar 13, 2026
Merged

refactor: deduplicate cache, pagination, and OAuth error helpers#101
appleboy merged 2 commits intomainfrom
worktree-mellow-riding-pelican

Conversation

@appleboy
Copy link
Copy Markdown
Member

Summary

  • Consolidate three identical cache type constant sets (MetricsCacheType*, UserCacheType*, ClientCountCacheType*) into a single CacheType* set with a shared validateCacheType helper, reducing ~80 lines of duplicated validation logic
  • Create generic initializeCache[T any] in bootstrap/cache.go to replace three nearly identical cache init functions (~160 lines removed)
  • Eliminate redundant DB lookup in RequireAdmin middleware by reading the user already cached in gin context by RequireAuth (saves one DB query per admin request)
  • Add parsePaginationParams, respondOAuthError, and getUserFromContext handler helpers to replace repeated patterns across 6+ handler files
  • Extract defaultTokenType in token/http_api.go to deduplicate token type defaulting

Net result: 350 insertions, 478 deletions (−128 lines)

Test plan

  • make generate — templates and swagger compile
  • make test — all existing tests pass
  • make lint — zero lint issues
  • Manual: verify admin pages still load (RequireAdmin middleware change)
  • Manual: verify OAuth token endpoints still return correct error shapes

🤖 Generated with Claude Code

…lpers

- Consolidate three identical cache type constant sets into single CacheType* set
- Extract validateCacheType helper to replace tripled validation logic
- Create generic initializeCache[T any] to deduplicate three cache init functions
- Eliminate redundant DB lookup in RequireAdmin by reading cached user from context
- Add parsePaginationParams, respondOAuthError, and getUserFromContext helpers
- Replace 15+ inline OAuth error responses with respondOAuthError calls
- Extract defaultTokenType helper for duplicated token type defaulting
- Remove unused imports from handlers after consolidation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 12, 2026 14:26
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 12, 2026

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors repeated logic across handlers and bootstrap code by introducing small shared helpers (pagination parsing, OAuth error responses, user retrieval), simplifying admin middleware by relying on RequireAuth’s cached user, and consolidating cache-type constants plus cache initialization/validation logic.

Changes:

  • Centralize handler utilities: pagination param parsing, OAuth error JSON responses, and user extraction from gin.Context.
  • Simplify RequireAdmin to rely on RequireAuth-loaded user in context rather than re-fetching via UserService.
  • Unify cache-type constants and deduplicate cache init + config validation logic for metrics/user/client-count caches.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
internal/token/http_api.go Extracts token-type defaulting into a helper for reuse.
internal/middleware/auth.go Updates RequireAdmin to use the cached user from context.
internal/handlers/utils.go Adds shared helper functions for pagination, OAuth errors, and user lookup.
internal/handlers/user_client.go Switches to shared pagination/user-context helpers.
internal/handlers/token.go Switches repeated OAuth error responses to respondOAuthError.
internal/handlers/session.go Switches pagination parsing to shared helper.
internal/handlers/docs.go Uses shared user-context helper for optional navbar user info.
internal/handlers/client.go Switches to shared pagination/user-context helpers.
internal/handlers/authorization.go Uses shared user-context helper (fallbacking to DB lookup).
internal/handlers/audit.go Uses shared user-context helper.
internal/config/config.go Consolidates cache-type constants; deduplicates cache-type validation.
internal/config/config_test.go Updates tests to use unified cache-type constants and expectations.
internal/bootstrap/cache.go Deduplicates cache initialization via a generic helper and shared options struct.
internal/bootstrap/bootstrap_test.go Updates bootstrap test to use unified cache-type constants.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +175 to +177
respondOAuthError(
c,
http.StatusBadRequest,
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

In the refresh_token grant error mapping, client authentication failures are returned as 400 invalid_client. Per RFC 6749 §5.2, invalid_client should be a 401 response and include a WWW-Authenticate header (similar to the client_credentials grant handling below). Consider switching this branch to 401 and setting the header to keep behavior consistent and standards-compliant.

Suggested change
respondOAuthError(
c,
http.StatusBadRequest,
// Per RFC 6749 §5.2, invalid_client should use 401 and include WWW-Authenticate
c.Header("WWW-Authenticate", `Basic realm="token"`)
respondOAuthError(
c,
http.StatusUnauthorized,

Copilot uses AI. Check for mistakes.
func RequireAdmin(userService *services.UserService) gin.HandlerFunc {
// RequireAdmin is a middleware that requires the user to have admin role.
// This middleware should be used after RequireAuth, which already fetches
// and caches the user in the gin context via loadUserFromSession.
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

RequireAdmin no longer uses the provided UserService, but the parameter is still part of the API (now named _). This is misleading for callers and suggests a dependency that no longer exists. Consider removing the parameter entirely and updating call sites, or (if you need to keep the signature) document why the argument is intentionally unused.

Suggested change
// and caches the user in the gin context via loadUserFromSession.
// and caches the user in the gin context via loadUserFromSession.
//
// The *services.UserService parameter is intentionally unused and retained
// only for backward compatibility with existing call sites. RequireAdmin
// no longer depends on UserService and instead reads the user from context.

Copilot uses AI. Check for mistakes.
- Return 401 with WWW-Authenticate header for invalid_client per RFC 6749 §5.2
- Remove unused userService parameter from RequireAdmin middleware
- Add unit tests for RequireAdmin, parsePaginationParams, respondOAuthError, getUserFromContext

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@appleboy appleboy merged commit 21e3aad into main Mar 13, 2026
17 checks passed
@appleboy appleboy deleted the worktree-mellow-riding-pelican branch March 13, 2026 14:15
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.

2 participants