- OCaml 99%
- Shell <1%
atproto-oauth #
ATProto OAuth client for OCaml.
Signing in to an AT Protocol service such as Bluesky with OAuth takes more
than plain OAuth 2.0. The client has to find the user's Personal Data Server
(PDS) from their handle, find the authorisation server from the PDS, and then
combine three extensions: pushed authorisation requests (PAR,
RFC 9126), PKCE (RFC 7636) and DPoP-bound tokens
(RFC 9449). atproto-oauth implements that profile, as specified by
the ATProto OAuth clients specification, on top of the
generic oauth library, and refuses any server metadata that does
not follow it.
Each extension closes one hole in plain OAuth. With PAR the client POSTs
the whole authorisation request to the server first and puts only the
returned request_uri in the browser redirect, so the parameters cannot be
altered in the browser. With PKCE the client sends the SHA-256 of a random
secret with the request and the secret itself with the code exchange, so
a stolen authorisation code is useless on its own. DPoP binds the tokens to
a key pair of the client: every request carries a JWT signed with that key
over the method, the URL and a server nonce, so a stolen token cannot be
used without the private key.
Packages #
atproto-oauthholds the profile rules, the client metadata builders and the session type, with token refresh.atproto-oauth.discoveryresolves a handle to its DID, the DID to its PDS, and the PDS to its resource and authorisation server metadata, checking the profile at every step.atproto-oauth.loginruns the whole sign-in of a command-line public client: a loopback listener for the redirect, then PAR, PKCE and DPoP. A command-line client has no web server, so it listens on127.0.0.1on a free port for the length of the sign-in, and the authorisation server redirects the user's browser there with the authorisation code.
Installation #
Install with opam:
$ opam install atproto-oauth
If opam cannot find the package, it has probably not reached the
public opam-repository yet. Add the overlay repository, then install
it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install atproto-oauth
Usage #
Scopes and client metadata #
The profile mandates the atproto scope; ATProto clients typically
combine it with transition:generic for legacy API access:
# Atproto_oauth.Profile.default_scope
- : string = "atproto transition:generic"
Build a public loopback-client metadata document for a CLI tool:
# let meta =
Atproto_oauth.Client_metadata.public_loopback
~client_id:"https://example.com/client-metadata.json"
~redirect_uris:[ "http://127.0.0.1/callback" ]
() in
Oauth.Client.(meta.scope, meta.dpop_bound_access_tokens)
- : string option * bool = (Some "atproto transition:generic", true)
Full login flow (public loopback client) #
Atproto_oauth_login.login walks handle resolution, discovery,
profile validation, PAR, browser consent, and the DPoP-bound token
exchange. The caller hands it an Eio switch, clocks, net, a secure
random source, and a Requests_eio.t, plus the client id of a hosted
metadata document:
let login env ~http handle =
Eio.Switch.run @@ fun sw ->
match
Atproto_oauth_login.login
~sw
~clock:(Eio.Stdenv.clock env)
~mono:(Eio.Stdenv.mono_clock env)
~net:(Eio.Stdenv.net env)
~random:(Eio.Stdenv.secure_random env)
~http
~client_id:"https://example.com/client-metadata.json"
handle
with
| Ok session -> Fmt.pr "logged in: %a@." Atproto_oauth.Session.pp session
| Error e -> Fmt.pr "login failed: %a@." Atproto_oauth_login.pp_error e
Discovery on its own #
To get the PDS and authorisation server metadata of a handle without running the full flow:
let discover env ~http handle =
Eio.Switch.run @@ fun sw ->
Atproto_oauth_discovery.of_handle
~sw
~clock:(Eio.Stdenv.clock env)
~net:(Eio.Stdenv.net env)
~http handle
The result bundles the DID, the PDS URL, the RFC 9728 resource metadata and the RFC 8414 server metadata, all of them already checked against the ATProto profile.
Session refresh and persistence #
Atproto_oauth.Session.refresh rotates the access token using the
stored refresh token and DPoP key, handling DPoP-Nonce retries
transparently. Sessions serialise to JSON via
Atproto_oauth.Session.save / load (write at 0600; the file
contains private key material and the refresh token).
Licence #
ISC