oauth4s
OAuth 2.0 / 2.1 and OpenID Connect client for Scala 3, with Google, Microsoft, Apple and five more providers ready to
use. No hardcoded cats-effect, ZIO or Kyo, no JSON library and no HTTP client: the runtime (cats-effect /
ZIO / Kyo / rapid), the codec (circe / zio-json / jsoniter) and the transport (java.net.http / http4s /
sttp) are modules wired in through a given, and core depends on nothing.
Install
libraryDependencies ++= Seq(
"now.sendline" %% "oauth4s-cats" % "0.1.1",
"now.sendline" %% "oauth4s-http-jdk" % "0.1.1",
"now.sendline" %% "oauth4s-providers" % "0.1.1",
)
import oauth4s.runtime.cats.CatsInstances.givenlibraryDependencies ++= Seq(
"now.sendline" %% "oauth4s-zio" % "0.1.1",
"now.sendline" %% "oauth4s-http-jdk" % "0.1.1",
"now.sendline" %% "oauth4s-providers" % "0.1.1",
)
import oauth4s.runtime.zio.ZioInstances.givenlibraryDependencies ++= Seq(
"now.sendline" %% "oauth4s-kyo" % "0.1.1",
"now.sendline" %% "oauth4s-http-jdk" % "0.1.1",
"now.sendline" %% "oauth4s-providers" % "0.1.1",
)
import oauth4s.runtime.kyo.KyoInstances.givenlibraryDependencies ++= Seq(
"now.sendline" %% "oauth4s-rapid" % "0.1.1",
"now.sendline" %% "oauth4s-http-jdk" % "0.1.1",
"now.sendline" %% "oauth4s-providers" % "0.1.1",
)
import oauth4s.runtime.rapid.RapidInstances.givenThe runtime you pick here is remembered on every page and in every library. An HTTP client goes next to the runtime: java.net.http here, or http4s or sttp. oauth4s-rapid needs JDK 21 and oauth4s-kyo JDK 25; every other module runs on JDK 17.
At a glance
The checks a specification asks people to remember are what the types do:
- PKCE is on unless you turn it off.
authorizegenerates the verifier and keeps it in the flow,completesends it back. OAuth 2.1 is the default, and OAuth 2.0 is what a provider gets when it cannot do better. - State is checked where the callback arrives.
complete(flow, callback)compares thestate, and the issuer of RFC 9207 when the server sends one, before the code goes anywhere. - A client that needs a secret cannot be built without one.
ClientAuthenticationisPublic,SecretBasic,SecretPostorSecretMinted. There is no optional secret to forget. - An endpoint is
https, or loopback.Endpointrefuses anything else, in your configuration and in a discovery document alike. - An ID token is verified before it is read. Signature against the provider's keys,
iss,aud,azp,exp,nonceandat_hash. Symmetric algorithms andnoneare off by default. - An error is a value.
OAuthError.Rejected(ErrorCode.InvalidGrant, …)is a pattern, not a string comparison, and the providers that answer in their own dialect are read into the same value.
flowchart LR
app["your service"] --> providers
app --> oidc
app --> core
subgraph core["oauth4s-core · no dependencies"]
C["OAuthClient · code + PKCE, client credentials, refresh, device, revoke, introspect"]
M["ClientId · Scope · Endpoint · TokenResponse · OAuthError"]
S["Effect · HttpClient · JsonCodec"]
end
providers["oauth4s-providers<br/>Google · Microsoft · Apple · Facebook<br/>Instagram · Strava · VK ID · X"] --> oidc
oidc["oauth4s-oidc<br/>discovery · ID token · UserInfo · logout"] --> jwt
jwt["oauth4s-jwt<br/>JWT · JWK · JWKS"] --> core
RT["runtime<br/>cats-effect · ZIO · Kyo · rapid"] -->|given Effect| core
HT["http<br/>java.net.http · http4s · sttp"] -->|HttpClient| core
JS["json<br/>circe · zio-json · jsoniter"] -->|given JsonCodec| core
Quick start
Runs on JDK 17 or newer. oauth4s-rapid needs 21 and oauth4s-kyo needs 25, like the libraries they wrap.
libraryDependencies ++= Seq(
"now.sendline" %% "oauth4s-cats" % "0.1.1", // core + cats-effect
"now.sendline" %% "oauth4s-http-jdk" % "0.1.1", // java.net.http: no HTTP library to add
"now.sendline" %% "oauth4s-providers" % "0.1.1", // Google, Microsoft, Apple, …; brings oidc and jwt
"now.sendline" %% "oauth4s-testkit" % "0.1.1" % Test,
)import cats.effect.IO
import oauth4s.*
import oauth4s.http.HttpClient
import oauth4s.http.jdk.JdkHttpClient
import oauth4s.providers.{Google, Login}
import oauth4s.runtime.cats.CatsInstances.given
given HttpClient[IO] = JdkHttpClient[IO]()
val google = Google[IO](ClientId("…apps.googleusercontent.com"), ClientSecret("…"))
val redirect = RedirectUri("https://app.example.com/callback")
// 1. send the browser to flow.url and keep the flow next to the user's session
val start: IO[AuthorizationFlow] = google.authorize(redirect)
// 2. the provider sends the browser back
def signedIn(flow: AuthorizationFlow, requestUrl: String): IO[Login] =
google.login(flow, AuthorizationCallback.fromUrl(requestUrl))Login is the tokens and a UserProfile(provider, id, name, username, email, emailVerified, picture, raw). Inside
login the state was compared, the code was exchanged with the PKCE verifier, the ID token was verified against
Google's keys and the nonce of this flow, and the profile was read.
Every piece is replaceable without touching the code between the imports:
| To change | Swap | For |
|---|---|---|
| the runtime | oauth4s-cats and its import |
oauth4s-zio, oauth4s-kyo, oauth4s-rapid, or nothing at all for scala.util.Try |
| the HTTP client | oauth4s-http-jdk and JdkHttpClient |
oauth4s-http4s, oauth4s-sttp, or twelve lines of your own |
| the JSON library | nothing: a codec is built in | oauth4s-circe, oauth4s-zio-json, oauth4s-jsoniter |
| the provider | Google |
another of the eight, any OpenID Connect issuer, or a plain OAuth 2.0 server |