SENDLINE open source
open source / oauth4s / errors

Errors

Every failure the library raises in F is an OAuth4sException, and there are two of them. An OAuthException carries an OAuthError: the server refused, answered something unreadable or could not be reached. A JwtException carries a JwtError: an ID token or a JWT did not verify. Provider.login and OidcClient.complete raise either, so a sign-in endpoint that turns refusals into a 4xx catches OAuth4sException. Catching only OAuthException there would let a forged or foreign ID token through to a 500.

An OAuthException is matched on its error:

scala
def renewed(client: OAuthClient[IO], token: RefreshToken): IO[Option[TokenResponse]] =
  client.refresh(token).map(Option(_)).recoverWith { case failure: OAuthException =>
    failure.error match
      case OAuthError.Rejected(ErrorCode.InvalidGrant, _, _) => IO.none              // revoked or expired: sign in again
      case OAuthError.Transport(cause)                       => IO.raiseError(cause) // the network: safe to try again
      case other                                             => IO.raiseError(IllegalStateException(other.message))
  }
OAuthError When
Rejected(code, description, uri) the server answered with an error document, or the callback carried one
UnexpectedStatus(status, body) a status that is not 2xx with a body that is not an error document
MalformedResponse(reason) a 2xx that is not JSON, or has a field of the wrong shape
MissingField(name) a response without something the protocol requires, such as access_token, or stored fields given to AuthorizationFlow.fromFields without state
MissingEndpoint(name) the client was asked for something its configuration has no endpoint for
StateMismatch(expected, received) the callback is not this flow's
IssuerMismatch(expected, received) the callback or the discovery document speaks for another issuer
InvalidUrl(url, reason) an endpoint in a discovery document cannot be used
InvalidCodeVerifier(reason) CodeVerifier.parse or AuthorizationFlow.fromFields was given something RFC 7636 does not allow
Transport(cause) the HTTP client threw; getCause of the exception is that cause

ErrorCode has the codes of RFC 6749, the device grant and OpenID Connect — InvalidGrant, InvalidClient, AccessDenied, AuthorizationPending, SlowDown, ExpiredToken, LoginRequired, ConsentRequired and the rest — and Other(raw) for one a provider made up. An error document is read whatever its status: VK ID sends errors with HTTP 200, Facebook nests the error in an object, Instagram names the fields differently, and all three are Rejected.

A JwtException is matched on its JwtError, before the OAuthException case:

scala
def signedIn(oidc: OidcClient[IO], flow: AuthorizationFlow, callback: AuthorizationCallback): IO[Either[String, OidcTokens]] =
  oidc.complete(flow, callback).attempt.map {
    case Right(tokens)                                                          => Right(tokens)
    case Left(failure: JwtException) if failure.error == JwtError.NonceMismatch => Left("this sign-in was started somewhere else")
    case Left(failure: JwtException)                                            => Left(s"the ID token was refused: ${failure.error.message}")
    case Left(failure: OAuthException)                                          => Left(failure.error.message)
    case Left(other)                                                            => Left(other.getMessage)
  }

Nothing in the library prints a secret: toString of a configuration, of a ClientAuthentication and of a TokenResponse leaves tokens and secrets out, and the library logs nothing.

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