Ktor client authentication and authorization plugin that handles various authentication schemes including Basic, Bearer, and Digest authentication.
—
Challenge-response authentication using HTTP Digest authentication scheme. Provides improved security over Basic auth by using cryptographic hashes instead of plain text credentials.
Install Digest authentication with credential and algorithm configuration.
/**
* Install Digest authentication provider
* @param block Configuration block for DigestAuthConfig
*/
fun AuthConfig.digest(block: DigestAuthConfig.() -> Unit)Usage Example:
install(Auth) {
digest {
credentials {
// Load from secure storage
val creds = credentialStorage.getDigestCredentials()
DigestAuthCredentials(creds.username, creds.password)
}
algorithmName = "MD5" // Default hash algorithm
realm = "Protected Area" // Optional realm restriction
}
}Container for Digest authentication credentials.
/**
* Container for digest authentication credentials
* @param username The username for authentication
* @param password The password for authentication (used for hash computation)
*/
class DigestAuthCredentials(
val username: String,
val password: String
)Configuration for Digest authentication provider.
/**
* Configuration for Digest authentication
*/
class DigestAuthConfig {
/**
* Hash algorithm for digest computation (default: "MD5")
*/
var algorithmName: String
/**
* 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 () -> DigestAuthCredentials?)
}Implementation of Digest authentication provider with challenge-response handling.
/**
* Digest authentication provider implementation
*/
class DigestAuthProvider(
private val credentials: suspend () -> DigestAuthCredentials?,
val realm: String? = null,
val algorithmName: String = "MD5"
) : 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()
}The digest is calculated using this process:
HA1 = MD5(username:realm:password)
HA2 = MD5(method:uri)
response = MD5(HA1:nonce:nc:cnonce:qop:HA2)Where:
nonce: Random value from server challengenc: Request counter (hexadecimal, zero-padded to 8 digits)cnonce: Client-generated random valueqop: Quality of protection (from server challenge)Digest authentication handles these WWW-Authenticate parameters:
Example Challenge:
WWW-Authenticate: Digest realm="Protected Area",
nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
qop="auth",
opaque="5ccc069c403ebaf9f0171e9517f40e41"install(Auth) {
digest {
credentials {
DigestAuthCredentials("user", "password")
}
}
}install(Auth) {
digest {
credentials {
// Load from secure configuration
val config = loadSecureConfig()
DigestAuthCredentials(
username = config.digestUsername,
password = config.digestPassword
)
}
algorithmName = "SHA-256" // Use stronger algorithm if supported
realm = "API Access" // Restrict to specific realm
}
}install(Auth) {
// Admin realm
digest {
credentials { getAdminDigestCredentials() }
realm = "Admin"
}
// User realm
digest {
credentials { getUserDigestCredentials() }
realm = "User"
algorithmName = "SHA-256"
}
}Supported algorithms (server dependent):
The provider automatically manages request counters:
To migrate from Basic to Digest authentication:
// Before: Basic Auth
install(Auth) {
basic {
credentials { BasicAuthCredentials("user", "pass") }
}
}
// After: Digest Auth
install(Auth) {
digest {
credentials { DigestAuthCredentials("user", "pass") }
algorithmName = "SHA-256" // Upgrade algorithm if possible
}
}No code changes needed in request handling - the provider handles all digest computation automatically.
Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-client-auth