Skip to content

feat: add auth package with file-based token support for K8s projected volumes#3244

Open
akshat-kumar-singhal wants to merge 17 commits into
gofr-dev:developmentfrom
akshat-kumar-singhal:feature/file-token-auth
Open

feat: add auth package with file-based token support for K8s projected volumes#3244
akshat-kumar-singhal wants to merge 17 commits into
gofr-dev:developmentfrom
akshat-kumar-singhal:feature/file-token-auth

Conversation

@akshat-kumar-singhal
Copy link
Copy Markdown
Contributor

Description:

  • Introduced pkg/gofr/service/auth package that consolidates authentication for outgoing HTTP service calls
  • Added Provider interface (header key + value) and TokenSource interface (raw token) to cleanly separate auth header injection from token acquisition
  • Added NewBearerAuthOption(TokenSource) for bearer-style auth and NewAuthOption(Provider) for custom header-based auth
  • Added file-based token auth (NewFileTokenAuthConfig) supporting Kubernetes projected service account tokens with automatic periodic refresh and sync.Once-guarded shutdown
  • OAuth implementation now caches TokenSource at construction time, leveraging golang.org/x/oauth2's built-in token refresh instead of re-creating a token source per request
  • BasicAuth pre-computes the Base64-encoded header value at construction time instead of re-encoding per request
  • Added Observable interface to service.Options enabling automatic logger/metrics injection from NewHTTPService into options that support it. When AddOption is called directly, callers must set logger/metrics manually
  • Err type implements Unwrap() for proper error chain traversal with errors.Is/errors.As
  • Deprecated existing auth types in service/ (BasicAuthConfig, APIKeyConfig, OAuthConfig, AuthErr) — deprecated code remains fully functional
  • Moved auth test coverage to auth/ package with table-driven tests; all concrete types are unexported to minimize API surface

Breaking Changes: None. All existing service.NewBasicAuthConfig, service.NewAPIKeyConfig, service.NewOAuthConfig continue to work unchanged.

Additional Information:

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.

@Umang01-hash
Copy link
Copy Markdown
Member

Umang01-hash commented Apr 12, 2026

Hey @akshat-kumar-singhal ! Thanks for the PR.

  • The new authProvider in this package is a different type from service.authProvider. extractHTTPService in connection_pool.go does a type switch on *authProvider (the service package one) to unwrap the decorator chain. When the new auth options are used, it'll hit the default: return nil case — meaning ConnectionPoolConfig, CircuitBreakerConfig, and RetryConfig silently stop working when combined with any auth option from this package.

  • These tests files that you deleted cover the existing public types which are still exported and functional (just deprecated). Removing tests for code that's still in use will drop coverage on those code paths. We should keep these tests until the deprecated types are actually removed.

  • The file-token auth feature is a good addition — the use case is real and the refresh logic looks solid. But could we scope this down to just adding FileTokenAuthConfig in the existing service/ package, following the same pattern as BasicAuthConfig and OAuthConfig? The restructuring into a sub-package, the new interfaces (Provider, TokenSource, Observable), and the deprecations are a lot of surface area change for what's essentially one new auth type.

akshat-kumar-singhal and others added 6 commits April 13, 2026 11:36
Addresses PR gofr-dev#3244 review from @Umang01-hash:

1. Reverts the service/ restructure, sub-package migration of existing
   auth types, and deprecations. BasicAuth/APIKey/OAuth and their tests
   are restored untouched.

2. Adds FileTokenAuthConfig in a new pkg/gofr/service/auth/ sub-package
   containing only new code, so existing types are unaffected. The
   decorator exposes Unwrap() service.HTTP so ConnectionPool /
   CircuitBreaker / Retry can still reach the base *httpService.

3. Adds a generic `interface{ Unwrap() HTTP }` case to
   extractHTTPService in connection_pool.go. This fixes the silent
   no-op the reviewer flagged where pool/CB/retry options stopped
   working when combined with an auth decorator defined outside the
   service package, and future-proofs the unwrap path against the
   same class of bug.

Tests include a regression lock-in that combines FileTokenAuthConfig
with ConnectionPoolConfig end-to-end.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- err113: replace fmt.Errorf with static errAuthHeaderPresent
- bodyclose: close response bodies in tests
- wsl_v5: required blank lines in tests

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- NewFileTokenAuthConfig now requires a file.FileSystem (typically
  file.NewLocalFileSystem(app.Logger())) and reads the token through it,
  dropping the direct os.ReadFile dependency. Mock filesystems can be
  injected in tests.
- Adds examples/using-file-token-auth demonstrating the setup in a
  gofr app pointed at the Kubernetes projected SA token path.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Let the example use gofr's default port instead of hardcoding 9016.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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