SENDLINE open source
open source / oauth4s / openid connect

OpenID Connect

oauth4s-oidc is the same client plus what makes a token an identity.

scala
val discovered: IO[OidcClient[IO]] =
  OidcClient.discover[IO](Issuer("https://auth.example.com"), ClientId("client-1"), ClientAuthentication.Public)

def start(oidc: OidcClient[IO]): IO[AuthorizationFlow] =
  oidc.authorize(redirect, AuthorizationOptions.scoped("email")) // openid and a nonce are added for you

def finish(oidc: OidcClient[IO], flow: AuthorizationFlow, requestUrl: String): IO[(Option[String], Option[String], UserInfo)] =
  for
    tokens <- oidc.complete(flow, AuthorizationCallback.fromUrl(requestUrl)) // exchanges the code, then verifies the ID token
    info   <- oidc.userInfo(tokens.response.accessToken)
  yield (tokens.user.subject, tokens.user.email, info)

OidcTokens is the token response and the verified ID token. tokens.user reads its claims as the standard ones — subject, name, givenName, familyName, preferredUsername, email, emailVerified, picture, locale — and UserInfo reads the same from the UserInfo endpoint.

What complete verifies

After everything the OAuth client checks, and before it returns:

Check Failure
the response has an id_token OAuthError.MissingField("id_token")
alg is on the allow-list: asymmetric algorithms only, unless you say otherwise JwtError.AlgorithmNotAllowed, UnsupportedAlgorithm
no crit header JwtError.UnsupportedCritical
the signature verifies with a key of the provider JwtError.InvalidSignature, UnknownKey
exp is in the future and nbf in the past, with a minute of leeway JwtError.Expired, NotYetValid, MissingClaim("exp")
iss is the issuer JwtError.IssuerMismatch
aud contains the client id JwtError.AudienceMismatch
azp, when present, is the client id; it must be present when there are several audiences JwtError.InvalidClaim("azp", …), MissingClaim("azp")
nonce is the flow's JwtError.NonceMismatch
at_hash, when present, matches the access token JwtError.InvalidClaim("at_hash", …)

A failed check fails the effect with a JwtException.

The provider's keys

The key set is fetched from jwks_uri the first time a token is verified and kept for an hour. A token that names a key id the set does not have triggers a refetch — that is how a rotation is picked up — but at most once a minute, so tokens with invented key ids cannot be turned into requests to the provider.

Verifying alone, and signing out

scala
def verified(oidc: OidcClient[IO], idToken: IdToken): IO[Jwt] =
  oidc.verify(idToken) // the checks of complete, for an ID token that arrived another way

def signOut(oidc: OidcClient[IO], idToken: IdToken): Either[OAuthError, String] =
  oidc.logoutUrl(Some(idToken), Some(RedirectUri("https://app.example.com/bye"))) // where to send the browser

Without discovery

scala
def byHand(client: OAuthClientConfig): OidcConfig =
  OidcConfig(client, Issuer("https://auth.example.com"))
    .withKeysLifetime(6.hours)   // how long the provider's keys are kept
    .withKeysCooldown(5.minutes) // how often an unknown key id may cause a refetch
    .withValidation(_.withLeeway(30.seconds))

The client's endpoints must include the key set (Endpoints.withJwks). The leeway is the clock skew an expiry is forgiven, a minute by default; withValidation reaches everything else a JwtValidation has.

built from README.md at v0.1.1 edit this page on GitHub