@sixb/server 0.0.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/LICENSE +21 -0
- package/README.md +123 -0
- package/dist/auth/access-token-boundary.d.ts +23 -0
- package/dist/auth/access-token-boundary.d.ts.map +1 -0
- package/dist/auth/application-access.d.ts +4 -0
- package/dist/auth/application-access.d.ts.map +1 -0
- package/dist/auth/browser-origin.d.ts +62 -0
- package/dist/auth/browser-origin.d.ts.map +1 -0
- package/dist/auth/csrf.d.ts +2 -0
- package/dist/auth/csrf.d.ts.map +1 -0
- package/dist/auth/guard.d.ts +35 -0
- package/dist/auth/guard.d.ts.map +1 -0
- package/dist/auth/public-routes.d.ts +8 -0
- package/dist/auth/public-routes.d.ts.map +1 -0
- package/dist/auth/responses.d.ts +11 -0
- package/dist/auth/responses.d.ts.map +1 -0
- package/dist/auth/return-to.d.ts +3 -0
- package/dist/auth/return-to.d.ts.map +1 -0
- package/dist/auth/scope.d.ts +43 -0
- package/dist/auth/scope.d.ts.map +1 -0
- package/dist/auth/session-activity.d.ts +4 -0
- package/dist/auth/session-activity.d.ts.map +1 -0
- package/dist/auth/session-cookies.d.ts +12 -0
- package/dist/auth/session-cookies.d.ts.map +1 -0
- package/dist/files/content.d.ts +141 -0
- package/dist/files/content.d.ts.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11522 -0
- package/dist/index.js.map +78 -0
- package/dist/openapi/security.d.ts +26 -0
- package/dist/openapi/security.d.ts.map +1 -0
- package/dist/openapi/tags.d.ts +151 -0
- package/dist/openapi/tags.d.ts.map +1 -0
- package/dist/registerRoutes.d.ts +31 -0
- package/dist/registerRoutes.d.ts.map +1 -0
- package/dist/routes/action-runs.d.ts +244 -0
- package/dist/routes/action-runs.d.ts.map +1 -0
- package/dist/routes/actions.d.ts +183 -0
- package/dist/routes/actions.d.ts.map +1 -0
- package/dist/routes/agent-api-gateway.d.ts +4 -0
- package/dist/routes/agent-api-gateway.d.ts.map +1 -0
- package/dist/routes/agents.d.ts +1061 -0
- package/dist/routes/agents.d.ts.map +1 -0
- package/dist/routes/auth.d.ts +1366 -0
- package/dist/routes/auth.d.ts.map +1 -0
- package/dist/routes/connectors.d.ts +108 -0
- package/dist/routes/connectors.d.ts.map +1 -0
- package/dist/routes/datasets.d.ts +339 -0
- package/dist/routes/datasets.d.ts.map +1 -0
- package/dist/routes/events.d.ts +116 -0
- package/dist/routes/events.d.ts.map +1 -0
- package/dist/routes/files.d.ts +340 -0
- package/dist/routes/files.d.ts.map +1 -0
- package/dist/routes/links.d.ts +175 -0
- package/dist/routes/links.d.ts.map +1 -0
- package/dist/routes/logs.d.ts +88 -0
- package/dist/routes/logs.d.ts.map +1 -0
- package/dist/routes/objects.d.ts +450 -0
- package/dist/routes/objects.d.ts.map +1 -0
- package/dist/routes/ontology.d.ts +240 -0
- package/dist/routes/ontology.d.ts.map +1 -0
- package/dist/routes/pipelines.d.ts +399 -0
- package/dist/routes/pipelines.d.ts.map +1 -0
- package/dist/routes/project.d.ts +56 -0
- package/dist/routes/project.d.ts.map +1 -0
- package/dist/routes/projections.d.ts +916 -0
- package/dist/routes/projections.d.ts.map +1 -0
- package/dist/routes/rules.d.ts +165 -0
- package/dist/routes/rules.d.ts.map +1 -0
- package/dist/routes/status.d.ts +81 -0
- package/dist/routes/status.d.ts.map +1 -0
- package/dist/routes/syncs.d.ts +294 -0
- package/dist/routes/syncs.d.ts.map +1 -0
- package/dist/routes/telemetry.d.ts +241 -0
- package/dist/routes/telemetry.d.ts.map +1 -0
- package/dist/routes/webhook-runs.d.ts +89 -0
- package/dist/routes/webhook-runs.d.ts.map +1 -0
- package/dist/routes/webhooks.d.ts +60 -0
- package/dist/routes/webhooks.d.ts.map +1 -0
- package/dist/routes/workflows.d.ts +973 -0
- package/dist/routes/workflows.d.ts.map +1 -0
- package/dist/routes/ws/agents.d.ts +99 -0
- package/dist/routes/ws/agents.d.ts.map +1 -0
- package/dist/routes/ws/events.d.ts +89 -0
- package/dist/routes/ws/events.d.ts.map +1 -0
- package/dist/routes/ws/index.d.ts +31 -0
- package/dist/routes/ws/index.d.ts.map +1 -0
- package/dist/routes/ws/log-subscription-hub.d.ts +47 -0
- package/dist/routes/ws/log-subscription-hub.d.ts.map +1 -0
- package/dist/routes/ws/logs.d.ts +110 -0
- package/dist/routes/ws/logs.d.ts.map +1 -0
- package/dist/schemas/actions.d.ts +644 -0
- package/dist/schemas/actions.d.ts.map +1 -0
- package/dist/schemas/agents.d.ts +3157 -0
- package/dist/schemas/agents.d.ts.map +1 -0
- package/dist/schemas/auth.d.ts +2023 -0
- package/dist/schemas/auth.d.ts.map +1 -0
- package/dist/schemas/common.d.ts +30 -0
- package/dist/schemas/common.d.ts.map +1 -0
- package/dist/schemas/connectors.d.ts +82 -0
- package/dist/schemas/connectors.d.ts.map +1 -0
- package/dist/schemas/datasets.d.ts +744 -0
- package/dist/schemas/datasets.d.ts.map +1 -0
- package/dist/schemas/event-payloads.d.ts +477 -0
- package/dist/schemas/event-payloads.d.ts.map +1 -0
- package/dist/schemas/events.d.ts +665 -0
- package/dist/schemas/events.d.ts.map +1 -0
- package/dist/schemas/files.d.ts +241 -0
- package/dist/schemas/files.d.ts.map +1 -0
- package/dist/schemas/links.d.ts +90 -0
- package/dist/schemas/links.d.ts.map +1 -0
- package/dist/schemas/logs.d.ts +231 -0
- package/dist/schemas/logs.d.ts.map +1 -0
- package/dist/schemas/objects.d.ts +288 -0
- package/dist/schemas/objects.d.ts.map +1 -0
- package/dist/schemas/ontology.d.ts +778 -0
- package/dist/schemas/ontology.d.ts.map +1 -0
- package/dist/schemas/pipelines.d.ts +1373 -0
- package/dist/schemas/pipelines.d.ts.map +1 -0
- package/dist/schemas/project.d.ts +9 -0
- package/dist/schemas/project.d.ts.map +1 -0
- package/dist/schemas/projections.d.ts +6992 -0
- package/dist/schemas/projections.d.ts.map +1 -0
- package/dist/schemas/rules.d.ts +204 -0
- package/dist/schemas/rules.d.ts.map +1 -0
- package/dist/schemas/status.d.ts +189 -0
- package/dist/schemas/status.d.ts.map +1 -0
- package/dist/schemas/syncs.d.ts +538 -0
- package/dist/schemas/syncs.d.ts.map +1 -0
- package/dist/schemas/telemetry.d.ts +265 -0
- package/dist/schemas/telemetry.d.ts.map +1 -0
- package/dist/schemas/webhook-runs.d.ts +169 -0
- package/dist/schemas/webhook-runs.d.ts.map +1 -0
- package/dist/schemas/workflows.d.ts +1720 -0
- package/dist/schemas/workflows.d.ts.map +1 -0
- package/dist/server.d.ts +64 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/dist/utils/http.d.ts +23 -0
- package/dist/utils/http.d.ts.map +1 -0
- package/dist/utils/request-body.d.ts +15 -0
- package/dist/utils/request-body.d.ts.map +1 -0
- package/dist/utils/ws.d.ts +10 -0
- package/dist/utils/ws.d.ts.map +1 -0
- package/package.json +68 -0
- package/src/auth/access-token-boundary.ts +68 -0
- package/src/auth/application-access.ts +16 -0
- package/src/auth/browser-origin.ts +367 -0
- package/src/auth/csrf.ts +1 -0
- package/src/auth/guard.ts +169 -0
- package/src/auth/public-routes.ts +87 -0
- package/src/auth/responses.ts +54 -0
- package/src/auth/return-to.ts +21 -0
- package/src/auth/scope.ts +64 -0
- package/src/auth/session-activity.ts +19 -0
- package/src/auth/session-cookies.ts +43 -0
- package/src/files/content.ts +419 -0
- package/src/index.ts +2 -0
- package/src/openapi/security.ts +37 -0
- package/src/openapi/tags.ts +151 -0
- package/src/registerRoutes.ts +49 -0
- package/src/routes/action-runs.ts +238 -0
- package/src/routes/actions.ts +126 -0
- package/src/routes/agent-api-gateway.ts +260 -0
- package/src/routes/agents.ts +943 -0
- package/src/routes/auth.ts +2233 -0
- package/src/routes/connectors.ts +69 -0
- package/src/routes/datasets.ts +402 -0
- package/src/routes/events.ts +55 -0
- package/src/routes/files.ts +675 -0
- package/src/routes/links.ts +143 -0
- package/src/routes/logs.ts +65 -0
- package/src/routes/objects.ts +838 -0
- package/src/routes/ontology.ts +138 -0
- package/src/routes/pipelines.ts +332 -0
- package/src/routes/project.ts +16 -0
- package/src/routes/projections.ts +251 -0
- package/src/routes/rules.ts +124 -0
- package/src/routes/status.ts +53 -0
- package/src/routes/syncs.ts +243 -0
- package/src/routes/telemetry.ts +207 -0
- package/src/routes/webhook-runs.ts +81 -0
- package/src/routes/webhooks.ts +508 -0
- package/src/routes/workflows.ts +1333 -0
- package/src/routes/ws/agents.ts +301 -0
- package/src/routes/ws/events.ts +278 -0
- package/src/routes/ws/index.ts +12 -0
- package/src/routes/ws/log-subscription-hub.ts +375 -0
- package/src/routes/ws/logs.ts +117 -0
- package/src/schemas/actions.ts +123 -0
- package/src/schemas/agents.ts +293 -0
- package/src/schemas/auth.ts +317 -0
- package/src/schemas/common.ts +60 -0
- package/src/schemas/connectors.ts +21 -0
- package/src/schemas/datasets.ts +107 -0
- package/src/schemas/event-payloads.ts +79 -0
- package/src/schemas/events.ts +117 -0
- package/src/schemas/files.ts +82 -0
- package/src/schemas/links.ts +38 -0
- package/src/schemas/logs.ts +76 -0
- package/src/schemas/objects.ts +759 -0
- package/src/schemas/ontology.ts +77 -0
- package/src/schemas/pipelines.ts +150 -0
- package/src/schemas/project.ts +5 -0
- package/src/schemas/projections.ts +144 -0
- package/src/schemas/rules.ts +98 -0
- package/src/schemas/status.ts +51 -0
- package/src/schemas/syncs.ts +83 -0
- package/src/schemas/telemetry.ts +59 -0
- package/src/schemas/webhook-runs.ts +40 -0
- package/src/schemas/workflows.ts +229 -0
- package/src/server.ts +383 -0
- package/src/utils/http.ts +108 -0
- package/src/utils/request-body.ts +62 -0
- package/src/utils/ws.ts +59 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import type { OntologySource, Sixb } from "@sixb/core"
|
|
2
|
+
import {
|
|
3
|
+
type AuthenticatedRequestAuthSession,
|
|
4
|
+
verifyDoubleSubmitCsrf,
|
|
5
|
+
} from "@sixb/core/internal/auth"
|
|
6
|
+
import { isAccessTokenRoute, shouldVerifyCsrfForAuthSource } from "./access-token-boundary"
|
|
7
|
+
import { sessionCanAccessApplication } from "./application-access"
|
|
8
|
+
import { BrowserOriginError, type ResolveRequestAuthContext } from "./browser-origin"
|
|
9
|
+
import { classifyRoute } from "./public-routes"
|
|
10
|
+
import {
|
|
11
|
+
htmlAuthRedirectResponse,
|
|
12
|
+
jsonAuthRequiredResponse,
|
|
13
|
+
jsonCsrfFailedResponse,
|
|
14
|
+
jsonForbiddenResponse,
|
|
15
|
+
websocketAuthFailedResponse,
|
|
16
|
+
} from "./responses"
|
|
17
|
+
import { hasForegroundSessionActivity, isSessionTerminationRequest } from "./session-activity"
|
|
18
|
+
|
|
19
|
+
export interface ServerAuthGuardOptions {
|
|
20
|
+
readonly sixb: Sixb<readonly OntologySource[]>
|
|
21
|
+
readonly resolveAuthContext: ResolveRequestAuthContext
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Per-request authentication decision.
|
|
26
|
+
*
|
|
27
|
+
* `deny` carries the short-circuit response. `allow` carries the resolved
|
|
28
|
+
* session so downstream handlers can use it without resolving it again
|
|
29
|
+
* (null when auth is disabled or the route is public).
|
|
30
|
+
*/
|
|
31
|
+
export type ServerAuthGuardDecision =
|
|
32
|
+
| { readonly kind: "deny"; readonly response: Response }
|
|
33
|
+
| { readonly kind: "allow"; readonly session: AuthenticatedRequestAuthSession | null }
|
|
34
|
+
|
|
35
|
+
export class ServerAuthGuard {
|
|
36
|
+
private readonly sixb: Sixb<readonly OntologySource[]>
|
|
37
|
+
private readonly resolveAuthContext: ResolveRequestAuthContext
|
|
38
|
+
|
|
39
|
+
constructor(options: ServerAuthGuardOptions) {
|
|
40
|
+
this.sixb = options.sixb
|
|
41
|
+
this.resolveAuthContext = options.resolveAuthContext
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
isAuthEnabled(): boolean {
|
|
45
|
+
return this.sixb.auth.isEnabled()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
assertCanServeHttp(params: { readonly production: boolean }): void {
|
|
49
|
+
this.sixb.auth.assertCanServeHttp(params)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async resolve(request: Request): Promise<ServerAuthGuardDecision> {
|
|
53
|
+
if (!this.isAuthEnabled()) {
|
|
54
|
+
return { kind: "allow", session: null }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const route = classifyRoute(request)
|
|
58
|
+
if (route.kind === "public") {
|
|
59
|
+
return { kind: "allow", session: null }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const authContext = this.tryResolveAuthContext(request)
|
|
63
|
+
if (authContext instanceof Response) {
|
|
64
|
+
return { kind: "deny", response: authContext }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let session = await this.sixb.auth.getSession(request, {
|
|
68
|
+
audience: authContext.audience,
|
|
69
|
+
credentialSource: "any",
|
|
70
|
+
})
|
|
71
|
+
if (!session.authenticated) {
|
|
72
|
+
if (route.kind === "html") {
|
|
73
|
+
return {
|
|
74
|
+
kind: "deny",
|
|
75
|
+
response: htmlAuthRedirectResponse(request, {
|
|
76
|
+
absoluteReturnTo: authContext.absoluteReturnTo ?? false,
|
|
77
|
+
audience: authContext.audience,
|
|
78
|
+
}),
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (route.kind === "websocket") {
|
|
83
|
+
return { kind: "deny", response: websocketAuthFailedResponse() }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return { kind: "deny", response: jsonAuthRequiredResponse() }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!sessionCanAccessApplication(this.sixb, session, authContext.audience)) {
|
|
90
|
+
return {
|
|
91
|
+
kind: "deny",
|
|
92
|
+
response: jsonForbiddenResponse("Application access is not allowed"),
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (session.credentialSource === "accessToken" && !isAccessTokenRoute(request)) {
|
|
97
|
+
return {
|
|
98
|
+
kind: "deny",
|
|
99
|
+
response: jsonForbiddenResponse("Access tokens cannot authenticate this route"),
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (
|
|
104
|
+
shouldVerifyCsrfForAuthSource(route, session.credentialSource) &&
|
|
105
|
+
!this.verifyCsrf(request, session)
|
|
106
|
+
) {
|
|
107
|
+
return { kind: "deny", response: jsonCsrfFailedResponse() }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (
|
|
111
|
+
session.credentialSource === "session" &&
|
|
112
|
+
route.kind !== "websocket" &&
|
|
113
|
+
!isSessionTerminationRequest(request) &&
|
|
114
|
+
hasForegroundSessionActivity(request)
|
|
115
|
+
) {
|
|
116
|
+
const activeSession = await this.sixb.auth.getSession(request, {
|
|
117
|
+
audience: authContext.audience,
|
|
118
|
+
credentialSource: "session",
|
|
119
|
+
sessionActivity: "foreground",
|
|
120
|
+
})
|
|
121
|
+
if (!activeSession.authenticated) {
|
|
122
|
+
if (route.kind === "html") {
|
|
123
|
+
return {
|
|
124
|
+
kind: "deny",
|
|
125
|
+
response: htmlAuthRedirectResponse(request, {
|
|
126
|
+
absoluteReturnTo: authContext.absoluteReturnTo ?? false,
|
|
127
|
+
audience: authContext.audience,
|
|
128
|
+
}),
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return { kind: "deny", response: jsonAuthRequiredResponse() }
|
|
132
|
+
}
|
|
133
|
+
if (!sessionCanAccessApplication(this.sixb, activeSession, authContext.audience)) {
|
|
134
|
+
return {
|
|
135
|
+
kind: "deny",
|
|
136
|
+
response: jsonForbiddenResponse("Application access is not allowed"),
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
session = activeSession
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return { kind: "allow", session }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
getCsrfCookieName(request: Request): string {
|
|
146
|
+
const audience = this.resolveAuthContext(request).audience
|
|
147
|
+
return this.sixb.auth.getCookieOptions({ audience }).csrfCookieName
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
verifyCsrf(request: Request, _session: AuthenticatedRequestAuthSession): boolean {
|
|
151
|
+
return verifyDoubleSubmitCsrf(request, {
|
|
152
|
+
cookieName: this.getCsrfCookieName(request),
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private tryResolveAuthContext(
|
|
157
|
+
request: Request
|
|
158
|
+
): ReturnType<ResolveRequestAuthContext> | Response {
|
|
159
|
+
try {
|
|
160
|
+
return this.resolveAuthContext(request)
|
|
161
|
+
} catch (error) {
|
|
162
|
+
if (error instanceof BrowserOriginError) {
|
|
163
|
+
return jsonForbiddenResponse("Browser origin is not allowed")
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
throw error
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { AGENT_API_GATEWAY_PREFIX } from "@sixb/core/internal/agents"
|
|
2
|
+
import { isCsrfExemptMethod } from "@sixb/core/internal/auth"
|
|
3
|
+
|
|
4
|
+
export type RouteAccessKind = "public" | "api" | "html" | "websocket"
|
|
5
|
+
|
|
6
|
+
export interface RouteAccess {
|
|
7
|
+
readonly kind: RouteAccessKind
|
|
8
|
+
readonly csrfProtected: boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function classifyRoute(request: Request): RouteAccess {
|
|
12
|
+
const url = new URL(request.url)
|
|
13
|
+
const { pathname } = url
|
|
14
|
+
|
|
15
|
+
if (isPublicRoute(pathname, request.method)) {
|
|
16
|
+
return { kind: "public", csrfProtected: false }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (pathname.startsWith("/ws/")) {
|
|
20
|
+
return { kind: "websocket", csrfProtected: false }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (pathname.startsWith("/api/")) {
|
|
24
|
+
return { kind: "api", csrfProtected: !isCsrfExemptMethod(request.method) }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (pathname === "/docs" || pathname.startsWith("/docs/")) {
|
|
28
|
+
return { kind: "html", csrfProtected: !isCsrfExemptMethod(request.method) }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Unreachable today — every registered route matches a branch above, and an unregistered
|
|
32
|
+
// path 404s before these hooks run. It defaults to `api` so the next route mounted outside
|
|
33
|
+
// those prefixes is protected until someone puts it on the allow-list, not the reverse.
|
|
34
|
+
return { kind: "api", csrfProtected: !isCsrfExemptMethod(request.method) }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function isPublicRoute(pathname: string, method: string): boolean {
|
|
38
|
+
const normalizedMethod = method.toUpperCase()
|
|
39
|
+
|
|
40
|
+
if (normalizedMethod === "OPTIONS") {
|
|
41
|
+
return true
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if ((pathname === "/favicon.svg" || pathname === "/favicon.ico") && normalizedMethod === "GET") {
|
|
45
|
+
return true
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if ((pathname === "/health" || pathname === "/ready") && normalizedMethod === "GET") {
|
|
49
|
+
return true
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// The gateway authenticates each request with the run's own execution token.
|
|
53
|
+
if (pathname.startsWith(`${AGENT_API_GATEWAY_PREFIX}/`)) {
|
|
54
|
+
return true
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Public because a webhook cannot carry a session: the signature its connector
|
|
58
|
+
// verifies is the credential. POST only — `WebhookDefinition.method` is the literal
|
|
59
|
+
// `"POST"`, so nothing else is ever registered here, and the allow-list should not be
|
|
60
|
+
// wider than what it allows.
|
|
61
|
+
if (pathname.startsWith("/api/webhooks/") && normalizedMethod === "POST") {
|
|
62
|
+
return true
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (pathname === "/api/auth/session" && normalizedMethod === "GET") {
|
|
66
|
+
return true
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (pathname === "/api/auth/sign-out" && normalizedMethod === "POST") {
|
|
70
|
+
return true
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (pathname === "/auth/sign-in" && (normalizedMethod === "GET" || normalizedMethod === "POST")) {
|
|
74
|
+
return true
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// GET renders the magic-link confirmation page; POST consumes the emailed
|
|
78
|
+
// token. The token itself is the credential, and no CSRF cookie exists yet.
|
|
79
|
+
if (
|
|
80
|
+
pathname === "/auth/callback" &&
|
|
81
|
+
(normalizedMethod === "GET" || normalizedMethod === "POST")
|
|
82
|
+
) {
|
|
83
|
+
return true
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return false
|
|
87
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { AuthSessionAudience } from "@sixb/core"
|
|
2
|
+
import { returnToForRequest } from "./return-to"
|
|
3
|
+
|
|
4
|
+
export function jsonAuthRequiredResponse(): Response {
|
|
5
|
+
return jsonResponse({ error: "Authentication required" }, 401)
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function jsonCsrfFailedResponse(): Response {
|
|
9
|
+
return jsonResponse({ error: "CSRF verification failed" }, 403)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function jsonForbiddenResponse(message = "Forbidden"): Response {
|
|
13
|
+
return jsonResponse({ error: message }, 403)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function htmlAuthRedirectResponse(
|
|
17
|
+
request: Request,
|
|
18
|
+
options: {
|
|
19
|
+
readonly absoluteReturnTo?: boolean
|
|
20
|
+
readonly audience?: AuthSessionAudience
|
|
21
|
+
} = {}
|
|
22
|
+
): Response {
|
|
23
|
+
const requestUrl = new URL(request.url)
|
|
24
|
+
const rawReturnTo = options.absoluteReturnTo
|
|
25
|
+
? new URL(returnToForRequest(request), requestUrl.origin).toString()
|
|
26
|
+
: returnToForRequest(request)
|
|
27
|
+
const params = new URLSearchParams({ returnTo: rawReturnTo })
|
|
28
|
+
if (options.absoluteReturnTo && options.audience) {
|
|
29
|
+
params.set("audience", options.audience)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return new Response(null, {
|
|
33
|
+
status: 302,
|
|
34
|
+
headers: {
|
|
35
|
+
location: `/auth/sign-in?${params.toString()}`,
|
|
36
|
+
"cache-control": "no-store",
|
|
37
|
+
},
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function websocketAuthFailedResponse(): Response {
|
|
42
|
+
return jsonAuthRequiredResponse()
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function jsonResponse(body: unknown, status = 200, headers?: HeadersInit): Response {
|
|
46
|
+
return new Response(JSON.stringify(body), {
|
|
47
|
+
status,
|
|
48
|
+
headers: {
|
|
49
|
+
"content-type": "application/json; charset=utf-8",
|
|
50
|
+
"cache-control": "no-store",
|
|
51
|
+
...headers,
|
|
52
|
+
},
|
|
53
|
+
})
|
|
54
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export function sanitizeReturnTo(value: string | null | undefined): string {
|
|
2
|
+
if (!value || !value.startsWith("/") || value.startsWith("//")) {
|
|
3
|
+
return "/"
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
try {
|
|
7
|
+
const parsed = new URL(value, "http://sixb.local")
|
|
8
|
+
if (parsed.origin !== "http://sixb.local") {
|
|
9
|
+
return "/"
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return `${parsed.pathname}${parsed.search}${parsed.hash}`
|
|
13
|
+
} catch {
|
|
14
|
+
return "/"
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function returnToForRequest(request: Request): string {
|
|
19
|
+
const url = new URL(request.url)
|
|
20
|
+
return sanitizeReturnTo(`${url.pathname}${url.search}`)
|
|
21
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { AuthorizationContext, OntologySource, ScopedSixb } from "@sixb/core"
|
|
2
|
+
import type { AgentRunRecord } from "@sixb/core/storage"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Per-request authorization state attached by the server's auth derive.
|
|
6
|
+
*
|
|
7
|
+
* Both fields are null when auth is disabled or the route is public — those
|
|
8
|
+
* requests run against the privileged runtime, preserving authenticated-only
|
|
9
|
+
* behavior for surfaces that are not grant-enforced yet.
|
|
10
|
+
*
|
|
11
|
+
* SECURITY — privileged is the silent default. The core runtime treats a
|
|
12
|
+
* missing authorization context as fully privileged (workers, syncs, tests
|
|
13
|
+
* need that). A scoped principal therefore gains no protection unless the route
|
|
14
|
+
* actively routes through `scoped`. Concretely:
|
|
15
|
+
*
|
|
16
|
+
* - A grant-enforced handler MUST use `scoped` (falling back to the raw
|
|
17
|
+
* `sixb` only when `scoped` is null). That now covers writes as well as
|
|
18
|
+
* reads: object, link, and telemetry writes enforce `edit:object` and
|
|
19
|
+
* `append:telemetry`. Using the raw `sixb` on a path an authenticated
|
|
20
|
+
* principal can reach is a silent god-mode bypass — it will not fail to
|
|
21
|
+
* compile or to run.
|
|
22
|
+
* - Surfaces with no grant family yet (auth admin, raw storage) stay
|
|
23
|
+
* authenticated-only by design and use the raw `sixb` deliberately, not by
|
|
24
|
+
* omission. Add such a route consciously, and lock it down when its grant
|
|
25
|
+
* family lands.
|
|
26
|
+
*
|
|
27
|
+
* This implicit default is a known V1 trade-off (kept for runtime ergonomics);
|
|
28
|
+
* if it proves error-prone, make privileged access explicit at this boundary.
|
|
29
|
+
*/
|
|
30
|
+
export interface RequestAuthState {
|
|
31
|
+
readonly authz: AuthorizationContext | null
|
|
32
|
+
readonly scoped: ScopedSixb<readonly OntologySource[]> | null
|
|
33
|
+
/** Present for requests proxied through the run-scoped agent API gateway. */
|
|
34
|
+
readonly agentRun?: AgentRunRecord
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const internalRequestAuthState = new WeakMap<Request, RequestAuthState>()
|
|
38
|
+
|
|
39
|
+
export function registerInternalRequestAuthState(
|
|
40
|
+
request: Request,
|
|
41
|
+
authState: RequestAuthState
|
|
42
|
+
): void {
|
|
43
|
+
internalRequestAuthState.set(request, authState)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function consumeInternalRequestAuthState(request: Request): RequestAuthState | undefined {
|
|
47
|
+
const authState = internalRequestAuthState.get(request)
|
|
48
|
+
if (authState) {
|
|
49
|
+
internalRequestAuthState.delete(request)
|
|
50
|
+
}
|
|
51
|
+
return authState
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Read the derived auth state from a route handler's context.
|
|
56
|
+
*
|
|
57
|
+
* Route registrars type `app` as plain `Elysia`, so the derived properties are
|
|
58
|
+
* not visible to handler signatures; this helper is the single typed access
|
|
59
|
+
* point until route registration carries the derived context type.
|
|
60
|
+
*/
|
|
61
|
+
export function requestAuthState(context: unknown): RequestAuthState {
|
|
62
|
+
const { authz = null, scoped = null, agentRun } = context as Partial<RequestAuthState>
|
|
63
|
+
return { authz, scoped, ...(agentRun === undefined ? {} : { agentRun }) }
|
|
64
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const SESSION_ACTIVITY_HEADER_NAME = "x-sixb-session-activity"
|
|
2
|
+
const SESSION_ACTIVITY_HEADER_VALUE = "1"
|
|
3
|
+
|
|
4
|
+
export function hasForegroundSessionActivity(request: Request): boolean {
|
|
5
|
+
return request.headers.get(SESSION_ACTIVITY_HEADER_NAME) === SESSION_ACTIVITY_HEADER_VALUE
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function isSessionTerminationRequest(request: Request): boolean {
|
|
9
|
+
if (request.method.toUpperCase() !== "POST") {
|
|
10
|
+
return false
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const pathname = new URL(request.url).pathname
|
|
14
|
+
return (
|
|
15
|
+
pathname === "/api/auth/sign-out" ||
|
|
16
|
+
pathname === "/api/auth/sign-out-all" ||
|
|
17
|
+
/^\/api\/auth\/sessions\/[^/]+\/revoke$/.test(pathname)
|
|
18
|
+
)
|
|
19
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { OntologySource, Sixb } from "@sixb/core"
|
|
2
|
+
import {
|
|
3
|
+
type AuthenticatedAuthSession,
|
|
4
|
+
createCsrfCookieHeader,
|
|
5
|
+
createSessionCookieHeader,
|
|
6
|
+
generateCsrfToken,
|
|
7
|
+
getCookie,
|
|
8
|
+
} from "@sixb/core/internal/auth"
|
|
9
|
+
|
|
10
|
+
export function createSessionRenewalCookieHeaders(input: {
|
|
11
|
+
readonly sixb: Sixb<readonly OntologySource[]>
|
|
12
|
+
readonly request: Request
|
|
13
|
+
readonly session: AuthenticatedAuthSession
|
|
14
|
+
readonly csrfToken?: string
|
|
15
|
+
}): { readonly csrfToken: string; readonly headers: readonly [string, string] } | null {
|
|
16
|
+
const cookieOptions = input.sixb.auth.getCookieOptions({
|
|
17
|
+
audience: input.session.session.audience,
|
|
18
|
+
})
|
|
19
|
+
const sessionValue = getCookie(input.request, cookieOptions.sessionCookieName)
|
|
20
|
+
if (!sessionValue) {
|
|
21
|
+
return null
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const csrfToken =
|
|
25
|
+
input.csrfToken ?? getCookie(input.request, cookieOptions.csrfCookieName) ?? generateCsrfToken()
|
|
26
|
+
return {
|
|
27
|
+
csrfToken,
|
|
28
|
+
headers: [
|
|
29
|
+
createSessionCookieHeader({
|
|
30
|
+
request: input.request,
|
|
31
|
+
value: sessionValue,
|
|
32
|
+
expiresAt: input.session.session.expiresAt,
|
|
33
|
+
options: cookieOptions,
|
|
34
|
+
}),
|
|
35
|
+
createCsrfCookieHeader({
|
|
36
|
+
request: input.request,
|
|
37
|
+
value: csrfToken,
|
|
38
|
+
expiresAt: input.session.session.expiresAt,
|
|
39
|
+
options: cookieOptions,
|
|
40
|
+
}),
|
|
41
|
+
],
|
|
42
|
+
}
|
|
43
|
+
}
|