cromo The Cromo Guide
Download PDF IT

std.oauth

OAuth 2.0 and OpenID Connect, for a backend whose users log in with an account of Microsoft, of Google, of a Keycloak or of any provider that speaks them: the login with a code (RFC 6749, with PKCE of RFC 7636), the tokens of a service for another service, and the check of the tokens that arrive at an API.

cromo
import std.oauth

oauth.Client app
app.provider = oauth.google()
app.id = "the id of the client"
app.secret = "its secret"
app.redirectUrl = "https://example.com/callback"

oauth.Login login = app.login()        // keep it in the session, send the user to login.url
// ... the provider sends the user back to /callback?state=...&code=...
oauth.Tokens tokens = app.exchange(login, state, code)
println("{} <{}>", tokens.identity.name, tokens.identity.email)

An API checks the token of each request with a Verifier, which downloads the keys of the provider once and keeps them:

cromo
oauth.Verifier verifier
verifier.provider = oauth.keycloak("https://id.example.com", "shop")
verifier.audience = "shop-api"
oauth.Claims who = verifier.claims(oauth.bearer(req))   // or an exception: answer 401

The guide: Networking and HTTP.

Made from the documentation in the sources of the library.

#Types

#Provider

cromo
struct Provider

The addresses of a provider of identity, as its configuration of OpenID Connect gives them. discover reads them from the provider; a program can also write them by hand, for a provider of OAuth 2.0 without OpenID Connect, as GitHub.

  • string issuer: "https://accounts.google.com": the iss of its tokens
  • string authorizeUrl: where the user logs in
  • string tokenUrl: where the tokens are asked
  • string userInfoUrl: what the provider knows of the user of a token, or ""
  • string keysUrl: the keys that sign its tokens, a JWK Set, or ""
  • string logoutUrl: where the session of the user is ended, or ""

#Claims

cromo
struct Claims

What a token of OpenID Connect says of its user: the claims of the standard, each empty when the token does not have it. A program that needs others, as the roles of a Keycloak, reads them into a struct of its own with Verifier.verify.

  • string iss: the issuer: the provider
  • string sub: the id of the user at the provider, which never changes
  • json.Value aud: for whom the token is: a text, or an array of them
  • i64 exp: when it ends, in seconds of Unix
  • i64 iat: when it was made
  • string email
  • string name: the full name
  • string username
  • string givenName
  • string familyName
  • string picture: the address of a picture of the user
  • string nonce: of the login that asked the token
  • string azp: the client that asked the token
  • string tid: of Microsoft: the id of the organization of the user
  • string scope: of an access token: what it allows, separated by spaces

#Tokens

cromo
struct Tokens

The tokens that a provider gives.

  • string access: the access token: sent to the APIs as "Authorization: Bearer ..."
  • string type: "Bearer"
  • i64 expires: when the access token ends, in seconds of Unix; 0 when the provider does not say
  • string refresh: the refresh token, for refresh, or ""
  • string id: the ID token, a JWT, or "" without the scope openid
  • string scope: what the access token allows, when the provider says it
  • Claims identity: the claims of the ID token, checked; empty without one

#Login

cromo
struct Login

A login that has started: where to send the user, and what to keep until the provider sends the user back, as in the session of the user on the server.

  • string url: the page of the provider, with the request of the client
  • string state: comes back with the user: exchange checks it
  • string verifier: of PKCE: only who started the login can end it
  • string nonce: comes back in the ID token: exchange checks it

#Verifier

cromo
struct Verifier

Checks the tokens of a provider, as an API does with the token of each request: that a key of the provider signed it, that its time has not passed, that it is of the issuer of the provider and, when audience is set, for this API. The keys are downloaded at the first token and kept for a day, and again when a token names a key that is not among them. Its methods are const: the tasks of a server share one verifier. It can be moved, not copied.

Only a JWT can be checked this way: the ID tokens, and the access tokens of Microsoft and of Keycloak. An access token of Google is not one: Client.userInfo asks Google whose it is.

  • Provider provider
  • string audience: The audience that the aud of the tokens must name: the id of the API at the provider. "" does not check it, and then a token made for another service of the same provider passes.
  • i64 leeway: the seconds of tolerance on the times, for the clocks that differ

#Verifier.verify

cromo
T verify<T : json.Data>(string token) const

The claims of the token as the struct T, when it passes the checks. Otherwise jwt.Invalid with its reason, as Expired, or an Error for a token of another issuer or a provider that does not give its keys.

#Verifier.claims

cromo
Claims claims(string token) const

The claims of the standard of the token, when it passes the checks, as verify<oauth.Claims>.

#Client

cromo
struct Client

An application registered at a provider: a backend that logs its users in, or a service that asks tokens for itself. Its methods are const: the tasks of a server share one client. It can be moved, not copied.

  • Provider provider
  • string id: the id of the client at the provider
  • string secret: its secret; "" for a client without one, which PKCE protects
  • string redirectUrl: where the provider sends the user back: registered at the provider
  • list<string> scopes: What the client asks: "openid" for the ID token, "profile" and "email" for the name and the address, "offline_access" for a refresh token at Microsoft and Keycloak. Without any, login asks openid, profile and email.
  • i64 leeway: the seconds of tolerance on the times of the ID token

#Client.login

cromo
Login login() const

Starts a login: the address of the page of the provider where to send the user, with a state, a nonce and a challenge of PKCE made for this login. The program keeps the Login until the user comes back, in the session of the user, and gives it to exchange.

#Client.exchange

cromo
Tokens exchange(Login login, string state, string code) const

Ends a login: state and code are those of the query of the request with which the provider sent the user back to redirectUrl. It checks that the state is the one of the login, asks the tokens with the code and the verifier of PKCE, and checks the ID token, when there is one: its signature, its times, that it is of the provider, for this client and of this login. The user is identity of the tokens. An exception when a check fails or the provider refuses the code, as one used twice.

#Client.refresh

cromo
Tokens refresh(string refreshToken) const

New tokens from a refresh token, when the access token has ended, without asking the user again. The provider can give a new refresh token: when refresh of the result is empty, the old one still holds.

#Client.clientCredentials

cromo
Tokens clientCredentials() const

The tokens of the client itself, with its id and its secret and no user, for a service that calls another service (the grant client_credentials). It asks the scopes of the client.

#Client.userInfo

cromo
json.Value userInfo(string accessToken) const

What the provider knows of the user of an access token, from its address of userinfo: an object of JSON with "sub", and "email" and "name" when the scopes allow them. An exception when the provider refuses the token.

#Client.logoutUrl

cromo
string logoutUrl(string idToken, string returnUrl) const

The address where to send the user to end the session at the provider too, with the ID token of the user and the address where the provider sends the user after it, registered at the provider; "" for a provider without such an address.

#Functions

#discover

cromo
Provider discover(string issuer)

The addresses of the provider whose issuer is issuer, as "https://accounts.google.com", read from its configuration of OpenID Connect, /.well-known/openid-configuration under the issuer. An exception if the provider does not answer, or answers something else.

#google

cromo
Provider google()

The accounts of Google, as discover reads them.

#microsoft

cromo
Provider microsoft(string tenant)

The accounts of Microsoft, of Entra ID, as discover reads them. tenant is the id or the domain of an organization, or "common" for every account, "organizations" for those of work and school, "consumers" for the personal ones.

#keycloak

cromo
Provider keycloak(string server, string realm)

A realm of a Keycloak, as discover reads it: server is the address of the Keycloak, as "https://id.example.com", and realm the name of the realm.

#bearer

cromo
string bearer(http.Request req)

The token of a request to an API, from its header "Authorization: Bearer ...", or "" when the request has none.