@julr/sesame 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +1 -1
- package/README.md +405 -62
- package/build/authorize_controller-BiycO4be.js +251 -0
- package/build/chunk-DF48asd8.js +9 -0
- package/build/{client_info_controller-BucHGx4u.js → client_info_controller-AcOG8lWu.js} +11 -3
- package/build/commands/sesame_client.d.ts +20 -0
- package/build/commands/sesame_key.d.ts +12 -0
- package/build/commands/sesame_purge.d.ts +0 -2
- package/build/commands/sesame_purge.js +15 -3
- package/build/configure-DkDkIlt8.js +27 -0
- package/build/configure.js +2 -24
- package/build/consent_controller-Dsdhv6-f.js +108 -0
- package/build/id_token_service-CpTzOUDe.js +54 -0
- package/build/index.d.ts +1 -1
- package/build/index.js +30 -10
- package/build/{introspect_controller-un95fs4y.js → introspect_controller-DvOp9scr.js} +21 -7
- package/build/issue_authorization_code-B9ERu1uO.js +40 -0
- package/build/jwks_controller-keo4kBZc.js +26 -0
- package/build/{main-B3M6ihoS.js → main-DGBJhq3E.js} +34 -4
- package/build/metadata_controller-BVsTo0Gp.js +158 -0
- package/build/{oauth_access_token-bsoM5KeU.js → oauth_access_token-Cz_5gNBx.js} +12 -1
- package/build/oauth_client-BSanvSql.js +63 -0
- package/build/oauth_error-C7UhDb2q.js +189 -0
- package/build/providers/sesame_provider.js +14 -3
- package/build/{register_controller-Dch4ecyD.js → register_controller-gbq7p8a5.js} +46 -7
- package/build/{revoke_controller-DnPmzYMd.js → revoke_controller-z_ghrEB7.js} +21 -8
- package/build/services/main.js +7 -3
- package/build/sesame_manager-B1Jgq1v2.js +6 -0
- package/build/sesame_manager-DYUSZ0NC.js +693 -0
- package/build/src/actions/authorize.d.ts +46 -0
- package/build/src/actions/exchange_authorization_code.d.ts +34 -0
- package/build/src/actions/exchange_client_credentials.d.ts +28 -0
- package/build/src/actions/exchange_refresh_token.d.ts +59 -0
- package/build/src/actions/issue_authorization_code.d.ts +26 -0
- package/build/src/controllers/authorize_controller.d.ts +13 -12
- package/build/src/controllers/consent_controller.d.ts +5 -0
- package/build/src/controllers/jwks_controller.d.ts +14 -0
- package/build/src/controllers/metadata_controller.d.ts +9 -2
- package/build/src/controllers/token_controller.d.ts +8 -5
- package/build/src/controllers/userinfo_controller.d.ts +14 -0
- package/build/src/guard/main.js +5 -5
- package/build/src/middleware/any_scope_middleware.js +11 -1
- package/build/src/middleware/scope_middleware.js +11 -1
- package/build/src/models/oauth_authorization_code.d.ts +1 -0
- package/build/src/models/oauth_pending_authorization_request.d.ts +1 -0
- package/build/src/oauth_error.d.ts +1 -1
- package/build/src/routes.d.ts +3 -1
- package/build/src/services/id_token_service.d.ts +30 -0
- package/build/src/services/key_service.d.ts +20 -0
- package/build/src/sesame_manager.d.ts +54 -3
- package/build/src/types.d.ts +124 -3
- package/build/stubs/main.ts +5 -0
- package/build/stubs/migrations/create_oauth_authorization_codes_table.stub +1 -0
- package/build/stubs/migrations/create_oauth_pending_authorization_requests_table.stub +1 -0
- package/build/stubs/migrations/create_oauth_refresh_tokens_table.stub +1 -1
- package/build/token_controller-DyI7oy-U.js +481 -0
- package/build/token_service-DwnfAR9F.js +59 -0
- package/build/userinfo_controller-RLk8cN_o.js +40 -0
- package/build/vite.config.d.ts +2 -0
- package/package.json +26 -41
- package/build/authorize_controller-BGzxPvYU.js +0 -138
- package/build/client_service-C3rfXGk_.js +0 -65
- package/build/consent_controller-BHoB9mip.js +0 -85
- package/build/decorate-BKZEjPRg.js +0 -15
- package/build/metadata_controller-CJeZG93_.js +0 -81
- package/build/oauth_client-BIoY5jBR.js +0 -24
- package/build/oauth_error-CnJ3L8tf.js +0 -94
- package/build/sesame_manager-BQIW2mqt.js +0 -4
- package/build/sesame_manager-C-eEFFHM.js +0 -167
- package/build/src/grants/authorization_code_grant.d.ts +0 -27
- package/build/src/grants/client_credentials_grant.d.ts +0 -23
- package/build/src/grants/refresh_token_grant.d.ts +0 -27
- package/build/token_controller-hGDAYuBS.js +0 -194
- package/build/token_service-fhoA4slP.js +0 -31
package/LICENSE.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# The MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2025-present Julien Ripouteau
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
6
|
|
package/README.md
CHANGED
|
@@ -2,20 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
> OAuth 2.1 + OIDC server for AdonisJS
|
|
4
4
|
|
|
5
|
-
Sésame
|
|
5
|
+
Sésame turns your AdonisJS application into a full-featured OAuth 2.1 authorization server. This guide covers:
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- Installing and configuring the package
|
|
8
|
+
- Registering OAuth and discovery routes
|
|
9
|
+
- Protecting your API with the OAuth guard and scope checking
|
|
10
|
+
- Managing tokens (refresh, revoke, introspect)
|
|
11
|
+
- Enabling OpenID Connect (OIDC) for `id_token` emission, `/userinfo`, and JWKS
|
|
12
|
+
- Using the Client Credentials grant for machine-to-machine authentication
|
|
13
|
+
- Dynamic client registration and MCP support
|
|
8
14
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
- **OAuth Server Metadata** (RFC 8414)
|
|
15
|
-
- **Protected Resource Metadata** (RFC 9728) for MCP servers
|
|
16
|
-
- **Type-safe scopes** via module augmentation
|
|
17
|
-
- **OAuth guard** for `@adonisjs/auth` with scope-checking middleware
|
|
18
|
-
- **Token cleanup** via `sesame:purge` Ace command
|
|
15
|
+
## Overview
|
|
16
|
+
|
|
17
|
+
Modern applications need a reliable way to delegate authorization. Sésame implements the OAuth 2.1 specification on top of AdonisJS, giving you an authorization code flow with PKCE, refresh token rotation with replay detection, token introspection, revocation, and dynamic client registration out of the box.
|
|
18
|
+
|
|
19
|
+
When you need identity claims on top of authorization, Sésame supports OpenID Connect. You provide an RSA key pair, wire up a user provider, and the server starts issuing signed `id_token` JWTs alongside access tokens.
|
|
19
20
|
|
|
20
21
|
## Installation
|
|
21
22
|
|
|
@@ -23,13 +24,7 @@ Sésame is an AdonisJS package that turns your application into a full-featured
|
|
|
23
24
|
node ace add @julr/sesame
|
|
24
25
|
```
|
|
25
26
|
|
|
26
|
-
This will:
|
|
27
|
-
|
|
28
|
-
- Publish the configuration file to `config/sesame.ts`
|
|
29
|
-
- Publish database migrations (6 tables)
|
|
30
|
-
- Register the service provider and commands
|
|
31
|
-
|
|
32
|
-
Then run the migrations:
|
|
27
|
+
This will publish the configuration file to `config/sesame.ts`, create six database migration files, and register the service provider and commands. Then run the migrations:
|
|
33
28
|
|
|
34
29
|
```bash
|
|
35
30
|
node ace migration:run
|
|
@@ -37,9 +32,9 @@ node ace migration:run
|
|
|
37
32
|
|
|
38
33
|
## Configuration
|
|
39
34
|
|
|
40
|
-
The configuration file lives at `config/sesame.ts
|
|
35
|
+
The configuration file lives at `config/sesame.ts`. You define your issuer URL, available scopes, grant types, token lifetimes, and page redirects for the authorization flow.
|
|
41
36
|
|
|
42
|
-
```ts
|
|
37
|
+
```ts title="config/sesame.ts"
|
|
43
38
|
import env from '#start/env'
|
|
44
39
|
import { defineConfig } from '@julr/sesame'
|
|
45
40
|
import type { InferScopes } from '@julr/sesame/types'
|
|
@@ -74,13 +69,13 @@ declare module '@julr/sesame/types' {
|
|
|
74
69
|
}
|
|
75
70
|
```
|
|
76
71
|
|
|
77
|
-
The `SesameScopes` augmentation gives you type-safe scope names throughout your application.
|
|
72
|
+
The `SesameScopes` module augmentation gives you type-safe scope names throughout your application. When you reference a scope in middleware or guard calls, TypeScript will autocomplete and validate against the scopes you declared.
|
|
78
73
|
|
|
79
74
|
## Routes
|
|
80
75
|
|
|
81
|
-
|
|
76
|
+
You must register OAuth routes from your `start/routes.ts` file. The OAuth endpoints go inside a prefix group, and the discovery endpoints go at the root level so they remain at `/.well-known/...`.
|
|
82
77
|
|
|
83
|
-
```ts
|
|
78
|
+
```ts title="start/routes.ts"
|
|
84
79
|
import sesame from '@julr/sesame/services/main'
|
|
85
80
|
|
|
86
81
|
// OAuth endpoints under /oauth
|
|
@@ -90,31 +85,50 @@ router
|
|
|
90
85
|
})
|
|
91
86
|
.prefix('/oauth')
|
|
92
87
|
|
|
93
|
-
// Discovery endpoints at
|
|
94
|
-
sesame.
|
|
88
|
+
// Discovery + JWKS endpoints at root
|
|
89
|
+
sesame.registerDiscoveryRoutes()
|
|
95
90
|
```
|
|
96
91
|
|
|
97
92
|
This registers the following endpoints:
|
|
98
93
|
|
|
99
|
-
| Method
|
|
100
|
-
|
|
|
101
|
-
| `POST`
|
|
102
|
-
| `GET`
|
|
103
|
-
| `POST`
|
|
104
|
-
| `POST`
|
|
105
|
-
| `POST`
|
|
106
|
-
| `POST`
|
|
107
|
-
| `GET`
|
|
108
|
-
| `GET`
|
|
109
|
-
| `GET`
|
|
94
|
+
| Method | Path | Description |
|
|
95
|
+
| ---------- | ----------------------------------------- | ---------------------------------------- |
|
|
96
|
+
| `POST` | `/oauth/token` | Token endpoint (RFC 6749 §3.2) |
|
|
97
|
+
| `GET` | `/oauth/authorize` | Authorization endpoint (RFC 6749 §3.1) |
|
|
98
|
+
| `POST` | `/oauth/consent` | Consent submission |
|
|
99
|
+
| `POST` | `/oauth/introspect` | Token introspection (RFC 7662) |
|
|
100
|
+
| `POST` | `/oauth/revoke` | Token revocation (RFC 7009) |
|
|
101
|
+
| `POST` | `/oauth/register` | Dynamic client registration (RFC 7591) |
|
|
102
|
+
| `GET` | `/oauth/client-info` | Public client information |
|
|
103
|
+
| `GET/POST` | `/oauth/userinfo` | OpenID Connect UserInfo (OIDC Core §5.3) |
|
|
104
|
+
| `GET` | `/.well-known/oauth-authorization-server` | Server metadata (RFC 8414) |
|
|
105
|
+
| `GET` | `/.well-known/openid-configuration` | OIDC discovery |
|
|
106
|
+
| `GET` | `/.well-known/oauth-protected-resource` | Protected resource metadata (RFC 9728) |
|
|
107
|
+
| `GET` | `/jwks` | JSON Web Key Set (RFC 7517) |
|
|
108
|
+
|
|
109
|
+
The JWKS path defaults to `/jwks`. You can customize it:
|
|
110
|
+
|
|
111
|
+
```ts title="start/routes.ts"
|
|
112
|
+
sesame.registerDiscoveryRoutes({ jwksPath: '/.well-known/jwks.json' })
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Authorization Code Flow
|
|
116
|
+
|
|
117
|
+
The authorization code flow works in three steps. All clients must use PKCE with S256 (mandatory per OAuth 2.1).
|
|
118
|
+
|
|
119
|
+
1. The consuming app redirects the user to `GET /oauth/authorize` with `client_id`, `redirect_uri`, `response_type=code`, `scope`, `state`, `code_challenge`, and `code_challenge_method=S256`. If the user is not logged in, they are sent to your `loginPage`. Once authenticated, they see the consent screen (your `consentPage`). If the user has already approved the requested scopes, consent is skipped and the code is issued directly.
|
|
120
|
+
|
|
121
|
+
2. After the user approves, they are redirected back to the `redirect_uri` with a `code` and `state` parameter. The consuming app exchanges the code at `POST /oauth/token` with `grant_type=authorization_code`, the `code`, `redirect_uri`, client credentials, and the PKCE `code_verifier`. The response contains an `access_token`, `refresh_token` (when the `refresh_token` grant is enabled), `token_type`, `expires_in`, and `scope`. The `iss` parameter is included in all redirect responses per RFC 9207.
|
|
122
|
+
|
|
123
|
+
3. The consuming app passes the access token as a `Bearer` token in the `Authorization` header when calling your API.
|
|
110
124
|
|
|
111
125
|
## Authentication Guard
|
|
112
126
|
|
|
113
|
-
Sésame provides an OAuth guard for `@adonisjs/auth
|
|
127
|
+
Sésame provides an OAuth guard for `@adonisjs/auth` that verifies opaque Bearer tokens against the database, checks revocation and expiry, and resolves the user model. Configure it in `config/auth.ts`:
|
|
114
128
|
|
|
115
|
-
```ts
|
|
129
|
+
```ts title="config/auth.ts"
|
|
130
|
+
import { defineConfig } from '@adonisjs/auth'
|
|
116
131
|
import { oauthGuard, oauthUserProvider } from '@julr/sesame/guard'
|
|
117
|
-
import User from '#models/user'
|
|
118
132
|
|
|
119
133
|
const authConfig = defineConfig({
|
|
120
134
|
default: 'web',
|
|
@@ -127,25 +141,32 @@ const authConfig = defineConfig({
|
|
|
127
141
|
})
|
|
128
142
|
```
|
|
129
143
|
|
|
130
|
-
Then use
|
|
144
|
+
Then use the guard in your controllers. After authentication, you have access to the user, the granted scopes, and the client ID.
|
|
145
|
+
|
|
146
|
+
```ts title="app/controllers/api_controller.ts"
|
|
147
|
+
import type { HttpContext } from '@adonisjs/core/http'
|
|
131
148
|
|
|
132
|
-
```ts
|
|
133
149
|
export default class ApiController {
|
|
134
150
|
async index({ auth }: HttpContext) {
|
|
135
151
|
const guard = auth.use('oauth')
|
|
136
152
|
await guard.authenticate()
|
|
137
153
|
|
|
138
154
|
const user = auth.user!
|
|
139
|
-
const scopes = guard.scopes
|
|
155
|
+
const scopes = guard.scopes // e.g. ['read', 'write']
|
|
156
|
+
const clientId = guard.clientId // e.g. 'my-app-client-id'
|
|
157
|
+
|
|
158
|
+
return { user, scopes, clientId }
|
|
140
159
|
}
|
|
141
160
|
}
|
|
142
161
|
```
|
|
143
162
|
|
|
144
|
-
##
|
|
163
|
+
## Scopes
|
|
145
164
|
|
|
146
|
-
|
|
165
|
+
### Scope Middleware
|
|
147
166
|
|
|
148
|
-
|
|
167
|
+
Two named middleware are available for checking scopes on authenticated requests. Use `scopes` when the client must have **all** listed scopes, and `anyScope` when having **at least one** is sufficient.
|
|
168
|
+
|
|
169
|
+
```ts title="start/routes.ts"
|
|
149
170
|
// Requires ALL listed scopes
|
|
150
171
|
router.get('/admin', [AdminController]).use(middleware.scopes({ scopes: ['admin', 'write'] }))
|
|
151
172
|
|
|
@@ -153,32 +174,349 @@ router.get('/admin', [AdminController]).use(middleware.scopes({ scopes: ['admin'
|
|
|
153
174
|
router.get('/data', [DataController]).use(middleware.anyScope({ scopes: ['read', 'write'] }))
|
|
154
175
|
```
|
|
155
176
|
|
|
156
|
-
|
|
177
|
+
Important: these middleware are `TransientToken`-like. If the request carries an OAuth Bearer token, scopes are enforced against that token. If there is no Bearer token but the request is already authenticated through a session/web guard, the middleware lets the request through instead of rejecting on missing OAuth scopes.
|
|
178
|
+
|
|
179
|
+
Use these middleware on routes that are allowed to accept either:
|
|
180
|
+
|
|
181
|
+
- a scoped OAuth access token
|
|
182
|
+
- or a first-party session-authenticated user
|
|
183
|
+
|
|
184
|
+
If you want to require OAuth scopes strictly, authenticate with `auth.use('oauth').authenticate()` in your controller or route pipeline and check scopes on that guard explicitly.
|
|
157
185
|
|
|
158
|
-
|
|
186
|
+
### Programmatic Scope Checking
|
|
187
|
+
|
|
188
|
+
You can also check scopes directly in your controller logic using `hasScope()` and `hasAnyScope()` on the guard instance. This is useful when you need conditional behavior based on scopes rather than a hard reject.
|
|
189
|
+
|
|
190
|
+
```ts title="app/controllers/posts_controller.ts"
|
|
191
|
+
import type { HttpContext } from '@adonisjs/core/http'
|
|
192
|
+
|
|
193
|
+
export default class PostsController {
|
|
194
|
+
async index({ auth }: HttpContext) {
|
|
195
|
+
const guard = auth.use('oauth')
|
|
196
|
+
await guard.authenticate()
|
|
197
|
+
|
|
198
|
+
// Check if the token has a specific scope
|
|
199
|
+
if (guard.hasScope('write')) {
|
|
200
|
+
return { posts: await Post.all(), canEdit: true }
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return { posts: await Post.all(), canEdit: false }
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
`hasScope()` requires **all** provided scopes. `hasAnyScope()` requires **at least one**.
|
|
209
|
+
|
|
210
|
+
## Managing Tokens
|
|
211
|
+
|
|
212
|
+
### Refreshing tokens
|
|
213
|
+
|
|
214
|
+
The consuming app sends `POST /oauth/token` with `grant_type=refresh_token`, the `refresh_token`, and client credentials to get a new token pair. Sésame uses **refresh token rotation**: every refresh returns a new refresh token and the old one is revoked immediately. If an attacker replays a revoked refresh token, all tokens for that client+user pair are nuked as a security measure. The client can request a narrower set of scopes by passing a `scope` parameter, but cannot request scopes that were not in the original grant.
|
|
215
|
+
|
|
216
|
+
### Revoking tokens
|
|
217
|
+
|
|
218
|
+
The consuming app can call `POST /oauth/revoke` with the `token`, optional `token_type_hint`, and client credentials. The endpoint always returns HTTP 200, even if the token was not found (to prevent information leakage per RFC 7009). When revoking a refresh token, the associated access token is also revoked automatically.
|
|
219
|
+
|
|
220
|
+
On the server side, you can revoke all tokens for a user at once. This is useful when a user is deleted or deactivated.
|
|
159
221
|
|
|
160
222
|
```ts
|
|
161
|
-
sesame
|
|
162
|
-
|
|
163
|
-
|
|
223
|
+
import sesame from '@julr/sesame/services/main'
|
|
224
|
+
|
|
225
|
+
await sesame.revokeAllForUser(user.id)
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Introspecting tokens
|
|
229
|
+
|
|
230
|
+
Resource servers can verify a token's state by calling `POST /oauth/introspect` with the `token`, optional `token_type_hint`, and client credentials. The response is `{ "active": true, "token_type": "Bearer", "client_id": "...", "sub": "...", "scope": "...", ... }` for valid tokens, or `{ "active": false }` for invalid, expired, or revoked tokens. This is useful when a separate service needs to validate tokens without sharing database access.
|
|
231
|
+
|
|
232
|
+
## OpenID Connect (OIDC)
|
|
233
|
+
|
|
234
|
+
Sésame supports OpenID Connect on top of OAuth 2.1. When OIDC is enabled, the server issues signed `id_token` JWTs alongside access tokens, exposes a `/userinfo` endpoint for retrieving user claims, and publishes a JWKS so relying parties can verify token signatures.
|
|
235
|
+
|
|
236
|
+
OIDC is opt-in. You need two things: an RSA key pair (JWK) for signing ID tokens, and a user provider so the server can resolve user claims.
|
|
237
|
+
|
|
238
|
+
### Generating a JWK
|
|
239
|
+
|
|
240
|
+
You need an RSA private key in JWK format. The easiest way is to write it directly to your `.env` file:
|
|
241
|
+
|
|
242
|
+
```bash
|
|
243
|
+
node ace sesame:key --write-env
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
This generates a JWK and adds (or replaces) `OIDC_JWK` in your `.env` file. Never commit the private key to your repository.
|
|
247
|
+
|
|
248
|
+
You can also output the raw JSON for piping to a secret manager or file:
|
|
249
|
+
|
|
250
|
+
```bash
|
|
251
|
+
node ace sesame:key --raw > jwk.json
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
Or run `node ace sesame:key` without flags to see the key with usage instructions.
|
|
255
|
+
|
|
256
|
+
### Configuration
|
|
257
|
+
|
|
258
|
+
Pass the JWK and a user provider to `defineConfig`. The `oidcProvider` uses the same `oauthUserProvider` helper you configure for the auth guard.
|
|
259
|
+
|
|
260
|
+
```ts title="config/sesame.ts"
|
|
261
|
+
import env from '#start/env'
|
|
262
|
+
import { defineConfig } from '@julr/sesame'
|
|
263
|
+
import { oauthUserProvider } from '@julr/sesame/guard'
|
|
264
|
+
|
|
265
|
+
const sesameConfig = defineConfig({
|
|
266
|
+
issuer: env.get('APP_URL'),
|
|
267
|
+
|
|
268
|
+
scopes: {
|
|
269
|
+
read: 'Read access',
|
|
270
|
+
write: 'Write access',
|
|
271
|
+
},
|
|
272
|
+
|
|
273
|
+
loginPage: '/login',
|
|
274
|
+
consentPage: '/oauth/consent',
|
|
275
|
+
|
|
276
|
+
// OIDC configuration
|
|
277
|
+
jwk: JSON.parse(env.get('OIDC_JWK')),
|
|
278
|
+
oidcProvider: oauthUserProvider({ model: () => import('#models/user') }),
|
|
279
|
+
idTokenTtl: '1h',
|
|
164
280
|
})
|
|
165
281
|
```
|
|
166
282
|
|
|
167
|
-
|
|
283
|
+
Both `jwk` and `oidcProvider` must be set for OIDC to be active. If either is missing, the server works as a pure OAuth 2.1 server and OIDC endpoints return 404.
|
|
168
284
|
|
|
169
|
-
|
|
285
|
+
### User Claims
|
|
170
286
|
|
|
171
|
-
|
|
287
|
+
When the `openid` scope is granted, Sésame calls `getOidcClaims()` on your User model to populate the `id_token` and `/userinfo` response with user-specific claims. If the method is not implemented, only protocol-level claims (`sub`, `iss`, `aud`, `exp`, `iat`) are included.
|
|
288
|
+
|
|
289
|
+
Implement the `OidcSubject` interface and use the `collectOidcClaims` helper for a type-safe, declarative mapping of scopes to claims:
|
|
290
|
+
|
|
291
|
+
```ts title="app/models/user.ts"
|
|
292
|
+
import { BaseModel, column } from '@adonisjs/lucid/orm'
|
|
293
|
+
import { collectOidcClaims } from '@julr/sesame/types'
|
|
294
|
+
import type { OidcSubject, Scope } from '@julr/sesame/types'
|
|
295
|
+
|
|
296
|
+
export default class User extends BaseModel implements OidcSubject {
|
|
297
|
+
@column({ isPrimary: true })
|
|
298
|
+
declare id: number
|
|
299
|
+
|
|
300
|
+
@column()
|
|
301
|
+
declare fullName: string
|
|
302
|
+
|
|
303
|
+
@column()
|
|
304
|
+
declare email: string
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Return OIDC claims based on the granted scopes.
|
|
308
|
+
* Protocol-managed claims (sub, iss, aud, exp, iat, nonce, at_hash)
|
|
309
|
+
* are filtered out automatically so you cannot accidentally override them.
|
|
310
|
+
*/
|
|
311
|
+
getOidcClaims(scopes: Scope[]) {
|
|
312
|
+
return collectOidcClaims(scopes, {
|
|
313
|
+
profile: { name: this.fullName },
|
|
314
|
+
email: { email: this.email },
|
|
315
|
+
})
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
### OIDC Scopes
|
|
321
|
+
|
|
322
|
+
Three scopes are OIDC-specific: `openid`, `profile`, and `email`. They are recognized by the server without needing to be declared in your `scopes` config.
|
|
323
|
+
|
|
324
|
+
- `openid` triggers `id_token` emission. The `profile` and `email` scopes are only valid when `openid` is also requested.
|
|
325
|
+
- If a client requests `openid` but OIDC is not configured, the authorization endpoint rejects the request with `invalid_scope`.
|
|
326
|
+
|
|
327
|
+
### How `id_token` Is Issued
|
|
328
|
+
|
|
329
|
+
When the `openid` scope is present in the authorization code or refresh token exchange, the token response includes an `id_token` field alongside `access_token` and `refresh_token`:
|
|
330
|
+
|
|
331
|
+
```json
|
|
332
|
+
{
|
|
333
|
+
"access_token": "oat_...",
|
|
334
|
+
"token_type": "Bearer",
|
|
335
|
+
"expires_in": 3600,
|
|
336
|
+
"refresh_token": "ort_...",
|
|
337
|
+
"id_token": "eyJhbGciOiJSUzI1NiIs..."
|
|
338
|
+
}
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
The `id_token` is a signed JWT containing `iss`, `sub`, `aud`, `iat`, `exp`, `at_hash`, and any claims returned by `getOidcClaims()`. When the authorization request included a `nonce` parameter, it is echoed in the `id_token` payload. On refresh token exchanges, the `nonce` is omitted per OIDC Core §12.2.
|
|
342
|
+
|
|
343
|
+
### UserInfo Endpoint
|
|
344
|
+
|
|
345
|
+
The `/userinfo` endpoint (GET and POST) returns claims about the authenticated user. It requires a valid access token with the `openid` scope. The token can be passed as a `Bearer` header or as an `access_token` body parameter.
|
|
346
|
+
|
|
347
|
+
```bash
|
|
348
|
+
curl -H "Authorization: Bearer oat_..." https://auth.example.com/oauth/userinfo
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
```json
|
|
352
|
+
{
|
|
353
|
+
"sub": "42",
|
|
354
|
+
"name": "Julien Ripouteau",
|
|
355
|
+
"email": "julien@example.com"
|
|
356
|
+
}
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
### JWKS Endpoint
|
|
360
|
+
|
|
361
|
+
The `/jwks` endpoint serves the public key(s) used to sign ID tokens. Relying parties use this to verify `id_token` signatures without needing the private key. The response includes a `Cache-Control` header (`public, max-age=900`) so clients can cache the key set.
|
|
362
|
+
|
|
363
|
+
## Client Credentials Grant
|
|
364
|
+
|
|
365
|
+
For machine-to-machine (M2M) authentication, enable the `client_credentials` grant. This allows a confidential client to send `POST /oauth/token` with `grant_type=client_credentials`, its credentials (via Basic auth or POST body), and the requested `scope`. No refresh token is issued.
|
|
366
|
+
|
|
367
|
+
```ts title="config/sesame.ts"
|
|
368
|
+
const sesameConfig = defineConfig({
|
|
369
|
+
// ...
|
|
370
|
+
grantTypes: ['authorization_code', 'refresh_token', 'client_credentials'],
|
|
371
|
+
clientCredentialsAccessTokenTtl: '2h',
|
|
372
|
+
})
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
User-centric scopes (`openid`, `profile`, `email`, `offline_access`) are rejected for client credentials since they are meaningless in an M2M context. The client must be associated with a user (`userId` on the client record) and must be confidential (not public).
|
|
376
|
+
|
|
377
|
+
## Dynamic Client Registration
|
|
378
|
+
|
|
379
|
+
Sésame supports RFC 7591 dynamic client registration. Clients send their metadata (`redirect_uris`, `client_name`, `grant_types`, `scope`, `token_endpoint_auth_method`) to `POST /oauth/register` and receive a `client_id` and `client_secret` in return. Set `token_endpoint_auth_method` to `"none"` for public clients (no secret issued). Requested scopes and grant types are validated against your server config.
|
|
380
|
+
|
|
381
|
+
```ts title="config/sesame.ts"
|
|
172
382
|
const sesameConfig = defineConfig({
|
|
173
383
|
// ...
|
|
174
384
|
allowDynamicRegistration: true,
|
|
175
|
-
allowPublicRegistration: true,
|
|
385
|
+
allowPublicRegistration: true, // allows unauthenticated registration
|
|
176
386
|
})
|
|
177
387
|
```
|
|
178
388
|
|
|
389
|
+
## Managing Clients
|
|
390
|
+
|
|
391
|
+
### Creating clients from the CLI
|
|
392
|
+
|
|
393
|
+
The `sesame:client` Ace command creates a new OAuth client interactively. It prompts for a name, redirect URIs, and client type, then outputs the generated credentials.
|
|
394
|
+
|
|
395
|
+
```bash
|
|
396
|
+
node ace sesame:client
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
You can also pass flags to skip the prompts:
|
|
400
|
+
|
|
401
|
+
```bash
|
|
402
|
+
node ace sesame:client --name "My App" --redirect-uris https://app.example.com/callback
|
|
403
|
+
node ace sesame:client --name "SPA" --public --redirect-uris https://spa.example.com/callback
|
|
404
|
+
node ace sesame:client --name "M2M Service" --grant-types client_credentials --user-id 42
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
The client secret is displayed once at creation time and cannot be retrieved later (it is stored as a SHA-256 hash).
|
|
408
|
+
|
|
409
|
+
### Programmatic client management
|
|
410
|
+
|
|
411
|
+
The `SesameManager` exposes methods for managing clients from your application code. This is useful for admin panels, seeding scripts, or any workflow where you need to create and manage clients without the CLI or dynamic registration.
|
|
412
|
+
|
|
413
|
+
```ts
|
|
414
|
+
import sesame from '@julr/sesame/services/main'
|
|
415
|
+
|
|
416
|
+
// Create a confidential client
|
|
417
|
+
const { client, clientSecret } = await sesame.createClient({
|
|
418
|
+
name: 'Partner App',
|
|
419
|
+
redirectUris: ['https://partner.example.com/callback'],
|
|
420
|
+
scopes: ['read', 'write'],
|
|
421
|
+
grantTypes: ['authorization_code', 'refresh_token'],
|
|
422
|
+
})
|
|
423
|
+
|
|
424
|
+
// Create a public client (no secret)
|
|
425
|
+
const { client: spa } = await sesame.createClient({
|
|
426
|
+
name: 'SPA',
|
|
427
|
+
redirectUris: ['https://spa.example.com/callback'],
|
|
428
|
+
isPublic: true,
|
|
429
|
+
})
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
`createClient` returns the client model and the raw secret. The secret is only available at creation time.
|
|
433
|
+
|
|
434
|
+
To find, list, update, or delete clients:
|
|
435
|
+
|
|
436
|
+
```ts
|
|
437
|
+
// Find by public client_id
|
|
438
|
+
const client = await sesame.findClient('a1b2c3...')
|
|
439
|
+
|
|
440
|
+
// List all clients (optionally filtered by owner)
|
|
441
|
+
const allClients = await sesame.listClients()
|
|
442
|
+
const userClients = await sesame.listClients({ userId: '42' })
|
|
443
|
+
|
|
444
|
+
// Update specific fields
|
|
445
|
+
await sesame.updateClient('a1b2c3...', {
|
|
446
|
+
name: 'New Name',
|
|
447
|
+
redirectUris: ['https://new.example.com/callback'],
|
|
448
|
+
isDisabled: true,
|
|
449
|
+
})
|
|
450
|
+
|
|
451
|
+
// Delete a client and all its tokens, codes, and consents
|
|
452
|
+
await sesame.deleteClient('a1b2c3...')
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
To rotate a confidential client's secret (e.g. after a suspected leak):
|
|
456
|
+
|
|
457
|
+
```ts
|
|
458
|
+
const newSecret = await sesame.rotateClientSecret('a1b2c3...')
|
|
459
|
+
// Returns the new raw secret, or null if the client is public or not found
|
|
460
|
+
```
|
|
461
|
+
|
|
462
|
+
## MCP Support
|
|
463
|
+
|
|
464
|
+
For MCP (Model Context Protocol) servers, you can register per-resource discovery endpoints following RFC 9728. This tells MCP clients which authorization server protects a given resource.
|
|
465
|
+
|
|
466
|
+
```ts title="start/routes.ts"
|
|
467
|
+
sesame.registerProtectedResource({
|
|
468
|
+
resource: '/api/mcp',
|
|
469
|
+
scopes: ['read:mcp'],
|
|
470
|
+
})
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
This creates a `/.well-known/oauth-protected-resource/api/mcp` endpoint. MCP clients that support the latest spec will discover this automatically.
|
|
474
|
+
|
|
475
|
+
MCP clients typically need to self-register, so you will want to enable dynamic client registration with public access (see the [Dynamic Client Registration](#dynamic-client-registration) section above).
|
|
476
|
+
|
|
477
|
+
## Events
|
|
478
|
+
|
|
479
|
+
The OAuth guard emits events during authentication that you can listen to for logging, analytics, or custom behavior.
|
|
480
|
+
|
|
481
|
+
| Event | When |
|
|
482
|
+
| ------------------------------------- | ---------------------------------------------------------------------- |
|
|
483
|
+
| `oauth_auth:authentication_attempted` | A bearer token has been received and authentication starts |
|
|
484
|
+
| `oauth_auth:authentication_succeeded` | The token is valid and the user has been resolved |
|
|
485
|
+
| `oauth_auth:authentication_failed` | The token is invalid, expired, revoked, or the user cannot be resolved |
|
|
486
|
+
|
|
487
|
+
```ts title="start/events.ts"
|
|
488
|
+
import emitter from '@adonisjs/core/services/emitter'
|
|
489
|
+
|
|
490
|
+
emitter.on('oauth_auth:authentication_failed', (event) => {
|
|
491
|
+
logger.warn({ guardName: event.guardName, err: event.error }, 'OAuth authentication failed')
|
|
492
|
+
})
|
|
493
|
+
```
|
|
494
|
+
|
|
495
|
+
## Testing
|
|
496
|
+
|
|
497
|
+
The OAuth guard implements `authenticateAsClient`, which integrates with Japa's `loginAs` helper. This automatically creates a test OAuth client and access token in the database, so your tests can make authenticated API requests without going through the full authorization flow.
|
|
498
|
+
|
|
499
|
+
```ts title="tests/functional/api.spec.ts"
|
|
500
|
+
import { test } from '@japa/runner'
|
|
501
|
+
import User from '#models/user'
|
|
502
|
+
|
|
503
|
+
test.group('API', () => {
|
|
504
|
+
test('returns user data for authenticated request', async ({ client }) => {
|
|
505
|
+
const user = await User.find(1)
|
|
506
|
+
|
|
507
|
+
const response = await client.get('/api/me').loginAs(user, 'oauth')
|
|
508
|
+
|
|
509
|
+
response.assertStatus(200)
|
|
510
|
+
response.assertBodyContains({ id: user.id })
|
|
511
|
+
})
|
|
512
|
+
})
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
The test client is created with `defaultScopes` from your config. The token is scoped to a `__test_client__` OAuth client that gets auto-created on first use.
|
|
516
|
+
|
|
179
517
|
## Token Cleanup
|
|
180
518
|
|
|
181
|
-
|
|
519
|
+
Expired and revoked tokens accumulate over time. Purge them with the Ace command:
|
|
182
520
|
|
|
183
521
|
```bash
|
|
184
522
|
node ace sesame:purge
|
|
@@ -187,22 +525,27 @@ node ace sesame:purge --expired-only
|
|
|
187
525
|
node ace sesame:purge --retention-hours=168
|
|
188
526
|
```
|
|
189
527
|
|
|
528
|
+
The `--retention-hours` flag (default: 168, i.e. 7 days) controls how long expired tokens are kept for audit purposes before deletion.
|
|
529
|
+
|
|
190
530
|
You can also call it programmatically:
|
|
191
531
|
|
|
192
532
|
```ts
|
|
193
533
|
import sesame from '@julr/sesame/services/main'
|
|
194
534
|
|
|
195
535
|
const result = await sesame.purgeTokens({ retentionHours: 168 })
|
|
536
|
+
// => { accessTokens: 42, refreshTokens: 12, authorizationCodes: 3, pendingRequests: 7 }
|
|
196
537
|
```
|
|
197
538
|
|
|
198
539
|
## Security
|
|
199
540
|
|
|
200
|
-
-
|
|
201
|
-
- PKCE with **S256** is
|
|
202
|
-
- Refresh tokens use **rotation
|
|
203
|
-
- **Replay detection**: if a revoked refresh token is
|
|
204
|
-
- Client secret verification uses **timing-safe comparison
|
|
205
|
-
-
|
|
541
|
+
- All tokens (access tokens, refresh tokens, authorization codes, client secrets) are stored as **SHA-256 hashes**. Raw values are never persisted in the database.
|
|
542
|
+
- PKCE with **S256** is mandatory for all clients (OAuth 2.1).
|
|
543
|
+
- Refresh tokens use **rotation**. The old token is revoked immediately on use.
|
|
544
|
+
- **Replay detection**: if a revoked refresh token is presented, all tokens for that client+user pair are revoked to mitigate stolen token reuse.
|
|
545
|
+
- Client secret verification uses **timing-safe comparison**.
|
|
546
|
+
- ID tokens are signed with **RS256** using the configured JWK. The JWKS endpoint only exposes public key components.
|
|
547
|
+
- Protocol-managed claims (`sub`, `iss`, `aud`, `exp`, `iat`, `nonce`, `at_hash`) cannot be overridden by `getOidcClaims()`.
|
|
548
|
+
- OAuth errors follow the standard JSON format with proper HTTP status codes and `WWW-Authenticate` headers.
|
|
206
549
|
|
|
207
550
|
## License
|
|
208
551
|
|