@julr/sesame 0.1.0 → 0.2.1
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/README.md +167 -86
- package/build/{authorize_controller-CUdEDNEi.js → authorize_controller-BNWhlPZQ.js} +21 -20
- package/build/{client_info_controller-DeIVcW8B.js → client_info_controller-BucHGx4u.js} +3 -3
- package/build/{client_service-B9fD3ZGe.js → client_service-BqPSlaTS.js} +1 -1
- package/build/commands/sesame_purge.js +3 -3
- package/build/configure.js +11 -2
- package/build/{consent_controller-DFfx7qVs.js → consent_controller-COvvkpHM.js} +28 -30
- package/build/index.d.ts +1 -3
- package/build/index.js +9 -10
- package/build/{introspect_controller-BzwfaUUE.js → introspect_controller-JjAAXFIV.js} +7 -7
- package/build/metadata_controller-BzCjyqUG.js +73 -0
- package/build/{oauth_access_token-BpG8sq-c.js → oauth_access_token-bsoM5KeU.js} +1 -1
- package/build/{oauth_client-eh0e5ql-.js → oauth_client-BIoY5jBR.js} +1 -1
- package/build/{oauth_error-BQPqV-MV.js → oauth_error-CnJ3L8tf.js} +17 -1
- package/build/providers/sesame_provider.d.ts +0 -8
- package/build/providers/sesame_provider.js +3 -8
- package/build/{register_controller-BA7uQAgt.js → register_controller-DOlN9wNl.js} +6 -6
- package/build/{revoke_controller-CNIgNKH3.js → revoke_controller-B281b8ZO.js} +7 -7
- package/build/{sesame_manager-B4tO2PLO.js → sesame_manager-CFq4VEIZ.js} +75 -15
- package/build/src/controllers/metadata_controller.d.ts +1 -0
- package/build/src/define_config.d.ts +6 -1
- package/build/src/guard/guard.d.ts +4 -3
- package/build/src/guard/main.js +6 -6
- package/build/src/middleware/any_scope_middleware.d.ts +15 -0
- package/build/src/middleware/any_scope_middleware.js +18 -0
- package/build/src/middleware/scope_middleware.d.ts +15 -0
- package/build/src/middleware/scope_middleware.js +18 -0
- package/build/src/models/oauth_pending_authorization_request.d.ts +29 -0
- package/build/src/oauth_error.d.ts +32 -0
- package/build/src/routes.d.ts +21 -20
- package/build/src/sesame_manager.d.ts +39 -8
- package/build/src/types.d.ts +34 -0
- package/build/stubs/config/sesame.stub +6 -1
- package/build/stubs/migrations/create_oauth_pending_authorization_requests_table.stub +32 -0
- package/build/{token_controller-C9wh813f.js → token_controller-ll9sjcvn.js} +12 -11
- package/build/{token_service-Czz9v5GI.js → token_service-fhoA4slP.js} +2 -1
- package/build/{user_provider-B3rXEUT3.js → user_provider-DXAOfv8-.js} +3 -3
- package/package.json +7 -2
- package/build/main-kn40V-hF.js +0 -2
- package/build/metadata_controller-BSRRElQX.js +0 -51
- package/build/rolldown-runtime-BASaM9lw.js +0 -12
- package/build/routes-D6QCu0Pz.js +0 -43
- /package/build/{decorate-2_8Ex77k.js → decorate-BKZEjPRg.js} +0 -0
package/README.md
CHANGED
|
@@ -1,130 +1,211 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Sésame
|
|
2
2
|
|
|
3
|
-
>
|
|
4
|
-
> This starter kit targets **AdonisJS v7**
|
|
3
|
+
> OAuth 2.1 + OIDC server for AdonisJS
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
Sésame is an AdonisJS package that turns your application into a full-featured OAuth 2.1 authorization server. It implements the core OAuth 2.1 specification along with OIDC discovery, token introspection, dynamic client registration, and MCP (Model Context Protocol) support.
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
## Features
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
- **Authorization Code Grant** with PKCE (S256)
|
|
10
|
+
- **Refresh Token Rotation** with replay detection
|
|
11
|
+
- **Token Introspection** (RFC 7662) and **Revocation** (RFC 7009)
|
|
12
|
+
- **Dynamic Client Registration** (RFC 7591)
|
|
13
|
+
- **OIDC Discovery** (`/.well-known/openid-configuration`)
|
|
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
|
|
11
19
|
|
|
12
|
-
|
|
13
|
-
```sh
|
|
14
|
-
npx giget@latest gh:adonisjs/pkg-starter-kit
|
|
15
|
-
```
|
|
16
|
-
- Install dependencies.
|
|
17
|
-
- Update the `package.json` file and define the `name`, `description`, `keywords`, and `author` properties.
|
|
18
|
-
- The repo is configured with an MIT license. Feel free to change that if you are not publishing under the MIT license.
|
|
20
|
+
## Installation
|
|
19
21
|
|
|
20
|
-
|
|
22
|
+
```bash
|
|
23
|
+
node ace add @julr/sesame
|
|
24
|
+
```
|
|
21
25
|
|
|
22
|
-
|
|
26
|
+
This will:
|
|
27
|
+
- Publish the configuration file to `config/sesame.ts`
|
|
28
|
+
- Publish database migrations (6 tables)
|
|
29
|
+
- Register the service provider and commands
|
|
23
30
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
├── stubs
|
|
29
|
-
├── configure.ts
|
|
30
|
-
├── index.ts
|
|
31
|
-
├── LICENSE.md
|
|
32
|
-
├── package.json
|
|
33
|
-
├── README.md
|
|
34
|
-
├── tsconfig.json
|
|
35
|
-
├── tsnode.esm.js
|
|
31
|
+
Then run the migrations:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
node ace migration:run
|
|
36
35
|
```
|
|
37
36
|
|
|
38
|
-
|
|
39
|
-
- The `index.ts` file is the main entry point of the package.
|
|
40
|
-
- The `tsnode.esm.js` file runs TypeScript code using TS-Node + SWC. Please read the code comment in this file to learn more.
|
|
41
|
-
- The `bin` directory contains the entry point file to run Japa tests.
|
|
42
|
-
- Learn more about [the `providers` directory](./providers/README.md).
|
|
43
|
-
- Learn more about [the `src` directory](./src/README.md).
|
|
44
|
-
- Learn more about [the `stubs` directory](./stubs/README.md).
|
|
37
|
+
## Configuration
|
|
45
38
|
|
|
46
|
-
|
|
39
|
+
The configuration file lives at `config/sesame.ts`:
|
|
47
40
|
|
|
48
|
-
|
|
41
|
+
```ts
|
|
42
|
+
import env from '#start/env'
|
|
43
|
+
import { defineConfig } from '@julr/sesame'
|
|
44
|
+
import type { InferScopes } from '@julr/sesame/types'
|
|
49
45
|
|
|
50
|
-
|
|
46
|
+
const sesameConfig = defineConfig({
|
|
47
|
+
issuer: env.get('APP_URL'),
|
|
51
48
|
|
|
52
|
-
|
|
49
|
+
scopes: {
|
|
50
|
+
'read': 'Read access',
|
|
51
|
+
'write': 'Write access',
|
|
52
|
+
},
|
|
53
53
|
|
|
54
|
-
|
|
54
|
+
defaultScopes: ['read'],
|
|
55
55
|
|
|
56
|
-
|
|
56
|
+
grantTypes: ['authorization_code', 'refresh_token'],
|
|
57
57
|
|
|
58
|
-
|
|
58
|
+
accessTokenTtl: '1h',
|
|
59
|
+
refreshTokenTtl: '30d',
|
|
60
|
+
authorizationCodeTtl: '10m',
|
|
59
61
|
|
|
60
|
-
|
|
62
|
+
loginPage: '/login',
|
|
63
|
+
consentPage: '/oauth/consent',
|
|
61
64
|
|
|
62
|
-
|
|
65
|
+
allowDynamicRegistration: false,
|
|
66
|
+
allowPublicRegistration: false,
|
|
67
|
+
})
|
|
63
68
|
|
|
64
|
-
|
|
69
|
+
export default sesameConfig
|
|
65
70
|
|
|
66
|
-
|
|
67
|
-
{
|
|
68
|
-
"files": [
|
|
69
|
-
"build/src",
|
|
70
|
-
"build/providers",
|
|
71
|
-
"build/stubs",
|
|
72
|
-
"build/index.d.ts",
|
|
73
|
-
"build/index.js",
|
|
74
|
-
"build/configure.d.ts",
|
|
75
|
-
"build/configure.js"
|
|
76
|
-
]
|
|
71
|
+
declare module '@julr/sesame/types' {
|
|
72
|
+
interface SesameScopes extends InferScopes<typeof sesameConfig> {}
|
|
77
73
|
}
|
|
78
74
|
```
|
|
79
75
|
|
|
80
|
-
|
|
76
|
+
The `SesameScopes` augmentation gives you type-safe scope names throughout your application.
|
|
77
|
+
|
|
78
|
+
## Routes
|
|
79
|
+
|
|
80
|
+
Register OAuth routes from your `start/routes.ts` file:
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { SesameManager } from '@julr/sesame'
|
|
84
|
+
|
|
85
|
+
const sesame = await app.container.make(SesameManager)
|
|
86
|
+
|
|
87
|
+
// OAuth endpoints under /oauth
|
|
88
|
+
router.group(() => {
|
|
89
|
+
sesame.registerRoutes(router)
|
|
90
|
+
}).prefix('/oauth')
|
|
91
|
+
|
|
92
|
+
// Discovery endpoints at the root
|
|
93
|
+
sesame.registerWellKnownRoutes(router)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
This registers the following endpoints:
|
|
97
|
+
|
|
98
|
+
| Method | Path | Description |
|
|
99
|
+
| ------ | ----------------------------------------- | -------------------------------------- |
|
|
100
|
+
| `POST` | `/oauth/token` | Token endpoint |
|
|
101
|
+
| `GET` | `/oauth/authorize` | Authorization endpoint |
|
|
102
|
+
| `POST` | `/oauth/consent` | Consent submission |
|
|
103
|
+
| `POST` | `/oauth/introspect` | Token introspection (RFC 7662) |
|
|
104
|
+
| `POST` | `/oauth/revoke` | Token revocation (RFC 7009) |
|
|
105
|
+
| `POST` | `/oauth/register` | Dynamic client registration (RFC 7591) |
|
|
106
|
+
| `GET` | `/oauth/client-info` | Public client info |
|
|
107
|
+
| `GET` | `/.well-known/oauth-authorization-server` | Server metadata (RFC 8414) |
|
|
108
|
+
| `GET` | `/.well-known/openid-configuration` | OIDC discovery |
|
|
109
|
+
|
|
110
|
+
## Authentication Guard
|
|
111
|
+
|
|
112
|
+
Sésame provides an OAuth guard for `@adonisjs/auth`. Configure it in `config/auth.ts`:
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
import { oauthGuard, oauthUserProvider } from '@julr/sesame/guard'
|
|
116
|
+
import User from '#models/user'
|
|
117
|
+
|
|
118
|
+
const authConfig = defineConfig({
|
|
119
|
+
default: 'web',
|
|
120
|
+
guards: {
|
|
121
|
+
// ...your other guards
|
|
122
|
+
oauth: oauthGuard({
|
|
123
|
+
provider: oauthUserProvider({ model: () => import('#models/user') }),
|
|
124
|
+
}),
|
|
125
|
+
},
|
|
126
|
+
})
|
|
127
|
+
```
|
|
81
128
|
|
|
82
|
-
|
|
129
|
+
Then use it in your controllers:
|
|
83
130
|
|
|
84
|
-
|
|
131
|
+
```ts
|
|
132
|
+
export default class ApiController {
|
|
133
|
+
async index({ auth }: HttpContext) {
|
|
134
|
+
const guard = auth.use('oauth')
|
|
135
|
+
await guard.authenticate()
|
|
85
136
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
"exports": {
|
|
89
|
-
".": "./build/index.js",
|
|
90
|
-
"./types": "./build/src/types.js"
|
|
137
|
+
const user = auth.user!
|
|
138
|
+
const scopes = guard.scopes
|
|
91
139
|
}
|
|
92
140
|
}
|
|
93
141
|
```
|
|
94
142
|
|
|
95
|
-
|
|
96
|
-
|
|
143
|
+
## Scope Middleware
|
|
144
|
+
|
|
145
|
+
Two named middleware are available for checking scopes on authenticated requests:
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
// Requires ALL listed scopes
|
|
149
|
+
router
|
|
150
|
+
.get('/admin', [AdminController])
|
|
151
|
+
.use(middleware.scopes({ scopes: ['admin', 'write'] }))
|
|
97
152
|
|
|
98
|
-
|
|
153
|
+
// Requires AT LEAST ONE of the listed scopes
|
|
154
|
+
router
|
|
155
|
+
.get('/data', [DataController])
|
|
156
|
+
.use(middleware.anyScope({ scopes: ['read', 'write'] }))
|
|
157
|
+
```
|
|
99
158
|
|
|
100
|
-
##
|
|
159
|
+
## MCP Support
|
|
101
160
|
|
|
102
|
-
|
|
161
|
+
For MCP (Model Context Protocol) servers, register per-resource discovery:
|
|
103
162
|
|
|
104
|
-
|
|
105
|
-
|
|
163
|
+
```ts
|
|
164
|
+
sesame.registerProtectedResource(router, {
|
|
165
|
+
resource: '/api/mcp',
|
|
166
|
+
scopes: ['read:mcp'],
|
|
167
|
+
})
|
|
168
|
+
```
|
|
106
169
|
|
|
107
|
-
|
|
170
|
+
This creates a `/.well-known/oauth-protected-resource/api/mcp` endpoint following RFC 9728.
|
|
108
171
|
|
|
109
|
-
|
|
172
|
+
You can also enable public client registration for MCP clients:
|
|
110
173
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
174
|
+
```ts
|
|
175
|
+
const sesameConfig = defineConfig({
|
|
176
|
+
// ...
|
|
177
|
+
allowDynamicRegistration: true,
|
|
178
|
+
allowPublicRegistration: true,
|
|
179
|
+
})
|
|
180
|
+
```
|
|
115
181
|
|
|
116
|
-
|
|
182
|
+
## Token Cleanup
|
|
117
183
|
|
|
118
|
-
|
|
184
|
+
Purge expired and revoked tokens with the Ace command:
|
|
185
|
+
|
|
186
|
+
```bash
|
|
187
|
+
node ace sesame:purge
|
|
188
|
+
node ace sesame:purge --revoked-only
|
|
189
|
+
node ace sesame:purge --expired-only
|
|
190
|
+
node ace sesame:purge --retention-hours=168
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
You can also call it programmatically:
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
const sesame = await app.container.make(SesameManager)
|
|
197
|
+
const result = await sesame.purgeTokens({ retentionHours: 168 })
|
|
198
|
+
```
|
|
119
199
|
|
|
120
|
-
|
|
121
|
-
using our [shared config](https://github.com/adonisjs/tooling-config/tree/main/packages).
|
|
122
|
-
ESLint configuration is stored within the `eslint.config.js` file.
|
|
123
|
-
Prettier configuration is stored within the `package.json` file.
|
|
124
|
-
Feel free to change the configuration, use custom plugins, or remove both tools altogether.
|
|
200
|
+
## Security
|
|
125
201
|
|
|
126
|
-
|
|
202
|
+
- Tokens (access, refresh, authorization codes, client secrets) are stored as **SHA-256 hashes** — raw values are never persisted
|
|
203
|
+
- PKCE with **S256** is required for public clients
|
|
204
|
+
- Refresh tokens use **rotation** — the old token is revoked on each use
|
|
205
|
+
- **Replay detection**: if a revoked refresh token is reused, all tokens for that client+user pair are revoked
|
|
206
|
+
- Client secret verification uses **timing-safe comparison**
|
|
207
|
+
- OAuth errors follow the standard JSON format with proper HTTP status codes and `WWW-Authenticate` headers
|
|
127
208
|
|
|
128
|
-
|
|
209
|
+
## License
|
|
129
210
|
|
|
130
|
-
|
|
211
|
+
MIT
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import "./
|
|
2
|
-
import "./
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import { t as OAuthClient } from "./oauth_client-
|
|
6
|
-
import { t as TokenService } from "./token_service-
|
|
7
|
-
import { t as ClientService } from "./client_service-
|
|
1
|
+
import { a as OAuthAuthorizationCode, i as OAuthConsent, r as OAuthPendingAuthorizationRequest, t as SesameManager } from "./sesame_manager-CFq4VEIZ.js";
|
|
2
|
+
import "./decorate-BKZEjPRg.js";
|
|
3
|
+
import "./oauth_access_token-bsoM5KeU.js";
|
|
4
|
+
import { d as E_UNSUPPORTED_RESPONSE_TYPE, o as E_INVALID_REQUEST, r as E_INVALID_CLIENT } from "./oauth_error-CnJ3L8tf.js";
|
|
5
|
+
import { t as OAuthClient } from "./oauth_client-BIoY5jBR.js";
|
|
6
|
+
import { t as TokenService } from "./token_service-fhoA4slP.js";
|
|
7
|
+
import { t as ClientService } from "./client_service-BqPSlaTS.js";
|
|
8
8
|
import { DateTime } from "luxon";
|
|
9
|
+
import string from "@adonisjs/core/helpers/string";
|
|
9
10
|
import vine from "@vinejs/vine";
|
|
10
11
|
var AuthorizeController = class AuthorizeController {
|
|
11
12
|
static validator = vine.create({
|
|
@@ -17,11 +18,6 @@ var AuthorizeController = class AuthorizeController {
|
|
|
17
18
|
code_challenge: vine.string().optional(),
|
|
18
19
|
code_challenge_method: vine.string().optional()
|
|
19
20
|
});
|
|
20
|
-
#getAuthorizationSession(ctx) {
|
|
21
|
-
const session = ctx.session;
|
|
22
|
-
if (!session || typeof session.put !== "function") throw new E_INVALID_REQUEST("Session middleware is required for the browser authorization flow");
|
|
23
|
-
return session;
|
|
24
|
-
}
|
|
25
21
|
#redirectWithError(ctx, manager, redirectUri, error, description, state) {
|
|
26
22
|
const url = new URL(redirectUri);
|
|
27
23
|
url.searchParams.set("error", error);
|
|
@@ -30,17 +26,21 @@ var AuthorizeController = class AuthorizeController {
|
|
|
30
26
|
url.searchParams.set("iss", manager.config.issuer);
|
|
31
27
|
return ctx.response.redirect().toPath(url.toString());
|
|
32
28
|
}
|
|
33
|
-
#buildAuthorizationRequestParams(
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
29
|
+
async #buildAuthorizationRequestParams(manager, options) {
|
|
30
|
+
const tokenService = new TokenService(manager);
|
|
31
|
+
const rawRequestToken = tokenService.generateOpaqueToken();
|
|
32
|
+
const ttl = string.seconds.parse(manager.config.authorizationRequestTtl);
|
|
33
|
+
await OAuthPendingAuthorizationRequest.create({
|
|
34
|
+
id: crypto.randomUUID(),
|
|
35
|
+
token: tokenService.hashToken(rawRequestToken),
|
|
36
|
+
userId: options.userId,
|
|
38
37
|
clientId: options.clientId,
|
|
39
38
|
redirectUri: options.redirectUri,
|
|
40
39
|
scopes: options.scopes,
|
|
41
40
|
state: options.state ?? null,
|
|
42
41
|
codeChallenge: options.codeChallenge ?? null,
|
|
43
|
-
codeChallengeMethod: options.codeChallengeMethod ?? null
|
|
42
|
+
codeChallengeMethod: options.codeChallengeMethod ?? null,
|
|
43
|
+
expiresAt: DateTime.now().plus({ seconds: ttl })
|
|
44
44
|
});
|
|
45
45
|
const params = new URLSearchParams();
|
|
46
46
|
params.set("auth_token", rawRequestToken);
|
|
@@ -60,7 +60,7 @@ var AuthorizeController = class AuthorizeController {
|
|
|
60
60
|
const tokenService = new TokenService(manager);
|
|
61
61
|
const raw = tokenService.generateOpaqueToken();
|
|
62
62
|
const hashed = tokenService.hashToken(raw);
|
|
63
|
-
const ttl =
|
|
63
|
+
const ttl = string.seconds.parse(manager.config.authorizationCodeTtl);
|
|
64
64
|
await OAuthAuthorizationCode.create({
|
|
65
65
|
id: crypto.randomUUID(),
|
|
66
66
|
code: hashed,
|
|
@@ -120,7 +120,8 @@ var AuthorizeController = class AuthorizeController {
|
|
|
120
120
|
state: query.state
|
|
121
121
|
});
|
|
122
122
|
}
|
|
123
|
-
const params = this.#buildAuthorizationRequestParams(
|
|
123
|
+
const params = await this.#buildAuthorizationRequestParams(manager, {
|
|
124
|
+
userId: String(user.id),
|
|
124
125
|
clientId: client.clientId,
|
|
125
126
|
redirectUri: query.redirect_uri,
|
|
126
127
|
scopes: requestedScopes,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import "./decorate-
|
|
2
|
-
import {
|
|
3
|
-
import { t as OAuthClient } from "./oauth_client-
|
|
1
|
+
import "./decorate-BKZEjPRg.js";
|
|
2
|
+
import { o as E_INVALID_REQUEST, r as E_INVALID_CLIENT } from "./oauth_error-CnJ3L8tf.js";
|
|
3
|
+
import { t as OAuthClient } from "./oauth_client-BIoY5jBR.js";
|
|
4
4
|
import vine from "@vinejs/vine";
|
|
5
5
|
var ClientInfoController = class ClientInfoController {
|
|
6
6
|
static validator = vine.create({ client_id: vine.string() });
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { t as
|
|
2
|
-
import "../
|
|
3
|
-
import
|
|
1
|
+
import { t as SesameManager } from "../sesame_manager-CFq4VEIZ.js";
|
|
2
|
+
import { t as __decorate } from "../decorate-BKZEjPRg.js";
|
|
3
|
+
import "../oauth_access_token-bsoM5KeU.js";
|
|
4
4
|
import { BaseCommand, flags } from "@adonisjs/core/ace";
|
|
5
5
|
var SesamePurge = class extends BaseCommand {
|
|
6
6
|
static commandName = "sesame:purge";
|
package/build/configure.js
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { join } from "node:path";
|
|
2
2
|
async function configure(command) {
|
|
3
3
|
const codemods = await command.createCodemods();
|
|
4
|
+
const stubsRoot = join(import.meta.url, "./stubs");
|
|
4
5
|
await codemods.makeUsingStub(stubsRoot, "config/sesame.stub", {});
|
|
5
6
|
for (const stub of [
|
|
6
7
|
"migrations/create_oauth_clients_table.stub",
|
|
7
8
|
"migrations/create_oauth_authorization_codes_table.stub",
|
|
8
9
|
"migrations/create_oauth_access_tokens_table.stub",
|
|
9
10
|
"migrations/create_oauth_refresh_tokens_table.stub",
|
|
10
|
-
"migrations/create_oauth_consents_table.stub"
|
|
11
|
+
"migrations/create_oauth_consents_table.stub",
|
|
12
|
+
"migrations/create_oauth_pending_authorization_requests_table.stub"
|
|
11
13
|
]) await codemods.makeUsingStub(stubsRoot, stub, {});
|
|
12
14
|
await codemods.updateRcFile((rcFile) => {
|
|
13
15
|
rcFile.addProvider("@julr/sesame/sesame_provider").addCommand("@julr/sesame/commands");
|
|
14
16
|
});
|
|
17
|
+
await codemods.registerMiddleware("named", [{
|
|
18
|
+
name: "scopes",
|
|
19
|
+
path: "@julr/sesame/scope_middleware"
|
|
20
|
+
}, {
|
|
21
|
+
name: "anyScope",
|
|
22
|
+
path: "@julr/sesame/any_scope_middleware"
|
|
23
|
+
}]);
|
|
15
24
|
}
|
|
16
25
|
export { configure };
|
|
@@ -1,23 +1,26 @@
|
|
|
1
|
-
import "./
|
|
2
|
-
import "./
|
|
3
|
-
import
|
|
4
|
-
import { a as
|
|
5
|
-
import { t as OAuthClient } from "./oauth_client-
|
|
6
|
-
import { t as TokenService } from "./token_service-
|
|
1
|
+
import { a as OAuthAuthorizationCode, i as OAuthConsent, r as OAuthPendingAuthorizationRequest, t as SesameManager } from "./sesame_manager-CFq4VEIZ.js";
|
|
2
|
+
import "./decorate-BKZEjPRg.js";
|
|
3
|
+
import "./oauth_access_token-bsoM5KeU.js";
|
|
4
|
+
import { a as E_INVALID_GRANT, o as E_INVALID_REQUEST, r as E_INVALID_CLIENT } from "./oauth_error-CnJ3L8tf.js";
|
|
5
|
+
import { t as OAuthClient } from "./oauth_client-BIoY5jBR.js";
|
|
6
|
+
import { t as TokenService } from "./token_service-fhoA4slP.js";
|
|
7
7
|
import { DateTime } from "luxon";
|
|
8
|
+
import string from "@adonisjs/core/helpers/string";
|
|
8
9
|
import vine from "@vinejs/vine";
|
|
9
10
|
var ConsentController = class ConsentController {
|
|
10
11
|
static validator = vine.create({ auth_token: vine.string() });
|
|
11
|
-
#
|
|
12
|
-
const
|
|
13
|
-
if (!
|
|
14
|
-
|
|
12
|
+
async #consumePendingRequest(hashedToken, userId) {
|
|
13
|
+
const row = await OAuthPendingAuthorizationRequest.query().where("token", hashedToken).where("userId", userId).where("expiresAt", ">", DateTime.now().toSQL()).first();
|
|
14
|
+
if (!row) return null;
|
|
15
|
+
const deleted = await OAuthPendingAuthorizationRequest.query().where("id", row.id).delete();
|
|
16
|
+
if ((Array.isArray(deleted) ? Number(deleted[0]) : Number(deleted)) === 0) return null;
|
|
17
|
+
return row;
|
|
15
18
|
}
|
|
16
19
|
async #issueAuthorizationCode(ctx, manager, options) {
|
|
17
20
|
const tokenService = new TokenService(manager);
|
|
18
21
|
const raw = tokenService.generateOpaqueToken();
|
|
19
22
|
const hashed = tokenService.hashToken(raw);
|
|
20
|
-
const ttl =
|
|
23
|
+
const ttl = string.seconds.parse(manager.config.authorizationCodeTtl);
|
|
21
24
|
await OAuthAuthorizationCode.create({
|
|
22
25
|
id: crypto.randomUUID(),
|
|
23
26
|
code: hashed,
|
|
@@ -37,50 +40,45 @@ var ConsentController = class ConsentController {
|
|
|
37
40
|
}
|
|
38
41
|
async handle(ctx) {
|
|
39
42
|
const manager = await ctx.containerResolver.make(SesameManager);
|
|
40
|
-
const session = this.#getAuthorizationSession(ctx);
|
|
41
43
|
await ctx.auth.check();
|
|
42
44
|
const user = ctx.auth.user;
|
|
43
45
|
if (!user) throw new E_INVALID_REQUEST("User must be authenticated");
|
|
44
46
|
const [error, body] = await ConsentController.validator.tryValidate(ctx.request.body());
|
|
45
47
|
if (error) throw new E_INVALID_REQUEST("Missing required parameter: auth_token");
|
|
46
48
|
const accept = ctx.request.body().accept;
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
if (!
|
|
50
|
-
|
|
51
|
-
throw new E_INVALID_GRANT("Authorization request token mismatch");
|
|
52
|
-
}
|
|
53
|
-
if (!authorizationRequest) throw new E_INVALID_GRANT("Authorization request not found");
|
|
54
|
-
const client = await OAuthClient.query().where("clientId", authorizationRequest.clientId).first();
|
|
49
|
+
const hashedToken = new TokenService(manager).hashToken(body.auth_token);
|
|
50
|
+
const pendingRequest = await this.#consumePendingRequest(hashedToken, String(user.id));
|
|
51
|
+
if (!pendingRequest) throw new E_INVALID_GRANT("Authorization request not found or expired");
|
|
52
|
+
const client = await OAuthClient.query().where("clientId", pendingRequest.clientId).first();
|
|
55
53
|
if (!client) throw new E_INVALID_CLIENT("Client not found");
|
|
56
54
|
if (client.isDisabled) throw new E_INVALID_CLIENT("Client is disabled");
|
|
57
|
-
if (!client.redirectUris.includes(
|
|
55
|
+
if (!client.redirectUris.includes(pendingRequest.redirectUri)) throw new E_INVALID_REQUEST("Invalid redirect_uri");
|
|
58
56
|
if (!accept) {
|
|
59
|
-
const url = new URL(
|
|
57
|
+
const url = new URL(pendingRequest.redirectUri);
|
|
60
58
|
url.searchParams.set("error", "access_denied");
|
|
61
59
|
url.searchParams.set("error_description", "The user denied the authorization request");
|
|
62
|
-
if (
|
|
60
|
+
if (pendingRequest.state) url.searchParams.set("state", pendingRequest.state);
|
|
63
61
|
url.searchParams.set("iss", manager.config.issuer);
|
|
64
62
|
return ctx.response.redirect().toPath(url.toString());
|
|
65
63
|
}
|
|
66
64
|
const existingConsent = await OAuthConsent.query().where("clientId", client.clientId).where("userId", String(user.id)).first();
|
|
67
65
|
if (existingConsent) {
|
|
68
|
-
existingConsent.scopes = [...new Set([...existingConsent.scopes, ...
|
|
66
|
+
existingConsent.scopes = [...new Set([...existingConsent.scopes, ...pendingRequest.scopes])];
|
|
69
67
|
await existingConsent.save();
|
|
70
68
|
} else await OAuthConsent.create({
|
|
71
69
|
id: crypto.randomUUID(),
|
|
72
70
|
clientId: client.clientId,
|
|
73
71
|
userId: String(user.id),
|
|
74
|
-
scopes:
|
|
72
|
+
scopes: pendingRequest.scopes
|
|
75
73
|
});
|
|
76
74
|
return this.#issueAuthorizationCode(ctx, manager, {
|
|
77
75
|
client,
|
|
78
76
|
userId: String(user.id),
|
|
79
|
-
scopes:
|
|
80
|
-
redirectUri:
|
|
81
|
-
codeChallenge:
|
|
82
|
-
codeChallengeMethod:
|
|
83
|
-
state:
|
|
77
|
+
scopes: pendingRequest.scopes,
|
|
78
|
+
redirectUri: pendingRequest.redirectUri,
|
|
79
|
+
codeChallenge: pendingRequest.codeChallenge ?? void 0,
|
|
80
|
+
codeChallengeMethod: pendingRequest.codeChallengeMethod ?? void 0,
|
|
81
|
+
state: pendingRequest.state ?? void 0
|
|
84
82
|
});
|
|
85
83
|
}
|
|
86
84
|
};
|
package/build/index.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
export { configure } from './configure.ts';
|
|
2
|
-
export { stubsRoot } from './stubs/main.ts';
|
|
3
2
|
export { defineConfig } from './src/define_config.ts';
|
|
4
3
|
export { SesameManager } from './src/sesame_manager.ts';
|
|
5
|
-
export { OAuthError, E_INVALID_REQUEST, E_INVALID_CLIENT, E_INVALID_GRANT, E_INVALID_SCOPE, E_INVALID_TOKEN, E_UNSUPPORTED_GRANT_TYPE, E_UNSUPPORTED_RESPONSE_TYPE, E_ACCESS_DENIED, E_INVALID_CLIENT_METADATA, E_SERVER_ERROR, } from './src/oauth_error.ts';
|
|
4
|
+
export { OAuthError, E_INVALID_REQUEST, E_INVALID_CLIENT, E_INVALID_GRANT, E_INVALID_SCOPE, E_INVALID_TOKEN, E_UNSUPPORTED_GRANT_TYPE, E_UNSUPPORTED_RESPONSE_TYPE, E_ACCESS_DENIED, E_INVALID_CLIENT_METADATA, E_SERVER_ERROR, E_INSUFFICIENT_SCOPE, } from './src/oauth_error.ts';
|
|
6
5
|
export { OAuthClient } from './src/models/oauth_client.ts';
|
|
7
6
|
export { OAuthAccessToken } from './src/models/oauth_access_token.ts';
|
|
8
7
|
export { OAuthRefreshToken } from './src/models/oauth_refresh_token.ts';
|
|
@@ -11,4 +10,3 @@ export { OAuthConsent } from './src/models/oauth_consent.ts';
|
|
|
11
10
|
export { OAuthGuard } from './src/guard/guard.ts';
|
|
12
11
|
export { OAuthLucidUserProvider } from './src/guard/user_provider.ts';
|
|
13
12
|
export { oauthGuard, oauthUserProvider } from './src/guard/main.ts';
|
|
14
|
-
export { registerRoutes, registerProtectedResource } from './src/routes.ts';
|
package/build/index.js
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
import { t as
|
|
1
|
+
import { a as OAuthAuthorizationCode, i as OAuthConsent, o as OAuthRefreshToken, t as SesameManager } from "./sesame_manager-CFq4VEIZ.js";
|
|
2
2
|
import { configure } from "./configure.js";
|
|
3
|
-
import "./decorate-
|
|
4
|
-
import { t as OAuthAccessToken } from "./oauth_access_token-
|
|
5
|
-
import { a as
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
8
|
-
import "./
|
|
9
|
-
import { n as OAuthGuard, t as OAuthLucidUserProvider } from "./user_provider-B3rXEUT3.js";
|
|
3
|
+
import "./decorate-BKZEjPRg.js";
|
|
4
|
+
import { t as OAuthAccessToken } from "./oauth_access_token-bsoM5KeU.js";
|
|
5
|
+
import { a as E_INVALID_GRANT, c as E_INVALID_TOKEN, d as E_UNSUPPORTED_RESPONSE_TYPE, f as OAuthError, i as E_INVALID_CLIENT_METADATA, l as E_SERVER_ERROR, n as E_INSUFFICIENT_SCOPE, o as E_INVALID_REQUEST, r as E_INVALID_CLIENT, s as E_INVALID_SCOPE, t as E_ACCESS_DENIED, u as E_UNSUPPORTED_GRANT_TYPE } from "./oauth_error-CnJ3L8tf.js";
|
|
6
|
+
import { t as OAuthClient } from "./oauth_client-BIoY5jBR.js";
|
|
7
|
+
import "./token_service-fhoA4slP.js";
|
|
8
|
+
import { n as OAuthGuard, t as OAuthLucidUserProvider } from "./user_provider-DXAOfv8-.js";
|
|
10
9
|
import { oauthGuard, oauthUserProvider } from "./src/guard/main.js";
|
|
11
|
-
import { n as registerRoutes, t as registerProtectedResource } from "./routes-D6QCu0Pz.js";
|
|
12
10
|
function defineConfig(config) {
|
|
13
11
|
return {
|
|
14
12
|
issuer: config.issuer,
|
|
@@ -18,10 +16,11 @@ function defineConfig(config) {
|
|
|
18
16
|
accessTokenTtl: config.accessTokenTtl ?? "1h",
|
|
19
17
|
refreshTokenTtl: config.refreshTokenTtl ?? "30d",
|
|
20
18
|
authorizationCodeTtl: config.authorizationCodeTtl ?? "10m",
|
|
19
|
+
authorizationRequestTtl: config.authorizationRequestTtl ?? config.authorizationCodeTtl ?? "10m",
|
|
21
20
|
loginPage: config.loginPage,
|
|
22
21
|
consentPage: config.consentPage,
|
|
23
22
|
allowDynamicRegistration: config.allowDynamicRegistration ?? false,
|
|
24
23
|
allowPublicRegistration: config.allowPublicRegistration ?? false
|
|
25
24
|
};
|
|
26
25
|
}
|
|
27
|
-
export { E_ACCESS_DENIED, E_INVALID_CLIENT, E_INVALID_CLIENT_METADATA, E_INVALID_GRANT, E_INVALID_REQUEST, E_INVALID_SCOPE, E_INVALID_TOKEN, E_SERVER_ERROR, E_UNSUPPORTED_GRANT_TYPE, E_UNSUPPORTED_RESPONSE_TYPE, OAuthAccessToken, OAuthAuthorizationCode, OAuthClient, OAuthConsent, OAuthError, OAuthGuard, OAuthLucidUserProvider, OAuthRefreshToken, SesameManager, configure, defineConfig, oauthGuard, oauthUserProvider
|
|
26
|
+
export { E_ACCESS_DENIED, E_INSUFFICIENT_SCOPE, E_INVALID_CLIENT, E_INVALID_CLIENT_METADATA, E_INVALID_GRANT, E_INVALID_REQUEST, E_INVALID_SCOPE, E_INVALID_TOKEN, E_SERVER_ERROR, E_UNSUPPORTED_GRANT_TYPE, E_UNSUPPORTED_RESPONSE_TYPE, OAuthAccessToken, OAuthAuthorizationCode, OAuthClient, OAuthConsent, OAuthError, OAuthGuard, OAuthLucidUserProvider, OAuthRefreshToken, SesameManager, configure, defineConfig, oauthGuard, oauthUserProvider };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import "./
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { t as OAuthClient } from "./oauth_client-
|
|
6
|
-
import { t as TokenService } from "./token_service-
|
|
7
|
-
import { t as ClientService } from "./client_service-
|
|
1
|
+
import { o as OAuthRefreshToken, t as SesameManager } from "./sesame_manager-CFq4VEIZ.js";
|
|
2
|
+
import "./decorate-BKZEjPRg.js";
|
|
3
|
+
import { t as OAuthAccessToken } from "./oauth_access_token-bsoM5KeU.js";
|
|
4
|
+
import { r as E_INVALID_CLIENT } from "./oauth_error-CnJ3L8tf.js";
|
|
5
|
+
import { t as OAuthClient } from "./oauth_client-BIoY5jBR.js";
|
|
6
|
+
import { t as TokenService } from "./token_service-fhoA4slP.js";
|
|
7
|
+
import { t as ClientService } from "./client_service-BqPSlaTS.js";
|
|
8
8
|
const INACTIVE = { active: false };
|
|
9
9
|
var IntrospectController = class {
|
|
10
10
|
async handle(ctx) {
|