@theokit/sdk 4.42.0 → 4.42.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/CHANGELOG.md +31 -0
- package/dist/{agent-HGDXQEQQ.cjs → agent-DRUORJTU.cjs} +5 -5
- package/dist/{agent-HGDXQEQQ.cjs.map → agent-DRUORJTU.cjs.map} +1 -1
- package/dist/{agent-WUVYXIFX.js → agent-MVYGMWPM.js} +4 -4
- package/dist/{agent-WUVYXIFX.js.map → agent-MVYGMWPM.js.map} +1 -1
- package/dist/{chunk-YHQ3DG24.cjs → chunk-4OQXDCMN.cjs} +10 -10
- package/dist/chunk-4OQXDCMN.cjs.map +1 -0
- package/dist/{chunk-22EMHGBO.cjs → chunk-GAXYZXYY.cjs} +8 -7
- package/dist/chunk-GAXYZXYY.cjs.map +1 -0
- package/dist/{chunk-EYEZDADP.cjs → chunk-HLGEXZZP.cjs} +5 -5
- package/dist/{chunk-EYEZDADP.cjs.map → chunk-HLGEXZZP.cjs.map} +1 -1
- package/dist/{chunk-MPD5EGJ6.js → chunk-O67XODD5.js} +3 -3
- package/dist/{chunk-MPD5EGJ6.js.map → chunk-O67XODD5.js.map} +1 -1
- package/dist/{chunk-KO5V6L3E.js → chunk-V5V5A6B7.js} +5 -5
- package/dist/chunk-V5V5A6B7.js.map +1 -0
- package/dist/{chunk-Q2PPSHBS.js → chunk-ZZAAQOAZ.js} +9 -9
- package/dist/chunk-ZZAAQOAZ.js.map +1 -0
- package/dist/context/index.cjs +5 -5
- package/dist/context/index.js +2 -2
- package/dist/cron.cjs +4 -4
- package/dist/cron.js +3 -3
- package/dist/eval.cjs +3 -3
- package/dist/eval.js +2 -2
- package/dist/index.cjs +19 -19
- package/dist/index.js +5 -5
- package/dist/internal/runtime/context/path-containment.d.ts +24 -0
- package/dist/server/auth/index.d.cts +207 -11
- package/dist/server/auth/index.d.ts +207 -11
- package/dist/server/errors-envelope.d.cts +12 -6
- package/dist/server/errors-envelope.d.ts +12 -6
- package/package.json +13 -12
- package/dist/chunk-22EMHGBO.cjs.map +0 -1
- package/dist/chunk-KO5V6L3E.js.map +0 -1
- package/dist/chunk-Q2PPSHBS.js.map +0 -1
- package/dist/chunk-YHQ3DG24.cjs.map +0 -1
|
@@ -1,15 +1,211 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
|
-
* @theokit/sdk/server/auth —
|
|
4
|
+
* @theokit/sdk/server/auth — typed error classes
|
|
3
5
|
*
|
|
4
|
-
*
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
* Plan T1.2 + v1.1 EC-1 (AuthCancelledError for OAuth provider error response RFC 6749 §4.1.2.1).
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Thrown at `defineAuth()` time when configuration is invalid
|
|
10
|
+
* (e.g., duplicate provider name, invalid email shape per EC-V1-12).
|
|
11
|
+
*/
|
|
12
|
+
declare class AuthConfigError extends Error {
|
|
13
|
+
readonly name = "AuthConfigError";
|
|
14
|
+
readonly code: string;
|
|
15
|
+
constructor(code: string, message: string);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Thrown at `startSignIn(providerName, ...)` or `finishSignIn(providerName, ...)`
|
|
19
|
+
* when the named provider is not registered in `providers[]`.
|
|
20
|
+
*/
|
|
21
|
+
declare class AuthProviderNotFoundError extends Error {
|
|
22
|
+
readonly name = "AuthProviderNotFoundError";
|
|
23
|
+
readonly providerName: string;
|
|
24
|
+
constructor(providerName: string);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Thrown during OAuth callback handling for state mismatches, expired
|
|
28
|
+
* transactions, missing query params, or provider 4xx/5xx errors.
|
|
29
|
+
*
|
|
30
|
+
* Typed `code` field lets consumers branch on cause:
|
|
31
|
+
* - 'oauth_transaction_expired' — cookie tx > 10min old (per ADR D5)
|
|
32
|
+
* - 'oauth_state_mismatch' — query state ≠ cookie state (CSRF defense per RFC 6749 §10.12)
|
|
33
|
+
* - 'oauth_provider_error' — non-access_denied error in callback URL
|
|
34
|
+
* - 'oauth_token_exchange_failed' — provider rejected code-for-tokens swap
|
|
35
|
+
* - 'oauth_userinfo_failed' — userinfo endpoint returned error
|
|
36
|
+
* - 'oauth_missing_code_or_state' — required query params absent
|
|
37
|
+
*/
|
|
38
|
+
declare class AuthCallbackError extends Error {
|
|
39
|
+
readonly name: string;
|
|
40
|
+
readonly code: string;
|
|
41
|
+
constructor(code: string, message?: string);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Per v1.1 EC-1 MUST FIX — typed subclass of AuthCallbackError for the
|
|
45
|
+
* specific case where user declined consent at provider screen.
|
|
46
|
+
*
|
|
47
|
+
* OAuth 2.0 RFC 6749 §4.1.2.1: provider redirects with `?error=access_denied`.
|
|
48
|
+
* Apps can catch this distinctly from network/server errors to render
|
|
49
|
+
* "Login cancelled — try again" UX instead of opaque "callback failed".
|
|
50
|
+
*/
|
|
51
|
+
declare class AuthCancelledError extends AuthCallbackError {
|
|
52
|
+
readonly name: string;
|
|
53
|
+
readonly errorDescription?: string;
|
|
54
|
+
constructor(errorDescription?: string);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @theokit/sdk/server/auth — orchestrator types (Path C Hybrid per G11)
|
|
59
|
+
*
|
|
60
|
+
* Plan: g11-auth-architecture-implementation v1.4 (sha256 4d381020...)
|
|
61
|
+
* Blueprint: g11-auth-architecture-decision v1.1 (SHIPPABLE 97.9)
|
|
62
|
+
* AUTH-DELEGATION lock (theokit/CLAUDE.md:217-225) — these types are the
|
|
63
|
+
* orchestrator contract; concrete OAuth/email providers ship in opt-in
|
|
64
|
+
* @theokit/auth-* packages (adapters layer per ADR D11).
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* SessionManager contract (matches theokit/packages/theo/src/server/auth/session.ts:49).
|
|
69
|
+
* Imported as type-only — runtime depends via peerDep `theokit@>=0.2.4`.
|
|
70
|
+
*/
|
|
71
|
+
interface SessionManager<TSession> {
|
|
72
|
+
getSession(req: IncomingMessage): Promise<TSession | null>;
|
|
73
|
+
createSession(res: ServerResponse, data: TSession): Promise<void>;
|
|
74
|
+
destroySession(res: ServerResponse): void;
|
|
75
|
+
rotateSession(req: IncomingMessage, res: ServerResponse): Promise<TSession | null>;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Per ADR D5 — OAuth transaction state stored in encrypted HttpOnly cookie
|
|
79
|
+
* (cookie-state pattern). Expires within 10 minutes per invariant.
|
|
80
|
+
*/
|
|
81
|
+
interface OAuthTransaction {
|
|
82
|
+
state: string;
|
|
83
|
+
pkceVerifier?: string;
|
|
84
|
+
returnTo?: string;
|
|
85
|
+
createdAt: number;
|
|
86
|
+
expiresAt: number;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Per ADR D9 — provider profile types are provider-specific (not unified).
|
|
90
|
+
* Each @theokit/auth-* package exports its own profile shape.
|
|
91
|
+
* Generic param TProfile lets consumers narrow via discriminated unions on providerName.
|
|
92
|
+
*/
|
|
93
|
+
interface AuthResult<TProfile, TName extends string = string> {
|
|
94
|
+
profile: TProfile;
|
|
95
|
+
providerName: TName;
|
|
96
|
+
rawTokens?: {
|
|
97
|
+
accessToken: string;
|
|
98
|
+
refreshToken?: string;
|
|
99
|
+
idToken?: string;
|
|
100
|
+
expiresAt?: number;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Provider contract — each @theokit/auth-* package implements this.
|
|
105
|
+
* Per blueprint Q5 + ADR D11 (adapters layer).
|
|
106
|
+
*/
|
|
107
|
+
interface AuthProvider<TProfile, TName extends string = string> {
|
|
108
|
+
name: TName;
|
|
109
|
+
createAuthorizationURL(tx: OAuthTransaction): URL | Promise<URL>;
|
|
110
|
+
handleCallback(req: IncomingMessage, tx: OAuthTransaction): Promise<AuthResult<TProfile, TName>>;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* `defineAuth(opts)` configuration shape — Path C (Hybrid).
|
|
114
|
+
* `providers` optional: empty = Path A escape hatch (manual signIn only).
|
|
115
|
+
* `onSignIn` invoked after provider callback success; returns TSession to persist.
|
|
116
|
+
*/
|
|
117
|
+
interface DefineAuthOptions<TSession> {
|
|
118
|
+
session: SessionManager<TSession>;
|
|
119
|
+
providers?: AuthProvider<unknown, string>[];
|
|
120
|
+
onSignIn?: <TProfile>(args: {
|
|
121
|
+
profile: TProfile;
|
|
122
|
+
provider: string;
|
|
123
|
+
}) => Promise<TSession>;
|
|
124
|
+
onSignOut?: (session: TSession | null) => Promise<void> | void;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Returned by `defineAuth<TSession>(opts)` — 5-method orchestrator surface.
|
|
128
|
+
*
|
|
129
|
+
* - startSignIn: returns Response.redirect to provider authorization URL with state cookie
|
|
130
|
+
* - finishSignIn: handles provider callback; verifies state; calls onSignIn; rotates session ID
|
|
131
|
+
* (OWASP A07:2021 per EC-10); creates session cookie; clears transaction cookie
|
|
132
|
+
* - signIn: Path A escape hatch — skip OAuth flow; directly persist session from external profile
|
|
133
|
+
* - signOut: destroys session cookie + invokes onSignOut callback
|
|
134
|
+
* - getSession: read-only passthrough to session.getSession
|
|
135
|
+
*/
|
|
136
|
+
interface AuthOrchestrator<TSession> {
|
|
137
|
+
startSignIn(providerName: string, req: IncomingMessage, opts?: {
|
|
138
|
+
returnTo?: string;
|
|
139
|
+
}): Promise<Response>;
|
|
140
|
+
finishSignIn(providerName: string, req: IncomingMessage, res: ServerResponse): Promise<{
|
|
141
|
+
session: TSession;
|
|
142
|
+
returnTo?: string;
|
|
143
|
+
}>;
|
|
144
|
+
signIn<TProfile>(profile: TProfile, providerName: string, req: IncomingMessage, res: ServerResponse): Promise<TSession>;
|
|
145
|
+
signOut(res: ServerResponse): void | Promise<void>;
|
|
146
|
+
getSession(req: IncomingMessage): Promise<TSession | null>;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* @theokit/sdk/server/auth — encrypted OAuth transaction cookie store
|
|
151
|
+
*
|
|
152
|
+
* Per ADR D5 — cookie-state pattern (no Redis/db dependency in core).
|
|
153
|
+
*
|
|
154
|
+
* Stores OAuthTransaction (state + pkceVerifier + returnTo + expiry) in a
|
|
155
|
+
* single signed+encrypted HttpOnly cookie. Stateless, works in edge/serverless.
|
|
156
|
+
*
|
|
157
|
+
* Cookie name: `__Host-theo_oauth_tx` (T5.3 — RFC 6265bis prefix)
|
|
158
|
+
* Lifetime: 10 minutes (per D5 invariant)
|
|
159
|
+
* Encryption: AES-256-GCM via Node's webcrypto subtle API
|
|
160
|
+
*
|
|
161
|
+
* Note: this is a minimal in-package implementation. Production deployments
|
|
162
|
+
* may prefer using `theokit/server/auth/crypto`'s encrypt/decrypt helpers
|
|
163
|
+
* via the SessionManager's existing secret rotation chain. For T1.2 we keep
|
|
164
|
+
* it self-contained to avoid cross-package peer-dep complexity; T2+ may
|
|
165
|
+
* refactor to share SessionManager's encrypt path.
|
|
166
|
+
*/
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* T5.1 — Typed error thrown when an OAuth tx-cookie secret has < 32
|
|
170
|
+
* bytes of entropy. Surfaces the actual byte length so operators can
|
|
171
|
+
* diagnose mis-configured env vars without leaking the secret itself.
|
|
172
|
+
*
|
|
173
|
+
* @public
|
|
174
|
+
*/
|
|
175
|
+
declare class AuthSecretTooShortError extends Error {
|
|
176
|
+
readonly name = "AuthSecretTooShortError";
|
|
177
|
+
constructor(actualBytes: number);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* @theokit/sdk/server/auth — defineAuth orchestrator runtime (Path C Hybrid)
|
|
182
|
+
*
|
|
183
|
+
* Plan T1.2 implementation per blueprint Q5 § Path C signatures.
|
|
184
|
+
* Composes existing primitives + the v1.1 EC-1/EC-2/EC-10 fixes.
|
|
185
|
+
*/
|
|
186
|
+
|
|
187
|
+
/** SE36 — `Auth.create` replaces `defineAuth` (ADR 0015). @public */
|
|
188
|
+
declare class Auth {
|
|
189
|
+
private constructor();
|
|
190
|
+
static create<TSession>(opts: DefineAuthOptions<TSession>): AuthOrchestrator<TSession>;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* @theokit/sdk/server/auth — same-origin returnTo validator
|
|
195
|
+
*
|
|
196
|
+
* Per v1.1 EC-2 MUST FIX — OWASP A01:2021 open-redirect mitigation.
|
|
197
|
+
*
|
|
198
|
+
* Without this check, attacker craft `/login?returnTo=https://evil.com` would
|
|
199
|
+
* cause post-login redirect to attacker domain with authenticated session cookie.
|
|
7
200
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
201
|
+
* Rules:
|
|
202
|
+
* - undefined/empty returnTo → default '/'
|
|
203
|
+
* - protocol-relative `//evil.com` → default '/' (URL parser would resolve to baseUrl protocol)
|
|
204
|
+
* - absolute URL with origin ≠ baseUrl.origin → default '/' (cross-origin redirect)
|
|
205
|
+
* - absolute URL with origin === baseUrl.origin → keep (same-origin allowed)
|
|
206
|
+
* - relative path starting with '/' → keep (same-app navigation)
|
|
207
|
+
* - relative path not starting with '/' → default '/' (defensive)
|
|
10
208
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
export { Auth
|
|
14
|
-
export type { AuthOrchestrator, AuthProvider, AuthResult, DefineAuthOptions, OAuthTransaction, SessionManager, } from "./types.js";
|
|
15
|
-
export { validateReturnTo } from "./validate-return-to.js";
|
|
209
|
+
declare function validateReturnTo(returnTo: string | undefined, baseUrl: URL): string;
|
|
210
|
+
|
|
211
|
+
export { Auth, AuthCallbackError, AuthCancelledError, AuthConfigError, type AuthOrchestrator, type AuthProvider, AuthProviderNotFoundError, type AuthResult, AuthSecretTooShortError, type DefineAuthOptions, type OAuthTransaction, type SessionManager, validateReturnTo };
|
|
@@ -1,15 +1,211 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
|
-
* @theokit/sdk/server/auth —
|
|
4
|
+
* @theokit/sdk/server/auth — typed error classes
|
|
3
5
|
*
|
|
4
|
-
*
|
|
5
|
-
|
|
6
|
-
|
|
6
|
+
* Plan T1.2 + v1.1 EC-1 (AuthCancelledError for OAuth provider error response RFC 6749 §4.1.2.1).
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Thrown at `defineAuth()` time when configuration is invalid
|
|
10
|
+
* (e.g., duplicate provider name, invalid email shape per EC-V1-12).
|
|
11
|
+
*/
|
|
12
|
+
declare class AuthConfigError extends Error {
|
|
13
|
+
readonly name = "AuthConfigError";
|
|
14
|
+
readonly code: string;
|
|
15
|
+
constructor(code: string, message: string);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Thrown at `startSignIn(providerName, ...)` or `finishSignIn(providerName, ...)`
|
|
19
|
+
* when the named provider is not registered in `providers[]`.
|
|
20
|
+
*/
|
|
21
|
+
declare class AuthProviderNotFoundError extends Error {
|
|
22
|
+
readonly name = "AuthProviderNotFoundError";
|
|
23
|
+
readonly providerName: string;
|
|
24
|
+
constructor(providerName: string);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Thrown during OAuth callback handling for state mismatches, expired
|
|
28
|
+
* transactions, missing query params, or provider 4xx/5xx errors.
|
|
29
|
+
*
|
|
30
|
+
* Typed `code` field lets consumers branch on cause:
|
|
31
|
+
* - 'oauth_transaction_expired' — cookie tx > 10min old (per ADR D5)
|
|
32
|
+
* - 'oauth_state_mismatch' — query state ≠ cookie state (CSRF defense per RFC 6749 §10.12)
|
|
33
|
+
* - 'oauth_provider_error' — non-access_denied error in callback URL
|
|
34
|
+
* - 'oauth_token_exchange_failed' — provider rejected code-for-tokens swap
|
|
35
|
+
* - 'oauth_userinfo_failed' — userinfo endpoint returned error
|
|
36
|
+
* - 'oauth_missing_code_or_state' — required query params absent
|
|
37
|
+
*/
|
|
38
|
+
declare class AuthCallbackError extends Error {
|
|
39
|
+
readonly name: string;
|
|
40
|
+
readonly code: string;
|
|
41
|
+
constructor(code: string, message?: string);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Per v1.1 EC-1 MUST FIX — typed subclass of AuthCallbackError for the
|
|
45
|
+
* specific case where user declined consent at provider screen.
|
|
46
|
+
*
|
|
47
|
+
* OAuth 2.0 RFC 6749 §4.1.2.1: provider redirects with `?error=access_denied`.
|
|
48
|
+
* Apps can catch this distinctly from network/server errors to render
|
|
49
|
+
* "Login cancelled — try again" UX instead of opaque "callback failed".
|
|
50
|
+
*/
|
|
51
|
+
declare class AuthCancelledError extends AuthCallbackError {
|
|
52
|
+
readonly name: string;
|
|
53
|
+
readonly errorDescription?: string;
|
|
54
|
+
constructor(errorDescription?: string);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @theokit/sdk/server/auth — orchestrator types (Path C Hybrid per G11)
|
|
59
|
+
*
|
|
60
|
+
* Plan: g11-auth-architecture-implementation v1.4 (sha256 4d381020...)
|
|
61
|
+
* Blueprint: g11-auth-architecture-decision v1.1 (SHIPPABLE 97.9)
|
|
62
|
+
* AUTH-DELEGATION lock (theokit/CLAUDE.md:217-225) — these types are the
|
|
63
|
+
* orchestrator contract; concrete OAuth/email providers ship in opt-in
|
|
64
|
+
* @theokit/auth-* packages (adapters layer per ADR D11).
|
|
65
|
+
*/
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* SessionManager contract (matches theokit/packages/theo/src/server/auth/session.ts:49).
|
|
69
|
+
* Imported as type-only — runtime depends via peerDep `theokit@>=0.2.4`.
|
|
70
|
+
*/
|
|
71
|
+
interface SessionManager<TSession> {
|
|
72
|
+
getSession(req: IncomingMessage): Promise<TSession | null>;
|
|
73
|
+
createSession(res: ServerResponse, data: TSession): Promise<void>;
|
|
74
|
+
destroySession(res: ServerResponse): void;
|
|
75
|
+
rotateSession(req: IncomingMessage, res: ServerResponse): Promise<TSession | null>;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Per ADR D5 — OAuth transaction state stored in encrypted HttpOnly cookie
|
|
79
|
+
* (cookie-state pattern). Expires within 10 minutes per invariant.
|
|
80
|
+
*/
|
|
81
|
+
interface OAuthTransaction {
|
|
82
|
+
state: string;
|
|
83
|
+
pkceVerifier?: string;
|
|
84
|
+
returnTo?: string;
|
|
85
|
+
createdAt: number;
|
|
86
|
+
expiresAt: number;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Per ADR D9 — provider profile types are provider-specific (not unified).
|
|
90
|
+
* Each @theokit/auth-* package exports its own profile shape.
|
|
91
|
+
* Generic param TProfile lets consumers narrow via discriminated unions on providerName.
|
|
92
|
+
*/
|
|
93
|
+
interface AuthResult<TProfile, TName extends string = string> {
|
|
94
|
+
profile: TProfile;
|
|
95
|
+
providerName: TName;
|
|
96
|
+
rawTokens?: {
|
|
97
|
+
accessToken: string;
|
|
98
|
+
refreshToken?: string;
|
|
99
|
+
idToken?: string;
|
|
100
|
+
expiresAt?: number;
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Provider contract — each @theokit/auth-* package implements this.
|
|
105
|
+
* Per blueprint Q5 + ADR D11 (adapters layer).
|
|
106
|
+
*/
|
|
107
|
+
interface AuthProvider<TProfile, TName extends string = string> {
|
|
108
|
+
name: TName;
|
|
109
|
+
createAuthorizationURL(tx: OAuthTransaction): URL | Promise<URL>;
|
|
110
|
+
handleCallback(req: IncomingMessage, tx: OAuthTransaction): Promise<AuthResult<TProfile, TName>>;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* `defineAuth(opts)` configuration shape — Path C (Hybrid).
|
|
114
|
+
* `providers` optional: empty = Path A escape hatch (manual signIn only).
|
|
115
|
+
* `onSignIn` invoked after provider callback success; returns TSession to persist.
|
|
116
|
+
*/
|
|
117
|
+
interface DefineAuthOptions<TSession> {
|
|
118
|
+
session: SessionManager<TSession>;
|
|
119
|
+
providers?: AuthProvider<unknown, string>[];
|
|
120
|
+
onSignIn?: <TProfile>(args: {
|
|
121
|
+
profile: TProfile;
|
|
122
|
+
provider: string;
|
|
123
|
+
}) => Promise<TSession>;
|
|
124
|
+
onSignOut?: (session: TSession | null) => Promise<void> | void;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Returned by `defineAuth<TSession>(opts)` — 5-method orchestrator surface.
|
|
128
|
+
*
|
|
129
|
+
* - startSignIn: returns Response.redirect to provider authorization URL with state cookie
|
|
130
|
+
* - finishSignIn: handles provider callback; verifies state; calls onSignIn; rotates session ID
|
|
131
|
+
* (OWASP A07:2021 per EC-10); creates session cookie; clears transaction cookie
|
|
132
|
+
* - signIn: Path A escape hatch — skip OAuth flow; directly persist session from external profile
|
|
133
|
+
* - signOut: destroys session cookie + invokes onSignOut callback
|
|
134
|
+
* - getSession: read-only passthrough to session.getSession
|
|
135
|
+
*/
|
|
136
|
+
interface AuthOrchestrator<TSession> {
|
|
137
|
+
startSignIn(providerName: string, req: IncomingMessage, opts?: {
|
|
138
|
+
returnTo?: string;
|
|
139
|
+
}): Promise<Response>;
|
|
140
|
+
finishSignIn(providerName: string, req: IncomingMessage, res: ServerResponse): Promise<{
|
|
141
|
+
session: TSession;
|
|
142
|
+
returnTo?: string;
|
|
143
|
+
}>;
|
|
144
|
+
signIn<TProfile>(profile: TProfile, providerName: string, req: IncomingMessage, res: ServerResponse): Promise<TSession>;
|
|
145
|
+
signOut(res: ServerResponse): void | Promise<void>;
|
|
146
|
+
getSession(req: IncomingMessage): Promise<TSession | null>;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* @theokit/sdk/server/auth — encrypted OAuth transaction cookie store
|
|
151
|
+
*
|
|
152
|
+
* Per ADR D5 — cookie-state pattern (no Redis/db dependency in core).
|
|
153
|
+
*
|
|
154
|
+
* Stores OAuthTransaction (state + pkceVerifier + returnTo + expiry) in a
|
|
155
|
+
* single signed+encrypted HttpOnly cookie. Stateless, works in edge/serverless.
|
|
156
|
+
*
|
|
157
|
+
* Cookie name: `__Host-theo_oauth_tx` (T5.3 — RFC 6265bis prefix)
|
|
158
|
+
* Lifetime: 10 minutes (per D5 invariant)
|
|
159
|
+
* Encryption: AES-256-GCM via Node's webcrypto subtle API
|
|
160
|
+
*
|
|
161
|
+
* Note: this is a minimal in-package implementation. Production deployments
|
|
162
|
+
* may prefer using `theokit/server/auth/crypto`'s encrypt/decrypt helpers
|
|
163
|
+
* via the SessionManager's existing secret rotation chain. For T1.2 we keep
|
|
164
|
+
* it self-contained to avoid cross-package peer-dep complexity; T2+ may
|
|
165
|
+
* refactor to share SessionManager's encrypt path.
|
|
166
|
+
*/
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* T5.1 — Typed error thrown when an OAuth tx-cookie secret has < 32
|
|
170
|
+
* bytes of entropy. Surfaces the actual byte length so operators can
|
|
171
|
+
* diagnose mis-configured env vars without leaking the secret itself.
|
|
172
|
+
*
|
|
173
|
+
* @public
|
|
174
|
+
*/
|
|
175
|
+
declare class AuthSecretTooShortError extends Error {
|
|
176
|
+
readonly name = "AuthSecretTooShortError";
|
|
177
|
+
constructor(actualBytes: number);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* @theokit/sdk/server/auth — defineAuth orchestrator runtime (Path C Hybrid)
|
|
182
|
+
*
|
|
183
|
+
* Plan T1.2 implementation per blueprint Q5 § Path C signatures.
|
|
184
|
+
* Composes existing primitives + the v1.1 EC-1/EC-2/EC-10 fixes.
|
|
185
|
+
*/
|
|
186
|
+
|
|
187
|
+
/** SE36 — `Auth.create` replaces `defineAuth` (ADR 0015). @public */
|
|
188
|
+
declare class Auth {
|
|
189
|
+
private constructor();
|
|
190
|
+
static create<TSession>(opts: DefineAuthOptions<TSession>): AuthOrchestrator<TSession>;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* @theokit/sdk/server/auth — same-origin returnTo validator
|
|
195
|
+
*
|
|
196
|
+
* Per v1.1 EC-2 MUST FIX — OWASP A01:2021 open-redirect mitigation.
|
|
197
|
+
*
|
|
198
|
+
* Without this check, attacker craft `/login?returnTo=https://evil.com` would
|
|
199
|
+
* cause post-login redirect to attacker domain with authenticated session cookie.
|
|
7
200
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
201
|
+
* Rules:
|
|
202
|
+
* - undefined/empty returnTo → default '/'
|
|
203
|
+
* - protocol-relative `//evil.com` → default '/' (URL parser would resolve to baseUrl protocol)
|
|
204
|
+
* - absolute URL with origin ≠ baseUrl.origin → default '/' (cross-origin redirect)
|
|
205
|
+
* - absolute URL with origin === baseUrl.origin → keep (same-origin allowed)
|
|
206
|
+
* - relative path starting with '/' → keep (same-app navigation)
|
|
207
|
+
* - relative path not starting with '/' → default '/' (defensive)
|
|
10
208
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
export { Auth
|
|
14
|
-
export type { AuthOrchestrator, AuthProvider, AuthResult, DefineAuthOptions, OAuthTransaction, SessionManager, } from "./types.js";
|
|
15
|
-
export { validateReturnTo } from "./validate-return-to.js";
|
|
209
|
+
declare function validateReturnTo(returnTo: string | undefined, baseUrl: URL): string;
|
|
210
|
+
|
|
211
|
+
export { Auth, AuthCallbackError, AuthCancelledError, AuthConfigError, type AuthOrchestrator, type AuthProvider, AuthProviderNotFoundError, type AuthResult, AuthSecretTooShortError, type DefineAuthOptions, type OAuthTransaction, type SessionManager, validateReturnTo };
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import { T as TheokitAgentError } from '../errors-BSoXcl3F.cjs';
|
|
2
|
+
export { M as MemoryAdapterError } from '../errors-BSoXcl3F.cjs';
|
|
3
|
+
import '../run-C8FBAC8o.cjs';
|
|
4
|
+
import 'zod';
|
|
5
|
+
|
|
1
6
|
/**
|
|
2
7
|
* @theokit/sdk/server/errors-envelope — G5 T2.2 boundary translation.
|
|
3
8
|
*
|
|
@@ -12,7 +17,7 @@
|
|
|
12
17
|
* dependency on `theokit`. Consumer code that uses both packages gets the
|
|
13
18
|
* same shape via duck-typing.
|
|
14
19
|
*/
|
|
15
|
-
|
|
20
|
+
|
|
16
21
|
/**
|
|
17
22
|
* Canonical envelope code union for cross-layer SDK boundary. Subset of the
|
|
18
23
|
* full `TheoErrorCode` (theokit/server) covering codes the SDK actually emits.
|
|
@@ -21,13 +26,13 @@ import { MemoryAdapterError, TheokitAgentError } from "../errors.js";
|
|
|
21
26
|
*
|
|
22
27
|
* @public
|
|
23
28
|
*/
|
|
24
|
-
|
|
29
|
+
type TheokitErrorCode = "UNAUTHORIZED" | "RATE_LIMITED" | "INTERNAL_SERVER_ERROR" | "SERVICE_UNAVAILABLE" | "GATEWAY_TIMEOUT" | "AGENT_RUN_ERROR" | "PROVIDER_KEY_MISSING" | "BUDGET_EXCEEDED" | "CREDENTIAL_POOL_EXHAUSTED";
|
|
25
30
|
/**
|
|
26
31
|
* Envelope shape — structurally identical to theokit/server `TheoErrorEnvelope`.
|
|
27
32
|
*
|
|
28
33
|
* @public
|
|
29
34
|
*/
|
|
30
|
-
|
|
35
|
+
interface TheokitErrorEnvelope<TExt = unknown> {
|
|
31
36
|
readonly code: TheokitErrorCode;
|
|
32
37
|
readonly message: string;
|
|
33
38
|
readonly cause?: unknown;
|
|
@@ -49,7 +54,7 @@ export interface TheokitErrorEnvelope<TExt = unknown> {
|
|
|
49
54
|
*
|
|
50
55
|
* @public
|
|
51
56
|
*/
|
|
52
|
-
|
|
57
|
+
declare function toEnvelope(value: unknown): TheokitErrorEnvelope;
|
|
53
58
|
/**
|
|
54
59
|
* Hydrate an envelope back into the SDK class hierarchy. Use at the inbound
|
|
55
60
|
* boundary (e.g., on a worker process receiving an envelope from the main
|
|
@@ -57,5 +62,6 @@ export declare function toEnvelope(value: unknown): TheokitErrorEnvelope;
|
|
|
57
62
|
*
|
|
58
63
|
* @public
|
|
59
64
|
*/
|
|
60
|
-
|
|
61
|
-
|
|
65
|
+
declare function fromEnvelope(env: TheokitErrorEnvelope): TheokitAgentError;
|
|
66
|
+
|
|
67
|
+
export { type TheokitErrorCode, type TheokitErrorEnvelope, fromEnvelope, toEnvelope };
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import { T as TheokitAgentError } from '../errors-CHllybaU.js';
|
|
2
|
+
export { M as MemoryAdapterError } from '../errors-CHllybaU.js';
|
|
3
|
+
import '../run-C8FBAC8o.js';
|
|
4
|
+
import 'zod';
|
|
5
|
+
|
|
1
6
|
/**
|
|
2
7
|
* @theokit/sdk/server/errors-envelope — G5 T2.2 boundary translation.
|
|
3
8
|
*
|
|
@@ -12,7 +17,7 @@
|
|
|
12
17
|
* dependency on `theokit`. Consumer code that uses both packages gets the
|
|
13
18
|
* same shape via duck-typing.
|
|
14
19
|
*/
|
|
15
|
-
|
|
20
|
+
|
|
16
21
|
/**
|
|
17
22
|
* Canonical envelope code union for cross-layer SDK boundary. Subset of the
|
|
18
23
|
* full `TheoErrorCode` (theokit/server) covering codes the SDK actually emits.
|
|
@@ -21,13 +26,13 @@ import { MemoryAdapterError, TheokitAgentError } from "../errors.js";
|
|
|
21
26
|
*
|
|
22
27
|
* @public
|
|
23
28
|
*/
|
|
24
|
-
|
|
29
|
+
type TheokitErrorCode = "UNAUTHORIZED" | "RATE_LIMITED" | "INTERNAL_SERVER_ERROR" | "SERVICE_UNAVAILABLE" | "GATEWAY_TIMEOUT" | "AGENT_RUN_ERROR" | "PROVIDER_KEY_MISSING" | "BUDGET_EXCEEDED" | "CREDENTIAL_POOL_EXHAUSTED";
|
|
25
30
|
/**
|
|
26
31
|
* Envelope shape — structurally identical to theokit/server `TheoErrorEnvelope`.
|
|
27
32
|
*
|
|
28
33
|
* @public
|
|
29
34
|
*/
|
|
30
|
-
|
|
35
|
+
interface TheokitErrorEnvelope<TExt = unknown> {
|
|
31
36
|
readonly code: TheokitErrorCode;
|
|
32
37
|
readonly message: string;
|
|
33
38
|
readonly cause?: unknown;
|
|
@@ -49,7 +54,7 @@ export interface TheokitErrorEnvelope<TExt = unknown> {
|
|
|
49
54
|
*
|
|
50
55
|
* @public
|
|
51
56
|
*/
|
|
52
|
-
|
|
57
|
+
declare function toEnvelope(value: unknown): TheokitErrorEnvelope;
|
|
53
58
|
/**
|
|
54
59
|
* Hydrate an envelope back into the SDK class hierarchy. Use at the inbound
|
|
55
60
|
* boundary (e.g., on a worker process receiving an envelope from the main
|
|
@@ -57,5 +62,6 @@ export declare function toEnvelope(value: unknown): TheokitErrorEnvelope;
|
|
|
57
62
|
*
|
|
58
63
|
* @public
|
|
59
64
|
*/
|
|
60
|
-
|
|
61
|
-
|
|
65
|
+
declare function fromEnvelope(env: TheokitErrorEnvelope): TheokitAgentError;
|
|
66
|
+
|
|
67
|
+
export { type TheokitErrorCode, type TheokitErrorEnvelope, fromEnvelope, toEnvelope };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theokit/sdk",
|
|
3
|
-
"version": "4.42.
|
|
3
|
+
"version": "4.42.1",
|
|
4
4
|
"description": "TypeScript SDK for the Theo agent harness — same surface, local or cloud.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://github.com/usetheodev/theokit-sdk#readme",
|
|
@@ -349,6 +349,17 @@
|
|
|
349
349
|
"**/agent.js",
|
|
350
350
|
"**/agent.cjs"
|
|
351
351
|
],
|
|
352
|
+
"scripts": {
|
|
353
|
+
"build": "tsup && cp src/internal/providers/provider-catalog.json dist/provider-catalog.json && node scripts/copy-docs.mjs",
|
|
354
|
+
"test": "vitest run --no-file-parallelism",
|
|
355
|
+
"test:watch": "vitest",
|
|
356
|
+
"eval": "vitest run --no-file-parallelism tests/eval/suites",
|
|
357
|
+
"test:contract": "vitest run --no-file-parallelism tests/theokit-consumer-contract.test.ts",
|
|
358
|
+
"prepublishOnly": "pnpm build && pnpm test:contract && node ../../scripts/check-publish-no-workspace.mjs .",
|
|
359
|
+
"typecheck": "tsc --noEmit",
|
|
360
|
+
"clean": "rm -rf dist",
|
|
361
|
+
"docs:json": "typedoc --options typedoc.json"
|
|
362
|
+
},
|
|
352
363
|
"peerDependencies": {
|
|
353
364
|
"@lancedb/lancedb": "^0.30.0",
|
|
354
365
|
"@types/ws": ">=8.0.0",
|
|
@@ -416,15 +427,5 @@
|
|
|
416
427
|
"typedoc": "^0.28.19",
|
|
417
428
|
"ws": "^8.18.0",
|
|
418
429
|
"zod": "^4.0.0"
|
|
419
|
-
},
|
|
420
|
-
"scripts": {
|
|
421
|
-
"build": "tsup && cp src/internal/providers/provider-catalog.json dist/provider-catalog.json && node scripts/copy-docs.mjs",
|
|
422
|
-
"test": "vitest run --no-file-parallelism",
|
|
423
|
-
"test:watch": "vitest",
|
|
424
|
-
"eval": "vitest run --no-file-parallelism tests/eval/suites",
|
|
425
|
-
"test:contract": "vitest run --no-file-parallelism tests/theokit-consumer-contract.test.ts",
|
|
426
|
-
"typecheck": "tsc --noEmit",
|
|
427
|
-
"clean": "rm -rf dist",
|
|
428
|
-
"docs:json": "typedoc --options typedoc.json"
|
|
429
430
|
}
|
|
430
|
-
}
|
|
431
|
+
}
|