Introduction
The auth domain answers who is calling: authentication decorators, tokens and sessions, users and external identities.
Overview
The auth domain of @antelopejs/interface-dms answers one question for the DMS: who is calling. It resolves the bearer token of an HTTP request into a User row. It issues and validates the tokens that make such a request possible, records logins as sessions, and binds the accounts a user owns on external login providers. It also owns the users, sessions and user_external_identities tables and their models.
The domain stops at identity. The Core domain decides what an authenticated caller may do, through permissions, roles and tenant membership. Its tenant guards build on the authenticateRequestUser function this domain exports.
The domain is published under three subpaths:
| Subpath | Contents |
|---|---|
@antelopejs/interface-dms/auth | The authentication decorators, authenticateRequestUser, the token generators and validators, createSession, sanitizeUser, the external-identity functions, announceRegistration, the account mail functions, bindVerifiedRequestIdentity, and the RequestPrincipal, RequestAuthenticator and VerifiedRequestIdentity types. |
@antelopejs/interface-dms/auth/db | The User, Session and UserExternalIdentity tables, their models, the table-name constants, normalizeEmail, isCurrentRefreshToken and buildExternalIdentityId. |
@antelopejs/interface-dms/auth/password | The password policy: PASSWORD_MIN_LENGTH, PASSWORD_PATTERN and isPasswordCompliant. These are plain values and a pure function, usable without the DMS loaded. |
What the domain provides
- Request authentication decorators.
AuthUserandAuthRawUsergate a route, a controller property, or a whole controller class on a valid bearer token, and inject the resolvedUser. - Optional authentication.
IfAuthUserserves the same route to signed-in and anonymous callers. It injectsUser | undefinedinstead of rejecting the request. - An owner-only gate.
AuthOwnerOnlyrestricts administration routes to platform owners. It answers403to an authenticated caller who is not an owner. - Composable authentication.
authenticateRequestUserruns the same checks outside a decorator. The DMS tenant guards start from it, and your own parameter providers can too. - Token lifecycle. The domain issues and validates access and refresh tokens scoped to a tenant. It also issues the short-lived two-factor and tenant-assignment tokens that sit between a password check and a real session.
- Revocable sessions. Each login is a
Sessionrow with its device, IP, and the hash of its current refresh token. You can revoke one session, or every session of a user. - The user table and model. The
Usertable hashes passwords throughHashModifier.UserModellooks users up by email and by owner flag. - External login providers.
linkExternalIdentitybinds the accounts a user owns on GitHub, Google, or any configured provider. On the next login, the DMS resolves the provider profile back to the user. - Safe responses.
sanitizeUserstrips authentication secrets from a user before you return it over HTTP. - One password policy.
isPasswordCompliantchecks a password against the policy the DMS enforces, so the signup of a module accepts exactly what the DMS accepts. - Account emails. The domain sends the second-factor, email-validation, password-reset and invite messages that the authentication flows depend on.
- One registration path.
announceRegistrationruns the notifications and theUSER_REGISTEREDhook, whichever flow created the account.
Dependencies
This domain relies on the following AntelopeJS packages:
@antelopejs/interface-api: Controllers,RequestContext,SetParameterProviderandHTTPResult, used to read the request and to reject it.@antelopejs/interface-core:InterfaceFunction, which wires the token, sanitization and mail functions to their implementation, and the decorator factories the authentication decorators are built with.@antelopejs/interface-database-decorators: Table definitions, theHashModifiermixin, andBasicDataModelfor the user, session and identity models.@antelopejs/interface-database: The query value proxies behind the owner lookups ofUserModel.
Quick start
The following controller gates a route on an authenticated user and returns the account with its secrets stripped.
import { Controller, Get } from "@antelopejs/interface-api";
import {
AuthUser,
sanitizeUser,
} from "@antelopejs/interface-dms/auth";
import type { User } from "@antelopejs/interface-dms/auth/db";
export class ProfileController extends Controller("/api/profile") {
@Get("")
profile(@AuthUser() user: User): Promise<Partial<User>> {
// 401 before this line runs when the token is missing or invalid
return sanitizeUser(user);
}
}
A client calls it with the access token issued at login, in the Authorization header, as Bearer <access token>.
Pages in this section
- Authentication: Resolve a request to a user with the authentication decorators and
authenticateRequestUser. - Tokens and sessions: Issue and validate access, refresh, two-factor and tenant-assignment tokens, and record login sessions.
- Users: The user table and model, password hashing, the password policy, sanitization, the account mail functions, and announcing a registration.
- External identities: Bind and read the accounts a user owns on external login providers.