Ktor client authentication and authorization plugin that handles various authentication schemes including Basic, Bearer, and Digest authentication.
—
OAuth2/JWT token authentication with automatic token refresh and management. Ideal for API authentication with access tokens and optional refresh tokens.
Install Bearer authentication with token loading and refresh configuration.
/**
* Install Bearer authentication provider
* @param block Configuration block for BearerAuthConfig
*/
fun AuthConfig.bearer(block: BearerAuthConfig.() -> Unit)Usage Example:
install(Auth) {
bearer {
loadTokens {
// Load from secure storage
val tokens = tokenStorage.getTokens()
BearerTokens(tokens.accessToken, tokens.refreshToken)
}
refreshTokens { params ->
// Call refresh endpoint
val response = authClient.post("/oauth/refresh") {
setBody(RefreshRequest(params.oldTokens?.refreshToken))
}
val newTokens = response.body<TokenResponse>()
BearerTokens(newTokens.accessToken, newTokens.refreshToken)
}
sendWithoutRequest { request ->
// Send tokens proactively for API calls
request.url.host == "api.example.com"
}
realm = "api" // Optional realm restriction
}
}Container for Bearer authentication tokens.
/**
* Container for bearer tokens
* @param accessToken The access token for API requests
* @param refreshToken Optional refresh token for obtaining new access tokens
*/
class BearerTokens(
val accessToken: String,
val refreshToken: String?
)Configuration for Bearer authentication provider.
/**
* Configuration for Bearer authentication
*/
class BearerAuthConfig {
/**
* Optional realm restriction for this provider
*/
var realm: String?
/**
* Configure callback to load cached tokens from storage
* Note: Using the same client instance here will result in deadlock
* @param block Function that returns cached tokens or null
*/
fun loadTokens(block: suspend () -> BearerTokens?)
/**
* Configure callback to refresh tokens when 401 is received
* @param block Function that receives refresh parameters and returns new tokens
*/
fun refreshTokens(block: suspend RefreshTokensParams.() -> BearerTokens?)
/**
* Configure when to send credentials without waiting for 401
* @param block Function that returns true if credentials should be sent preemptively
*/
fun sendWithoutRequest(block: (HttpRequestBuilder) -> Boolean)
}Parameters provided to the token refresh callback.
/**
* Parameters for token refresh callback
*/
class RefreshTokensParams(
val client: HttpClient,
val response: HttpResponse,
val oldTokens: BearerTokens?
) {
/**
* Mark refresh request to prevent authentication loops
*/
fun HttpRequestBuilder.markAsRefreshTokenRequest()
}Usage Example:
refreshTokens { params ->
// Use the provided client to make refresh request
val refreshRequest = HttpRequestBuilder().apply {
markAsRefreshTokenRequest() // Prevent auth loop
method = HttpMethod.Post
url("https://auth.example.com/refresh")
setBody(params.oldTokens?.refreshToken)
}
val response = params.client.request(refreshRequest)
if (response.status.isSuccess()) {
val tokenData = response.body<TokenResponse>()
BearerTokens(tokenData.accessToken, tokenData.refreshToken)
} else {
null // Refresh failed
}
}Implementation of Bearer authentication provider.
/**
* Bearer authentication provider implementation
*/
class BearerAuthProvider(
private val refreshTokens: suspend RefreshTokensParams.() -> BearerTokens?,
loadTokens: suspend () -> BearerTokens?,
private val sendWithoutRequestCallback: (HttpRequestBuilder) -> Boolean = { true },
private val realm: String?
) : AuthProvider {
/**
* Clear cached tokens from memory
* Call when tokens are updated externally or during logout
*/
fun clearToken()
}The Bearer provider includes sophisticated token management:
When tokens are available, the provider automatically adds the Authorization header:
Authorization: Bearer <access_token>loadTokens is called only when cache is emptyclearToken() forces reload on next requestrefreshTokens callback is invokedIf a realm is specified in configuration:
loadTokens returns nullrefreshTokens returns nullInstall with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-client-auth