Authentication and authorization

Identities

An Identity is { subject, tenantId, permissions, roles, via, name? }. It comes from one of:

  • Session cookie (browsers). startSession({ subject, tenantId, roles }) inside a route sets fellow_session (HttpOnly, SameSite=Lax, Secure over HTTPS). The cookie holds a random id; only its SHA-256 is stored in fellow_sessions. Starting a session discards the previous one (fixation). endSession() logs out; endSessionsOf(subject) revokes every session (after a password change); pruneSessions() deletes expired ones.
  • Bearer token (APIs, integrations). signToken({ subject, tenantId, roles, permissions }), HS256 with FELLOW_SECRET.
  • OpenID Connect. createOidcClient({ issuer, clientId, clientSecret, redirectUri }) gives authorizationUrl() and callback(url): authorization code with PKCE; state and nonce are single-use and kept server-side for 10 minutes; the ID token signature, issuer and audience are verified. Map the claims to a user, then startSession.

fellow make:auth creates the accounts module: users (argon2id via Bun.password), tenant memberships, POST /api/accounts/session (rate limited), DELETE /api/accounts/session, GET /api/accounts/me, the /login page and tests. It is application code: edit it.

CSRF

Requests authenticated by the session cookie with an unsafe method must send the session’s token in x-csrf-token. Pages receive it as the csrfToken prop and @fellowframework/vue sends it automatically. Bearer tokens are not sent by browsers on their own, so they are exempt.

Roles and permissions

Modules declare permissions and roles in the manifest:

permissions: { 'records.document.read': 'Read documents', 'records.document.create': 'Create documents' },
roles: { 'records.clerk': { description: 'Registers documents', permissions: ['records.document.*'] } },

Sessions and tokens store roles; permissions are expanded from the installed manifests on every request, so changing a role takes effect immediately. Routes and pages declare access: { permission }.

Policies (per record)

export default definePolicy<Document, 'view' | 'update'>({
  name: 'records.document',
  abilities: {
    view: () => true,
    update: (identity, document) => document.owner === identity.subject && document.status !== 'archived',
  },
})

policy.authorize('update', document) throws 403 records.document.update; policy.can(...) returns a boolean; policy.abilitiesFor(document) returns every ability, to send to the page for the UI. The UI only hides actions; the server always decides.