OAuth client for Kotlin: Ktor OAuth library
The Ktor OAuth library is an official extension for the Ktor HTTP client, designed to implement the OAuth 2.0 protocol in Kotlin applications. It provides a flexible and type-safe way to integrate with popular OAuth providers (Google, GitHub, Facebook, VK, etc.) and also supports custom endpoints. The library is part of the Ktor ecosystem and can be used in both server-side and client-side Kotlin applications.
The main use case is obtaining access tokens for working with protected REST APIs. The library automates the process of exchanging an authorization code for tokens, refreshing tokens (refresh token), and managing session state.
Key Features
- Support for OAuth 2.0 flows: Authorization Code Grant, Implicit Grant, Client Credentials, Resource Owner Password Credentials.
- Built-in support for popular providers: Google, GitHub, Facebook, Twitter, VK, Yandex, Microsoft, and others.
- Custom providers: ability to manually set authorization, token, and redirect endpoints.
- Automatic token refresh: support for refresh token with automatic invocation when the access token expires.
- Session management: storing tokens in memory, files, databases, or custom storages.
- Integration with Ktor Client: unified configuration via plugins, support for all transports (CIO, Apache, OkHttp).
- Security: support for PKCE (Proof Key for Code Exchange) to protect against CSRF attacks.
Installation
To add the library, include the dependency in build.gradle.kts:
dependencies { // Main Ktor client implementation("io.ktor:ktor-client-core:2.3.7") // OAuth plugin implementation("io.ktor:ktor-client-auth:2.3.7") // Specific engine (e.g., CIO) implementation("io.ktor:ktor-client-cio:2.3.7")}For Maven:
<dependency> <groupId>io.ktor</groupId> <artifactId>ktor-client-auth</artifactId> <version>2.3.7</version></dependency>Example: Authorization via Google OAuth
Create an HTTP client with the OAuth plugin configured for Google:
import io.ktor.client.*import io.ktor.client.plugins.auth.*import io.ktor.client.plugins.auth.providers.*import io.ktor.client.request.*import io.ktor.client.statement.*
suspend fun main() { val client = HttpClient { install(Auth) { oauth { clientId = System.getenv("GOOGLE_CLIENT_ID") clientSecret = System.getenv("GOOGLE_CLIENT_SECRET") providerLookup = { OAuthServerSettings.OAuthAuthorizationCodeServerSettings( authorizeUrl = "https://accounts.google.com/o/oauth2/auth", tokenUrl = "https://oauth2.googleapis.com/token", clientId = clientId, clientSecret = clientSecret, redirectUrl = "http://localhost:8080/callback" ) } urlProvider = { request -> // Generate URL for user redirect "http://localhost:8080/login" } } } }
// Example request to Google Sheets API val response: HttpResponse = client.get("https://sheets.googleapis.com/v4/spreadsheets/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgVE2upms/values/Sheet1!A1:D5") println(response.bodyAsText())}Example: Custom provider with PKCE
To enhance security, use PKCE (P
Also in library
NodaTime C# (.NET) Guide: Complete Tutorial with Examples
WebSocket Libraries TypeScript: ws, Socket.IO, uWebSockets.js
Working with Excel in PHP: PhpSpreadsheet and OpenSpout Libraries