KAuthProvider

sealed class KAuthProvider(source)

Defines how authentication headers are injected into every outgoing request.

Pass an instance via KHttpConfig.authProvider. When null, no auth header is added automatically (useful for cookie-based auth or public APIs).

Built-in providers:

  • Bearer — Authorization: Bearer <token>

  • Jwt — alias of Bearer, explicit for readability

  • Basic — Authorization: Basic <base64(user:pass)>

  • ApiKey — custom header or Authorization: ApiKey <key>

  • Custom — full control via a lambda

Usage:

// Bearer / JWT
KHttpConfig(
baseUrl = "https://api.example.com/",
authProvider = KAuthProvider.Bearer { tokenStore.getToken() },
)

// API key in custom header
KHttpConfig(
baseUrl = "https://api.example.com/",
authProvider = KAuthProvider.ApiKey(header = "X-Api-Key") { "my-secret-key" },
)

// Custom
KHttpConfig(
baseUrl = "https://api.example.com/",
authProvider = KAuthProvider.Custom { request ->
request.header("X-Tenant-Id", "acme")
request.header("X-Signature", computeSignature())
},
)

Inheritors

Constructors

Link copied to clipboard
protected constructor()

Types

Link copied to clipboard
class ApiKey(header: String = HttpHeaders.Authorization, scheme: String = "ApiKey", keyProvider: suspend () -> String?) : KAuthProvider

Injects an API key either in a custom header or as Authorization: ApiKey <key>.

Link copied to clipboard
class Basic(credentialsProvider: suspend () -> Pair<String, String>?) : KAuthProvider

Injects Authorization: Basic <base64(username:password)>.

Link copied to clipboard
class Bearer(tokenProvider: suspend () -> String?) : KAuthProvider

Injects Authorization: Bearer <token>.

Link copied to clipboard
class Custom(block: suspend (HttpRequestBuilder) -> Unit) : KAuthProvider

Escape hatch for any auth scheme not covered above.

Link copied to clipboard
class Jwt(tokenProvider: suspend () -> String?) : KAuthProvider

Semantic alias for Bearer — identical behaviour, clearer intent for JWT flows.