Ktor client authentication and authorization plugin that handles various authentication schemes including Basic, Bearer, and Digest authentication.
—
Username/password authentication using HTTP Basic authentication scheme. Suitable for simple authentication scenarios and internal APIs with credential caching support.
Install Basic authentication with credential configuration.
/**
* Install Basic authentication provider
* @param block Configuration block for BasicAuthConfig
*/
fun AuthConfig.basic(block: BasicAuthConfig.() -> Unit)Usage Example:
install(Auth) {
basic {
credentials {
// Load from secure storage or configuration
val creds = credentialStorage.getBasicCredentials()
BasicAuthCredentials(creds.username, creds.password)
}
sendWithoutRequest { request ->
// Send credentials preemptively for specific hosts
request.url.host in listOf("api.internal.com", "secure.example.com")
}
realm = "Admin Area" // Optional realm restriction
}
}Container for Basic authentication credentials.
/**
* Container for basic authentication credentials
* @param username The username for authentication
* @param password The password for authentication
*/
class BasicAuthCredentials(
val username: String,
val password: String
)Configuration for Basic authentication provider.
/**
* Configuration for Basic authentication
*/
class BasicAuthConfig {
/**
* Optional realm restriction for this provider
*/
var realm: String?
/**
* Configure callback to load authentication credentials
* @param block Function that returns credentials or null
*/
fun credentials(block: suspend () -> BasicAuthCredentials?)
/**
* 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)
}The default behavior is to wait for 401 Unauthorized before sending credentials:
sendWithoutRequest { false } // Default: wait for challengeImplementation of Basic authentication provider with credential caching.
/**
* Basic authentication provider implementation
*/
class BasicAuthProvider(
private val credentials: suspend () -> BasicAuthCredentials?,
private val realm: String? = null,
private val sendWithoutRequestCallback: (HttpRequestBuilder) -> Boolean = { false }
) : AuthProvider {
/**
* Clear cached credentials from memory
* Call when credentials are updated or during logout
* Note: This is an internal API and may change in future versions
*/
@InternalAPI
fun clearToken()
}Basic authentication uses the Authorization header with Base64-encoded credentials:
Authorization: Basic <base64(username:password)>Example for username "admin" and password "secret":
Authorization: Basic YWRtaW46c2VjcmV0credentials callback is called only when cache is emptyclearToken() forces reload on next authentication attemptIf a realm is specified:
Example with realm:
WWW-Authenticate: Basic realm="Admin Area"install(Auth) {
basic {
credentials {
BasicAuthCredentials("username", "password")
}
}
}install(Auth) {
basic {
credentials {
val username = System.getenv("API_USERNAME")
val password = System.getenv("API_PASSWORD")
if (username != null && password != null) {
BasicAuthCredentials(username, password)
} else {
null // No credentials available
}
}
}
}install(Auth) {
basic {
credentials {
// Load from database or external service
val userSession = getCurrentUserSession()
if (userSession?.isValid == true) {
BasicAuthCredentials(
userSession.username,
userSession.apiKey
)
} else {
null
}
}
sendWithoutRequest { request ->
// Only send for specific API endpoints
request.url.encodedPath.startsWith("/api/admin/")
}
realm = "Admin API"
}
}install(Auth) {
// Admin API credentials
basic {
credentials { getAdminCredentials() }
realm = "Admin"
}
// User API credentials
basic {
credentials { getUserCredentials() }
realm = "User"
}
}credentials returns nullInstall with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-client-auth