SENDLINE open source
open source / oauth4s / providers

Providers

A provider is the endpoints, the default scope and everything the provider does differently, behind one interface:

Member What it does
authorize(redirectUri, options) starts a flow, with the provider's scope unless the options name one
login(flow, callback) finishes it: tokens and a UserProfile
profile(tokens) the profile again, from tokens you kept
oauth the OAuthClient underneath: refresh, revoke, anything else
Provider Protocol PKCE Client authentication Profile comes from Email
Google OAuth 2.0 + OIDC S256 secret in the form the UserInfo endpoint yes, with email_verified
Microsoft OAuth 2.0 + OIDC S256 secret in the form Microsoft Graph's UserInfo yes, never verified
Apple OAuth 2.0, OIDC-like off an ES256 JWT minted per request the ID token, and user on the first sign-in yes, may be a relay address
Facebook OAuth 2.0 off secret in the form Graph API /me when the user has one
Instagram Meta OAuth 2.0 off secret in the form Graph API /me no
Strava OAuth 2.0 off secret in the form the athlete in the token response no
VkId OAuth 2.1 S256, mandatory none: a public client user_info, by POST with the email scope
X OAuth 2.0 + PKCE S256, mandatory Basic, or none for a public client /2/users/me with the users.email scope

What login puts in the UserProfile. The id is the key to store an account under, and the only one a provider promises to keep. Link a new sign-in to an existing account by its email only when emailVerified is Some(true). A None means the provider does not say, and an address it does not vouch for may belong to someone else.

Provider id id stays the same email emailVerified
Google sub for the Google account with the email scope the email_verified claim
Microsoft sub for the account, and different in each application the email claim None: Microsoft does not verify it, and in a work tenant an administrator sets it
Apple sub for the Apple ID, within one developer team on every sign-in, may be a private relay address the email_verified claim
Facebook id for the user, and different in each application when the user has one None
Instagram user_id for the account, and different in each application never None
Strava the athlete's id for the athlete never None
VkId user_id for the VK account with the email scope None: VK ID's user_info carries no verification flag, so treat the address as unconfirmed
X id for the account confirmed_email, with the users.email scope Some(true) when there is one, since X only returns a confirmed address

Google

scala
val google: Provider[IO] = Google[IO](id, secret)

val withRefreshToken: IO[AuthorizationFlow] =
  google.authorize(redirect, Google.Offline) // access_type=offline and prompt=consent

Google returns a refresh token on the first consent only; Google.Offline asks for consent again so that it returns one every time. The ID token's issuer is accepted in both spellings Google uses, with and without https://.

Microsoft

scala
val microsoft: Provider[IO] = Microsoft[IO](id, secret)                          // Microsoft.Common: any account
val workOnly: Provider[IO]  = Microsoft[IO](id, secret, Microsoft.Organizations) // or Microsoft.Consumers, or a tenant id

The multi-tenant endpoints publish an issuer with a placeholder in it, so the issuer cannot be compared as a string. The token's tid claim names its tenant, the issuer must be that tenant's, and when you gave a tenant id, tid must be it. Microsoft has no email-verified flag and says not to use the email for authorization; emailVerified is None.

Apple

scala
def apple(p8: String): Provider[IO] =
  val key = JwtSigner.privateKey(p8, "EC").fold(error => throw error.toThrowable, identity)

  Apple[IO](ClientId("com.example.web"), "TEAM123456", "KEY1234567", key) // Services ID, Team ID, Key ID

def appleCallback(provider: Provider[IO], flow: AuthorizationFlow, requestBody: String): IO[Login] =
  provider.login(flow, AuthorizationCallback.fromForm(requestBody)) // Apple posts; `user` comes on the first sign-in only

Apple's client secret is a JWT signed with your key, valid for five minutes and made again for each request. Apple has no profile endpoint: the id and the email come from the ID token, and the name comes once, in the user field of the first callback, so keep it when it arrives. Apple does not allow localhost as a redirect URI.

Facebook

scala
val facebook: Provider[IO] = Facebook[IO](id, secret)

def sixtyDays(token: AccessToken): IO[TokenResponse] =
  Facebook.longLived(facebook, token) // fb_exchange_token

Facebook issues no refresh tokens; a short-lived token is exchanged for one that lasts about sixty days. The Graph API version is Facebook.Version and is a parameter of Facebook(…) and Facebook.config(…).

Instagram

scala
val instagram: Provider[IO] = Instagram[IO](id, secret)

def instagramSixtyDays(token: AccessToken): IO[TokenResponse] =
  Instagram.longLived[IO](secret, token) // ig_exchange_token, on graph.instagram.com

def instagramAgain(token: AccessToken): IO[TokenResponse] =
  Instagram.refreshed[IO](token) // ig_refresh_token, for a long-lived token that is at least a day old

This is the Instagram API with Instagram Login, for professional accounts. The Basic Display API it replaced was shut down in December 2024.

Strava

scala
val strava: Provider[IO] = Strava[IO](id, secret)

val activities: IO[AuthorizationFlow] =
  strava.authorize(redirect, AuthorizationOptions.scoped("read", "activity:read")) // sent as read,activity:read

Users can untick scopes on Strava's consent screen: tokens.scope is what was granted. Strava rotates refresh tokens, and the old one stops working at once.

VK ID

scala
val vk: Provider[IO] = VkId[IO](id)

def vkAgain(token: RefreshToken, callback: AuthorizationCallback, state: State): IO[TokenResponse] =
  VkId.refresh(vk, token, callback.parameter(VkId.DeviceId).getOrElse(""), state) // the device_id of the first callback

VK ID returns a device_id on the callback and wants it back in the code exchange and in every refresh; the exchange is handled, the refresh needs the value you kept. Errors arrive with HTTP 200 and are read as errors all the same. VkId.config(clientId, serviceToken) adds the service token of a confidential application.

X

scala
val x: Provider[IO]       = X[IO](id, secret) // a confidential client: Basic authentication
val xPublic: Provider[IO] = X.public[IO](id)  // a public client: PKCE alone

val xWithRefreshToken: IO[AuthorizationFlow] =
  x.authorize(redirect, AuthorizationOptions.default.withScope(X.Offline)) // adds offline.access

An authorization code from X lives thirty seconds. The email is in the profile only when the application asks for the users.email scope.

Changing what a provider does

Every provider object has config(…), which returns the configuration it would use, and from(config), which builds the provider from one you changed. Each profile(json) is a pure function you can test against a recorded response.

scala
val olderGraph: Provider[IO] =
  Facebook.from(Facebook.config(id, secret, version = "v26.0").withScope(Scope.of("public_profile")))

def whoIs(provider: Provider[IO], tokens: TokenResponse): IO[UserProfile] =
  provider.profile(tokens) // the profile again, later, from tokens you kept

def revoke(tokens: TokenResponse): IO[Unit] =
  google.oauth.revokeAccessToken(tokens.accessToken) // the OAuth client underneath, for everything else

For a provider that is not in the list, see a provider of your own.

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