Ktor client authentication and authorization plugin for JavaScript platforms supporting Basic, Digest, and Bearer token authentication with automatic token refresh
npx @tessl/cli install tessl/maven-io-ktor--ktor-client-auth-js@3.2.0Ktor Client Auth is a comprehensive authentication and authorization plugin for Ktor HTTP clients targeting JavaScript platforms. It provides a modular, provider-based authentication system supporting multiple authentication schemes including Basic, Digest, and Bearer token authentication with automatic token refresh, unauthorized response detection, and circuit breaker functionality to prevent infinite authentication loops.
build.gradle.kts:implementation("io.ktor:ktor-client-auth-js:3.2.0")import io.ktor.client.plugins.auth.*
import io.ktor.client.plugins.auth.providers.*import io.ktor.client.*
import io.ktor.client.plugins.auth.*
import io.ktor.client.plugins.auth.providers.*
// Create HTTP client with Basic authentication
val client = HttpClient {
Auth {
basic {
credentials {
BasicAuthCredentials("username", "password")
}
}
}
}
// Create HTTP client with Bearer token authentication
val clientWithBearer = HttpClient {
Auth {
bearer {
loadTokens {
BearerTokens("access_token", "refresh_token")
}
refreshTokens { params ->
// Refresh logic here
BearerTokens("new_access_token", "new_refresh_token")
}
}
}
}Ktor Client Auth is built around several key components:
Core authentication plugin installation and configuration with multiple provider support and customizable unauthorized response detection.
val Auth: ClientPlugin<AuthConfig>
fun HttpClientConfig<*>.Auth(block: AuthConfig.() -> Unit)
class AuthConfig {
val providers: MutableList<AuthProvider>
fun reAuthorizeOnResponse(block: suspend (HttpResponse) -> Boolean)
}
val AuthCircuitBreaker: AttributeKey<Unit>HTTP Basic authentication provider supporting username/password credentials with optional realm specification and configurable sending behavior.
fun AuthConfig.basic(block: BasicAuthConfig.() -> Unit)
class BasicAuthConfig {
var realm: String?
fun credentials(block: suspend () -> BasicAuthCredentials?)
fun sendWithoutRequest(block: (HttpRequestBuilder) -> Boolean)
}
class BasicAuthCredentials(
val username: String,
val password: String
)
class BasicAuthProvider : AuthProviderBearer token authentication provider with support for automatic token refresh, access/refresh token pairs, and circuit breaker functionality.
fun AuthConfig.bearer(block: BearerAuthConfig.() -> Unit)
class BearerAuthConfig {
var realm: String?
fun refreshTokens(block: suspend RefreshTokensParams.() -> BearerTokens?)
fun loadTokens(block: suspend () -> BearerTokens?)
fun sendWithoutRequest(block: (HttpRequestBuilder) -> Boolean)
}
class BearerTokens(
val accessToken: String,
val refreshToken: String?
)
class RefreshTokensParams(
val client: HttpClient,
val response: HttpResponse,
val oldTokens: BearerTokens?
) {
fun HttpRequestBuilder.markAsRefreshTokenRequest()
}
class BearerAuthProvider : AuthProviderHTTP Digest authentication provider supporting MD5 and other hash algorithms with automatic nonce handling and client nonce generation.
fun AuthConfig.digest(block: DigestAuthConfig.() -> Unit)
class DigestAuthConfig {
var algorithmName: String
var realm: String?
fun credentials(block: suspend () -> DigestAuthCredentials?)
}
class DigestAuthCredentials(
val username: String,
val password: String
)
class DigestAuthProvider : AuthProviderinterface AuthProvider {
fun sendWithoutRequest(request: HttpRequestBuilder): Boolean
fun isApplicable(auth: HttpAuthHeader): Boolean
suspend fun addRequestHeaders(request: HttpRequestBuilder, authHeader: HttpAuthHeader? = null)
suspend fun refreshToken(response: HttpResponse): Boolean
}
val HttpClient.authProviders: List<AuthProvider>
inline fun <reified T : AuthProvider> HttpClient.authProvider(): T?