Authentication & Authorization
Relevant source files
The following files were used as context for generating this wiki page:
Polar employs a custom authentication and authorization system built on FastAPI's dependency injection framework. The system is designed to handle multiple subject types—including Users, Organizations, and Customers—while enforcing granular access control through a robust Scopes mechanism.
The architecture follows a modular pattern where each functional domain (e.g., Discounts, Organizations) defines its own authentication requirements, allowed subjects, and necessary scopes. This ensures that the principle of least privilege is maintained across the REST API, background workers, and the internal backoffice.
Sources: AGENTS.md:144-150, server/CLAUDE.md:131-140
Core Authentication Concepts
AuthSubject
The AuthSubject[T] type is the central entity in Polar's security model. It represents the authenticated identity currently interacting with the system. The generic type T indicates the nature of the subject:
- User: A standard logged-in individual.
- Organization: An entity acting via an Organization Access Token.
- Customer: An entity authenticated via a customer session.
- Anonymous: Used for public endpoints where no credentials are provided.
Sources: AGENTS.md:147-152, server/CLAUDE.md:144-150
Scopes and Permissions
Access control is managed through Scopes. An AuthSubject possesses a set of granted scopes, and API endpoints define required_scopes. Access is granted only if there is an intersection between the subject's scopes and the endpoint's requirements.
Sources: AGENTS.md:160-164, server/CLAUDE.md:144-150
| Scope Component | Description |
|---|---|
web_default | Default scope for web dashboard users. |
write suffixes | Granular write permissions (e.g., discounts_write). |
read suffixes | Granular read permissions (e.g., organizations_read). |
Sources: AGENTS.md:154-158, server/CLAUDE.md:144-150
Authentication Flow
Polar resolves identity by checking credentials in a specific hierarchy:
- Customer Session Tokens: High priority for customer-facing portals.
- User Session Cookies: Primary for the web dashboard.
- API Tokens: Includes Personal Access Tokens, Organization Access Tokens, and OAuth2 tokens.
The following diagram illustrates how a request is resolved into an authenticated subject:
The system defaults to Anonymous if no credentials are found, but the Authenticator linked to the specific endpoint will then reject the request if Anonymous is not in its allowed_subjects.
Sources: AGENTS.md:172-176, server/CLAUDE.md:144-150
Implementation Patterns
Module-Specific Authenticators
Each module typically contains an auth.py file defining specialized dependencies using the Authenticator class.
# Example from AGENTS.md
_DiscountWrite = Authenticator(
required_scopes={Scope.web_default, Scope.discounts_write},
allowed_subjects={User, Organization},
)
DiscountWrite = Annotated[AuthSubject[User | Organization], Depends(_DiscountWrite)]
Sources: AGENTS.md:153-158, server/CLAUDE.md:144-150
Standard Dependencies
For common access patterns, Polar provides predefined dependencies in server/polar/auth/dependencies.py:
WebUser: Requires a logged-in User subject.WebUserOrAnonymous: Allows either a User or an Anonymous subject.AdminUser: Requires a user with administrative privileges (used for Backoffice).
Sources: AGENTS.md:158-160, server/polar/backoffice/DEVELOPMENT_GUIDE.md:20-25
OAuth2 and External Authorization
Polar supports OAuth2 for third-party integrations. The authorization flow involves a consent screen where users grant specific scopes to a client.
The consent page displays grouped scopes (e.g., grouping organizations:read and organizations:write under "Organizations") to provide clarity to the user.
Sources: clients/apps/web/src/app/(main)/oauth2/authorize/AuthorizePage.tsx:23-35, 65-80
Backoffice & Administrative Access
The backoffice system (server/polar/backoffice/) bypasses standard user-level organization checks and instead relies on AdminUser authentication. All backoffice endpoints automatically require admin privileges, and they utilize server-side rendering with HTMX for dynamic interactions.
Sources: server/polar/backoffice/DEVELOPMENT_GUIDE.md:15-30, 260-265
Summary
Polar's Authentication and Authorization system provides a unified interface for diverse identity types while maintaining strict security via scope-based access control. By leveraging FastAPI dependencies, the project ensures that security logic is declarative, testable, and consistently applied across both the API and administrative interfaces.
Sources: AGENTS.md:144-147, server/CLAUDE.md:131-135