Authentication #
Beachwave authenticates users with ATProto OAuth. Identity always comes from
ATProto; the application never asks for a password in the primary flow. The
implementation lives in src/sdk/atproto/ and is consumed by the reference
client exactly as a third-party application would consume it.
Why OAuth #
OAuth is how current and future ATProto applications are expected to authenticate. It gives the user a hosted consent screen on their own authorization server, issues sender-constrained (DPoP) tokens, and never exposes long-lived credentials to the application. App passwords remain available only as an optional development fallback.
The flow #
The client implements the public-client authorization-code flow:
- Identity resolution — the entered handle is resolved to a DID
(
com.atproto.identity.resolveHandle), the DID document is fetched (plc.directoryordid:web), and the account's PDS endpoint is read from it. (src/sdk/atproto/identity.ts) - Authorization server discovery — the PDS advertises its authorization
server via
/.well-known/oauth-protected-resource, whose metadata is read from/.well-known/oauth-authorization-server. (metadata.ts) - PKCE — a code verifier/challenge pair is generated. (
pkce.ts) - DPoP — a non-extractable P-256 key pair is generated. Every token and
resource request carries a signed DPoP proof, and the server's
use_dpop_noncechallenge is handled transparently. (dpop.ts,transport.ts) - Pushed Authorization Request (PAR) — authorization parameters are pushed
to the server, which returns a
request_uri. (oauth-client.ts) - Redirect — the browser is sent to the authorization endpoint with the
request_uri. - Callback — on return,
stateis validated, the code is exchanged for DPoP-bound access/refresh tokens, and the session is persisted. - Authenticated requests & refresh —
OAuthSessionsigns every XRPC call with DPoP, binds the access token via theathclaim, and refreshes tokens ahead of expiry. (oauth-session.ts)
Sessions and storage #
- The non-extractable DPoP
CryptoKeyis stored in IndexedDB (it survives the redirect via structured clone but is never serialized to JSON). - Session and pending-authorization JSON is stored in localStorage.
- This is implemented behind the
OAuthStoreinterface (store.ts); supply a custom store to change persistence.
Scopes #
The client requests atproto transition:generic today. atproto is the
required base scope; transition:generic grants the repository write access the
room lifecycle needs. The OAuth client is scope-agnostic — it forwards whatever
scope string it is given — so adopting granular scopes later is a one-line
configuration change in src/client/config.ts (and the hosted client metadata).
Client identity (client_id) #
ATProto identifies OAuth clients by a URL that resolves to a metadata document.
- Production:
client_idishttps://<host>/client-metadata.json. On Vercel that path is served byapi/client-metadata.js(via avercel.jsonrewrite), which derives every URL in the document from the request's Host header — so it matches any deployed origin automatically with nothing to edit. On other static hosts, serve an equivalentclient-metadata.jsonwhose URLs match your origin. - Local development: on
localhost/127.0.0.1the client uses ATProto's special loopback client, encoding the redirect URI and scope directly into theclient_id. No hosted document is required —npm run devworks out of the box. (src/client/config.ts)
App-password fallback (development only) #
AppPasswordSession (app-password.ts) signs in with a handle and an app
password using legacy com.atproto.server.createSession Bearer tokens. It is
exposed under "Developer options" in the client and is intended only for local
testing before a client metadata document is hosted. Do not rely on it in
production.
SDK surface #
import { OAuthClient, AtprotoRepositoryClient } from 'beachwave/sdk';
const oauth = new OAuthClient({ clientId, redirectUri });
// Start sign-in (redirects away):
await oauth.authorize('alice.bsky.social');
// On the redirect callback:
const session = await oauth.callback(new URLSearchParams(location.search));
// Or restore a persisted session on load:
const restored = await oauth.restore();
// Drive the room SDK against the real PDS:
const client = new AtprotoRepositoryClient(session);
Both OAuthSession and AppPasswordSession implement the AtprotoAgent
interface, so AtprotoRepositoryClient (and therefore the whole room SDK) works
identically regardless of how the user signed in.