Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ross/docs/docs/authentication/api-key.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Rules enforced at startup:
- `userModel` is not used. There is no identity table.
- `rbac.enabled: true` is rejected: RBAC needs per-user roles, which a shared key cannot provide.

A valid minimal configuration therefore needs only `enabled` and `providers`. `jwtSecret` must be absent, and `userModel` / `tokenExpiration` have no effect.
A valid minimal configuration therefore needs only `enabled` and `providers`. `jwtSecret` must be absent, and `userModel` / `accessTokenExpiration` / `refreshTokenExpiration` / `sessionStorage` have no effect.

## Using the key

Expand Down
86 changes: 78 additions & 8 deletions ross/docs/docs/authentication/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ ROSS has a built-in authentication layer that is entirely configuration-driven.
The authentication surface is built around a single contract:

```text
provider verifies the caller → ROSS resolves a local user row → ROSS issues one JWT
provider verifies the caller → ROSS resolves a local user row → ROSS issues access + refresh tokens
```

Password login, OAuth2, OIDC, and SAML differ only in how the caller is verified. They all converge on the same user table, the same access token, the same `Authorization: Bearer <token>` header, and the same RBAC claims. An `api-key` provider is the exception: it authenticates the caller statically, without a user row or a JWT.
Password login, OAuth2, OIDC, and SAML differ only in how the caller is verified. They all converge on the same user table, the same access and refresh tokens, the same `Authorization: Bearer <token>` header, and the same RBAC claims. An `api-key` provider is the exception: it authenticates the caller statically, without a user row, a JWT, or a refresh token.

Authentication is optional at the top level. When it is omitted, ROSS starts normally and every generated API is public unless you opt into route-level authorization.
Authentication is optional at the top level. When it is omitted or disabled, ROSS starts normally with every generated API public, and setting route-level `authorization: true` is rejected at startup.

## Supported providers

| Provider | Identifier | Caller verified by | Access token | Documentation |
| Provider | Identifier | Caller verified by | Token | Documentation |
| --- | --- | --- | --- | --- |
| Local password | `"up-auth"` | Username and password (bcrypt) | JWT | [Password authentication](./up-auth.md) |
| OAuth2 | `"sso"` with `"protocol": "oauth2"` | External OAuth2 authorization server | JWT | [OAuth2](./oauth2.md) |
Expand All @@ -35,7 +35,9 @@ Authentication lives at the top level of your configuration file:
"authentication": {
"enabled": true,
"jwtSecret": "env:JWT_SECRET",
"tokenExpiration": "1d",
"accessTokenExpiration": "15m",
"refreshTokenExpiration": "7d",
"sessionStorage": "db",
"userModel": {
"modelName": "users",
"idField": "id",
Expand Down Expand Up @@ -73,7 +75,9 @@ Authentication lives at the top level of your configuration file:
| --- | --- | --- | --- | --- |
| `enabled` | `boolean` | **Yes** | — | Enables the auth layer. When `false`, no other key may be present. |
| `jwtSecret` | `string` | Conditional | — | Secret used to sign access tokens. Minimum 32 characters. Required when an identity provider is configured, forbidden when only `api-key` is configured. |
| `tokenExpiration` | `string` | No | `"1d"` | Access-token lifetime. Pattern: digits followed by `s`, `m`, `h`, or `d` (for example `"15m"`, `"8h"`, `"7d"`). |
| `accessTokenExpiration` | `string` | No | `"15m"` | Access-token lifetime. Pattern: digits followed by `s`, `m`, `h`, or `d` (for example `"15m"`, `"8h"`, `"7d"`). |
| `refreshTokenExpiration` | `string` | No | `"7d"` | Refresh-token session lifetime. Pattern: digits followed by `s`, `m`, `h`, or `d`. |
| `sessionStorage` | `"db" \| "cache"` | No | `"db"` | Backend used to store refresh token sessions (`"db"` uses an internal `_ross_sessions` table; `"cache"` uses the configured cache backend). |
| `userModel` | `object` | Conditional | — | The table that stores identities. Required when `up-auth` or `sso` is configured. |
| `providers` | `array` | **Yes** | — | One or more provider entries. Minimum one item. |

Expand Down Expand Up @@ -119,7 +123,7 @@ Validation runs before the server starts, so configuration mistakes fail fast:
- At most **one** `up-auth` provider and at most **one** `api-key` provider may be configured.
- `api-key` cannot be combined with any identity provider. An application either authenticates users or accepts a shared key.
- Every identity provider requires `jwtSecret` and a `userModel` whose fields exist in the referenced model.
- SSO provider `name` values are lowercase alphanumeric with optional hyphens, up to 32 characters, and must be unique. Names that collide with auth route segments are reserved: `api-key`, `callback`, `change-email`, `change-password`, `edit-me`, `forgot-password`, `login`, `me`, `metadata`, `providers`, `register`, `registration`, `resend-otp`, `sso`, `token`, `up-auth`, `user`, `verify`.
- SSO provider `name` values are lowercase alphanumeric with optional hyphens, up to 32 characters, and must be unique. Names that collide with auth route segments are reserved: `api-key`, `callback`, `change-email`, `change-password`, `edit-me`, `forgot-password`, `login`, `logout`, `me`, `metadata`, `providers`, `refresh`, `register`, `registration`, `resend-otp`, `sso`, `token`, `up-auth`, `user`, `verify`.
- `up-auth` and SSO providers can be combined. `api-key` cannot.

## Access tokens
Expand All @@ -141,7 +145,7 @@ Every identity provider issues the same JWT. The payload is built from `userMode
- `providerId` holds the external SSO subject identifier (for SSO logins) or `null` (for `up-auth` logins).
- `email` holds the user's email/username (or `null` if unconfigured or absent). If a custom `usernameField` is configured (such as `"username"`), that property is also included.
- `role` or `roles` appear only when RBAC is enabled. They are extracted from `rbac.roleField`, falling back to `rbac.defaultRole`.
- The token is signed with `jwtSecret` and expires after `tokenExpiration`.
- The token is signed with `jwtSecret` and expires after `accessTokenExpiration`.
- Clients send it as `Authorization: Bearer <token>`.

Protected routes validate the token before the handler runs. A missing or invalid token returns `401` with `"Invalid or expired authentication token"`.
Expand All @@ -152,6 +156,70 @@ Rotating `jwtSecret` invalidates every access token, every in-flight SSO flow, a

:::

## Refresh tokens and session storage

Every identity login (`POST /auth/login`, `POST /auth/login/verify/otp`, and SSO callback or token exchange) issues a `refreshToken` alongside `accessToken`:

```json
{
"accessToken": "eyJhbGciOi...",
"refreshToken": "xK9..."
}
```

- Refresh tokens are opaque 32-byte random strings (`base64url`). ROSS stores only their SHA-256 hex digest, never the raw token.
- Each session expires after `refreshTokenExpiration` (default `"7d"`).
- Refresh tokens are **single-use and rotated**: exchanging a refresh token deletes the consumed session and issues a new `accessToken` and `refreshToken` pair. Replaying an already-used or expired token returns `401`.

### Session storage backends

Configure where refresh token sessions live with `authentication.sessionStorage`:

- **`"db"` (default)**: Stores sessions in an internal `_ross_sessions` table (`id`, `user_id`, `token_hash`, `expires_at`, `created_at`). ROSS automatically includes `_ross_sessions` in database migrations when `sessionStorage` is `"db"`, and deletes all of a user's sessions when they call `DELETE /auth/user/me`. `_ross_sessions` is reserved and cannot be used as a model name under `data.models`.
- **`"cache"`**: Stores sessions in the configured cache under `session:refresh:<token_hash>` with a TTL matching `refreshTokenExpiration`. When no distributed cache (Redis or Memcached) is configured, sessions are stored in in-memory `node-cache` (not shared across processes and lost on restart), and ROSS logs a startup warning.

### Refreshing tokens

```http
POST /api/v1/auth/refresh
Content-Type: application/json

{"refreshToken": "xK9..."}
```

```json
{
"code": 200,
"message": "Token refreshed successfully",
"data": {
"accessToken": "eyJhbGciOi...",
"refreshToken": "mP4..."
}
}
```

If the token is missing, invalid, expired, already consumed, or belongs to a deleted user, the endpoint returns `401` with `"Invalid or expired refresh token"`.

### Logging out

```http
POST /api/v1/auth/logout
Authorization: Bearer <accessToken>
Content-Type: application/json

{"refreshToken": "mP4..."}
```

```json
{
"code": 200,
"message": "Logged out successfully",
"data": {"success": true}
}
```

Revokes the given refresh token session if it belongs to the authenticated user.

## Provider discovery

When at least one identity provider (`up-auth` or `sso`) is configured, ROSS exposes a discovery document:
Expand Down Expand Up @@ -202,6 +270,8 @@ Auth routes are mounted under the API prefix and variant, so the default base pa
| Route | Requires |
| --- | --- |
| `GET /auth/providers` | At least one identity provider |
| `POST /auth/refresh` | At least one identity provider |
| `POST /auth/logout` | At least one identity provider |
| `POST /auth/register` | `up-auth` |
| `POST /auth/login` | `up-auth` |
| `POST /auth/change-password` | `up-auth` |
Expand Down
10 changes: 6 additions & 4 deletions ross/docs/docs/authentication/oauth2.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ ROSS starts the flow, exchanges the authorization code for an access token, read
"authentication": {
"enabled": true,
"jwtSecret": "env:JWT_SECRET",
"tokenExpiration": "1d",
"accessTokenExpiration": "15m",
"refreshTokenExpiration": "7d",
"sessionStorage": "db",
"userModel": {
"modelName": "users",
"idField": "id",
Expand Down Expand Up @@ -99,7 +101,7 @@ The path is the login route with `/login` replaced by `/callback`. Register that
4. ROSS POST https://github.com/login/oauth/access_token
5. ROSS GET https://api.github.com/user (Bearer token)
6. ROSS find-or-create the local user for (github, <subject>)
7. ROSS 200 {"accessToken": "..."} or 302 <redirectUrl>?code=<one-time code>
7. ROSS 200 {"accessToken": "...", "refreshToken": "..."} or 302 <redirectUrl>?code=<one-time code>
```

### 1. Start the flow
Expand Down Expand Up @@ -170,7 +172,7 @@ When `redirectUrl` is not configured, the callback responds directly with the ac
{
"code": 200,
"message": "SSO authentication successful",
"data": {"accessToken": "eyJhbGciOi..."}
"data": {"accessToken": "eyJhbGciOi...", "refreshToken": "xK9..."}
}
```

Expand All @@ -196,7 +198,7 @@ Content-Type: application/json
{
"code": 200,
"message": "SSO authentication successful",
"data": {"accessToken": "eyJhbGciOi..."}
"data": {"accessToken": "eyJhbGciOi...", "refreshToken": "xK9..."}
}
```

Expand Down
10 changes: 6 additions & 4 deletions ross/docs/docs/authentication/oidc.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ ROSS discovers the provider's endpoints and signing keys from `{issuerUrl}/.well
"authentication": {
"enabled": true,
"jwtSecret": "env:JWT_SECRET",
"tokenExpiration": "1d",
"accessTokenExpiration": "15m",
"refreshTokenExpiration": "7d",
"sessionStorage": "db",
"userModel": {
"modelName": "users",
"idField": "id",
Expand Down Expand Up @@ -97,7 +99,7 @@ When `callbackUrl` is omitted, ROSS derives it per request from the login route,
5. ROSS POST token endpoint with code + PKCE verifier
6. ROSS validate ID token signature, issuer, audience, nonce, and state
7. ROSS find-or-create the local user for (google, sub)
8. ROSS 200 {"accessToken": "..."} or 302 <redirectUrl>?code=<one-time code>
8. ROSS 200 {"accessToken": "...", "refreshToken": "..."} or 302 <redirectUrl>?code=<one-time code>
```

### 1. Discovery and authorization
Expand Down Expand Up @@ -160,7 +162,7 @@ Without `redirectUrl`, the callback returns the token directly:
{
"code": 200,
"message": "SSO authentication successful",
"data": {"accessToken": "eyJhbGciOi..."}
"data": {"accessToken": "eyJhbGciOi...", "refreshToken": "xK9..."}
}
```

Expand All @@ -184,7 +186,7 @@ Content-Type: application/json
{
"code": 200,
"message": "SSO authentication successful",
"data": {"accessToken": "eyJhbGciOi..."}
"data": {"accessToken": "eyJhbGciOi...", "refreshToken": "xK9..."}
}
```

Expand Down
10 changes: 6 additions & 4 deletions ross/docs/docs/authentication/saml.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ SAML is the right choice for enterprise identity providers that do not expose OA
"authentication": {
"enabled": true,
"jwtSecret": "env:JWT_SECRET",
"tokenExpiration": "1d",
"accessTokenExpiration": "15m",
"refreshTokenExpiration": "7d",
"sessionStorage": "db",
"userModel": {
"modelName": "users",
"idField": "id",
Expand Down Expand Up @@ -111,7 +113,7 @@ The path is the login route with `/login` replaced by `/callback`, or the metada
SAMLResponse=<base64 assertion>&RelayState=...
5. ROSS validate signature, audience, conditions, and InResponseTo
6. ROSS find-or-create the local user for (corp, NameID)
7. ROSS 200 {"accessToken": "..."} or 302 <redirectUrl>?code=<one-time code>
7. ROSS 200 {"accessToken": "...", "refreshToken": "..."} or 302 <redirectUrl>?code=<one-time code>
```

### 1. Start the flow
Expand Down Expand Up @@ -170,7 +172,7 @@ Without `redirectUrl`, the ACS responds with the token:
{
"code": 200,
"message": "SSO authentication successful",
"data": {"accessToken": "eyJhbGciOi..."}
"data": {"accessToken": "eyJhbGciOi...", "refreshToken": "xK9..."}
}
```

Expand All @@ -194,7 +196,7 @@ Content-Type: application/json
{
"code": 200,
"message": "SSO authentication successful",
"data": {"accessToken": "eyJhbGciOi..."}
"data": {"accessToken": "eyJhbGciOi...", "refreshToken": "xK9..."}
}
```

Expand Down
22 changes: 16 additions & 6 deletions ross/docs/docs/authentication/up-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ This is the only provider that owns a password. It also provides registration, p
"authentication": {
"enabled": true,
"jwtSecret": "env:JWT_SECRET",
"tokenExpiration": "1d",
"accessTokenExpiration": "15m",
"refreshTokenExpiration": "7d",
"sessionStorage": "db",
"userModel": {
"modelName": "users",
"idField": "id",
Expand Down Expand Up @@ -147,7 +149,10 @@ The body accepts exactly the two configured fields; any additional property is r
{
"code": 200,
"message": "Login successful",
"data": {"accessToken": "eyJhbGciOi..."}
"data": {
"accessToken": "eyJhbGciOi...",
"refreshToken": "xK9..."
}
}
```

Expand Down Expand Up @@ -191,15 +196,18 @@ Content-Type: application/json
{
"code": 200,
"message": "OTP verification successful",
"data": {"accessToken": "eyJhbGciOi..."}
"data": {
"accessToken": "eyJhbGciOi...",
"refreshToken": "xK9..."
}
}
```

The same route behaviour is used for the three OTP flows:

| Route | Action | Body |
| --- | --- | --- |
| `POST /auth/login/verify/otp` | Completes login and returns `accessToken` | `ulid`, `otp`, username |
| `POST /auth/login/verify/otp` | Completes login and returns `accessToken` and `refreshToken` | `ulid`, `otp`, username |
| `POST /auth/register/verify/otp` | Marks the account verified, returns `data: null` | `ulid`, `otp`, username |
| `POST /auth/forgot-password/verify/otp` | Sets the new password, returns `{success: true}` | `ulid`, `otp`, username, `newPassword` |

Expand Down Expand Up @@ -411,7 +419,7 @@ Deletion runs inside a transaction. The row is removed permanently; ROSS does no
| `<usernameField>` | The username column, when it has a value |
| `role` | `rbac.roleField` when RBAC is enabled and the value is a scalar |
| `roles` | `rbac.roleField` when the value is an array or a JSON-encoded array |
| `iat`, `exp` | Issued-at and `tokenExpiration` |
| `iat`, `exp` | Issued-at and `accessTokenExpiration` |

## Full example with MFA and verification

Expand All @@ -429,7 +437,7 @@ Deletion runs inside a transaction. The row is removed permanently; ROSS does no
"authentication": {
"enabled": true,
"jwtSecret": "env:JWT_SECRET",
"tokenExpiration": "8h",
"accessTokenExpiration": "8h",
"userModel": {
"modelName": "users",
"idField": "id",
Expand Down Expand Up @@ -505,6 +513,8 @@ Password routes use the API policy identifiers `auth.<variant>.<userModel>.unkno
| --- | --- |
| Registration | `auth.v1.users.unknown.registration` |
| Login | `auth.v1.users.unknown.login` |
| Refresh token | `auth.v1.users.unknown.refresh` |
| Logout | `auth.v1.users.unknown.logout` |
| Change password | `auth.v1.users.unknown.changePassword` |
| Forgot password | `auth.v1.users.unknown.forgotPassword` |
| OTP verify (login) | `auth.v1.users.unknown.otpVerifyLogin` |
Expand Down
Loading
Loading