OAuth client implementation for ATProto in Ruby
README.md

AuthProto #

Gem Version YARD Docs

A Ruby implementation of an OAuth 2.1 client for use in AT Protocol apps.

Originally released as atproto_auth by Josh Huckabee in 2024, this is a fork that continues the development.

Note

Part of ATProto Ruby SDK: ruby.sdk.blue

Features #

The library supports all features of OAuth required by ATProto:

  • DPoP (Demonstrating Proof of Possession) and nonce management
  • client, resource server, and authorization server metadata JSON
  • PAR (Pushed Authorization Requests)
  • PKCE (Proof Key for Code Exchange)
  • client assertions for confidential clients

Both public and confidential clients are supported. The main intended use case is backend apps which users authorize to access their ATProto accounts server-side (likely as a confidential client), but it's also possible to use the library in command-line scripts (although in many cases app passwords might still be a better solution for those right now).

The gem manages the storage of session data and DPoP nonces, and handles at rest encryption of the stored tokens. SQLite, Redis and in-memory storage backends are included.

There is also an (a bit experimental) adapter for using AuthProto with the Minisky gem to make XRPC requests with OAuth login support.

Note

This gem deliberately does not support any features, profiles and configurations of OAuth that are not currently used in ATProto, and it does not support turning off any additional requirements and restrictions that ATProto introduces on top of standard OAuth specifications. For example, DPoP with server nonces, PAR, and PKCE are always required, and the client ID must be a URL from which the client metadata is fetched.

It will likely not be of much use in non-ATProto contexts. This is intentional, in order to not make the project more complicated than it already is.

Status #

The project was originally written in 2024. I have forked it, reviewed the code, refactored a lot of it, and made a lot of bug fixes to make everything more up to date and compliant with current ATProto OAuth. It has a pretty complete test coverage (converted from Minitest to RSpec), and it has been reviewed by a few AI agents, with no major outstanding issues left at the moment.

I'm not currently using it in production at all yet, so consider this still alpha quality. Might kill your cat, steal your girlfriend, and so on…

The public API will likely be changing in some initial releases. The AuthProto::Client entry point will likely be the most stable part, but treat most of the rest of the library as private API / implementation detail for now.

Installation & requirements #

AuthProto requires Ruby version 3.3 or newer (versions 3.2 and older no longer receive maintenance updates). Tested on versions up to 4.0.

To use it in your app, add this to your Gemfile:

gem 'authproto'

Depending on the configured storage, you will also need to add either the sqlite3 gem or the redis gem, and minisky if you want to use the Minisky XRPC client. These are not explicit dependencies so they're not forced on you if you don't need them.

Initial configuration #

The first thing you need to do is to set up a few required things in the global configuration:

AuthProto.configure do |config|
  ...
end

There are two required settings to be configured:

http_client: #

This configures a HTTP client for making requests to the authorization server and to metadata endpoints during the initial authorization process and later token refreshing (not for later authorized data requests to the PDS). Set it either to an instance of the default AuthProto::HTTPClient, or a compatible custom implementation/adapter.

config.http_client = AuthProto::HTTPClient.new

storage: #

This configures the storage adapter for storing session data and DPoP nonces.

The available options are:

  • an SQLite database (AuthProto::Storage::SQLite, requires additionally the sqlite3 gem)
  • a Redis database (AuthProto::Storage::Redis, requires additionally the redis gem)
  • an in-memory storage for testing (AuthProto::Storage::Memory)
  • it's also possible to implement a custom storage adapter, providing the same interface (see AuthProto::Storage::Interface)

A basic configuration may look like this:

require 'authproto'

AuthProto.configure do |config|
  config.http_client = AuthProto::HTTPClient.new(timeout: 10)

  config.storage = AuthProto::Storage::SQLite.new(
    path: File.join(__dir__, "db", "localhost_auth.sqlite3")
  )
end

For more complex production setups you may want to use Redis instead, which is configured like this:

require 'redis'

AuthProto.configure do |config|
  redis_client = Redis.new(
    url: ENV.fetch('REDIS_URL'),
    ssl_params: { verify_mode: OpenSSL::SSL::VERIFY_PEER }
  )

  config.storage = AuthProto::Storage::Redis.new(
    redis_client: redis_client
  )
end

The AuthProto::Storage::Memory adapter is configured by default, so you can skip the storage part if you want to use it.

For the SQLite adapter, you might call AuthProto.storage.cleanup_expired_records periodically to remove expired sessions and locks if you want. Memory and Redis handle this automatically.

It's not recommended to use AuthProto's storage for other purposes beyond what the library reads & writes to it internally.

ATPROTO_MASTER_KEY: #

This is technically not required, but you'll want to set it for anything more than brief testing. In the session storage, things like state token and access tokens are encrypted at rest using a key read from an ENV var ATPROTO_MASTER_KEY. The var needs to be Base64-encoded, and 32 bytes long when decoded.

You can generate it e.g. like this:

openssl rand -base64 32

And then set it e.g. by adding ATPROTO_MASTER_KEY=... on the command line, or with export, or with something like dotenv.

If you don't set it, you will see a warning like this:

WARNING: Using randomly generated encryption key - tokens will not persist across restarts

Usage #

The main class and the entry point to the library is the AuthProto::Client class. You will likely only need to create and use this one directly.

The initializer looks like this:

client = AuthProto::Client.new(
  client_id: "https://...",
  redirect_uri: "https://...",
  metadata: { ... },
  signing_key: signing_key
)

The first two arguments are required, the other two are optional. What you put into these will depend on how you're running the app: on a publicly accessible server or on localhost, and as a public or a confidential client.

On a production server #

The normal, expected way to run an OAuth app is that you have a publicly accessible server (with HTTPS) on which you can at least host the client metadata JSON file – even if you're building e.g. a native iOS app. This can be a completely static site hosting just the JSON file, if you don't need anything else, but it needs to be there (though in case of a Ruby app, you will also need some kind of callback endpoint to receive authorization codes).

The metadata can technically be located at any path you want, but it's strongly recommended to put it at /oauth-client-metadata.json: this will make the PDS display your app as just a hostname, like "foobar.com wants to access your account", otherwise the full URL with the path will be shown, which looks less friendly.

Client metadata is a JSON file that includes information about your app like the name and requested permissions, and the enabled security features. What exactly goes into it is a bit beyond the scope of this readme, you can see the details in the ATProto OAuth spec.

Once you have the file readable on the server, you can create the Client passing it the client_id – which is the full URL to the metadata JSON file – and a redirect_uri to which the user's browser will be redirected after authorization:

client = AuthProto::Client.new(
  client_id: "https://site.example.com/oauth-client-metadata.json",
  redirect_uri: "https://site.example.com/callback"
)

If you host the metadata file from the same codebase, you can also include the metadata here inline as a Hash. It's not required – it will be fetched from the live URL here otherwise – but if you do, it saves you one request whenever Client is created. (Note, this is a fetch on the client side so the library can get more info about the app and validate it – the authorization server will still need to fetch this JSON file even if you provide the metadata here.)

Confidential clients #

The example above is for a public client; this has nothing to do with it running on a public server – a "public client" is one that doesn't have a set of secret signing keys kept server-side, and those get shorter access & refresh token lifetimes.

The other type is a confidential client; to run one, you need a set of keys to sign client assertions. The key is a JSON representation of an elliptic curve key in the form of a JWK (JSON Web Key), like this:

{
  "kty": "EC",
  "crv": "P-256",
  "kid": "key-1",
  "x": "1vhfr8lFBg0jhXMNoXn-XWtpxR9nu8Ja1JGFrWqDIV8",
  "y": "sBgQICMTA8MBeD_TgdJduJmmhEQ9pA5l4EPUBtBeZOg",
  "d": "rqnhaaobMsqhWAnCWJGzioXbrqBFQ_59I2dhF_cw1dA",
  "use": "sig"
}

This is a private key, a public key lacks the d field. You can generate those e.g. using this generate_keys.rb script from the example app in examples folder here.

Once you have the keys, put the public one under jwks.keys in the metadata JSON:

{
  ...
  "jwks": {
    "keys": [{ "x": ... }]
  }
}

And pass the private one as signing_key to the Client:

client = AuthProto::Client.new(
  client_id: "https://site.example.com/oauth-client-metadata.json",
  redirect_uri: "https://site.example.com/callback",
  signing_key: { x: ... }
)

You also need these fields set in the metadata JSON like this:

  "token_endpoint_auth_method": "private_key_jwt",
  "token_endpoint_auth_signing_alg": "ES256",

With a setup like this, you get much longer potential session length, assuming correctly implemented token refresh.

Testing on localhost #

Running OAuth on localhost is a bit of a special exception, it doesn't work the way it works normally:

  • the client_id URL needs to be http://localhost/, without a port, and with this literal hostname, not as a loopback IP
  • the metadata JSON file isn't fetched from the URL, but it's instead generated from preset values and from URL query parameters (Client will reject a metadata parameter here)
  • you can configure redirect_uri and scope by appending them as query params (with encoded values) to the client_id
  • the redirect_uri needs to be on 127.0.0.1 or [::1] IP (likely with a port), not on localhost, confusingly
  • the app will always be a "public" client, not a "confidential" one

The client ID will then look something like this:

http://localhost/?redirect_uri=http%3A%2F%2F127.0.0.1%3A8000%2Fcallback&scope=atproto+repo%3Aapp.bsky.feed.post

AuthProto::Client includes a special helper Client.for_localhost which generates this client_id with encoded query params for you:

client = AuthProto::Client.for_localhost(
  redirect_uri: "http://127.0.0.1:8000/callback",
  scope: "atproto repo:app.bsky.feed.post"
)

Using the Client #

Regardless of the configuration, the rest of the API is the same. The expected steps are:

Create an authorization URL #

You can start the authorization process either with a specific user handle/DID – then their PDS will be looked up and they will be redirected there and asked to log in with this specific account – or from a PDS URL, in which case they will go to the PDS's site, but presented with a login form or account selection:

client.authorize(handle: 'user.handle', scope: 'atproto ...')
client.authorize(pds: 'https://pds.example.com', scope: 'atproto ...')

This returns a hash like:

{
  url: "https://lab.martianbase.net/oauth/authorize?request_uri=.....",
  session_id: "20044fc4-1131-47f9-90f8-99c26fe82a00"
}

Depending on your app, either redirect the user to this URL, or present it to the user and ask them to open it and log in there. Write down this session ID too – you'll need it in a moment (for a web app, store it e.g. in a session variable).

Handle the callback request #

After the user approves or rejects the request, they will be redirected to your configured redirect_uri. Check the received params for an error key – if it's set, handle the error in some appropriate way.

If there's no error, pass the received query parameters to Client#handle_callback. Note: you will need to also pass the ID of the session from which the login was initiated. This is to make sure that we don't log in a user completing authorization into another user's account:

client.handle_callback(
  params,
  expected_session_id: session[:session_id]
)

You can also skip checking for error and just pass the params to handle_callback, in which case you will get a CallbackError raised with the error details if the params included an error.

For localhost clients, there is also a helper class included called LocalhostServer, which starts a temporary localhost HTTP server inline just to receive the callback request, and returns the received codes to the block you pass to it:

AuthProto::LocalhostServer.start(client.client_metadata) do |json|
  client.handle_callback(json, expected_session_id: session_id)
end

Load session data #

When coming back later to a previously saved session, you will need to look it up in the storage using the session ID and see if's still usable and check its state:

info = client.get_session(session_id)

get_session returns a SessionInfo object with these methods:

  • session_id - the session's unique identifier
  • access_token - the current access token, or nil before authorization
  • refresh_token - the current refresh token, or nil if none was issued
  • expires_at - the access token's expiry time, or nil before authorization
  • expires_in - seconds remaining until the access token expires
  • requested_scope - list of requested permissions
  • granted_scope - list of actually granted permissions
  • did - the account's DID, if known
  • pds - the URL of the account's PDS, if known
  • pending? - if authorization is still pending
  • tokens_valid? or authorized? - if an access token was issued and is valid (not expired)
  • renewable? - if the session has a refresh token
  • has_limited_scope? - if any requested permissions are missing from the current grant
  • can_be_revoked? - if the authorization server supports token revocation

Refresh the tokens #

The access tokens usually have a lifetime of around 15-30 minutes, and after that time, you will get a 401 error (or Client can raise Client::TokenError if it detects an expired token earlier). In that case, call refresh_token to use the other token you got (the refresh token) to get a new access token:

client.refresh_token(session_id)

It might also be a good idea to check the token's expiration date before making the request and refreshing it in advance if needed:

if !session.tokens_valid?
  session = client.refresh_token(session_id)
end

Replace the previously returned value with the return value from refresh_token – the method looks up a new session object, and the previous object is not updated in place.

Make authenticated data requests #

This part is a bit more complicated, because this API is designed to allow working with any HTTP client library you're using. AuthProto::Client by itself doesn't implement a full XRPC client for making requests and handling responses. What it gives you is a way to get a set of authentication headers generated for a specific request (method + URL) that you can then pass to e.g. Net::HTTP or Faraday:

endpoint = "https://#{pds_host}/xrpc/com.atproto.repo.createRecord"
session = client.get_session(session_id)

if !session.tokens_valid?
  session = client.refresh_token(session_id)
end

headers = client.auth_headers(
  session_id: session_id,
  method: 'POST',
  url: endpoint
)

connection = Faraday.new do |f|
  f.request :json
end

post_record = {
  repo: session.did,
  collection: "app.bsky.feed.post",
  record: {
    text: "Hello world!",
    langs: ["en"],
    createdAt: Time.now.utc.iso8601(3)
  }
}

res = connection.post(endpoint) do |req|
  req.headers.merge!(headers)
  req.body = post_record
end

One additional complication is DPoP and its changing nonces. The server gives you a "nonce" that you need to use to generate the "proof" header, which changes after at most 5 minutes, so the requests will often fail with a header indicating a new nonce to use.

You handle this by passing the response to client.process_dpop like this to let it update the nonce. The method expects the response as a hash of { headers, body, status } (since different HTTP libraries will return it in a different format):

response = { headers: res.headers, body: res.body, status: res.status }

client.process_dpop(response,
  url: endpoint,
  session_id: session_id
)

Then, ask the client to check if the response contains a nonce error, and if so, you'll need to retry the whole request once again (with new headers):

if client.has_nonce_error?(response)
  make_request(...)
end

You can also use the with_nonce_handling helper, passing it a block which makes a request (remember to call auth_headers inside the block, since on the repeated call the headers will be different) and returns the response in the hash format:

endpoint = "https://#{pds_host}/xrpc/com.atproto.repo.createRecord"
session = client.get_session(session_id)

connection = Faraday.new do |f|
  f.request :json
end

post_record = {
  repo: session.did,
  collection: "app.bsky.feed.post",
  record: {
    text: "Hello world!",
    langs: ["en"],
    createdAt: Time.now.utc.iso8601(3)
  }
}

response = client.with_nonce_handling(session_id, endpoint) do
  headers = client.auth_headers(
    session_id: session_id,
    method: 'POST',
    url: endpoint
  )

  res = connection.post(endpoint) do |req|
    req.headers.merge!(headers)
    req.body = post_record
  end

  { headers: res.headers, body: res.body, status: res.status }
end

Here's an example using Net::HTTP:

endpoint = "https://#{pds_host}/xrpc/app.bsky.feed.getTimeline"

response = client.with_nonce_handling(session_id, endpoint) do
  headers = client.auth_headers(
    session_id: session_id,
    method: 'GET',
    url: endpoint
  )

  res = Net::HTTP.get_response(URI(endpoint), headers)

  { headers: res.to_hash, body: res.body, status: res.code }
end

And you can also use the #http method to access the global AuthProto HTTP client you've configured earlier:

endpoint = "https://#{pds_host}/xrpc/app.bsky.feed.getTimeline"

response = client.with_nonce_handling(session_id, endpoint) do
  headers = client.auth_headers(
    session_id: session_id,
    method: 'GET',
    url: endpoint
  )

  client.http.get(endpoint, headers)
end

For a more batteries-included client API, see Minisky below.

Log the user out #

Finally, if you want to log the user out, call delete_session to clear the session from the configured storage:

client.delete_session(session_id)

You can also pass revoke: true to additionally revoke the tokens on the PDS:

client.delete_session(session_id, revoke: true)

Minisky adapter #

AuthProto also includes an adapter for the Minisky gem for making XRPC requests, which gives you a client with a slightly more high-level API that handles the authenticated requests for you.

The API is a bit of a mix of a standard password-auth Minisky client and the AuthProto::Client for the initial authentication:

# needs to be required separately
# and requires `gem 'minisky'` in the Gemfile
require 'authproto/minisky'

sky = Minisky::OAuth.new(
  client_id: "https://#{server_hostname}/oauth-client-metadata.json",
  redirect_uri: "https://#{server_hostname}/callback",
  metadata: metadata,       # or nil/skip
  session_id: session_id,   # or nil/skip
  signing_key: signing_key  # or nil/skip
)

auth = sky.authorize(handle: 'user.handle', scope: "atproto ...")
p auth[:url]
# ...

sky.handle_callback(params, expected_session_id: auth[:session_id])

and then e.g.:

sky.get_request('app.bsky.feed.getTimeline', { limit: 100 })

sky.post_request('com.atproto.repo.createRecord', { repo: ..., ... })

posts = sky.fetch_all('app.bsky.graph.getFollows', { actor: did }, field: 'follows')

It also handles automatically the nonce updates and token refresh. Unlike AuthProto::Client, Minisky::OAuth handles a single session at a time.

For localhost development, use this convenience initializer to generate a localhost client ID with the callback URL and scope encoded as query params:

sky = Minisky::OAuth.for_localhost(
  redirect_uri: 'http://127.0.0.1:3000/callback',
  scope: 'atproto repo:app.bsky.feed.post',
  session_id: session_id   # optional, to resume a stored session
)

You can also use the with_oauth_client helper to reuse an existing instance of AuthProto::Client (normally Minisky::OAuth creates one internally):

sky = Minisky::OAuth.with_oauth_client(client, session_id: session_id)

This class is a bit more experimental and likely to change in future versions (pending a redesign of the Minisky internal architecture).

Additional configuration #

Some other things you can configure through AuthProto.configure:

  • client_assertion_lifetime – how long a client assertion is valid for a confidential client (default: 5 min)
  • dpop_nonce_lifetime – how long a DPoP nonce is valid (default: 5 min)
  • pending_session_ttl – how long a session that hasn't completed authentication with a callback is valid (default: 15 min)

You shouldn't generally need to change those defaults.

Further examples #

There are 3 example apps included in the examples folder in the repo:

  • minisky_console – shows how to use AuthProto & Minisky::OAuth from a console script or IRB
  • basic_webapp – demonstrates how to build a simple webapp where the user can log in and access some of their ATProto account data
  • bookmarks – another webapp, which imports and stores your Bluesky bookmarks, which demonstrates how to use AuthProto both in server endpoints and in Rake tasks to import data, how to integrate it with ActiveRecord, and how to make proxied calls to the AppView through the PDS

Other resources #

Credits #

Copyright © 2026 Kuba Suder (@mackuba.eu).

Original AtprotoAuth copyright © 2024 Josh Huckabee.

The code is available under the terms of the MIT license. Bug reports and pull requests are welcome 😎