authlet 0.0.0-stage → 0.1.0-beta.2
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 +201 -0
- package/README.md +179 -2
- package/dist/authlet.d.ts +3 -0
- package/dist/authlet.js +78 -0
- package/dist/context.d.ts +30 -0
- package/dist/context.js +10 -0
- package/dist/endpoints/authorize.d.ts +12 -0
- package/dist/endpoints/authorize.js +62 -0
- package/dist/endpoints/consent.d.ts +5 -0
- package/dist/endpoints/consent.js +96 -0
- package/dist/endpoints/metadata.d.ts +11 -0
- package/dist/endpoints/metadata.js +28 -0
- package/dist/endpoints/register.d.ts +9 -0
- package/dist/endpoints/register.js +51 -0
- package/dist/endpoints/token.d.ts +5 -0
- package/dist/endpoints/token.js +85 -0
- package/dist/http.d.ts +21 -0
- package/dist/http.js +57 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/pkce.d.ts +4 -0
- package/dist/pkce.js +15 -0
- package/dist/redirect-uri.d.ts +18 -0
- package/dist/redirect-uri.js +68 -0
- package/dist/seal.d.ts +63 -0
- package/dist/seal.js +77 -0
- package/dist/types.d.ts +151 -0
- package/dist/types.js +1 -0
- package/dist/urls.d.ts +32 -0
- package/dist/urls.js +73 -0
- package/package.json +73 -4
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/** A value fixed at setup, or derived from each request when the public URL is only known then. */
|
|
2
|
+
export type Resolvable<T> = T | ((req: Request) => T);
|
|
3
|
+
/**
|
|
4
|
+
* The signed-in browser user who can allow a client. Apps pass their own user type as `TUser`;
|
|
5
|
+
* authlet only reads `id`, which must identify the user across the whole app (for several user
|
|
6
|
+
* collections, prefix it, e.g. `users:42`).
|
|
7
|
+
*/
|
|
8
|
+
export interface AuthletUser {
|
|
9
|
+
id: string;
|
|
10
|
+
}
|
|
11
|
+
/** The client being authorized, as known from its registration or metadata document. */
|
|
12
|
+
export interface AuthletClient {
|
|
13
|
+
/** The `client_id`: a sealed registration (RFC 7591) or an https metadata document URL. */
|
|
14
|
+
id: string;
|
|
15
|
+
name: string;
|
|
16
|
+
/** The redirect URI used for this authorization, one of those the client registered. */
|
|
17
|
+
redirectUri: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* What `issue` creates for a new authorization: only the app's refresh token. It is sealed into
|
|
21
|
+
* the code, and redeeming the code rotates it into the client's first token pair, so no access
|
|
22
|
+
* token ever travels through the browser.
|
|
23
|
+
*/
|
|
24
|
+
export interface IssuedGrant {
|
|
25
|
+
refreshToken: string;
|
|
26
|
+
}
|
|
27
|
+
/** Tokens created by the app. authlet passes them through and never inspects them. */
|
|
28
|
+
export interface IssuedTokens {
|
|
29
|
+
accessToken: string;
|
|
30
|
+
expiresIn?: number;
|
|
31
|
+
/** Required, so the next refresh rotates it. Wrapped in a sealed, client-bound value before it reaches the client. */
|
|
32
|
+
refreshToken: string;
|
|
33
|
+
}
|
|
34
|
+
export interface IssueTokensArgs<TGrant, TUser extends AuthletUser> {
|
|
35
|
+
user: TUser;
|
|
36
|
+
client: AuthletClient;
|
|
37
|
+
/** The app data passed to `decide`, such as the capabilities the user picked on the consent screen. */
|
|
38
|
+
grant: TGrant;
|
|
39
|
+
/** The protected resource (RFC 8707) the tokens are for. */
|
|
40
|
+
resource: string;
|
|
41
|
+
}
|
|
42
|
+
export interface RefreshTokensArgs {
|
|
43
|
+
refreshToken: string;
|
|
44
|
+
client: Pick<AuthletClient, "id">;
|
|
45
|
+
/**
|
|
46
|
+
* `"code"` when redeeming an authorization code: the token must never have been used, so the
|
|
47
|
+
* code works once, and a second redemption must be refused (and should revoke the token family).
|
|
48
|
+
* `"refresh"` on a refresh grant, where the app may allow its own reuse leeway.
|
|
49
|
+
*/
|
|
50
|
+
kind: "code" | "refresh";
|
|
51
|
+
}
|
|
52
|
+
/** Token storage and minting, owned by the app. */
|
|
53
|
+
export interface TokenHooks<TGrant, TUser extends AuthletUser> {
|
|
54
|
+
/**
|
|
55
|
+
* Called when the user allows a client: stores the grant and returns a refresh token for it,
|
|
56
|
+
* which is sealed into the authorization code. `null` refuses the user (e.g. lost access since
|
|
57
|
+
* the consent page was shown), who is sent to `consentPage?error=forbidden`. Give that refresh
|
|
58
|
+
* token a short lifetime until its first `kind: "code"` rotation, since a code that is never
|
|
59
|
+
* redeemed leaves it unused.
|
|
60
|
+
*/
|
|
61
|
+
issue: (args: IssueTokensArgs<TGrant, TUser>) => Promise<IssuedGrant | null>;
|
|
62
|
+
/**
|
|
63
|
+
* Rotates a refresh token into a new token pair, invalidating the old refresh token and any
|
|
64
|
+
* access token issued with it. `null` refuses it. Redeeming a code goes through here too.
|
|
65
|
+
*/
|
|
66
|
+
refresh: (args: RefreshTokensArgs) => Promise<IssuedTokens | null>;
|
|
67
|
+
}
|
|
68
|
+
/** Why the authorization request can't be shown to the user. Never sent to the client's redirect URI. */
|
|
69
|
+
export type AuthorizeError = "invalid_link" | "expired" | "forbidden";
|
|
70
|
+
export interface AuthletOptions<TGrant = unknown, TUser extends AuthletUser = AuthletUser> {
|
|
71
|
+
/**
|
|
72
|
+
* The protected resource, the MCP endpoint URL, in canonical form: absolute https (http only
|
|
73
|
+
* to loopback), no credentials, query, fragment or trailing slash, lowercase host, no default
|
|
74
|
+
* port. Clients compare it exactly, so any other form is refused. Fixed
|
|
75
|
+
* strings are checked at setup, functions on each request. A function must not derive it
|
|
76
|
+
* from an untrusted `Host` header.
|
|
77
|
+
*/
|
|
78
|
+
resource: Resolvable<string>;
|
|
79
|
+
/**
|
|
80
|
+
* The authorization server URL, under the same rules. Every endpoint and metadata document is
|
|
81
|
+
* served below it, so a base path such as `/api/mcp/oauth` needs no route at the site root.
|
|
82
|
+
* Default `${resource}/oauth`. Requests are matched on their path only (to work behind
|
|
83
|
+
* TLS-terminating proxies), so an issuer at a host root (`https://auth.example.com`) claims
|
|
84
|
+
* `/authorize`, `/token` and `/register`: only pass `handle` requests for that host then.
|
|
85
|
+
* Proxies must keep the public path; one that strips a prefix breaks matching.
|
|
86
|
+
*/
|
|
87
|
+
issuer?: Resolvable<string>;
|
|
88
|
+
/**
|
|
89
|
+
* The user signed in to this browser. Requests carrying an `Authorization` header are never
|
|
90
|
+
* passed here, so a token can't approve a new client for itself.
|
|
91
|
+
*/
|
|
92
|
+
getUser: (req: Request) => Promise<TUser | null>;
|
|
93
|
+
/**
|
|
94
|
+
* Where to send a signed-out user. `returnTo` is the path and query of the authorization
|
|
95
|
+
* request to come back to; it is relative, so it stays correct behind a proxy. A relative
|
|
96
|
+
* result is sent as a relative `Location`.
|
|
97
|
+
*/
|
|
98
|
+
loginUrl: (returnTo: string, req: Request) => string;
|
|
99
|
+
/**
|
|
100
|
+
* The app's consent page. authlet sends the browser there with `?consent=<sealed>`, which the
|
|
101
|
+
* page reads with `readConsent` and submits to `decide`, or with `?error=<AuthorizeError>`.
|
|
102
|
+
* A relative URL stays relative in the `Location` header, so a proxy can't leak its own host.
|
|
103
|
+
*/
|
|
104
|
+
consentPage: Resolvable<string>;
|
|
105
|
+
/** For the built-in AES-GCM sealer: at least 32 characters, or an AES-GCM `CryptoKey`. */
|
|
106
|
+
secret: string | CryptoKey;
|
|
107
|
+
tokens: TokenHooks<TGrant, TUser>;
|
|
108
|
+
}
|
|
109
|
+
/** A pending authorization, as shown on the consent page. */
|
|
110
|
+
export interface Consent<TUser extends AuthletUser = AuthletUser> {
|
|
111
|
+
client: AuthletClient;
|
|
112
|
+
user: TUser;
|
|
113
|
+
resource: string;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The user's answer on the consent page. `consent` is the sealed consent the page was opened
|
|
117
|
+
* with; `grant` is what the user allowed, passed to `tokens.issue`.
|
|
118
|
+
*/
|
|
119
|
+
export type DecideArgs<TGrant> = {
|
|
120
|
+
consent: string;
|
|
121
|
+
allow: true;
|
|
122
|
+
grant: TGrant;
|
|
123
|
+
} | {
|
|
124
|
+
consent: string;
|
|
125
|
+
allow: false;
|
|
126
|
+
};
|
|
127
|
+
export interface Authlet<TGrant = unknown, TUser extends AuthletUser = AuthletUser> {
|
|
128
|
+
/**
|
|
129
|
+
* Serves the authorization endpoints (`/authorize`, `/token`, `/register`) and the
|
|
130
|
+
* authorization server and protected resource (RFC 9728) metadata. Hosts that route the
|
|
131
|
+
* well-known paths themselves pass those requests here too. `null` for any other request,
|
|
132
|
+
* so it can sit in front of the app's own routing.
|
|
133
|
+
*/
|
|
134
|
+
handle: (req: Request) => Promise<Response | null>;
|
|
135
|
+
/**
|
|
136
|
+
* The 401 for an MCP request without a valid token. Its `resource_metadata` points below the
|
|
137
|
+
* issuer, which MCP clients must follow, so discovery works under a base path.
|
|
138
|
+
*/
|
|
139
|
+
challenge: (req: Request) => Response;
|
|
140
|
+
/** The pending authorization behind a sealed consent, or `null` if it's invalid, expired or for another user. */
|
|
141
|
+
readConsent: (req: Request, consent: string) => Promise<Consent<TUser> | null>;
|
|
142
|
+
/**
|
|
143
|
+
* Answers the consent form, from its POST handler. `allow: true` calls `tokens.issue` and
|
|
144
|
+
* redirects to the client with a code carrying the issued refresh token; `allow: false`
|
|
145
|
+
* redirects with `access_denied`. It refuses requests that aren't a top-level same-origin form
|
|
146
|
+
* submission (`Sec-Fetch-Dest`, `Sec-Fetch-Site`, or `Origin` in older browsers), carry an
|
|
147
|
+
* `Authorization` header, or come from another user than the one the consent was shown to.
|
|
148
|
+
*/
|
|
149
|
+
decide: (req: Request, args: DecideArgs<TGrant>) => Promise<Response>;
|
|
150
|
+
}
|
|
151
|
+
export type CreateAuthlet = <TGrant = unknown, TUser extends AuthletUser = AuthletUser>(options: AuthletOptions<TGrant, TUser>) => Authlet<TGrant, TUser>;
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/urls.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** Every public URL authlet serves or advertises, derived from the issuer and the resource. */
|
|
2
|
+
interface AuthletUrls {
|
|
3
|
+
issuer: string;
|
|
4
|
+
resource: string;
|
|
5
|
+
authorizationEndpoint: string;
|
|
6
|
+
tokenEndpoint: string;
|
|
7
|
+
registrationEndpoint: string;
|
|
8
|
+
/** An empty key set: OpenID discovery requires `jwks_uri`, though no ID tokens are issued. */
|
|
9
|
+
jwksUri: string;
|
|
10
|
+
/**
|
|
11
|
+
* The protected resource metadata (RFC 9728) that `challenge` points to. It sits below the
|
|
12
|
+
* issuer, so a host mounted under a base path needs no route at the site root: MCP clients
|
|
13
|
+
* must follow `resource_metadata` from the challenge.
|
|
14
|
+
*/
|
|
15
|
+
resourceMetadata: string;
|
|
16
|
+
}
|
|
17
|
+
type AuthletEndpoint = "authorize" | "token" | "register" | "jwks" | "authorizationServerMetadata" | "resourceMetadata";
|
|
18
|
+
/** Derives all URLs. The issuer defaults to `${resource}/oauth`. */
|
|
19
|
+
declare const deriveUrls: (args: {
|
|
20
|
+
resource: string;
|
|
21
|
+
issuer?: string | undefined;
|
|
22
|
+
}) => AuthletUrls;
|
|
23
|
+
/**
|
|
24
|
+
* Which endpoint a request URL addresses, or `null`. Only the path is compared, because behind
|
|
25
|
+
* a TLS-terminating proxy the request URL can carry another scheme or host than the public one.
|
|
26
|
+
* Metadata answers at every location an MCP client tries: path-appended OpenID discovery below
|
|
27
|
+
* the issuer, which needs no root route, and the root-level well-known paths for hosts that
|
|
28
|
+
* route them here.
|
|
29
|
+
*/
|
|
30
|
+
declare const matchEndpoint: (requestUrl: string, urls: AuthletUrls) => AuthletEndpoint | null;
|
|
31
|
+
export { deriveUrls, matchEndpoint };
|
|
32
|
+
export type { AuthletEndpoint, AuthletUrls };
|
package/dist/urls.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { LOOPBACK_HOSTS } from "./redirect-uri.js";
|
|
2
|
+
/**
|
|
3
|
+
* Checks that an issuer or resource URL is already in canonical form: https (or http to a
|
|
4
|
+
* loopback host, for local development), no credentials, query, fragment or trailing slash,
|
|
5
|
+
* lowercase host, no default port, no dot segments. Clients compare the issuer and the
|
|
6
|
+
* resource exactly, so a value the URL parser would change is refused.
|
|
7
|
+
*/
|
|
8
|
+
const canonicalUrl = (value, name) => {
|
|
9
|
+
// URL.canParse rather than URL.parse, which needs Node 22.1.
|
|
10
|
+
const url = URL.canParse(value) ? new URL(value) : null;
|
|
11
|
+
const canonical = url && url.origin + url.pathname.replace(/\/+$/, "");
|
|
12
|
+
if (!url ||
|
|
13
|
+
canonical !== value ||
|
|
14
|
+
!(url.protocol === "https:" ||
|
|
15
|
+
(url.protocol === "http:" && LOOPBACK_HOSTS.has(url.hostname)))) {
|
|
16
|
+
throw new Error(`The authlet ${name} must be a canonical absolute https URL (no credentials, query, fragment or trailing slash; lowercase host; no default port), got "${value}"${canonical && canonical !== value ? `, did you mean "${canonical}"?` : "."}`);
|
|
17
|
+
}
|
|
18
|
+
return canonical;
|
|
19
|
+
};
|
|
20
|
+
/** Derives all URLs. The issuer defaults to `${resource}/oauth`. */
|
|
21
|
+
const deriveUrls = (args) => {
|
|
22
|
+
const resource = canonicalUrl(args.resource, "resource");
|
|
23
|
+
const issuer = canonicalUrl(args.issuer ?? `${resource}/oauth`, "issuer");
|
|
24
|
+
return {
|
|
25
|
+
issuer,
|
|
26
|
+
resource,
|
|
27
|
+
authorizationEndpoint: `${issuer}/authorize`,
|
|
28
|
+
tokenEndpoint: `${issuer}/token`,
|
|
29
|
+
registrationEndpoint: `${issuer}/register`,
|
|
30
|
+
jwksUri: `${issuer}/jwks`,
|
|
31
|
+
resourceMetadata: `${issuer}/.well-known/oauth-protected-resource`,
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
/** Inserts a well-known segment between origin and path, as RFC 8414 and RFC 9728 define. */
|
|
35
|
+
const insertWellKnown = (url, wellKnown) => {
|
|
36
|
+
const { origin, pathname } = new URL(url);
|
|
37
|
+
return `${origin}/.well-known/${wellKnown}${pathname === "/" ? "" : pathname}`;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Which endpoint a request URL addresses, or `null`. Only the path is compared, because behind
|
|
41
|
+
* a TLS-terminating proxy the request URL can carry another scheme or host than the public one.
|
|
42
|
+
* Metadata answers at every location an MCP client tries: path-appended OpenID discovery below
|
|
43
|
+
* the issuer, which needs no root route, and the root-level well-known paths for hosts that
|
|
44
|
+
* route them here.
|
|
45
|
+
*/
|
|
46
|
+
const matchEndpoint = (requestUrl, urls) => {
|
|
47
|
+
const { pathname } = new URL(requestUrl);
|
|
48
|
+
const candidates = [
|
|
49
|
+
[urls.authorizationEndpoint, "authorize"],
|
|
50
|
+
[urls.tokenEndpoint, "token"],
|
|
51
|
+
[urls.registrationEndpoint, "register"],
|
|
52
|
+
[urls.jwksUri, "jwks"],
|
|
53
|
+
[
|
|
54
|
+
`${urls.issuer}/.well-known/openid-configuration`,
|
|
55
|
+
"authorizationServerMetadata",
|
|
56
|
+
],
|
|
57
|
+
[
|
|
58
|
+
insertWellKnown(urls.issuer, "oauth-authorization-server"),
|
|
59
|
+
"authorizationServerMetadata",
|
|
60
|
+
],
|
|
61
|
+
[
|
|
62
|
+
insertWellKnown(urls.issuer, "openid-configuration"),
|
|
63
|
+
"authorizationServerMetadata",
|
|
64
|
+
],
|
|
65
|
+
[urls.resourceMetadata, "resourceMetadata"],
|
|
66
|
+
[
|
|
67
|
+
insertWellKnown(urls.resource, "oauth-protected-resource"),
|
|
68
|
+
"resourceMetadata",
|
|
69
|
+
],
|
|
70
|
+
];
|
|
71
|
+
return (candidates.find(([url]) => new URL(url).pathname === pathname)?.[1] ?? null);
|
|
72
|
+
};
|
|
73
|
+
export { deriveUrls, matchEndpoint };
|
package/package.json
CHANGED
|
@@ -1,6 +1,75 @@
|
|
|
1
1
|
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/package.json",
|
|
2
3
|
"name": "authlet",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
4
|
+
"version": "0.1.0-beta.2",
|
|
5
|
+
"description": "Runtime-agnostic OAuth 2.1 authorization server for MCP endpoints, built on Web Request and Response.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"oauth",
|
|
8
|
+
"oauth2",
|
|
9
|
+
"mcp",
|
|
10
|
+
"model-context-protocol",
|
|
11
|
+
"authorization-server"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/abinnovision/authlet.git",
|
|
16
|
+
"directory": "packages/authlet"
|
|
17
|
+
},
|
|
18
|
+
"license": "Apache-2.0",
|
|
19
|
+
"author": {
|
|
20
|
+
"name": "abi group GmbH",
|
|
21
|
+
"email": "info@abigroup.io",
|
|
22
|
+
"url": "https://abigroup.io"
|
|
23
|
+
},
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"LICENSE",
|
|
35
|
+
"README.md"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsc --build tsconfig.build.json",
|
|
39
|
+
"format:check": "prettier --check '{{src,test}/**/*,*}.{{t,j}s{,x},json{,5},md,y{,a}ml,css,html,gql,graphql}'",
|
|
40
|
+
"format:fix": "prettier --write '{{src,test}/**/*,*}.{{t,j}s{,x},json{,5},md,y{,a}ml,css,html,gql,graphql}'",
|
|
41
|
+
"lint:check": "eslint '{{src,test}/**/*,*}.{t,j}s{,x}'",
|
|
42
|
+
"lint:fix": "eslint '{{src,test}/**/*,*}.{t,j}s{,x}' --fix",
|
|
43
|
+
"test-unit": "vitest --passWithNoTests --run --coverage --config vitest.config.ts --project='*#unit'",
|
|
44
|
+
"test-unit:watch": "vitest --passWithNoTests --config vitest.config.ts --project='*#unit'",
|
|
45
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
46
|
+
},
|
|
47
|
+
"lint-staged": {
|
|
48
|
+
"{{src,test}/**/*,*}.{{t,j}s{,x},json{,5},md,y{,a}ml,css,html,gql,graphql}": "prettier --write",
|
|
49
|
+
"{{src,test}/**/*,*}.{t,j}s{,x}": "eslint --fix"
|
|
50
|
+
},
|
|
51
|
+
"prettier": "@abinnovision/prettier-config",
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"jose": "^6.2.18"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@abinnovision/eslint-config-base": "^3.4.2",
|
|
57
|
+
"@abinnovision/prettier-config": "^2.2.0",
|
|
58
|
+
"@internal/tsconfig": "0.0.0",
|
|
59
|
+
"@types/node": "^24",
|
|
60
|
+
"@vitest/coverage-v8": "^5.0.3",
|
|
61
|
+
"eslint": "^10.12.0",
|
|
62
|
+
"oauth4webapi": "^3.8.9",
|
|
63
|
+
"prettier": "^3.9.9",
|
|
64
|
+
"typescript": "^6.0.3",
|
|
65
|
+
"vite": "^8.3.4",
|
|
66
|
+
"vitest": "^5.0.3"
|
|
67
|
+
},
|
|
68
|
+
"engines": {
|
|
69
|
+
"node": ">=22"
|
|
70
|
+
},
|
|
71
|
+
"publishConfig": {
|
|
72
|
+
"npm": true,
|
|
73
|
+
"npmAccess": "public"
|
|
74
|
+
}
|
|
75
|
+
}
|