Authentication and Authorization
This document explains how CoCalc authenticates users and authorizes access — remember-me cookies, SSO/OAuth, API keys, registration tokens, and project-level permissions.
Overview
CoCalc supports multiple authentication methods:
Email/password — traditional sign-up with hashed passwords
Remember-me cookies — persistent sessions stored in the database
SSO/OAuth — Google, GitHub, Facebook, Twitter, SAML, and custom OAuth2
API keys — bearer tokens for programmatic access (per-account or per-project)
Registration tokens — gate account creation on private deployments
Authentication Methods
Email/Password
Password hashing: @cocalc/backend/auth/password-hash — uses HMAC-based iterative hashing. Format: algorithm$salt$iterations$hash.
Key files:
packages/server/auth/is-password-correct.ts— verify passwordpackages/server/auth/has-password.ts— check if account has password setpackages/server/auth/password-reset.ts— initiate reset emailpackages/server/auth/redeem-password-reset.ts— complete resetpackages/server/auth/password-strength.ts— validate strength
Remember-Me Cookies
packages/server/auth/remember-me.ts — the primary session mechanism:
How it works:
Generate a random UUID v4 as a session token
Hash it with
passwordHash(session_id)Store the hash in
remember_metable (not the raw token)Cookie value format:
algorithm$salt$iterations$session_idOn each request, hash the session ID from cookie and look up in DB
Database table (remember_me):
| Field | Type | Description |
|---|---|---|
hash | CHAR(127) | Hashed session token (primary key) |
expire | timestamp | Cookie expiration |
account_id | UUID | Account this session belongs to |
Request authentication (packages/server/auth/get-account.ts):
SSO / OAuth (Passport)
packages/server/auth/sso/ — integrates with Passport.js strategies:
Supported providers:
| Provider | Strategy | Package |
|---|---|---|
passport-google-oauth20 | Built-in | |
| GitHub | passport-github2 | Built-in |
passport-facebook | Built-in | |
@passport-js/passport-twitter | Built-in | |
| SAML | @node-saml/passport-saml | For enterprise SSO |
| OAuth2 | Custom | Generic OAuth2 support |
Configuration: SSO strategies are configured in the passport_settings database table (admin-editable via the admin panel).
Key files:
sso/types.ts—StrategyConf,LoginInfo, strategy type unionssso/passport-login.ts— core login flow: match/create account, set cookiesso/extra-strategies.ts— load custom strategies frompassport_settingssso/public-strategies.ts— return enabled strategies for login pagesso/sanitize-profile.ts— normalize profile data from providerssso/openid-parser.ts— OpenID Connect profile parsing
Login flow:
User clicks SSO button → redirect to provider
Provider callback →
passport-login.tsprocesses profileMatch by provider ID or email → existing account or create new
Create remember-me cookie → redirect to app
Exclusive SSO: packages/server/auth/check-email-exclusive-sso.ts — some email domains can be locked to a specific SSO provider, forcing users to sign in via SSO rather than email/password.
API Keys
packages/server/api/manage.ts — programmatic access tokens:
Database table (api_keys):
| Field | Type | Description |
|---|---|---|
id | serial | Primary key |
hash | text | Hashed API key (only hash stored) |
account_id | UUID | Owning account |
project_id | UUID | Optional project scope |
name | text | Human-readable name |
expire | timestamp | Expiration date |
created | timestamp | Creation time |
Key properties:
Keys are hashed before storage (like passwords)
Can be scoped to a specific project
Support
BearerandBasicHTTP authenticationMax 100,000 keys per account
Used by the Python
cocalc-apiclient
Authentication (packages/server/auth/api.ts):
Registration Tokens
packages/server/auth/tokens/ — control who can create accounts:
Database table (registration_tokens):
| Field | Type | Description |
|---|---|---|
token | text | The token string (primary key) |
descr | text | Admin description |
expires | timestamp | When the token expires |
limit | integer | Max number of uses |
counter | integer | Current use count |
disabled | boolean | Manually disabled |
ephemeral | integer | Hours until account auto-deletion |
Flow: Admin creates token → shares with users → users enter token during sign-up → token validated (expiry, counter, disabled) → account created.
Controlled by server setting account_creation_token_required.
Authorization
Project Access
Project access is checked via the projects table users JSONB field:
packages/server/projects/is-collaborator.ts — verifies that account_id appears in the project's users field.
Database Query Authorization
The user query system (packages/util/db-schema/) uses pg_where rules to restrict what data users can access:
See docs/database.md for full details on the query permission system.
Admin Authorization
Admins are identified by the groups field in the accounts table containing "admin". Admin-only tables use pg_where: ["account_id::UUID IS NOT NULL"] (meaning: any authenticated user — but the table's admin flag further restricts access).
Additional Security
Throttling
packages/server/auth/throttle.ts — rate limits authentication attempts per IP address and account to prevent brute-force attacks.
reCAPTCHA
packages/server/auth/recaptcha.ts — optional CAPTCHA verification during account creation, configured via server settings.
Email Verification
packages/server/auth/redeem-verify-email.ts — email verification flow where users click a link to confirm their email address.
Impersonation
packages/server/auth/impersonate.ts — admin-only feature to sign in as another user for debugging/support.
Banned Users
packages/server/accounts/is-banned.ts — banned users are blocked at every authentication checkpoint (cookie validation, API key use, SSO login).
Key Source Files
| File | Description |
|---|---|
packages/server/auth/get-account.ts | Main auth resolver: cookie → API key → account_id |
packages/server/auth/remember-me.ts | Remember-me cookie creation and management |
packages/server/auth/hash.ts | HMAC password hashing |
packages/server/auth/api.ts | API key extraction from HTTP headers |
packages/server/auth/is-password-correct.ts | Password verification |
packages/server/auth/password-reset.ts | Password reset initiation |
packages/server/auth/throttle.ts | Rate limiting |
packages/server/auth/recaptcha.ts | CAPTCHA verification |
packages/server/auth/set-sign-in-cookies.ts | Cookie setting on sign-in |
packages/server/auth/sso/types.ts | SSO strategy types and interfaces |
packages/server/auth/sso/passport-login.ts | SSO login flow |
packages/server/auth/sso/extra-strategies.ts | Load custom SSO strategies |
packages/server/auth/sso/public-strategies.ts | Available strategies for login UI |
packages/server/auth/sso/sanitize-profile.ts | Normalize SSO profiles |
packages/server/auth/check-email-exclusive-sso.ts | Domain-locked SSO enforcement |
packages/server/auth/tokens/redeem.ts | Registration token validation |
packages/server/auth/tokens/get-requires-token.ts | Check if tokens required |
packages/server/api/manage.ts | API key CRUD operations |
packages/server/projects/is-collaborator.ts | Project access check |
packages/server/accounts/is-banned.ts | Ban check |
packages/backend/auth/password-hash.ts | Password hashing library |
packages/backend/auth/cookie-names.ts | Cookie name constants |
packages/util/db-schema/api-keys.ts | API key schema |