ovr 6.2.4 → 7.0.0-next.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 +3 -1
- package/dist/app/index.d.ts +51 -7
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +163 -28
- package/dist/auth/auth-data.d.ts +26 -0
- package/dist/auth/auth-data.d.ts.map +1 -0
- package/dist/auth/auth-data.js +42 -0
- package/dist/auth/cbor.d.ts +50 -0
- package/dist/auth/cbor.d.ts.map +1 -0
- package/dist/auth/cbor.js +200 -0
- package/dist/auth/client-source.d.ts +3 -0
- package/dist/auth/client-source.d.ts.map +1 -0
- package/dist/auth/client-source.js +3 -0
- package/dist/auth/client.d.ts +3 -0
- package/dist/auth/client.d.ts.map +1 -0
- package/dist/auth/client.js +186 -0
- package/dist/auth/configure.d.ts +28 -0
- package/dist/auth/configure.d.ts.map +1 -0
- package/dist/auth/configure.js +228 -0
- package/dist/auth/der.d.ts +6 -0
- package/dist/auth/der.d.ts.map +1 -0
- package/dist/auth/der.js +36 -0
- package/dist/auth/index.d.ts +242 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +152 -0
- package/dist/auth/issue.d.ts +36 -0
- package/dist/auth/issue.d.ts.map +1 -0
- package/dist/auth/issue.js +47 -0
- package/dist/auth/metadata.d.ts +22 -0
- package/dist/auth/metadata.d.ts.map +1 -0
- package/dist/auth/metadata.js +134 -0
- package/dist/auth/oauth.d.ts +29 -0
- package/dist/auth/oauth.d.ts.map +1 -0
- package/dist/auth/oauth.js +724 -0
- package/dist/auth/passkey.d.ts +101 -0
- package/dist/auth/passkey.d.ts.map +1 -0
- package/dist/auth/passkey.js +393 -0
- package/dist/auth/state.d.ts +49 -0
- package/dist/auth/state.d.ts.map +1 -0
- package/dist/auth/state.js +184 -0
- package/dist/auth/storage.d.ts +193 -0
- package/dist/auth/storage.d.ts.map +1 -0
- package/dist/auth/storage.js +410 -0
- package/dist/auth/token.d.ts +12 -0
- package/dist/auth/token.d.ts.map +1 -0
- package/dist/auth/token.js +39 -0
- package/dist/context/index.d.ts +86 -16
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +166 -42
- package/dist/cookie/index.d.ts +3 -0
- package/dist/cookie/index.d.ts.map +1 -1
- package/dist/cookie/index.js +1 -0
- package/dist/database/index.d.ts +262 -0
- package/dist/database/index.d.ts.map +1 -0
- package/dist/database/index.js +807 -0
- package/dist/form/index.d.ts +699 -0
- package/dist/form/index.d.ts.map +1 -0
- package/dist/form/index.js +901 -0
- package/dist/index.d.ts +10 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -3
- package/dist/jsx/elements.d.ts +28 -2
- package/dist/jsx/elements.d.ts.map +1 -1
- package/dist/jsx/index.d.ts +20 -4
- package/dist/jsx/index.d.ts.map +1 -1
- package/dist/jsx/index.js +6 -65
- package/dist/limiter/index.d.ts +65 -0
- package/dist/limiter/index.d.ts.map +1 -0
- package/dist/limiter/index.js +157 -0
- package/dist/markdown/index.d.ts +61 -0
- package/dist/markdown/index.d.ts.map +1 -0
- package/dist/markdown/index.js +1160 -0
- package/dist/markup/index.d.ts +188 -0
- package/dist/markup/index.d.ts.map +1 -0
- package/dist/markup/index.js +718 -0
- package/dist/mcp/index.d.ts +73 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +988 -0
- package/dist/middleware/index.d.ts +3 -4
- package/dist/middleware/index.d.ts.map +1 -1
- package/dist/multipart/index.d.ts +53 -7
- package/dist/multipart/index.d.ts.map +1 -1
- package/dist/multipart/index.js +332 -124
- package/dist/route/index.d.ts +226 -80
- package/dist/route/index.d.ts.map +1 -1
- package/dist/route/index.js +239 -99
- package/dist/schema/index.d.ts +307 -734
- package/dist/schema/index.d.ts.map +1 -1
- package/dist/schema/index.js +783 -1050
- package/dist/table/index.d.ts +132 -0
- package/dist/table/index.d.ts.map +1 -0
- package/dist/table/index.js +188 -0
- package/dist/trie/index.js +4 -4
- package/dist/util/index.d.ts +37 -5
- package/dist/util/index.d.ts.map +1 -1
- package/dist/util/index.js +156 -41
- package/package.json +11 -4
- package/src/app/index.ts +230 -42
- package/src/auth/auth-data.ts +56 -0
- package/src/auth/cbor.ts +231 -0
- package/src/auth/client-source.ts +3 -0
- package/src/auth/client.ts +205 -0
- package/src/auth/configure.ts +279 -0
- package/src/auth/der.ts +35 -0
- package/src/auth/index.ts +387 -0
- package/src/auth/issue.ts +52 -0
- package/src/auth/metadata.ts +149 -0
- package/src/auth/oauth.ts +882 -0
- package/src/auth/passkey.ts +556 -0
- package/src/auth/state.ts +213 -0
- package/src/auth/storage.ts +572 -0
- package/src/auth/token.ts +67 -0
- package/src/context/index.ts +240 -65
- package/src/cookie/index.ts +3 -0
- package/src/database/index.ts +1193 -0
- package/src/form/index.ts +1720 -0
- package/src/index.ts +10 -3
- package/src/jsx/elements.ts +45 -25
- package/src/jsx/index.ts +24 -69
- package/src/limiter/index.ts +221 -0
- package/src/markdown/index.ts +1322 -0
- package/src/markup/index.ts +853 -0
- package/src/mcp/conformance.2025-11-25.yml +39 -0
- package/src/mcp/conformance.2026-07-28.yml +56 -0
- package/src/mcp/index.ts +1262 -0
- package/src/middleware/index.ts +5 -6
- package/src/multipart/index.ts +410 -158
- package/src/route/index.ts +559 -278
- package/src/schema/index.ts +1542 -2256
- package/src/table/index.ts +387 -0
- package/src/trie/index.ts +4 -4
- package/src/util/index.ts +163 -49
- package/dist/render/index.d.ts +0 -79
- package/dist/render/index.d.ts.map +0 -1
- package/dist/render/index.js +0 -257
- package/src/render/index.ts +0 -314
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { type Context as ContextType, agent } from "../context/index.js";
|
|
2
|
+
import { Codec, Time, noStore } from "../util/index.js";
|
|
3
|
+
import type { Auth } from "./index.js";
|
|
4
|
+
import { Issue } from "./issue.js";
|
|
5
|
+
import type { Storage } from "./storage.js";
|
|
6
|
+
import { Token } from "./token.js";
|
|
7
|
+
|
|
8
|
+
export namespace State {
|
|
9
|
+
/** Session persistence and signing configuration used by internal request state. */
|
|
10
|
+
export type Options = Pick<
|
|
11
|
+
Auth.Options,
|
|
12
|
+
"secret" | "origin" | "idle" | "lifetime"
|
|
13
|
+
> &
|
|
14
|
+
Pick<Storage, "challenges" | "sessions">;
|
|
15
|
+
/** Internal capabilities needed for request-local authentication state. */
|
|
16
|
+
export type Context = Pick<Auth.Context, "url" | "cookie" | "res">;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Internal authentication state for exactly one service and request. */
|
|
20
|
+
export class State {
|
|
21
|
+
static readonly #cookie = "__Host-auth-session";
|
|
22
|
+
static readonly #binding = "__Host-auth-browser";
|
|
23
|
+
static readonly #return = "__Host-auth-return";
|
|
24
|
+
readonly #c: State.Context;
|
|
25
|
+
#browser: string | undefined;
|
|
26
|
+
#session: Promise<Auth.Session | null> | undefined;
|
|
27
|
+
|
|
28
|
+
/** Normalized authentication configuration. */
|
|
29
|
+
readonly options;
|
|
30
|
+
|
|
31
|
+
/** Create authentication helpers for the current request. */
|
|
32
|
+
constructor(c: State.Context, options: State.Options) {
|
|
33
|
+
this.#c = c;
|
|
34
|
+
const lifetime = options.lifetime ?? 30 * Time.day;
|
|
35
|
+
this.options = {
|
|
36
|
+
...options,
|
|
37
|
+
lifetime,
|
|
38
|
+
// a session never outlives its lifetime, however recently it was used
|
|
39
|
+
idle: Math.min(options.idle ?? Time.week, lifetime),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A request on another host has no session there, its cookies are bound to
|
|
45
|
+
* Auth's origin, and passkeys cannot be used on it.
|
|
46
|
+
*
|
|
47
|
+
* @throws When the request is not on Auth's origin
|
|
48
|
+
*/
|
|
49
|
+
#origin() {
|
|
50
|
+
if (this.#c.url.origin !== this.options.origin)
|
|
51
|
+
throw new Error(
|
|
52
|
+
`Auth serves ${this.options.origin}, this request is for ${this.#c.url.origin}`,
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Read a browser binding; create it only while preparing a form in the handler. */
|
|
57
|
+
browser(create = false) {
|
|
58
|
+
if (create) this.#origin();
|
|
59
|
+
this.#browser ??= this.#c.cookie.get(State.#binding);
|
|
60
|
+
if (!this.#browser && create) {
|
|
61
|
+
this.#browser = crypto.randomUUID();
|
|
62
|
+
this.#set(State.#binding, this.#browser);
|
|
63
|
+
}
|
|
64
|
+
return this.#browser;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Set one of Auth's cookies, which only its origin's server reads.
|
|
69
|
+
*
|
|
70
|
+
* @param name Cookie name
|
|
71
|
+
* @param value Cookie value
|
|
72
|
+
* @param options How long it lasts, a browser session without it
|
|
73
|
+
*/
|
|
74
|
+
#set(name: string, value: string, options?: { maxAge: number }) {
|
|
75
|
+
this.#c.cookie.set(name, value, {
|
|
76
|
+
secure: true,
|
|
77
|
+
httpOnly: true,
|
|
78
|
+
sameSite: "Lax",
|
|
79
|
+
...options,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
#clear() {
|
|
84
|
+
noStore(this.#c.res.headers);
|
|
85
|
+
this.#set(State.#cookie, "", { maxAge: 0 });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async #read() {
|
|
89
|
+
// an agent's tool call, authorized by its token
|
|
90
|
+
const { req } = this.#c as Partial<Pick<ContextType, "req">>;
|
|
91
|
+
const call = req && agent.get(req);
|
|
92
|
+
if (call?.session) return call.session as Auth.Session;
|
|
93
|
+
|
|
94
|
+
// nobody is signed in on another host, its session cookie is not sent there
|
|
95
|
+
if (this.#c.url.origin !== this.options.origin) return null;
|
|
96
|
+
|
|
97
|
+
noStore(this.#c.res.headers);
|
|
98
|
+
const cookie = this.#c.cookie.get(State.#cookie);
|
|
99
|
+
if (!cookie) return null;
|
|
100
|
+
let id: string;
|
|
101
|
+
try {
|
|
102
|
+
id = await Token.open(this.options.secret, "session", cookie);
|
|
103
|
+
} catch (e) {
|
|
104
|
+
if (!(e instanceof Issue)) throw e;
|
|
105
|
+
this.#clear();
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
// Storage failures must propagate, rather than appear to be successful logouts.
|
|
109
|
+
const session = await this.options.sessions.get(id);
|
|
110
|
+
const now = Date.now();
|
|
111
|
+
if (
|
|
112
|
+
session &&
|
|
113
|
+
session.id === id &&
|
|
114
|
+
session.expiration > now &&
|
|
115
|
+
session.started + this.options.lifetime > now
|
|
116
|
+
)
|
|
117
|
+
return this.#renew(session, now);
|
|
118
|
+
this.#clear();
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Extend a session that is being used, up to its lifetime. Only the record
|
|
124
|
+
* changes: the cookie lasts the whole lifetime, so a renewal sets no header
|
|
125
|
+
* and works after the response has started streaming.
|
|
126
|
+
*
|
|
127
|
+
* @param session Valid stored session
|
|
128
|
+
* @param now Current time
|
|
129
|
+
* @returns The session with its current expiration
|
|
130
|
+
*/
|
|
131
|
+
async #renew(session: Storage.Session, now: number): Promise<Auth.Session> {
|
|
132
|
+
const { idle, lifetime } = this.options;
|
|
133
|
+
let { expiration } = session;
|
|
134
|
+
const later = Math.min(now + idle, session.started + lifetime);
|
|
135
|
+
// at most once a day, or each half of a shorter idle timeout
|
|
136
|
+
const renewed = expiration - idle;
|
|
137
|
+
if (now - renewed >= Math.min(Time.day, idle / 2) && later > expiration) {
|
|
138
|
+
await this.options.sessions.renew(session.id, later);
|
|
139
|
+
expiration = later;
|
|
140
|
+
}
|
|
141
|
+
return { id: session.id, user: session.user, expiration };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Read a non-expired, non-revoked session, renewing it while it is used. */
|
|
145
|
+
session() {
|
|
146
|
+
return (this.#session ??= this.#read());
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Return to a local path after the next login, such as an agent's
|
|
151
|
+
* authorization request.
|
|
152
|
+
*
|
|
153
|
+
* @param path Local path with its query
|
|
154
|
+
*/
|
|
155
|
+
remember(path: string) {
|
|
156
|
+
this.#origin();
|
|
157
|
+
this.#set(State.#return, path, {
|
|
158
|
+
maxAge: Math.floor((15 * Time.minute) / Time.second),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* @param fallback Destination without a remembered path
|
|
164
|
+
* @returns The remembered path, cleared, or the fallback
|
|
165
|
+
*/
|
|
166
|
+
resume(fallback: string) {
|
|
167
|
+
this.#origin();
|
|
168
|
+
const path = this.#c.cookie.get(State.#return);
|
|
169
|
+
if (!path) return fallback;
|
|
170
|
+
|
|
171
|
+
this.#set(State.#return, "", { maxAge: 0 });
|
|
172
|
+
|
|
173
|
+
return path.startsWith("/") &&
|
|
174
|
+
!path.startsWith("//") &&
|
|
175
|
+
!path.includes("\\")
|
|
176
|
+
? path
|
|
177
|
+
: fallback;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Replace the current session and issue a new signed, secure cookie. */
|
|
181
|
+
async login(user: string) {
|
|
182
|
+
this.#origin();
|
|
183
|
+
if (!user.trim() || Codec.encode(user).length > 1024)
|
|
184
|
+
throw new TypeError("Auth user ID must contain 1 to 1024 UTF-8 bytes");
|
|
185
|
+
const previous = await this.session();
|
|
186
|
+
if (previous) await this.options.sessions.delete(previous.id);
|
|
187
|
+
const now = Date.now();
|
|
188
|
+
const session = {
|
|
189
|
+
id: crypto.randomUUID(),
|
|
190
|
+
user,
|
|
191
|
+
expiration: now + this.options.idle,
|
|
192
|
+
};
|
|
193
|
+
await this.options.sessions.set(session.id, { ...session, started: now });
|
|
194
|
+
this.#set(
|
|
195
|
+
State.#cookie,
|
|
196
|
+
await Token.seal(this.options.secret, "session", session.id),
|
|
197
|
+
// renewals only change the record, so the cookie covers the lifetime
|
|
198
|
+
{ maxAge: Math.floor(this.options.lifetime / Time.second) },
|
|
199
|
+
);
|
|
200
|
+
this.#session = Promise.resolve(session);
|
|
201
|
+
return session;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Revoke the current session and expire its cookie. Always await this method. */
|
|
205
|
+
async logout() {
|
|
206
|
+
this.#origin();
|
|
207
|
+
const session = await this.session();
|
|
208
|
+
if (session) await this.options.sessions.delete(session.id);
|
|
209
|
+
this.#clear();
|
|
210
|
+
this.#session = Promise.resolve(null);
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
}
|