@julr/sesame 0.4.0 → 0.5.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/README.md +10 -11
- package/build/{authorize_controller-Bb54Bi_s.js → authorize_controller-BGzxPvYU.js} +2 -2
- package/build/{client_service-BD1CDQuC.js → client_service-C3rfXGk_.js} +4 -2
- package/build/commands/sesame_purge.js +1 -1
- package/build/{consent_controller-gMuS9nSx.js → consent_controller-BHoB9mip.js} +1 -1
- package/build/index.js +3 -2
- package/build/{introspect_controller-bKg1T2Xx.js → introspect_controller-un95fs4y.js} +2 -2
- package/build/{main-BJRXMGrZ.js → main-B3M6ihoS.js} +1 -1
- package/build/{metadata_controller-CGaKw3UM.js → metadata_controller-CJeZG93_.js} +1 -1
- package/build/providers/sesame_provider.js +1 -1
- package/build/{register_controller-CnzYR-iH.js → register_controller-Dch4ecyD.js} +2 -2
- package/build/{revoke_controller-dJtMG1zK.js → revoke_controller-DnPmzYMd.js} +2 -2
- package/build/services/main.js +1 -1
- package/build/{sesame_manager-BWqeaqn9.js → sesame_manager-BQIW2mqt.js} +1 -1
- package/build/{sesame_manager-BIHBwkqK.js → sesame_manager-C-eEFFHM.js} +11 -10
- package/build/src/grants/client_credentials_grant.d.ts +23 -0
- package/build/src/guard/main.js +1 -1
- package/build/src/sesame_manager.d.ts +1 -1
- package/build/src/types.d.ts +22 -2
- package/build/{token_controller-BijtWj5C.js → token_controller-hGDAYuBS.js} +40 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,6 +24,7 @@ node ace add @julr/sesame
|
|
|
24
24
|
```
|
|
25
25
|
|
|
26
26
|
This will:
|
|
27
|
+
|
|
27
28
|
- Publish the configuration file to `config/sesame.ts`
|
|
28
29
|
- Publish database migrations (6 tables)
|
|
29
30
|
- Register the service provider and commands
|
|
@@ -47,8 +48,8 @@ const sesameConfig = defineConfig({
|
|
|
47
48
|
issuer: env.get('APP_URL'),
|
|
48
49
|
|
|
49
50
|
scopes: {
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
read: 'Read access',
|
|
52
|
+
write: 'Write access',
|
|
52
53
|
},
|
|
53
54
|
|
|
54
55
|
defaultScopes: ['read'],
|
|
@@ -83,9 +84,11 @@ Register OAuth routes from your `start/routes.ts` file:
|
|
|
83
84
|
import sesame from '@julr/sesame/services/main'
|
|
84
85
|
|
|
85
86
|
// OAuth endpoints under /oauth
|
|
86
|
-
router
|
|
87
|
-
|
|
88
|
-
|
|
87
|
+
router
|
|
88
|
+
.group(() => {
|
|
89
|
+
sesame.registerRoutes()
|
|
90
|
+
})
|
|
91
|
+
.prefix('/oauth')
|
|
89
92
|
|
|
90
93
|
// Discovery endpoints at the root
|
|
91
94
|
sesame.registerWellKnownRoutes()
|
|
@@ -144,14 +147,10 @@ Two named middleware are available for checking scopes on authenticated requests
|
|
|
144
147
|
|
|
145
148
|
```ts
|
|
146
149
|
// Requires ALL listed scopes
|
|
147
|
-
router
|
|
148
|
-
.get('/admin', [AdminController])
|
|
149
|
-
.use(middleware.scopes({ scopes: ['admin', 'write'] }))
|
|
150
|
+
router.get('/admin', [AdminController]).use(middleware.scopes({ scopes: ['admin', 'write'] }))
|
|
150
151
|
|
|
151
152
|
// Requires AT LEAST ONE of the listed scopes
|
|
152
|
-
router
|
|
153
|
-
.get('/data', [DataController])
|
|
154
|
-
.use(middleware.anyScope({ scopes: ['read', 'write'] }))
|
|
153
|
+
router.get('/data', [DataController]).use(middleware.anyScope({ scopes: ['read', 'write'] }))
|
|
155
154
|
```
|
|
156
155
|
|
|
157
156
|
## MCP Support
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { i as OAuthAuthorizationCode, n as OAuthPendingAuthorizationRequest, r as OAuthConsent, t as SesameManager } from "./sesame_manager-
|
|
1
|
+
import { i as OAuthAuthorizationCode, n as OAuthPendingAuthorizationRequest, r as OAuthConsent, t as SesameManager } from "./sesame_manager-C-eEFFHM.js";
|
|
2
2
|
import "./decorate-BKZEjPRg.js";
|
|
3
3
|
import "./oauth_access_token-bsoM5KeU.js";
|
|
4
4
|
import { d as E_UNSUPPORTED_RESPONSE_TYPE, o as E_INVALID_REQUEST, r as E_INVALID_CLIENT } from "./oauth_error-CnJ3L8tf.js";
|
|
5
5
|
import { t as OAuthClient } from "./oauth_client-BIoY5jBR.js";
|
|
6
6
|
import { t as TokenService } from "./token_service-fhoA4slP.js";
|
|
7
|
-
import { t as ClientService } from "./client_service-
|
|
7
|
+
import { t as ClientService } from "./client_service-C3rfXGk_.js";
|
|
8
8
|
import { DateTime } from "luxon";
|
|
9
9
|
import string from "@adonisjs/core/helpers/string";
|
|
10
10
|
import vine from "@vinejs/vine";
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { o as BUILTIN_SCOPES } from "./sesame_manager-C-eEFFHM.js";
|
|
1
2
|
import { o as E_INVALID_REQUEST, r as E_INVALID_CLIENT, s as E_INVALID_SCOPE } from "./oauth_error-CnJ3L8tf.js";
|
|
2
3
|
import { t as OAuthClient } from "./oauth_client-BIoY5jBR.js";
|
|
3
4
|
import { createHash, randomBytes, timingSafeEqual } from "node:crypto";
|
|
@@ -37,9 +38,10 @@ var ClientService = class {
|
|
|
37
38
|
return client;
|
|
38
39
|
}
|
|
39
40
|
validateClientScopes(requestedScopes, clientScopes) {
|
|
40
|
-
|
|
41
|
+
const nonBuiltinScopes = requestedScopes.filter((s) => !BUILTIN_SCOPES.has(s));
|
|
42
|
+
if (clientScopes.length === 0 && nonBuiltinScopes.length > 0) throw new E_INVALID_SCOPE(`Scope not allowed: ${nonBuiltinScopes.join(", ")}`);
|
|
41
43
|
const allowedSet = new Set(clientScopes);
|
|
42
|
-
const invalid =
|
|
44
|
+
const invalid = nonBuiltinScopes.filter((s) => !allowedSet.has(s));
|
|
43
45
|
if (invalid.length > 0) throw new E_INVALID_SCOPE(`Scope not allowed: ${invalid.join(", ")}`);
|
|
44
46
|
}
|
|
45
47
|
hashSecret(secret) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as SesameManager } from "../sesame_manager-
|
|
1
|
+
import { t as SesameManager } from "../sesame_manager-C-eEFFHM.js";
|
|
2
2
|
import { t as __decorate } from "../decorate-BKZEjPRg.js";
|
|
3
3
|
import "../oauth_access_token-bsoM5KeU.js";
|
|
4
4
|
import { BaseCommand, flags } from "@adonisjs/core/ace";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as OAuthAuthorizationCode, n as OAuthPendingAuthorizationRequest, r as OAuthConsent, t as SesameManager } from "./sesame_manager-
|
|
1
|
+
import { i as OAuthAuthorizationCode, n as OAuthPendingAuthorizationRequest, r as OAuthConsent, t as SesameManager } from "./sesame_manager-C-eEFFHM.js";
|
|
2
2
|
import "./decorate-BKZEjPRg.js";
|
|
3
3
|
import "./oauth_access_token-bsoM5KeU.js";
|
|
4
4
|
import { a as E_INVALID_GRANT, o as E_INVALID_REQUEST, r as E_INVALID_CLIENT } from "./oauth_error-CnJ3L8tf.js";
|
package/build/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { configure } from "./configure.js";
|
|
2
|
-
import { a as OAuthRefreshToken, i as OAuthAuthorizationCode, r as OAuthConsent, t as SesameManager } from "./sesame_manager-
|
|
2
|
+
import { a as OAuthRefreshToken, i as OAuthAuthorizationCode, r as OAuthConsent, t as SesameManager } from "./sesame_manager-C-eEFFHM.js";
|
|
3
3
|
import "./decorate-BKZEjPRg.js";
|
|
4
4
|
import { t as OAuthAccessToken } from "./oauth_access_token-bsoM5KeU.js";
|
|
5
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
6
|
import { t as OAuthClient } from "./oauth_client-BIoY5jBR.js";
|
|
7
7
|
import "./token_service-fhoA4slP.js";
|
|
8
|
-
import { i as OAuthGuard, n as oauthUserProvider, r as OAuthLucidUserProvider, t as oauthGuard } from "./main-
|
|
8
|
+
import { i as OAuthGuard, n as oauthUserProvider, r as OAuthLucidUserProvider, t as oauthGuard } from "./main-B3M6ihoS.js";
|
|
9
9
|
function defineConfig(config) {
|
|
10
10
|
return {
|
|
11
11
|
issuer: config.issuer,
|
|
@@ -13,6 +13,7 @@ function defineConfig(config) {
|
|
|
13
13
|
defaultScopes: config.defaultScopes ?? [],
|
|
14
14
|
grantTypes: config.grantTypes ?? ["authorization_code", "refresh_token"],
|
|
15
15
|
accessTokenTtl: config.accessTokenTtl ?? "1h",
|
|
16
|
+
clientCredentialsAccessTokenTtl: config.clientCredentialsAccessTokenTtl ?? config.accessTokenTtl ?? "1h",
|
|
16
17
|
refreshTokenTtl: config.refreshTokenTtl ?? "30d",
|
|
17
18
|
authorizationCodeTtl: config.authorizationCodeTtl ?? "10m",
|
|
18
19
|
authorizationRequestTtl: config.authorizationRequestTtl ?? config.authorizationCodeTtl ?? "10m",
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { a as OAuthRefreshToken, t as SesameManager } from "./sesame_manager-
|
|
1
|
+
import { a as OAuthRefreshToken, t as SesameManager } from "./sesame_manager-C-eEFFHM.js";
|
|
2
2
|
import "./decorate-BKZEjPRg.js";
|
|
3
3
|
import { t as OAuthAccessToken } from "./oauth_access_token-bsoM5KeU.js";
|
|
4
4
|
import "./oauth_error-CnJ3L8tf.js";
|
|
5
5
|
import "./oauth_client-BIoY5jBR.js";
|
|
6
6
|
import { t as TokenService } from "./token_service-fhoA4slP.js";
|
|
7
|
-
import { t as ClientService } from "./client_service-
|
|
7
|
+
import { t as ClientService } from "./client_service-C3rfXGk_.js";
|
|
8
8
|
const INACTIVE = { active: false };
|
|
9
9
|
var IntrospectController = class {
|
|
10
10
|
async handle(ctx) {
|
|
@@ -150,7 +150,7 @@ var OAuthLucidUserProvider = class {
|
|
|
150
150
|
function oauthGuard(config) {
|
|
151
151
|
return { async resolver(name, app) {
|
|
152
152
|
const emitter = await app.container.make("emitter");
|
|
153
|
-
const { SesameManager } = await import("./sesame_manager-
|
|
153
|
+
const { SesameManager } = await import("./sesame_manager-BQIW2mqt.js");
|
|
154
154
|
const manager = await app.container.make(SesameManager);
|
|
155
155
|
return (ctx) => new OAuthGuard(name, ctx, emitter, config.provider, manager, config.resource);
|
|
156
156
|
} };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as SesameManager } from "./sesame_manager-
|
|
1
|
+
import { t as SesameManager } from "./sesame_manager-C-eEFFHM.js";
|
|
2
2
|
import "./decorate-BKZEjPRg.js";
|
|
3
3
|
import "./oauth_access_token-bsoM5KeU.js";
|
|
4
4
|
import { l as E_SERVER_ERROR } from "./oauth_error-CnJ3L8tf.js";
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { t as SesameManager } from "./sesame_manager-
|
|
1
|
+
import { t as SesameManager } from "./sesame_manager-C-eEFFHM.js";
|
|
2
2
|
import "./decorate-BKZEjPRg.js";
|
|
3
3
|
import "./oauth_access_token-bsoM5KeU.js";
|
|
4
4
|
import { i as E_INVALID_CLIENT_METADATA, o as E_INVALID_REQUEST, s as E_INVALID_SCOPE, t as E_ACCESS_DENIED } from "./oauth_error-CnJ3L8tf.js";
|
|
5
5
|
import { t as OAuthClient } from "./oauth_client-BIoY5jBR.js";
|
|
6
|
-
import { t as ClientService } from "./client_service-
|
|
6
|
+
import { t as ClientService } from "./client_service-C3rfXGk_.js";
|
|
7
7
|
import vine from "@vinejs/vine";
|
|
8
8
|
const DANGEROUS_SCHEMES = [
|
|
9
9
|
"javascript:",
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { a as OAuthRefreshToken, t as SesameManager } from "./sesame_manager-
|
|
1
|
+
import { a as OAuthRefreshToken, t as SesameManager } from "./sesame_manager-C-eEFFHM.js";
|
|
2
2
|
import "./decorate-BKZEjPRg.js";
|
|
3
3
|
import { t as OAuthAccessToken } from "./oauth_access_token-bsoM5KeU.js";
|
|
4
4
|
import "./oauth_error-CnJ3L8tf.js";
|
|
5
5
|
import "./oauth_client-BIoY5jBR.js";
|
|
6
6
|
import { t as TokenService } from "./token_service-fhoA4slP.js";
|
|
7
|
-
import { t as ClientService } from "./client_service-
|
|
7
|
+
import { t as ClientService } from "./client_service-C3rfXGk_.js";
|
|
8
8
|
import { DateTime } from "luxon";
|
|
9
9
|
var RevokeController = class {
|
|
10
10
|
async handle(ctx) {
|
package/build/services/main.js
CHANGED
|
@@ -2,14 +2,15 @@ import { n as json, t as __decorate } from "./decorate-BKZEjPRg.js";
|
|
|
2
2
|
import { t as OAuthAccessToken } from "./oauth_access_token-bsoM5KeU.js";
|
|
3
3
|
import { DateTime } from "luxon";
|
|
4
4
|
import { BaseModel, column } from "@adonisjs/lucid/orm";
|
|
5
|
+
const BUILTIN_SCOPES = new Set(["offline_access"]);
|
|
5
6
|
const controllers = {
|
|
6
|
-
token: () => import("./token_controller-
|
|
7
|
-
authorize: () => import("./authorize_controller-
|
|
8
|
-
consent: () => import("./consent_controller-
|
|
9
|
-
introspect: () => import("./introspect_controller-
|
|
10
|
-
revoke: () => import("./revoke_controller-
|
|
11
|
-
register: () => import("./register_controller-
|
|
12
|
-
metadata: () => import("./metadata_controller-
|
|
7
|
+
token: () => import("./token_controller-hGDAYuBS.js"),
|
|
8
|
+
authorize: () => import("./authorize_controller-BGzxPvYU.js"),
|
|
9
|
+
consent: () => import("./consent_controller-BHoB9mip.js"),
|
|
10
|
+
introspect: () => import("./introspect_controller-un95fs4y.js"),
|
|
11
|
+
revoke: () => import("./revoke_controller-DnPmzYMd.js"),
|
|
12
|
+
register: () => import("./register_controller-Dch4ecyD.js"),
|
|
13
|
+
metadata: () => import("./metadata_controller-CJeZG93_.js"),
|
|
13
14
|
clientInfo: () => import("./client_info_controller-BucHGx4u.js")
|
|
14
15
|
};
|
|
15
16
|
function registerOAuthRoutes(router) {
|
|
@@ -99,8 +100,8 @@ var SesameManager = class {
|
|
|
99
100
|
return scope in this.#config.scopes;
|
|
100
101
|
}
|
|
101
102
|
validateScopes(scopes) {
|
|
102
|
-
if (Object.keys(this.#config.scopes).length === 0) return scopes;
|
|
103
|
-
return scopes.filter((s) => !this.hasScope(s));
|
|
103
|
+
if (Object.keys(this.#config.scopes).length === 0) return scopes.filter((s) => !BUILTIN_SCOPES.has(s));
|
|
104
|
+
return scopes.filter((s) => !BUILTIN_SCOPES.has(s) && !this.hasScope(s));
|
|
104
105
|
}
|
|
105
106
|
isGrantTypeEnabled(grantType) {
|
|
106
107
|
return this.#config.grantTypes.includes(grantType);
|
|
@@ -163,4 +164,4 @@ var SesameManager = class {
|
|
|
163
164
|
return result.then((r) => Array.isArray(r) ? Number(r[0] ?? 0) : Number(r));
|
|
164
165
|
}
|
|
165
166
|
};
|
|
166
|
-
export { OAuthRefreshToken as a, OAuthAuthorizationCode as i, OAuthPendingAuthorizationRequest as n, OAuthConsent as r, SesameManager as t };
|
|
167
|
+
export { OAuthRefreshToken as a, OAuthAuthorizationCode as i, OAuthPendingAuthorizationRequest as n, BUILTIN_SCOPES as o, OAuthConsent as r, SesameManager as t };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { HttpContext } from '@adonisjs/core/http';
|
|
2
|
+
import type { SesameManager } from '../sesame_manager.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Handle the Client Credentials Grant (RFC 6749 §4.4).
|
|
5
|
+
*
|
|
6
|
+
* Issues an access token directly to the client for
|
|
7
|
+
* machine-to-machine (M2M) communication. The client
|
|
8
|
+
* authenticates with its own credentials and receives an
|
|
9
|
+
* access token without an interactive user step.
|
|
10
|
+
*
|
|
11
|
+
* No refresh token is issued (per spec and convention).
|
|
12
|
+
*
|
|
13
|
+
* Built-in OIDC scopes (e.g. `offline_access`) are rejected
|
|
14
|
+
* since they are user-centric and meaningless in an M2M context.
|
|
15
|
+
*
|
|
16
|
+
* @see https://datatracker.ietf.org/doc/html/rfc6749#section-4.4
|
|
17
|
+
*/
|
|
18
|
+
export declare function handleClientCredentialsGrant(ctx: HttpContext, manager: SesameManager): Promise<{
|
|
19
|
+
access_token: string;
|
|
20
|
+
token_type: string;
|
|
21
|
+
expires_in: number;
|
|
22
|
+
scope: string;
|
|
23
|
+
}>;
|
package/build/src/guard/main.js
CHANGED
|
@@ -2,5 +2,5 @@ import "../../decorate-BKZEjPRg.js";
|
|
|
2
2
|
import "../../oauth_access_token-bsoM5KeU.js";
|
|
3
3
|
import "../../oauth_client-BIoY5jBR.js";
|
|
4
4
|
import "../../token_service-fhoA4slP.js";
|
|
5
|
-
import { i as OAuthGuard, n as oauthUserProvider, r as OAuthLucidUserProvider, t as oauthGuard } from "../../main-
|
|
5
|
+
import { i as OAuthGuard, n as oauthUserProvider, r as OAuthLucidUserProvider, t as oauthGuard } from "../../main-B3M6ihoS.js";
|
|
6
6
|
export { OAuthGuard, OAuthLucidUserProvider, oauthGuard, oauthUserProvider };
|
package/build/src/types.d.ts
CHANGED
|
@@ -27,15 +27,29 @@ export type InferScopes<T extends {
|
|
|
27
27
|
[K in keyof T['scopes'] & string]: true;
|
|
28
28
|
};
|
|
29
29
|
/**
|
|
30
|
-
*
|
|
30
|
+
* Standard OAuth/OIDC scopes that are always valid regardless
|
|
31
|
+
* of server or client scope configuration.
|
|
32
|
+
*
|
|
33
|
+
* - `offline_access`: signals that the client needs a refresh token
|
|
34
|
+
* (OIDC Core §11, OAuth 2.1). Without this built-in treatment,
|
|
35
|
+
* MCP clients that don't explicitly configure scopes would never
|
|
36
|
+
* receive a refresh token and would expire after the access token TTL.
|
|
37
|
+
*
|
|
38
|
+
* @see https://openid.net/specs/openid-connect-core-1_0.html#OfflineAccess
|
|
39
|
+
*/
|
|
40
|
+
export declare const BUILTIN_SCOPES: Set<string>;
|
|
41
|
+
/**
|
|
42
|
+
* Supported OAuth 2.1 grant types.
|
|
31
43
|
*
|
|
32
44
|
* - `authorization_code`: RFC 6749 §4.1 — Authorization Code Grant
|
|
33
45
|
* - `refresh_token`: RFC 6749 §6 — Refreshing an Access Token
|
|
46
|
+
* - `client_credentials`: RFC 6749 §4.4 — Client Credentials Grant (M2M)
|
|
34
47
|
*
|
|
35
48
|
* @see https://datatracker.ietf.org/doc/html/rfc6749#section-4.1
|
|
49
|
+
* @see https://datatracker.ietf.org/doc/html/rfc6749#section-4.4
|
|
36
50
|
* @see https://datatracker.ietf.org/doc/html/rfc6749#section-6
|
|
37
51
|
*/
|
|
38
|
-
export type GrantType = 'authorization_code' | 'refresh_token';
|
|
52
|
+
export type GrantType = 'authorization_code' | 'refresh_token' | 'client_credentials';
|
|
39
53
|
/**
|
|
40
54
|
* User-facing configuration interface for Sésame.
|
|
41
55
|
*
|
|
@@ -77,6 +91,11 @@ export interface SesameConfig {
|
|
|
77
91
|
* Defaults to '30d'.
|
|
78
92
|
*/
|
|
79
93
|
refreshTokenTtl?: string;
|
|
94
|
+
/**
|
|
95
|
+
* Access token TTL for the client_credentials grant (M2M).
|
|
96
|
+
* Defaults to `accessTokenTtl`.
|
|
97
|
+
*/
|
|
98
|
+
clientCredentialsAccessTokenTtl?: string;
|
|
80
99
|
/**
|
|
81
100
|
* Authorization code TTL as a string duration.
|
|
82
101
|
* Defaults to '10m'.
|
|
@@ -125,6 +144,7 @@ export interface ResolvedSesameConfig {
|
|
|
125
144
|
defaultScopes: string[];
|
|
126
145
|
grantTypes: GrantType[];
|
|
127
146
|
accessTokenTtl: string;
|
|
147
|
+
clientCredentialsAccessTokenTtl: string;
|
|
128
148
|
refreshTokenTtl: string;
|
|
129
149
|
authorizationCodeTtl: string;
|
|
130
150
|
authorizationRequestTtl: string;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { a as OAuthRefreshToken, i as OAuthAuthorizationCode, t as SesameManager } from "./sesame_manager-
|
|
1
|
+
import { a as OAuthRefreshToken, i as OAuthAuthorizationCode, o as BUILTIN_SCOPES, t as SesameManager } from "./sesame_manager-C-eEFFHM.js";
|
|
2
2
|
import "./decorate-BKZEjPRg.js";
|
|
3
3
|
import { t as OAuthAccessToken } from "./oauth_access_token-bsoM5KeU.js";
|
|
4
4
|
import { a as E_INVALID_GRANT, o as E_INVALID_REQUEST, r as E_INVALID_CLIENT, s as E_INVALID_SCOPE, u as E_UNSUPPORTED_GRANT_TYPE } from "./oauth_error-CnJ3L8tf.js";
|
|
5
5
|
import "./oauth_client-BIoY5jBR.js";
|
|
6
6
|
import { t as TokenService } from "./token_service-fhoA4slP.js";
|
|
7
|
-
import { t as ClientService } from "./client_service-
|
|
7
|
+
import { t as ClientService } from "./client_service-C3rfXGk_.js";
|
|
8
8
|
import { DateTime } from "luxon";
|
|
9
9
|
import { createHash } from "node:crypto";
|
|
10
10
|
import string from "@adonisjs/core/helpers/string";
|
|
@@ -135,9 +135,46 @@ async function handleRefreshTokenGrant(ctx, manager) {
|
|
|
135
135
|
refresh_token: newRefreshTokenRaw
|
|
136
136
|
};
|
|
137
137
|
}
|
|
138
|
+
async function handleClientCredentialsGrant(ctx, manager) {
|
|
139
|
+
const tokenService = new TokenService(manager);
|
|
140
|
+
const clientService = new ClientService();
|
|
141
|
+
const body = ctx.request.body();
|
|
142
|
+
const client = await clientService.authenticateClient({
|
|
143
|
+
authorizationHeader: ctx.request.header("authorization"),
|
|
144
|
+
bodyClientId: body.client_id,
|
|
145
|
+
bodyClientSecret: body.client_secret
|
|
146
|
+
});
|
|
147
|
+
if (client.isPublic) throw new E_INVALID_CLIENT("Public clients cannot use the client_credentials grant");
|
|
148
|
+
if (!client.grantTypes.includes("client_credentials")) throw new E_INVALID_CLIENT("Client is not allowed to use the client_credentials grant");
|
|
149
|
+
const requestedScopes = body.scope ? body.scope.split(" ") : client.scopes.filter((scope) => !BUILTIN_SCOPES.has(scope));
|
|
150
|
+
const builtinRequested = requestedScopes.filter((s) => BUILTIN_SCOPES.has(s));
|
|
151
|
+
if (builtinRequested.length > 0) throw new E_INVALID_SCOPE(`Scopes not allowed for client_credentials: ${builtinRequested.join(", ")}`);
|
|
152
|
+
const invalidScopes = manager.validateScopes(requestedScopes);
|
|
153
|
+
if (invalidScopes.length > 0) throw new E_INVALID_SCOPE(`Invalid scopes: ${invalidScopes.join(", ")}`);
|
|
154
|
+
clientService.validateClientScopes(requestedScopes, client.scopes);
|
|
155
|
+
if (!client.userId) throw new E_INVALID_CLIENT("Client must be associated with a user to use the client_credentials grant");
|
|
156
|
+
const ttlSeconds = string.seconds.parse(manager.config.clientCredentialsAccessTokenTtl);
|
|
157
|
+
const { raw: accessTokenRaw, hash: tokenHash } = tokenService.createAccessToken();
|
|
158
|
+
const expiresAt = new Date(Date.now() + ttlSeconds * 1e3);
|
|
159
|
+
await OAuthAccessToken.create({
|
|
160
|
+
id: crypto.randomUUID(),
|
|
161
|
+
tokenHash,
|
|
162
|
+
clientId: client.clientId,
|
|
163
|
+
userId: client.userId,
|
|
164
|
+
scopes: requestedScopes,
|
|
165
|
+
expiresAt: DateTime.fromJSDate(expiresAt)
|
|
166
|
+
});
|
|
167
|
+
return {
|
|
168
|
+
access_token: accessTokenRaw,
|
|
169
|
+
token_type: "Bearer",
|
|
170
|
+
expires_in: ttlSeconds,
|
|
171
|
+
scope: requestedScopes.join(" ")
|
|
172
|
+
};
|
|
173
|
+
}
|
|
138
174
|
const grantHandlers = {
|
|
139
175
|
authorization_code: handleAuthorizationCodeGrant,
|
|
140
|
-
refresh_token: handleRefreshTokenGrant
|
|
176
|
+
refresh_token: handleRefreshTokenGrant,
|
|
177
|
+
client_credentials: handleClientCredentialsGrant
|
|
141
178
|
};
|
|
142
179
|
var TokenController = class TokenController {
|
|
143
180
|
static validator = vine.create({ grant_type: vine.string() });
|