@sixb/server 0.1.7 → 0.1.8
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/dist/auth/access-token-boundary.d.ts +3 -0
- package/dist/auth/access-token-boundary.d.ts.map +1 -1
- package/dist/auth/public-routes.d.ts.map +1 -1
- package/dist/auth/shared-access.d.ts +66 -0
- package/dist/auth/shared-access.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1419 -627
- package/dist/index.js.map +19 -14
- package/dist/openapi/security.d.ts +15 -1
- package/dist/openapi/security.d.ts.map +1 -1
- package/dist/openapi/tags.d.ts +6 -2
- package/dist/openapi/tags.d.ts.map +1 -1
- package/dist/registerRoutes.d.ts +2 -0
- package/dist/registerRoutes.d.ts.map +1 -1
- package/dist/routes/actions.d.ts.map +1 -1
- package/dist/routes/agents.d.ts +5 -5
- package/dist/routes/auth.d.ts +9 -9
- package/dist/routes/connector-connection-runs.d.ts +8 -8
- package/dist/routes/connector-connections.d.ts +4 -4
- package/dist/routes/datasets.d.ts +5 -3
- package/dist/routes/datasets.d.ts.map +1 -1
- package/dist/routes/logs.d.ts +1 -1
- package/dist/routes/pipelines.d.ts +4 -0
- package/dist/routes/pipelines.d.ts.map +1 -1
- package/dist/routes/share-grants.d.ts +264 -0
- package/dist/routes/share-grants.d.ts.map +1 -0
- package/dist/routes/shared-access.d.ts +155 -0
- package/dist/routes/shared-access.d.ts.map +1 -0
- package/dist/routes/syncs.d.ts +2 -0
- package/dist/routes/syncs.d.ts.map +1 -1
- package/dist/routes/workflows.d.ts +1 -1
- package/dist/schemas/agents.d.ts +22 -30
- package/dist/schemas/agents.d.ts.map +1 -1
- package/dist/schemas/auth.d.ts +40 -40
- package/dist/schemas/datasets.d.ts +28 -22
- package/dist/schemas/datasets.d.ts.map +1 -1
- package/dist/schemas/logs.d.ts +6 -6
- package/dist/schemas/pipelines.d.ts +36 -0
- package/dist/schemas/pipelines.d.ts.map +1 -1
- package/dist/schemas/share-grants.d.ts +518 -0
- package/dist/schemas/share-grants.d.ts.map +1 -0
- package/dist/schemas/shared-access.d.ts +42 -0
- package/dist/schemas/shared-access.d.ts.map +1 -0
- package/dist/schemas/syncs.d.ts +7 -0
- package/dist/schemas/syncs.d.ts.map +1 -1
- package/dist/schemas/workflows.d.ts +4 -4
- package/dist/server.d.ts +5 -0
- package/dist/server.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/auth/access-token-boundary.ts +17 -2
- package/src/auth/public-routes.ts +5 -0
- package/src/auth/shared-access.ts +376 -0
- package/src/index.ts +1 -0
- package/src/openapi/security.ts +20 -1
- package/src/openapi/tags.ts +4 -0
- package/src/registerRoutes.ts +6 -0
- package/src/routes/actions.ts +5 -10
- package/src/routes/agent-api-gateway.ts +1 -1
- package/src/routes/share-grants.ts +238 -0
- package/src/routes/shared-access.ts +165 -0
- package/src/schemas/agents.ts +1 -4
- package/src/schemas/datasets.ts +2 -1
- package/src/schemas/share-grants.ts +109 -0
- package/src/schemas/shared-access.ts +31 -0
- package/src/server.ts +91 -1
package/dist/server.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { SixbHostView } from "@sixb/core";
|
|
|
2
2
|
import { Elysia } from "elysia";
|
|
3
3
|
import { type AuthInvitationDestinationOptions, type AuthInvitationRedirectContext, type AuthInvitationRedirectInput, type RequestAuthContext, type ResolveAuthRedirectContext, type ResolvedSixbApiBrowserPolicy, type SixbApiBrowserPolicy } from "./auth/browser-origin";
|
|
4
4
|
import type { SixbAuthExperienceOptions } from "./auth/experience";
|
|
5
|
+
import { type SixbSharedAccessOptions } from "./auth/shared-access";
|
|
5
6
|
export interface SixbServerOptions {
|
|
6
7
|
host: SixbHostView;
|
|
7
8
|
port?: number;
|
|
@@ -10,6 +11,8 @@ export interface SixbServerOptions {
|
|
|
10
11
|
browser: SixbApiBrowserPolicy;
|
|
11
12
|
/** Optional custom-app auth bundle mounted under the API's `/auth` routes. */
|
|
12
13
|
authExperience?: SixbAuthExperienceOptions;
|
|
14
|
+
/** Optional server limits for short-lived shared-access sessions. */
|
|
15
|
+
sharedAccess?: SixbSharedAccessOptions;
|
|
13
16
|
}
|
|
14
17
|
export declare function createSixbServer(options: SixbServerOptions): SixbServer;
|
|
15
18
|
export declare class SixbServer {
|
|
@@ -21,6 +24,7 @@ export declare class SixbServer {
|
|
|
21
24
|
private readonly authContextResolver;
|
|
22
25
|
private readonly authRedirectContextResolver;
|
|
23
26
|
private readonly authExperience?;
|
|
27
|
+
private readonly sharedAccessOptions;
|
|
24
28
|
private app;
|
|
25
29
|
private bunServer;
|
|
26
30
|
private maintenance;
|
|
@@ -34,6 +38,7 @@ export declare class SixbServer {
|
|
|
34
38
|
resolveInvitationRedirectContext(request: Request, input: AuthInvitationRedirectInput): AuthInvitationRedirectContext;
|
|
35
39
|
getApiBrowserPolicy(): ResolvedSixbApiBrowserPolicy;
|
|
36
40
|
getAuthExperience(): SixbAuthExperienceOptions | undefined;
|
|
41
|
+
getSharedAccessOptions(): SixbSharedAccessOptions;
|
|
37
42
|
start(): Promise<void>;
|
|
38
43
|
stop(): Promise<void>;
|
|
39
44
|
}
|
package/dist/server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAA6B,YAAY,EAAE,MAAM,YAAY,CAAA;AAGzE,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAG/B,OAAO,EACL,KAAK,gCAAgC,EACrC,KAAK,6BAA6B,EAClC,KAAK,2BAA2B,EAMhC,KAAK,kBAAkB,EACvB,KAAK,0BAA0B,EAC/B,KAAK,4BAA4B,EAKjC,KAAK,oBAAoB,EAC1B,MAAM,uBAAuB,CAAA;AAE9B,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAA6B,YAAY,EAAE,MAAM,YAAY,CAAA;AAGzE,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAG/B,OAAO,EACL,KAAK,gCAAgC,EACrC,KAAK,6BAA6B,EAClC,KAAK,2BAA2B,EAMhC,KAAK,kBAAkB,EACvB,KAAK,0BAA0B,EAC/B,KAAK,4BAA4B,EAKjC,KAAK,oBAAoB,EAC1B,MAAM,uBAAuB,CAAA;AAE9B,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,mBAAmB,CAAA;AAKlE,OAAO,EAGL,KAAK,uBAAuB,EAC7B,MAAM,sBAAsB,CAAA;AAiB7B,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,YAAY,CAAA;IAClB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,OAAO,EAAE,oBAAoB,CAAA;IAC7B,8EAA8E;IAC9E,cAAc,CAAC,EAAE,yBAAyB,CAAA;IAC1C,qEAAqE;IACrE,YAAY,CAAC,EAAE,uBAAuB,CAAA;CACvC;AAED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,iBAAiB,GAAG,UAAU,CAEvE;AAED,qBAAa,UAAU;IACrB,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAc;IAC1C,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAQ;IAC7B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAQ;IACjC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAA8B;IAC/D,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAA2B;IAC/D,OAAO,CAAC,QAAQ,CAAC,2BAA2B,CAA4B;IACxE,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,CAA2B;IAC3D,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAyB;IAC7D,OAAO,CAAC,GAAG,CAAuB;IAClC,OAAO,CAAC,SAAS,CAA4C;IAC7D,OAAO,CAAC,WAAW,CAAyC;gBAEhD,OAAO,EAAE,iBAAiB;IActC,OAAO,IAAI,YAAY;IAIvB,OAAO,IAAI,MAAM;IAIjB,kBAAkB,CAAC,OAAO,EAAE,OAAO,GAAG,kBAAkB;IAIxD,0BAA0B,CACxB,OAAO,EAAE,UAAU,CAAC,0BAA0B,CAAC,CAAC,CAAC,CAAC,EAClD,KAAK,EAAE,UAAU,CAAC,0BAA0B,CAAC,CAAC,CAAC,CAAC,GAC/C,UAAU,CAAC,0BAA0B,CAAC;IAIzC,wBAAwB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM;IAIlD,+BAA+B,CAAC,QAAQ,EAAE,OAAO,GAAG,gCAAgC;IAIpF,gCAAgC,CAC9B,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,2BAA2B,GACjC,6BAA6B;IAIhC,mBAAmB,IAAI,4BAA4B;IAInD,iBAAiB,IAAI,yBAAyB,GAAG,SAAS;IAI1D,sBAAsB,IAAI,uBAAuB;IAI3C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IA8BtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAe5B;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4O/C;AAiED,MAAM,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sixb/server",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "HTTP and WebSocket API server for Sixb",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sixb",
|
|
@@ -37,13 +37,13 @@
|
|
|
37
37
|
"zod-to-json-schema": "^3.24.0"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"@sixb/core": "0.1.
|
|
40
|
+
"@sixb/core": "0.1.8"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@sixb/auth-magic-link": "0.1.3",
|
|
44
44
|
"@sixb/auth-oidc": "0.1.2",
|
|
45
|
-
"@sixb/core": "0.1.
|
|
46
|
-
"@sixb/sqlite": "0.1.
|
|
45
|
+
"@sixb/core": "0.1.8",
|
|
46
|
+
"@sixb/sqlite": "0.1.8",
|
|
47
47
|
"@types/bun": "latest",
|
|
48
48
|
"typescript": "^5.7.0"
|
|
49
49
|
},
|
|
@@ -3,6 +3,8 @@ import { matchesPathPattern, normalizeRoutePath, SIXB_API_ROUTES } from "@sixb/c
|
|
|
3
3
|
import {
|
|
4
4
|
SIXB_BEARER_SECURITY_REQUIREMENT,
|
|
5
5
|
SIXB_CSRF_OR_BEARER_SECURITY_REQUIREMENT,
|
|
6
|
+
SIXB_SHARED_MUTATION_SECURITY_REQUIREMENT,
|
|
7
|
+
SIXB_SHARED_READ_SECURITY_REQUIREMENT,
|
|
6
8
|
} from "../openapi/security"
|
|
7
9
|
import type { RouteAccess } from "./public-routes"
|
|
8
10
|
|
|
@@ -12,6 +14,7 @@ export interface AccessTokenRoute {
|
|
|
12
14
|
readonly operationId: string
|
|
13
15
|
readonly method: string
|
|
14
16
|
readonly path: string
|
|
17
|
+
readonly sharedSession: boolean
|
|
15
18
|
}
|
|
16
19
|
|
|
17
20
|
// The routes that accept bearer access tokens: the `accessToken` projection of the canonical
|
|
@@ -26,7 +29,12 @@ export interface AccessTokenRoute {
|
|
|
26
29
|
// from SIXB_API_ROUTES.
|
|
27
30
|
export const ACCESS_TOKEN_ROUTES: readonly AccessTokenRoute[] = SIXB_API_ROUTES.filter(
|
|
28
31
|
(route) => route.accessToken
|
|
29
|
-
).map((route) => ({
|
|
32
|
+
).map((route) => ({
|
|
33
|
+
operationId: route.operationId,
|
|
34
|
+
method: route.method,
|
|
35
|
+
path: route.path,
|
|
36
|
+
sharedSession: route.sharedSession ?? false,
|
|
37
|
+
}))
|
|
30
38
|
|
|
31
39
|
/**
|
|
32
40
|
* OpenAPI security requirement for a bearer-capable route, derived from the
|
|
@@ -43,9 +51,16 @@ export function bearerSecurityRequirement(operationId: string) {
|
|
|
43
51
|
)
|
|
44
52
|
}
|
|
45
53
|
|
|
46
|
-
|
|
54
|
+
const standard = isCsrfExemptMethod(route.method)
|
|
47
55
|
? SIXB_BEARER_SECURITY_REQUIREMENT
|
|
48
56
|
: SIXB_CSRF_OR_BEARER_SECURITY_REQUIREMENT
|
|
57
|
+
if (!route.sharedSession) return standard
|
|
58
|
+
return [
|
|
59
|
+
...standard,
|
|
60
|
+
isCsrfExemptMethod(route.method)
|
|
61
|
+
? SIXB_SHARED_READ_SECURITY_REQUIREMENT
|
|
62
|
+
: SIXB_SHARED_MUTATION_SECURITY_REQUIREMENT,
|
|
63
|
+
]
|
|
49
64
|
}
|
|
50
65
|
|
|
51
66
|
export function isAccessTokenRoute(request: Request): boolean {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AGENT_API_GATEWAY_PREFIX } from "@sixb/core/internal/agents"
|
|
2
2
|
import { isCsrfExemptMethod } from "@sixb/core/internal/auth"
|
|
3
|
+
import { isSharedAccessPublicPath } from "./shared-access"
|
|
3
4
|
|
|
4
5
|
const CONNECTOR_OAUTH_CALLBACK_PATH = "/auth/connectors/callback"
|
|
5
6
|
|
|
@@ -47,6 +48,10 @@ export function isPublicRoute(pathname: string, method: string): boolean {
|
|
|
47
48
|
return true
|
|
48
49
|
}
|
|
49
50
|
|
|
51
|
+
if (isSharedAccessPublicPath(pathname, method)) {
|
|
52
|
+
return true
|
|
53
|
+
}
|
|
54
|
+
|
|
50
55
|
if ((pathname === "/favicon.svg" || pathname === "/favicon.ico") && normalizedMethod === "GET") {
|
|
51
56
|
return true
|
|
52
57
|
}
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
import { createHash } from "node:crypto"
|
|
2
|
+
import type { SixbHostView } from "@sixb/core"
|
|
3
|
+
import {
|
|
4
|
+
CSRF_HEADER_NAME,
|
|
5
|
+
generateCsrfToken,
|
|
6
|
+
getCookie,
|
|
7
|
+
isCsrfExemptMethod,
|
|
8
|
+
verifyDoubleSubmitCsrf,
|
|
9
|
+
} from "@sixb/core/internal/auth"
|
|
10
|
+
import { matchesPathPattern, normalizeRoutePath, SIXB_API_ROUTES } from "@sixb/core/internal/http"
|
|
11
|
+
import { type SharedSessionContext, SharedSessionProtocol } from "@sixb/core/internal/shares"
|
|
12
|
+
import { hasForegroundSessionActivity } from "./session-activity"
|
|
13
|
+
|
|
14
|
+
export const SHARED_ACCESS_GRANT_HEADER_NAME = "x-sixb-share-grant"
|
|
15
|
+
|
|
16
|
+
const SHARED_SESSION_COOKIE_PREFIX = "sixb_share_session_"
|
|
17
|
+
const SHARED_CSRF_COOKIE_PREFIX = "sixb_share_csrf_"
|
|
18
|
+
const SHARED_COOKIE_PATH = "/api"
|
|
19
|
+
const STORED_IDENTIFIER_MAX_LENGTH = 128
|
|
20
|
+
|
|
21
|
+
const SHARED_SESSION_ROUTES = SIXB_API_ROUTES.filter((route) => route.sharedSession)
|
|
22
|
+
|
|
23
|
+
export interface SixbSharedAccessOptions {
|
|
24
|
+
/** Sliding inactivity window, always bounded by the grant's absolute expiration. */
|
|
25
|
+
readonly inactivityTtlMs?: number
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface SharedAccessCookieHeaders {
|
|
29
|
+
readonly setCookies: readonly string[]
|
|
30
|
+
readonly csrfToken: string
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ResolvedSharedAccess {
|
|
34
|
+
readonly context: SharedSessionContext
|
|
35
|
+
readonly cookies: SharedAccessCookieHeaders
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export type SharedAccessRequestDecision =
|
|
39
|
+
| { readonly kind: "absent" }
|
|
40
|
+
| { readonly kind: "deny"; readonly response: Response }
|
|
41
|
+
| ({ readonly kind: "allow" } & ResolvedSharedAccess)
|
|
42
|
+
|
|
43
|
+
export type SharedAccessSignOutResult =
|
|
44
|
+
| { readonly kind: "invalid"; readonly setCookies: readonly string[] }
|
|
45
|
+
| { readonly kind: "csrf" }
|
|
46
|
+
| { readonly kind: "signedOut"; readonly setCookies: readonly string[] }
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Selects shared authority before ambient browser or bearer authentication.
|
|
50
|
+
*
|
|
51
|
+
* The selector header is deliberately not a credential. It chooses one grant-specific HttpOnly
|
|
52
|
+
* cookie, allowing several shared pages to coexist without ever merging their authority.
|
|
53
|
+
*/
|
|
54
|
+
export class SharedAccessBoundary {
|
|
55
|
+
private readonly host: SixbHostView
|
|
56
|
+
private readonly options: SixbSharedAccessOptions
|
|
57
|
+
private protocol: SharedSessionProtocol | undefined
|
|
58
|
+
|
|
59
|
+
constructor(host: SixbHostView, options: SixbSharedAccessOptions = {}) {
|
|
60
|
+
this.host = host
|
|
61
|
+
this.options = options
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Fail closed before route parsing can emit a route-specific validation response. */
|
|
65
|
+
preflightResponse(request: Request): Response | undefined {
|
|
66
|
+
if (isSharedAccessPublicRoute(request)) return
|
|
67
|
+
const selectedGrantId = request.headers.get(SHARED_ACCESS_GRANT_HEADER_NAME)
|
|
68
|
+
if (selectedGrantId === null) return
|
|
69
|
+
if (request.headers.has("authorization") || !isSharedSessionRoute(request)) {
|
|
70
|
+
return sharedAccessForbiddenResponse()
|
|
71
|
+
}
|
|
72
|
+
if (!isStoredIdentifier(selectedGrantId)) return sharedAccessUnauthenticatedResponse()
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async resolveRequest(request: Request): Promise<SharedAccessRequestDecision> {
|
|
76
|
+
if (isSharedAccessPublicRoute(request)) return { kind: "absent" }
|
|
77
|
+
|
|
78
|
+
const selectedGrantId = request.headers.get(SHARED_ACCESS_GRANT_HEADER_NAME)
|
|
79
|
+
if (selectedGrantId === null) return { kind: "absent" }
|
|
80
|
+
|
|
81
|
+
const preflight = this.preflightResponse(request)
|
|
82
|
+
if (preflight) return { kind: "deny", response: preflight }
|
|
83
|
+
|
|
84
|
+
let resolved: ResolvedSharedAccess | null
|
|
85
|
+
try {
|
|
86
|
+
resolved = await this.resolveSession(request, selectedGrantId)
|
|
87
|
+
} catch {
|
|
88
|
+
return { kind: "deny", response: sharedAccessUnavailableResponse() }
|
|
89
|
+
}
|
|
90
|
+
if (!resolved) {
|
|
91
|
+
return { kind: "deny", response: sharedAccessUnauthenticatedResponse() }
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (
|
|
95
|
+
!isCsrfExemptMethod(request.method) &&
|
|
96
|
+
!verifyDoubleSubmitCsrf(request, {
|
|
97
|
+
cookieName: sharedCookieNames(selectedGrantId).csrf,
|
|
98
|
+
headerName: CSRF_HEADER_NAME,
|
|
99
|
+
})
|
|
100
|
+
) {
|
|
101
|
+
return { kind: "deny", response: sharedAccessForbiddenResponse() }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return { kind: "allow", ...resolved }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async exchange(
|
|
108
|
+
request: Request,
|
|
109
|
+
grantId: string,
|
|
110
|
+
secret: string
|
|
111
|
+
): Promise<ResolvedSharedAccess | null> {
|
|
112
|
+
if (!isStoredIdentifier(grantId)) return null
|
|
113
|
+
const protocol = this.getProtocol()
|
|
114
|
+
|
|
115
|
+
const credential = await protocol.exchange(grantId, secret)
|
|
116
|
+
if (!credential) return null
|
|
117
|
+
|
|
118
|
+
const csrfToken = generateCsrfToken()
|
|
119
|
+
return {
|
|
120
|
+
context: credential.context,
|
|
121
|
+
cookies: this.createCookieHeaders({
|
|
122
|
+
request,
|
|
123
|
+
grantId,
|
|
124
|
+
sessionValue: credential.cookieValue,
|
|
125
|
+
csrfToken,
|
|
126
|
+
expiresAt: credential.context.expiresAt,
|
|
127
|
+
}),
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async resolveSession(request: Request, grantId: string): Promise<ResolvedSharedAccess | null> {
|
|
132
|
+
if (!isStoredIdentifier(grantId)) return null
|
|
133
|
+
const protocol = this.getProtocol()
|
|
134
|
+
|
|
135
|
+
const names = sharedCookieNames(grantId)
|
|
136
|
+
const sessionValue = getCookie(request, names.session)
|
|
137
|
+
const foreground = hasForegroundSessionActivity(request)
|
|
138
|
+
const context = await protocol.resolve(grantId, sessionValue, {
|
|
139
|
+
activity: foreground ? "foreground" : "background",
|
|
140
|
+
})
|
|
141
|
+
if (!context || !sessionValue) return null
|
|
142
|
+
|
|
143
|
+
const existingCsrfToken = getCookie(request, names.csrf)
|
|
144
|
+
const csrfToken = existingCsrfToken ?? generateCsrfToken()
|
|
145
|
+
return {
|
|
146
|
+
context,
|
|
147
|
+
cookies: foreground
|
|
148
|
+
? this.createCookieHeaders({
|
|
149
|
+
request,
|
|
150
|
+
grantId,
|
|
151
|
+
sessionValue,
|
|
152
|
+
csrfToken,
|
|
153
|
+
expiresAt: context.expiresAt,
|
|
154
|
+
})
|
|
155
|
+
: {
|
|
156
|
+
csrfToken,
|
|
157
|
+
setCookies:
|
|
158
|
+
existingCsrfToken === undefined
|
|
159
|
+
? [
|
|
160
|
+
serializeSharedCookie({
|
|
161
|
+
request,
|
|
162
|
+
name: names.csrf,
|
|
163
|
+
value: csrfToken,
|
|
164
|
+
expiresAt: context.expiresAt,
|
|
165
|
+
maxAge: cookieMaxAge(context.expiresAt),
|
|
166
|
+
}),
|
|
167
|
+
]
|
|
168
|
+
: [],
|
|
169
|
+
},
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async signOut(request: Request, grantId: string): Promise<SharedAccessSignOutResult> {
|
|
174
|
+
if (!isStoredIdentifier(grantId)) {
|
|
175
|
+
return { kind: "invalid", setCookies: [] }
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const clearCookies = this.clearCookieHeaders(request, grantId)
|
|
179
|
+
const resolved = await this.resolveSession(request, grantId)
|
|
180
|
+
if (!resolved) return { kind: "invalid", setCookies: clearCookies }
|
|
181
|
+
|
|
182
|
+
if (
|
|
183
|
+
!verifyDoubleSubmitCsrf(request, {
|
|
184
|
+
cookieName: sharedCookieNames(grantId).csrf,
|
|
185
|
+
headerName: CSRF_HEADER_NAME,
|
|
186
|
+
})
|
|
187
|
+
) {
|
|
188
|
+
return { kind: "csrf" }
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const protocol = this.getProtocol()
|
|
192
|
+
await protocol.revoke(resolved.context)
|
|
193
|
+
return { kind: "signedOut", setCookies: clearCookies }
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
clearCookieHeaders(request: Request, grantId: string): readonly string[] {
|
|
197
|
+
if (!isStoredIdentifier(grantId)) return []
|
|
198
|
+
const names = sharedCookieNames(grantId)
|
|
199
|
+
return [
|
|
200
|
+
serializeSharedCookie({
|
|
201
|
+
request,
|
|
202
|
+
name: names.session,
|
|
203
|
+
value: "",
|
|
204
|
+
expiresAt: new Date(0),
|
|
205
|
+
maxAge: 0,
|
|
206
|
+
httpOnly: true,
|
|
207
|
+
}),
|
|
208
|
+
serializeSharedCookie({
|
|
209
|
+
request,
|
|
210
|
+
name: names.csrf,
|
|
211
|
+
value: "",
|
|
212
|
+
expiresAt: new Date(0),
|
|
213
|
+
maxAge: 0,
|
|
214
|
+
}),
|
|
215
|
+
]
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
private createCookieHeaders(input: {
|
|
219
|
+
readonly request: Request
|
|
220
|
+
readonly grantId: string
|
|
221
|
+
readonly sessionValue: string
|
|
222
|
+
readonly csrfToken: string
|
|
223
|
+
readonly expiresAt: Date
|
|
224
|
+
}): SharedAccessCookieHeaders {
|
|
225
|
+
const names = sharedCookieNames(input.grantId)
|
|
226
|
+
const maxAge = cookieMaxAge(input.expiresAt)
|
|
227
|
+
return {
|
|
228
|
+
csrfToken: input.csrfToken,
|
|
229
|
+
setCookies: [
|
|
230
|
+
serializeSharedCookie({
|
|
231
|
+
request: input.request,
|
|
232
|
+
name: names.session,
|
|
233
|
+
value: input.sessionValue,
|
|
234
|
+
expiresAt: input.expiresAt,
|
|
235
|
+
maxAge,
|
|
236
|
+
httpOnly: true,
|
|
237
|
+
}),
|
|
238
|
+
serializeSharedCookie({
|
|
239
|
+
request: input.request,
|
|
240
|
+
name: names.csrf,
|
|
241
|
+
value: input.csrfToken,
|
|
242
|
+
expiresAt: input.expiresAt,
|
|
243
|
+
maxAge,
|
|
244
|
+
}),
|
|
245
|
+
],
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
private getProtocol(): SharedSessionProtocol {
|
|
250
|
+
if (this.protocol) return this.protocol
|
|
251
|
+
if (!this.host.storage.shareGrants || !this.host.storage.shareSessions) {
|
|
252
|
+
throw new Error("[SixbServer] Shared-access session storage is not configured.")
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
this.protocol = new SharedSessionProtocol({
|
|
256
|
+
host: this.host,
|
|
257
|
+
...(this.options.inactivityTtlMs === undefined
|
|
258
|
+
? {}
|
|
259
|
+
: { inactivityTtlMs: this.options.inactivityTtlMs }),
|
|
260
|
+
})
|
|
261
|
+
return this.protocol
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function isSharedAccessPublicRoute(request: Request): boolean {
|
|
266
|
+
return isSharedAccessPublicPath(new URL(request.url).pathname, request.method)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function isSharedAccessPublicPath(pathname: string, requestMethod: string): boolean {
|
|
270
|
+
const method = requestMethod.toUpperCase()
|
|
271
|
+
const normalizedPathname = normalizeRoutePath(pathname)
|
|
272
|
+
const suffix =
|
|
273
|
+
method === "POST" && normalizedPathname.endsWith("/exchange")
|
|
274
|
+
? "exchange"
|
|
275
|
+
: method === "GET" && normalizedPathname.endsWith("/session")
|
|
276
|
+
? "session"
|
|
277
|
+
: method === "POST" && normalizedPathname.endsWith("/sign-out")
|
|
278
|
+
? "sign-out"
|
|
279
|
+
: null
|
|
280
|
+
if (!suffix) return false
|
|
281
|
+
return matchesPathPattern(normalizedPathname, `/api/shared-access/:grantId/${suffix}`)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export function isSharedSessionRoute(request: Request): boolean {
|
|
285
|
+
const method = request.method.toUpperCase()
|
|
286
|
+
const pathname = normalizeRoutePath(new URL(request.url).pathname)
|
|
287
|
+
return SHARED_SESSION_ROUTES.some(
|
|
288
|
+
(route) => route.method === method && matchesPathPattern(pathname, route.path)
|
|
289
|
+
)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export function sharedAccessCookieNames(grantId: string): {
|
|
293
|
+
readonly session: string
|
|
294
|
+
readonly csrf: string
|
|
295
|
+
} {
|
|
296
|
+
return sharedCookieNames(grantId)
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export function sharedAccessUnauthenticatedResponse(): Response {
|
|
300
|
+
return sharedBoundaryJsonResponse({ error: "Shared access session is invalid" }, 401)
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export function sharedAccessForbiddenResponse(): Response {
|
|
304
|
+
return sharedBoundaryJsonResponse({ error: "Shared access is not allowed for this request" }, 403)
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export function sharedAccessUnavailableResponse(): Response {
|
|
308
|
+
return sharedBoundaryJsonResponse({ error: "Shared access is unavailable" }, 503)
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export function sharedBoundaryJsonResponse(body: unknown, status: number): Response {
|
|
312
|
+
return new Response(JSON.stringify(body), {
|
|
313
|
+
status,
|
|
314
|
+
headers: sharedAccessSecurityHeaders(),
|
|
315
|
+
})
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export function sharedAccessSecurityHeaders(): Headers {
|
|
319
|
+
return new Headers({
|
|
320
|
+
"cache-control": "no-store",
|
|
321
|
+
"content-security-policy": "default-src 'none'; frame-ancestors 'none'",
|
|
322
|
+
"content-type": "application/json; charset=utf-8",
|
|
323
|
+
"referrer-policy": "no-referrer",
|
|
324
|
+
"x-content-type-options": "nosniff",
|
|
325
|
+
"x-robots-tag": "noindex, nofollow",
|
|
326
|
+
})
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function sharedCookieNames(grantId: string): { readonly session: string; readonly csrf: string } {
|
|
330
|
+
const digest = createHash("sha256").update(grantId).digest("hex")
|
|
331
|
+
return {
|
|
332
|
+
session: `${SHARED_SESSION_COOKIE_PREFIX}${digest}`,
|
|
333
|
+
csrf: `${SHARED_CSRF_COOKIE_PREFIX}${digest}`,
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function serializeSharedCookie(input: {
|
|
338
|
+
readonly request: Request
|
|
339
|
+
readonly name: string
|
|
340
|
+
readonly value: string
|
|
341
|
+
readonly expiresAt: Date
|
|
342
|
+
readonly maxAge: number
|
|
343
|
+
readonly httpOnly?: boolean
|
|
344
|
+
}): string {
|
|
345
|
+
const parts = [
|
|
346
|
+
`${input.name}=${input.value}`,
|
|
347
|
+
`Path=${SHARED_COOKIE_PATH}`,
|
|
348
|
+
"SameSite=Strict",
|
|
349
|
+
`Max-Age=${Math.trunc(input.maxAge)}`,
|
|
350
|
+
`Expires=${input.expiresAt.toUTCString()}`,
|
|
351
|
+
]
|
|
352
|
+
if (input.httpOnly) parts.push("HttpOnly")
|
|
353
|
+
if (shouldUseSecureSharedCookie(input.request)) parts.push("Secure")
|
|
354
|
+
return parts.join("; ")
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
function cookieMaxAge(expiresAt: Date): number {
|
|
358
|
+
return Math.max(0, Math.floor((expiresAt.getTime() - Date.now()) / 1_000))
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function shouldUseSecureSharedCookie(request: Request): boolean {
|
|
362
|
+
const url = new URL(request.url)
|
|
363
|
+
if (url.hostname === "localhost" || url.hostname === "127.0.0.1" || url.hostname === "::1") {
|
|
364
|
+
return false
|
|
365
|
+
}
|
|
366
|
+
return url.protocol === "https:" || process.env.NODE_ENV === "production"
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function isStoredIdentifier(value: string): boolean {
|
|
370
|
+
return (
|
|
371
|
+
value.length > 0 &&
|
|
372
|
+
value.length <= STORED_IDENTIFIER_MAX_LENGTH &&
|
|
373
|
+
value.trim() === value &&
|
|
374
|
+
!value.includes("\0")
|
|
375
|
+
)
|
|
376
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export type { SixbApiBrowserPolicy, SixbBrowserOrigin } from "./auth/browser-origin"
|
|
2
2
|
export type { SixbAuthExperienceOptions } from "./auth/experience"
|
|
3
|
+
export type { SixbSharedAccessOptions } from "./auth/shared-access"
|
|
3
4
|
export { createSixbServer, SixbServer, type SixbServerOptions } from "./server"
|
package/src/openapi/security.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { CSRF_HEADER_NAME } from "@sixb/core/internal/auth"
|
|
2
|
+
import { SHARED_ACCESS_GRANT_HEADER_NAME } from "../auth/shared-access"
|
|
2
3
|
|
|
3
4
|
export const SIXB_CSRF_SECURITY_SCHEME_ID = "sixbCsrf"
|
|
4
5
|
export const SIXB_BEARER_SECURITY_SCHEME_ID = "sixbBearer"
|
|
6
|
+
export const SIXB_SHARED_GRANT_SECURITY_SCHEME_ID = "sixbSharedGrant"
|
|
5
7
|
|
|
6
8
|
export const SIXB_CSRF_SECURITY_REQUIREMENT = [
|
|
7
9
|
{
|
|
@@ -20,12 +22,21 @@ export const SIXB_CSRF_OR_BEARER_SECURITY_REQUIREMENT = [
|
|
|
20
22
|
...SIXB_BEARER_SECURITY_REQUIREMENT,
|
|
21
23
|
]
|
|
22
24
|
|
|
25
|
+
export const SIXB_SHARED_READ_SECURITY_REQUIREMENT = {
|
|
26
|
+
[SIXB_SHARED_GRANT_SECURITY_SCHEME_ID]: [] as string[],
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const SIXB_SHARED_MUTATION_SECURITY_REQUIREMENT = {
|
|
30
|
+
[SIXB_SHARED_GRANT_SECURITY_SCHEME_ID]: [] as string[],
|
|
31
|
+
[SIXB_CSRF_SECURITY_SCHEME_ID]: [] as string[],
|
|
32
|
+
}
|
|
33
|
+
|
|
23
34
|
export const SIXB_CSRF_SECURITY_SCHEME = {
|
|
24
35
|
type: "apiKey",
|
|
25
36
|
in: "header",
|
|
26
37
|
name: CSRF_HEADER_NAME,
|
|
27
38
|
description:
|
|
28
|
-
"Required for authenticated mutating requests. Use the csrfToken returned by
|
|
39
|
+
"Required for cookie-authenticated mutating requests. Use the csrfToken returned by the corresponding session endpoint.",
|
|
29
40
|
} as const
|
|
30
41
|
|
|
31
42
|
export const SIXB_BEARER_SECURITY_SCHEME = {
|
|
@@ -35,3 +46,11 @@ export const SIXB_BEARER_SECURITY_SCHEME = {
|
|
|
35
46
|
description:
|
|
36
47
|
"Use a Sixb personal access token or service-account token. Bearer tokens are accepted only on routes that explicitly document this scheme.",
|
|
37
48
|
} as const
|
|
49
|
+
|
|
50
|
+
export const SIXB_SHARED_GRANT_SECURITY_SCHEME = {
|
|
51
|
+
type: "apiKey",
|
|
52
|
+
in: "header",
|
|
53
|
+
name: SHARED_ACCESS_GRANT_HEADER_NAME,
|
|
54
|
+
description:
|
|
55
|
+
"Selects the shared grant whose grant-specific HttpOnly session cookie must authenticate the request.",
|
|
56
|
+
} as const
|
package/src/openapi/tags.ts
CHANGED
|
@@ -159,6 +159,10 @@ export const OPENAPI_TAGS = {
|
|
|
159
159
|
name: "Auth Service Accounts",
|
|
160
160
|
description: "Service accounts and service-account access tokens",
|
|
161
161
|
},
|
|
162
|
+
sharedAccess: {
|
|
163
|
+
name: "Shared Access",
|
|
164
|
+
description: "Scoped shared-access grant lifecycle",
|
|
165
|
+
},
|
|
162
166
|
} as const
|
|
163
167
|
|
|
164
168
|
export const OPENAPI_TAG_METADATA = Object.values(OPENAPI_TAGS).map(({ name, description }) => ({
|
package/src/registerRoutes.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { SixbHostView } from "@sixb/core"
|
|
2
2
|
import type { Elysia } from "elysia"
|
|
3
|
+
import type { SharedAccessBoundary } from "./auth/shared-access"
|
|
3
4
|
import { registerActionRunRoutes } from "./routes/action-runs"
|
|
4
5
|
import { registerActionRoutes } from "./routes/actions"
|
|
5
6
|
import { registerAgentApiGatewayRoutes } from "./routes/agent-api-gateway"
|
|
@@ -23,6 +24,8 @@ import { registerPipelineRoutes } from "./routes/pipelines"
|
|
|
23
24
|
import { registerProjectRoutes } from "./routes/project"
|
|
24
25
|
import { registerProjectionRoutes } from "./routes/projections"
|
|
25
26
|
import { registerRuleRoutes } from "./routes/rules"
|
|
27
|
+
import { registerShareGrantRoutes } from "./routes/share-grants"
|
|
28
|
+
import { registerSharedAccessRoutes } from "./routes/shared-access"
|
|
26
29
|
import { registerStatusRoutes } from "./routes/status"
|
|
27
30
|
import { registerSyncRoutes } from "./routes/syncs"
|
|
28
31
|
import { registerTelemetryRoutes } from "./routes/telemetry"
|
|
@@ -31,6 +34,7 @@ import { registerWorkflowRoutes } from "./routes/workflows"
|
|
|
31
34
|
|
|
32
35
|
export interface HttpRouteOptions {
|
|
33
36
|
readonly connectorConnections: ConnectorConnectionRouteOptions
|
|
37
|
+
readonly sharedAccess: SharedAccessBoundary
|
|
34
38
|
}
|
|
35
39
|
|
|
36
40
|
export function registerHttpRoutes(app: Elysia, host: SixbHostView, options: HttpRouteOptions) {
|
|
@@ -46,6 +50,8 @@ export function registerHttpRoutes(app: Elysia, host: SixbHostView, options: Htt
|
|
|
46
50
|
registerPipelineRoutes(app, host)
|
|
47
51
|
registerWorkflowRoutes(app, host)
|
|
48
52
|
registerRuleRoutes(app, host)
|
|
53
|
+
registerShareGrantRoutes(app, host)
|
|
54
|
+
registerSharedAccessRoutes(app, options.sharedAccess)
|
|
49
55
|
registerOntologyRoutes(app, host)
|
|
50
56
|
registerObjectRoutes(app, host)
|
|
51
57
|
registerActionRoutes(app, host)
|
package/src/routes/actions.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ActionDescriptor, SixbHostView } from "@sixb/core"
|
|
2
2
|
import { schemaFieldsToJsonSchema } from "@sixb/core/internal/ontology"
|
|
3
3
|
import type { Elysia } from "elysia"
|
|
4
4
|
import { bearerSecurityRequirement } from "../auth/access-token-boundary"
|
|
@@ -15,13 +15,13 @@ import { ErrorResponseSchema } from "../schemas/common"
|
|
|
15
15
|
import { handleRouteError } from "../utils/http"
|
|
16
16
|
|
|
17
17
|
function serializeAction(
|
|
18
|
-
action:
|
|
18
|
+
action: ActionDescriptor
|
|
19
19
|
): ReturnType<typeof ActionCatalogItemSchema.parse> {
|
|
20
20
|
return ActionCatalogItemSchema.parse({
|
|
21
21
|
id: action.id,
|
|
22
22
|
name: action.id,
|
|
23
23
|
description: action.description,
|
|
24
|
-
...(action.binding.kind === "object" ? { objectTypeId: action.binding.
|
|
24
|
+
...(action.binding.kind === "object" ? { objectTypeId: action.binding.objectTypeId } : {}),
|
|
25
25
|
params: Object.entries(action.params).map(([id, config]) => ({
|
|
26
26
|
id,
|
|
27
27
|
name: id,
|
|
@@ -31,17 +31,12 @@ function serializeAction(
|
|
|
31
31
|
description: config.description,
|
|
32
32
|
semanticType: config.semanticType,
|
|
33
33
|
})),
|
|
34
|
-
phases:
|
|
35
|
-
validate: action.phases.validate.length > 0,
|
|
36
|
-
writeback: action.phases.writeback !== undefined,
|
|
37
|
-
edits: action.phases.edits !== undefined,
|
|
38
|
-
effects: action.phases.effects !== undefined,
|
|
39
|
-
},
|
|
34
|
+
phases: action.phases,
|
|
40
35
|
})
|
|
41
36
|
}
|
|
42
37
|
|
|
43
38
|
function serializeActionDetail(
|
|
44
|
-
action:
|
|
39
|
+
action: ActionDescriptor,
|
|
45
40
|
host: SixbHostView
|
|
46
41
|
): ReturnType<typeof ActionDetailSchema.parse> {
|
|
47
42
|
return ActionDetailSchema.parse({
|
|
@@ -291,7 +291,7 @@ async function resolveGatewayRunAuthorization(input: {
|
|
|
291
291
|
function inheritedRequestAuthorization(
|
|
292
292
|
resolved: AgentExecutionAuthorization,
|
|
293
293
|
authorizationRef: ExecutionRecord["authorizationRef"]
|
|
294
|
-
): RequestExecutionAuthorization {
|
|
294
|
+
): Extract<RequestExecutionAuthorization, { readonly type: "principal" | "disabled" }> {
|
|
295
295
|
if (
|
|
296
296
|
resolved.type !== "principal" ||
|
|
297
297
|
authorizationRef.type !== "principal" ||
|