OAuth2 Authorization Server
Relevant source files
The following files were used as context for generating this wiki page:
- server/polar/oauth2/authorization_server.py
- server/polar/oauth2/endpoints/oauth2.py
- server/polar/oauth2/service/oauth2_grant.py
- clients/apps/web/src/app/(main)/oauth2/authorize/AuthorizePage.tsx/oauth2/authorize/AuthorizePage.tsx)
- clients/apps/web/src/components/Settings/OAuth/EditOAuthClientModal.tsx
- AGENTS.md
- server/CLAUDE.md
The OAuth2 Authorization Server in Polar acts as the centralized authority for managing access to protected resources. It facilitates secure delegation of authority by issuing access tokens to third-party applications after authenticating a user or organization and obtaining their consent.
Within the Polar ecosystem, this server is integrated into the FastAPI backend and provides the necessary endpoints for the standard OAuth2 flow, including authorization requests, token issuance, and client management. It supports various subjects, including Users and Organizations, allowing for fine-grained permission control through scopes.
Sources: AGENTS.md, server/polar/oauth2/endpoints/oauth2.py, clients/apps/web/src/app/(main)/oauth2/authorize/AuthorizePage.tsx
Core Architecture and Components
The authorization server is built using a modular structure following the project's standard backend patterns. It utilizes FastAPI for routing and SQLAlchemy for persistence via a dedicated repository layer.
Primary Components
| Component | Description |
|---|---|
| Authorization Server | The central logic coordinator that handles standard OAuth2 requests and integrates with the security layer. |
| OAuth2 Grant Service | Manages the lifecycle of grants (authorization codes, tokens) and validates grant-specific logic. |
| Endpoints | FastAPI routers defining the public-facing OAuth2 routes (authorize, token, consent). |
| Schemas | Pydantic models used for validating incoming requests and serializing API responses. |
Sources: server/CLAUDE.md, server/polar/oauth2/authorization_server.py, server/polar/oauth2/service/oauth2_grant.py
Authorization Flow
The following sequence diagram illustrates the interaction between the User Interface, the Authorization Server, and the backend services during a standard authorization request.
The diagram shows how the AuthorizePage component interacts with the backend endpoints to collect user consent before a grant is persisted.
Sources: server/polar/oauth2/endpoints/oauth2.py, clients/apps/web/src/app/(main)/oauth2/authorize/AuthorizePage.tsx, server/polar/oauth2/service/oauth2_grant.py
Authentication and Scopes
Polar's OAuth2 implementation uses an AuthSubject type to represent the authenticated entity, which can be a User or an Organization.
Scope Management
Scopes are used to control access to specific operations (e.g., web_default, discounts_write). Access is granted if the subject possesses at least one of the required scopes defined in an Authenticator.
User Interface Integration
The frontend AuthorizePage displays requested permissions by grouping them. For instance, openid scopes are identified specifically, while others are formatted by replacing underscores with spaces.
const groupScopes = (scopes: schemas['Scope'][]) => {
return scopes.reduce<Record<string, schemas['Scope'][]>>((acc, scope) => {
const key = scope.split(':')[0]
if (!acc[key]) {
acc[key] = []
}
acc[key].push(scope)
return acc
}, {})
}
Sources: AGENTS.md:214-230, clients/apps/web/src/app/(main)/oauth2/authorize/AuthorizePage.tsx:23-33
Client Management
OAuth2 clients are managed through both the API and a web-based dashboard interface. Developers can create, update, and delete their OAuth applications.
Client Configuration
The configuration for an OAuth2 client includes:
- Client Name: Human-readable name for the application.
- Client ID & Secret: Credentials used for authentication.
- Redirect URIs: Whitelisted URLs where the server sends the user after authorization.
- Scopes: The set of permissions the application is allowed to request.
- Policies: Links to Terms of Service and Privacy Policy.
Update Logic
When updating a client, the frontend transforms complex structures (like URIs and Scopes) into the format expected by the backend OAuth2ClientConfigurationUpdate schema.
Sources: clients/apps/web/src/components/Settings/OAuth/EditOAuthClientModal.tsx:39-44, clients/apps/web/src/app/(main)/oauth2/authorize/AuthorizePage.tsx:82-105
Data Relationships
The following ER diagram describes the relationship between OAuth2 entities within the system.
The diagram represents how OAuth2Client entities generate OAuth2Grant records which are tied to either a User or an Organization.
Sources: server/polar/oauth2/authorization_server.py, server/polar/oauth2/service/oauth2_grant.py
Summary
The OAuth2 Authorization Server is a critical infrastructure component of Polar that manages delegated access. By utilizing standard FastAPI and SQLAlchemy patterns on the backend and a React-based consent UI on the frontend, it ensures that third-party applications can interact with User and Organization data securely and with explicit permission. The system's robustness is derived from strict scope validation and the clear separation of concerns between grant management and client configuration.