A social knowledge tool for researchers built on ATProto
Authentication Approach Summary#
This document outlines the authentication mechanism used in this AT Protocol Express application.
Core Components and Flow:#
-
OAuth 2.0 with AT Protocol:
- The primary authentication method is OAuth 2.0, delegating user authentication to their chosen AT Protocol PDS (Personal Data Server).
- The application acts as an OAuth client, configured in
src/auth/client.tsusing@atproto/oauth-client-node. - The flow typically starts when a user attempts to log in via the
/loginroute.
-
Session Management with
iron-session:- Once the user successfully authenticates with their PDS via the OAuth flow, the
/oauth/callbackroute is invoked. - This callback handler uses
ctx.oauthClient.callback(params)to finalize the OAuth exchange and retrieve an AT Protocol session (which includes the user's DID and potentially access/refresh tokens for the PDS). - Crucially, the user's
didis then stored in a server-side session managed byiron-session. iron-sessionencrypts this session data (containing thedid) and stores it in an HTTP cookie (namedsidby default insrc/routes.ts). This cookie is sent to the client's browser.
- Once the user successfully authenticates with their PDS via the OAuth flow, the
-
Cookie-Based Session Persistence:
- The browser automatically includes the
sidcookie in subsequent requests to the server. - This cookie is
HttpOnly(a default and recommended practice foriron-session), meaning it's not accessible to client-side JavaScript, enhancing security against XSS attacks.
- The browser automatically includes the
-
Session Restoration and AT Protocol Agent:
- For requests requiring authentication (e.g., posting a status, viewing personalized content), the
getSessionAgenthelper function insrc/routes.tsis used. getSessionAgentretrieves thedidfrom theiron-sessioncookie.- It then uses
ctx.oauthClient.restore(session.did)to restore the full AT Protocol OAuth session. This step might involve using stored refresh tokens (managed bySessionStoreinsrc/auth/storage.ts) to obtain fresh access tokens for interacting with the user's PDS. - If restoration is successful, an
Agentfrom@atproto/apiis instantiated. This agent is then used to make authenticated requests to the user's PDS on their behalf.
- For requests requiring authentication (e.g., posting a status, viewing personalized content), the
-
Storage (
src/auth/storage.ts):StateStore: Persists OAuth state parameters during the authorization code flow to prevent CSRF attacks.SessionStore: Persists the AT Protocol OAuth session details (including access tokens, refresh tokens, and scopes) associated with a user's DID. This allowsoauthClient.restore()to reconstruct an active PDS session.- Both stores use the application's database (SQLite by default) for persistence.
Key Routes in src/routes.ts:**#
/login(GET & POST): Initiates the OAuth flow by redirecting the user to their PDS for authentication./oauth/callback(GET): Handles the redirect from the PDS after authentication. Exchanges the authorization code for tokens (handled byoauthClient), and then establishes theiron-sessionby storing the user'sdid./logout(POST): Destroys theiron-session, effectively logging the user out by clearing the session cookie.- Authenticated Routes (e.g.,
/statusPOST,/GET for logged-in users): UsegetSessionAgentto retrieve an authenticated AT Protocol agent.
Summary of Technologies:#
- OAuth 2.0: Standard for delegated authentication.
- AT Protocol SDK (
@atproto/oauth-client-node,@atproto/api): For interacting with the AT Protocol network and PDS. iron-session: For server-side session management with encrypted cookies.- HTTP Cookies (
HttpOnly): For persisting session identifiers on the client-side securely. - Express.js: As the web server framework.