Skip to content

Conversation

@ruben-caceiro-digiwest
Copy link

Subsystem
Client, related module

Motivation
Allow non cache Bearer token

Solution
I simply add a cache flag when the flag is false it ignores the cached token and get always a new one.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 1, 2025

Walkthrough

Adds an optional caching flag to token handling. AuthTokenHolder gets a cache parameter and a conditional fast-path cached-value return. BearerAuthProvider and BearerAuthConfig are updated to accept and propagate the cache flag and initialize the token holder with it.

Changes

Cohort / File(s) Summary
Auth token caching support
ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/AuthTokenHolder.kt, ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/BearerAuthProvider.kt
Add cache: Boolean parameter to AuthTokenHolder and gate the hot-path cached-value return by it; introduce a volatile cached value field. Add cache: Boolean = true to BearerAuthConfig and update BearerAuthProvider constructor and its tokensHolder initialization to pass the cache flag. Adjust call sites in AuthConfig.bearer to forward cache.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • Stexxe
  • e5l
  • bjhham

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The pull request title "Add support to configure token caching in BearerAuthProvider" directly and clearly reflects the main objective of the changeset. The changes involve introducing a new cache parameter to control token caching behavior in the BearerAuthProvider and related components (AuthTokenHolder and BearerAuthConfig). The title is specific, concise, and accurately conveys the primary modification without being vague or overly broad.
Description Check ✅ Passed The pull request description includes all required sections from the template: Subsystem is identified as "Client, related module," Motivation is provided with a reference to the JetBrains YourTrack ticket #KTOR-8932, and Solution concisely explains the implementation of a cache flag to control token caching behavior. While the solution description is relatively brief and could provide more technical detail about the specific modifications to AuthTokenHolder and BearerAuthProvider, all critical sections are present with sufficient information to understand the purpose and approach of the change.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e35ffe2 and 6c0d37d.

📒 Files selected for processing (1)
  • ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/BearerAuthProvider.kt (4 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{kt,kts}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{kt,kts}: Follow Kotlin official style guide for all Kotlin source and build scripts
Use star imports for io.ktor.* packages
Max line length is 120 characters
Indent with 4 spaces in Kotlin code
Include a copyright header in new Kotlin files

Files:

  • ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/BearerAuthProvider.kt
**/*.kt

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.kt: Document all public Kotlin APIs, including parameters, return types, and exceptions
Annotate internal APIs with @internalapi
Follow Kotlin error-handling conventions and use specific Ktor exceptions

Files:

  • ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/BearerAuthProvider.kt
🔇 Additional comments (4)
ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/BearerAuthProvider.kt (4)

20-24: LGTM! Correct propagation of the cache parameter.

The bearer DSL function correctly passes the cache configuration to the BearerAuthProvider constructor using named arguments, ensuring proper initialization of the caching behavior.


63-73: LGTM! Comprehensive documentation for the public API.

The cache property is well-documented with clear explanations of the caching behavior, default value, and usage scenarios. This addresses the previous review feedback effectively.


128-128: LGTM! Correct initialization of the token holder with caching behavior.

The tokensHolder is properly initialized with the cache parameter, correctly wiring the configuration through to the AuthTokenHolder implementation.


117-117: No direct BearerAuthProvider usages found.
Adding the cache parameter remains a breaking change for any positional callers—document this change and include migration advice in the release notes.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/BearerAuthProvider.kt (1)

105-111: Document the new constructor parameter.

The BearerAuthProvider is a public class, and per coding guidelines, public APIs should be fully documented. The new cache parameter should be documented in the class KDoc.

Apply this diff to add parameter documentation:

 /**
  * An authentication provider for the Bearer HTTP authentication scheme.
  * Bearer authentication involves security tokens called bearer tokens.
  * As an example, these tokens can be used as a part of OAuth flow to authorize users of your application
  * by using external providers, such as Google, Facebook, Twitter, and so on.
  *
  * You can learn more from [Bearer authentication](https://ktor.io/docs/bearer-client.html).
  *
+ * @param refreshTokens A callback to refresh tokens when a 401 response is received.
+ * @param cache Whether to cache loaded and refreshed tokens. When `false`, tokens are loaded fresh on every request.
+ * @param loadTokens A callback to load tokens from local storage.
+ * @param sendWithoutRequestCallback A predicate to determine if credentials should be sent preemptively.
+ * @param realm The authentication realm to match, or null to match any realm.
+ *
  * [Report a problem](https://ktor.io/feedback/?fqname=io.ktor.client.plugins.auth.providers.BearerAuthProvider)
  */
 public class BearerAuthProvider(
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between af06bea and e35ffe2.

📒 Files selected for processing (2)
  • ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/AuthTokenHolder.kt (2 hunks)
  • ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/BearerAuthProvider.kt (4 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{kt,kts}

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.{kt,kts}: Follow Kotlin official style guide for all Kotlin source and build scripts
Use star imports for io.ktor.* packages
Max line length is 120 characters
Indent with 4 spaces in Kotlin code
Include a copyright header in new Kotlin files

Files:

  • ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/BearerAuthProvider.kt
  • ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/AuthTokenHolder.kt
**/*.kt

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.kt: Document all public Kotlin APIs, including parameters, return types, and exceptions
Annotate internal APIs with @internalapi
Follow Kotlin error-handling conventions and use specific Ktor exceptions

Files:

  • ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/BearerAuthProvider.kt
  • ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/AuthTokenHolder.kt
🔇 Additional comments (6)
ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/AuthTokenHolder.kt (3)

14-17: LGTM! Clean parameter addition.

The new cache parameter is well-positioned and follows Kotlin conventions. Making it private val ensures proper encapsulation.


18-22: LGTM! Improved volatile field formatting.

Moving @Volatile to its own line improves readability and aligns with common Kotlin practices for visibility of important annotations.


36-36: Thread-safety behavior remains unchanged
When cache=false, loadToken always acquires the mutex (bypassing the hot-path) and reloads under lock, so there’s no unsynchronized TOCTOU window. When cache=true, the optimistic if (cache && value != null) read is identical to the original implementation (using a @Volatile field), so this same race window still exists by design for performance. No changes required.

ktor-client/ktor-client-plugins/ktor-client-auth/common/src/io/ktor/client/plugins/auth/providers/BearerAuthProvider.kt (3)

22-22: LGTM! Correct integration.

The bearer extension properly passes the new cache parameter from config to provider.


107-107: LGTM! Constructor parameter added with sensible default.

The cache parameter is well-positioned (after refreshTokens, before loadTokens) and has a sensible default value of true to maintain backward compatibility. Since BearerAuthProvider is a public class, users may instantiate it directly, and the default ensures existing code continues to work.


118-118: LGTM! Correct initialization.

The tokensHolder initialization properly propagates the cache parameter to AuthTokenHolder.

…/ktor/client/plugins/auth/providers/BearerAuthProvider.kt

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.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.

1 participant