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.
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:
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 401The guide: Networking and HTTP.
Generata dalla documentazione nei sorgenti della libreria, che è in inglese.
#Tipi
#Provider
struct ProviderThe 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 tokensstring authorizeUrl: where the user logs instring tokenUrl: where the tokens are askedstring 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
struct ClaimsWhat 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 providerstring sub: the id of the user at the provider, which never changesjson.Value aud: for whom the token is: a text, or an array of themi64 exp: when it ends, in seconds of Unixi64 iat: when it was madestring emailstring name: the full namestring usernamestring givenNamestring familyNamestring picture: the address of a picture of the userstring nonce: of the login that asked the tokenstring azp: the client that asked the tokenstring tid: of Microsoft: the id of the organization of the userstring scope: of an access token: what it allows, separated by spaces
#Tokens
struct TokensThe 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 saystring refresh: the refresh token, forrefresh, or ""string id: the ID token, a JWT, or "" without the scope openidstring scope: what the access token allows, when the provider says itClaims identity: the claims of the ID token, checked; empty without one
#Login
struct LoginA 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 clientstring state: comes back with the user:exchangechecks itstring verifier: of PKCE: only who started the login can end itstring nonce: comes back in the ID token:exchangechecks it
#Verifier
struct VerifierChecks 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 providerstring 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
T verify<T : json.Data>(string token) constThe 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
Claims claims(string token) constThe claims of the standard of the token, when it passes the checks, as verify<oauth.Claims>.
#Client
struct ClientAn 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 providerstring id: the id of the client at the providerstring secret: its secret; "" for a client without one, which PKCE protectsstring redirectUrl: where the provider sends the user back: registered at the providerlist<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,loginasks openid, profile and email.i64 leeway: the seconds of tolerance on the times of the ID token
#Client.login
Login login() constStarts 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
Tokens exchange(Login login, string state, string code) constEnds 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
Tokens refresh(string refreshToken) constNew 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
Tokens clientCredentials() constThe 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
json.Value userInfo(string accessToken) constWhat 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
string logoutUrl(string idToken, string returnUrl) constThe 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.
#Funzioni
#discover
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.
Provider google()The accounts of Google, as discover reads them.
#microsoft
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
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
string bearer(http.Request req)The token of a request to an API, from its header "Authorization: Bearer ...", or "" when the request has none.