@hugex/artifact 0.1.2 → 0.2.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.
@@ -1,7 +1,9 @@
1
1
  /** Entire header value. No lowercase scheme, no extra whitespace, base64url segments only. */
2
2
  export declare const BEARER_RE: RegExp;
3
- export declare const WWW_AUTHENTICATE = "Bearer realm=\"hugex-artifact\"";
3
+ export declare const ANY_BEARER_RE: RegExp;
4
+ export declare const WWW_AUTHENTICATE = "Bearer realm=\"hugex\"";
4
5
  export interface ArtifactIdentity {
6
+ sub?: string;
5
7
  email: string;
6
8
  org: string;
7
9
  tenant: string;
@@ -10,19 +12,25 @@ export interface AuthEnv {
10
12
  TENANT: string;
11
13
  /** Keycloak RS256 public key: base64 SPKI body, no PEM armor. */
12
14
  KC_RS256_PK: string;
15
+ KC_BASE_URL?: string;
16
+ KC_CLIENT_ID?: string;
17
+ MCP_KC_ISSUER?: string;
18
+ MCP_LEGACY_ALLOWED_AZP?: string;
19
+ MCP_LEGACY_JWT?: string;
13
20
  }
14
21
  export declare function parseBearer(authorization: string | null | undefined): string | null;
22
+ export declare function parseAnyBearer(authorization: string | null | undefined): string | null;
15
23
  export declare function spkiPem(publicKeyBody: string): string;
16
24
  /** Import a Keycloak RS256 public key. `publicKey` is the PEM body (or a full PEM). */
17
25
  export declare function importRs256PublicKey(publicKey: string): Promise<CryptoKey>;
18
- export declare function unauthorizedResponse(): Response;
26
+ export declare function unauthorizedResponse(resourceMetadataUrl?: string): Response;
19
27
  export declare function canonicalEmail(payload: Record<string, unknown>): string | null;
20
28
  /**
21
29
  * Verify an already-parsed bearer token.
22
30
  * `publicKey` is a CryptoKey or the same SPKI body stored in KC_RS256_PK.
23
31
  * Returns null on any failure (bad signature, wrong tenant, missing email).
24
32
  */
25
- export declare function verifyArtifactToken(token: string, publicKey: CryptoKey | string, expectedTenant: string): Promise<ArtifactIdentity | null>;
33
+ export declare function verifyArtifactToken(token: string, publicKey: CryptoKey | string, expectedTenant: string, expectedIssuer: string, allowedAzp: ReadonlySet<string>): Promise<ArtifactIdentity | null>;
26
34
  export declare function authenticateRequest(request: Request, env: AuthEnv): Promise<{
27
35
  ok: true;
28
36
  identity: ArtifactIdentity;
@@ -0,0 +1,17 @@
1
+ import type { GrantSummary, ListResult } from '@cloudflare/workers-oauth-provider';
2
+ import type { ArtifactEnv } from '../types';
3
+ import type { ArtifactAccess } from './authz';
4
+ import type { ArtifactIdentity } from './auth';
5
+ export interface McpRequestContext {
6
+ env: ArtifactEnv;
7
+ identity: ArtifactIdentity;
8
+ upstreamToken: string;
9
+ credentialKind: 'oauth' | 'legacy-jwt';
10
+ scopes: ReadonlySet<string>;
11
+ access: ArtifactAccess | null;
12
+ accessMode: 'phase1' | 'rbac';
13
+ userId?: string;
14
+ resourceMetadataUrl: string;
15
+ listConnections?: (cursor?: string) => Promise<ListResult<GrantSummary>>;
16
+ revokeConnection?: (grantId: string) => Promise<boolean>;
17
+ }
@@ -0,0 +1,8 @@
1
+ import { type ToolDefinition } from '../tools';
2
+ export interface McpDomain {
3
+ id: string;
4
+ tools: readonly ToolDefinition[];
5
+ requiredScope(name: string, args: Record<string, unknown>): string;
6
+ listTools(scopes: ReadonlySet<string>): ToolDefinition[];
7
+ }
8
+ export declare const artifactDomain: McpDomain;
@@ -2,11 +2,13 @@
2
2
  * Stateless Streamable HTTP MCP for @hugex/artifact. JSON-RPC 2.0 by hand, no SDK, no CORS.
3
3
  */
4
4
  import type { ArtifactEnv } from '../types';
5
+ import type { McpRequestContext } from './context';
5
6
  export declare const MAX_BODY_BYTES: number;
6
- export declare const SERVER_NAME = "hugex-artifact";
7
- export declare const SERVER_VERSION = "1.0.0";
8
- export declare const DEFAULT_PROTOCOL_VERSION = "2025-06-18";
9
- export declare const SUPPORTED_PROTOCOL_VERSIONS: readonly ['2024-11-05', '2025-03-26', '2025-06-18'];
7
+ export declare const SERVER_NAME = "hugex";
8
+ export declare const SERVER_VERSION = "2.0.0";
9
+ export declare const DEFAULT_PROTOCOL_VERSION = "2025-11-25";
10
+ export declare const SUPPORTED_PROTOCOL_VERSIONS: readonly ['2024-11-05', '2025-03-26', '2025-06-18', '2025-11-25'];
10
11
  export type AccessMode = 'phase1' | 'rbac';
11
12
  export declare function mcpInstructions(mode: AccessMode): string;
12
13
  export declare function handleArtifactMcp(request: Request, env: ArtifactEnv, ctx: ExecutionContext): Promise<Response>;
14
+ export declare function handleArtifactMcpAuthenticated(request: Request, env: ArtifactEnv, input: Omit<McpRequestContext, 'env'>): Promise<Response>;
@@ -0,0 +1,16 @@
1
+ import type { ArtifactEnv } from '../../types';
2
+ export declare const OAUTH_SCOPES: readonly ['artifacts:read', 'artifacts:write', 'offline_access'];
3
+ export interface OAuthConfig {
4
+ resource: string;
5
+ issuer: string;
6
+ mount: string;
7
+ authorizeEndpoint: string;
8
+ tokenEndpoint: string;
9
+ registrationEndpoint: string;
10
+ callbackEndpoint: string;
11
+ clientIdMetadataDocumentEnabled: boolean;
12
+ }
13
+ export declare function oauthConfigured(env: ArtifactEnv): boolean;
14
+ export declare function getOAuthConfig(env: ArtifactEnv): OAuthConfig;
15
+ export declare function protectedResourceMetadataUrl(resource: string): string;
16
+ export declare function isAuthorizationOrigin(request: Request, config: OAuthConfig): boolean;
@@ -0,0 +1,4 @@
1
+ import type { ConsentDescription } from '@cloudflare/workers-oauth-provider';
2
+ import type { UpstreamIdentity } from './oidc';
3
+ export declare function consentResponse(request: Request, details: ConsentDescription, identity: UpstreamIdentity, handle: string, helperHeaders: Headers): Response;
4
+ export declare function oauthHtmlError(message: string, status?: number): Response;
@@ -0,0 +1,29 @@
1
+ import type { ArtifactEnv } from '../../types';
2
+ export interface UpstreamIdentity {
3
+ sub: string;
4
+ email: string;
5
+ org: string;
6
+ tenant: string;
7
+ }
8
+ export interface UpstreamTokens {
9
+ identity: UpstreamIdentity;
10
+ accessToken: string;
11
+ refreshToken: string;
12
+ accessExpiresAt: number;
13
+ refreshExpiresAt: number;
14
+ sessionStartedAt: number;
15
+ }
16
+ export declare function createPkceVerifier(): string;
17
+ export declare function pkceChallenge(verifier: string): Promise<string>;
18
+ export declare function createNonce(): string;
19
+ export declare function upstreamAuthorizeUrl(env: ArtifactEnv, callbackEndpoint: string, state: string, verifierChallenge: string, nonce: string): string;
20
+ export declare function exchangeAuthorizationCode(env: ArtifactEnv, code: string, callbackEndpoint: string, verifier: string, nonce: string): Promise<UpstreamTokens>;
21
+ export type RefreshResult = {
22
+ ok: true;
23
+ tokens: UpstreamTokens;
24
+ } | {
25
+ ok: false;
26
+ terminal: boolean;
27
+ retryAfter?: string;
28
+ };
29
+ export declare function refreshUpstream(env: ArtifactEnv, previous: UpstreamTokens): Promise<RefreshResult>;
@@ -0,0 +1,5 @@
1
+ import type { ArtifactEnv } from '../../types';
2
+ export declare function oauthTenantPrefix(tenantValue: string | undefined): string;
3
+ export declare function prefixedOAuthKv(binding: KVNamespace, tenant: string): KVNamespace;
4
+ export declare function withPrefixedOAuthKv(env: ArtifactEnv): ArtifactEnv;
5
+ export declare function getPrefixedOAuthKv(env: ArtifactEnv): KVNamespace;
@@ -0,0 +1,21 @@
1
+ import { OAuthAuthorizationServer, type AuthRequest, type GrantSummary, type ListResult, type OAuthHelpers } from '@cloudflare/workers-oauth-provider';
2
+ import type { ArtifactEnv } from '../../types';
3
+ import { type OAuthConfig } from './config';
4
+ import { type UpstreamTokens } from './oidc';
5
+ export interface McpGrantProps extends UpstreamTokens {
6
+ v: 1;
7
+ authKind: 'oauth';
8
+ }
9
+ export interface McpAccessTokenProps {
10
+ v: 1;
11
+ authKind: 'oauth';
12
+ identity: UpstreamTokens['identity'];
13
+ accessToken: string;
14
+ accessExpiresAt: number;
15
+ sessionStartedAt: number;
16
+ }
17
+ export declare function createAuthorizationServer(env: ArtifactEnv, config: OAuthConfig): OAuthAuthorizationServer<ArtifactEnv>;
18
+ export declare function handleOAuthRequest(request: Request, env: ArtifactEnv, ctx: ExecutionContext): Promise<Response>;
19
+ export declare function listResourceConnections(helpers: Pick<OAuthHelpers, 'listUserGrants'>, userId: string, resource: string, cursor?: string): Promise<ListResult<GrantSummary>>;
20
+ export declare function revokeResourceConnection(helpers: Pick<OAuthHelpers, 'listUserGrants' | 'revokeGrant'>, userId: string, resource: string, grantId: string): Promise<boolean>;
21
+ export declare function withDefaultAuthorizationScope(request: AuthRequest): AuthRequest;
@@ -0,0 +1,5 @@
1
+ import type { ArtifactEnv } from '../../types';
2
+ import type { UpstreamTokens } from './oidc';
3
+ export declare function storePendingTokens(env: ArtifactEnv, handle: string, tokens: UpstreamTokens): Promise<void>;
4
+ export declare function consumePendingTokens(env: ArtifactEnv, handle: string): Promise<UpstreamTokens | null>;
5
+ export declare function deletePendingTokens(env: ArtifactEnv, handle: string): Promise<void>;
@@ -0,0 +1,3 @@
1
+ import { type McpDomain } from './domains/artifacts';
2
+ export declare function domainForPath(pathname: string, resourcePath: string): McpDomain[] | null;
3
+ export declare function domainForTool(name: string): McpDomain | null;
@@ -0,0 +1,5 @@
1
+ export declare const ARTIFACT_READ_SCOPE = "artifacts:read";
2
+ export declare const ARTIFACT_WRITE_SCOPE = "artifacts:write";
3
+ export declare function effectiveScopes(scopes: ReadonlySet<string>): ReadonlySet<string>;
4
+ export declare function requiredScope(tool: string, args: Record<string, unknown>): string;
5
+ export declare function hasScope(scopes: ReadonlySet<string>, required: string): boolean;
@@ -11,8 +11,8 @@ interface ToolDeps {
11
11
  identity: ArtifactIdentity;
12
12
  requestUrl: string;
13
13
  access: ArtifactAccess | null;
14
- /** Verified MCP bearer token, forwarded only to hx-core. */
15
- token?: string;
14
+ /** Verified upstream Keycloak token, never the opaque MCP credential. */
15
+ upstreamToken?: string;
16
16
  }
17
17
  export interface ToolDefinition {
18
18
  name: string;
package/dist/types.d.ts CHANGED
@@ -27,6 +27,29 @@ export interface ArtifactEnv {
27
27
  ARTIFACT_DEV_PATH?: string;
28
28
  /** Keycloak RS256 public key (base64 SPKI body, no PEM header), same secret ora already uses. */
29
29
  KC_RS256_PK: string;
30
+ /** Existing password-login client. OAuth MCP uses the separate MCP_KC_* variables. */
31
+ KC_BASE_URL?: string;
32
+ KC_CLIENT_ID?: string;
33
+ /** Canonical protected resource, e.g. https://app-tenant.hugex.co/mcp. */
34
+ MCP_RESOURCE_URL?: string;
35
+ /** Trusted authorization-server origin, separate from ARTIFACT_HOST in production. */
36
+ OAUTH_ISSUER?: string;
37
+ /**
38
+ * Shared OAuth provider storage. The artifact OAuth layer prefixes every key with
39
+ * `mcp-oauth:<TENANT>:`; callers must never use this raw binding directly.
40
+ */
41
+ OAUTH_KV?: KVNamespace;
42
+ /** Dedicated confidential OIDC client for the MCP. */
43
+ MCP_KC_ISSUER?: string;
44
+ MCP_KC_CLIENT_ID?: string;
45
+ MCP_KC_CLIENT_SECRET?: string;
46
+ /** 32+ character secret for short-lived encrypted consent state. */
47
+ OAUTH_TRANSACTION_SECRET?: string;
48
+ /** Set true only where global_fetch_strictly_public is enabled. */
49
+ MCP_CIMD_ENABLED?: string;
50
+ /** Legacy JWT is on by default; set "off" to disable it. */
51
+ MCP_LEGACY_JWT?: string;
52
+ MCP_LEGACY_ALLOWED_AZP?: string;
30
53
  /**
31
54
  * hx-ora base URL, e.g. `http://localhost:3002`. Together with `HX_API_KEY` this turns on
32
55
  * Phase 2: RBAC via `POST /internal/artifacts/access` and index replication via `PUT`.
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@hugex/artifact",
3
- "version": "0.1.2",
3
+ "version": "0.2.1",
4
4
  "description": "HugeX Artifacts runtime for Cloudflare Workers",
5
5
  "author": "a@hugex.co",
6
6
  "license": "SEE LICENSE IN LICENSE",
7
7
  "type": "module",
8
8
  "dependencies": {
9
- "jose": "^6.2.3"
9
+ "jose": "^6.2.3",
10
+ "@cloudflare/workers-oauth-provider": "1.2.3"
10
11
  },
11
12
  "publishConfig": {
12
13
  "access": "public"