@flaghoist/server 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/dist/dashboard.cjs +30 -0
- package/dist/dashboard.d.cts +10 -0
- package/dist/dashboard.d.ts +10 -0
- package/dist/dashboard.js +5 -0
- package/dist/index.cjs +650 -0
- package/dist/index.d.cts +101 -0
- package/dist/index.d.ts +101 -0
- package/dist/index.js +621 -0
- package/package.json +64 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import * as hono_types from 'hono/types';
|
|
2
|
+
import { Hono } from 'hono';
|
|
3
|
+
import { StorageAdapter, AttributeValue } from '@flaghoist/core';
|
|
4
|
+
import { JWTVerifyGetKey } from 'jose';
|
|
5
|
+
|
|
6
|
+
/** Result of an authentication attempt. `ok: false` carries the status/message to return. */
|
|
7
|
+
interface AuthResult {
|
|
8
|
+
ok: boolean;
|
|
9
|
+
/** Caller identity (email/subject/'api-key') recorded in audit metadata when ok. */
|
|
10
|
+
identity?: string;
|
|
11
|
+
/** HTTP status to return when not ok. */
|
|
12
|
+
status?: 401 | 403;
|
|
13
|
+
/** Public, non-sensitive error message when not ok. */
|
|
14
|
+
message?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Authenticates a request from its headers. Verifiers extract whatever credential they need
|
|
18
|
+
* (an API key header, a bearer token, …) and never throw for an ordinary auth failure.
|
|
19
|
+
*/
|
|
20
|
+
type Authenticator = (headers: Headers) => Promise<AuthResult> | AuthResult;
|
|
21
|
+
interface ServerConfig {
|
|
22
|
+
/** Storage backend (any StorageAdapter — Cloudflare KV, memory, or your own). */
|
|
23
|
+
storage: StorageAdapter;
|
|
24
|
+
auth: {
|
|
25
|
+
/** Guards the admin/write path (create/update/delete + full flag reads). */
|
|
26
|
+
admin: Authenticator;
|
|
27
|
+
/** Guards the OFREP read/evaluate path. */
|
|
28
|
+
read: Authenticator;
|
|
29
|
+
};
|
|
30
|
+
/** Exact-match CORS origin allowlist. Omit for same-origin only (no CORS headers emitted). */
|
|
31
|
+
allowedOrigins?: string[];
|
|
32
|
+
/** TTL in seconds for the in-isolate flag-definition cache on the read path. Default: 30. */
|
|
33
|
+
cacheTtlSeconds?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Inject trusted context attributes derived from headers you control (e.g. a validated
|
|
36
|
+
* session). These override client-supplied context, so security-relevant targeting can be
|
|
37
|
+
* made trustworthy rather than self-asserted.
|
|
38
|
+
*/
|
|
39
|
+
trustedContext?: (headers: Headers) => Record<string, AttributeValue>;
|
|
40
|
+
/**
|
|
41
|
+
* Prebuilt admin dashboard HTML (a single-file SPA build) to serve at `/admin`. When set, a
|
|
42
|
+
* single deploy gives you the read API, the admin API, and the management UI together.
|
|
43
|
+
*/
|
|
44
|
+
dashboard?: string;
|
|
45
|
+
}
|
|
46
|
+
/** Config, or a function that derives it from the runtime environment (e.g. Workers bindings). */
|
|
47
|
+
type ConfigResolver<Env> = ServerConfig | ((env: Env) => ServerConfig);
|
|
48
|
+
|
|
49
|
+
/** Read-path verifier: matches the `x-api-key` header against a shared secret in constant time. */
|
|
50
|
+
declare function apiKey(expected: string): Authenticator;
|
|
51
|
+
/**
|
|
52
|
+
* Admin-path verifier: matches an `Authorization: Bearer <token>` against a shared secret in
|
|
53
|
+
* constant time. The zero-config default — possession of the token is admin authorization.
|
|
54
|
+
*/
|
|
55
|
+
declare function bearerToken(expected: string): Authenticator;
|
|
56
|
+
interface OidcOptions {
|
|
57
|
+
/** Token issuer URL, validated against the `iss` claim. */
|
|
58
|
+
issuer: string;
|
|
59
|
+
/** Expected `aud` claim (your app/client id). */
|
|
60
|
+
audience: string;
|
|
61
|
+
/** JWKS URL. Defaults to `<issuer>/.well-known/jwks.json`. */
|
|
62
|
+
jwksUri?: string;
|
|
63
|
+
/** Allowed signature algorithms. Defaults to `['RS256']`. `alg: none` is always rejected. */
|
|
64
|
+
algorithms?: string[];
|
|
65
|
+
/** Optional `token_use` claim to require (e.g. `'id'` for Cognito id tokens). */
|
|
66
|
+
tokenUse?: string;
|
|
67
|
+
/** Claim holding the caller's groups. Defaults to `'groups'`. */
|
|
68
|
+
groupsClaim?: string;
|
|
69
|
+
/** If set, the caller must belong to at least one of these groups (authorization). */
|
|
70
|
+
allowedGroups?: string[];
|
|
71
|
+
/** Advanced/testing: supply a key resolver instead of fetching the remote JWKS. */
|
|
72
|
+
keyResolver?: JWTVerifyGetKey;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Admin-path verifier backed by an OIDC provider (Cognito, Auth0, Okta, Keycloak, Entra, …).
|
|
76
|
+
* Validates the JWT signature against the provider's JWKS with a pinned algorithm allowlist,
|
|
77
|
+
* checks `iss`/`aud`/`exp`/`nbf` (and optionally `token_use`), then enforces group membership.
|
|
78
|
+
* Signature/claim validation is delegated to the audited `jose` library.
|
|
79
|
+
*/
|
|
80
|
+
declare function oidc(options: OidcOptions): Authenticator;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* OpenAPI 3.1 description of the Flaghoist HTTP API. It is served at `GET /api/v1/openapi.json`
|
|
84
|
+
* and exported for tooling (`import { openApiDocument } from '@flaghoist/server'`).
|
|
85
|
+
*
|
|
86
|
+
* The admin API (`/api/v1/flags`) is Flaghoist's own, versioned surface for building dashboards
|
|
87
|
+
* and integrations. The read API (`/ofrep/v1/...`) follows the OpenFeature Remote Evaluation
|
|
88
|
+
* Protocol and is described here for completeness.
|
|
89
|
+
*/
|
|
90
|
+
declare const openApiDocument: Record<string, unknown>;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Build a Flaghoist server as a Hono app. Pass a config object, or a function that derives the
|
|
94
|
+
* config from the runtime environment (e.g. Cloudflare Workers bindings). The returned app has a
|
|
95
|
+
* `fetch` handler, so `export default createFlagServer(...)` works as a Worker entrypoint.
|
|
96
|
+
*/
|
|
97
|
+
declare function createFlagServer<Env extends object = Record<string, unknown>>(config: ConfigResolver<Env>): Hono<{
|
|
98
|
+
Bindings: Env;
|
|
99
|
+
}, hono_types.BlankSchema, "/">;
|
|
100
|
+
|
|
101
|
+
export { type AuthResult, type Authenticator, type ConfigResolver, type OidcOptions, type ServerConfig, apiKey, bearerToken, createFlagServer, oidc, openApiDocument };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import * as hono_types from 'hono/types';
|
|
2
|
+
import { Hono } from 'hono';
|
|
3
|
+
import { StorageAdapter, AttributeValue } from '@flaghoist/core';
|
|
4
|
+
import { JWTVerifyGetKey } from 'jose';
|
|
5
|
+
|
|
6
|
+
/** Result of an authentication attempt. `ok: false` carries the status/message to return. */
|
|
7
|
+
interface AuthResult {
|
|
8
|
+
ok: boolean;
|
|
9
|
+
/** Caller identity (email/subject/'api-key') recorded in audit metadata when ok. */
|
|
10
|
+
identity?: string;
|
|
11
|
+
/** HTTP status to return when not ok. */
|
|
12
|
+
status?: 401 | 403;
|
|
13
|
+
/** Public, non-sensitive error message when not ok. */
|
|
14
|
+
message?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Authenticates a request from its headers. Verifiers extract whatever credential they need
|
|
18
|
+
* (an API key header, a bearer token, …) and never throw for an ordinary auth failure.
|
|
19
|
+
*/
|
|
20
|
+
type Authenticator = (headers: Headers) => Promise<AuthResult> | AuthResult;
|
|
21
|
+
interface ServerConfig {
|
|
22
|
+
/** Storage backend (any StorageAdapter — Cloudflare KV, memory, or your own). */
|
|
23
|
+
storage: StorageAdapter;
|
|
24
|
+
auth: {
|
|
25
|
+
/** Guards the admin/write path (create/update/delete + full flag reads). */
|
|
26
|
+
admin: Authenticator;
|
|
27
|
+
/** Guards the OFREP read/evaluate path. */
|
|
28
|
+
read: Authenticator;
|
|
29
|
+
};
|
|
30
|
+
/** Exact-match CORS origin allowlist. Omit for same-origin only (no CORS headers emitted). */
|
|
31
|
+
allowedOrigins?: string[];
|
|
32
|
+
/** TTL in seconds for the in-isolate flag-definition cache on the read path. Default: 30. */
|
|
33
|
+
cacheTtlSeconds?: number;
|
|
34
|
+
/**
|
|
35
|
+
* Inject trusted context attributes derived from headers you control (e.g. a validated
|
|
36
|
+
* session). These override client-supplied context, so security-relevant targeting can be
|
|
37
|
+
* made trustworthy rather than self-asserted.
|
|
38
|
+
*/
|
|
39
|
+
trustedContext?: (headers: Headers) => Record<string, AttributeValue>;
|
|
40
|
+
/**
|
|
41
|
+
* Prebuilt admin dashboard HTML (a single-file SPA build) to serve at `/admin`. When set, a
|
|
42
|
+
* single deploy gives you the read API, the admin API, and the management UI together.
|
|
43
|
+
*/
|
|
44
|
+
dashboard?: string;
|
|
45
|
+
}
|
|
46
|
+
/** Config, or a function that derives it from the runtime environment (e.g. Workers bindings). */
|
|
47
|
+
type ConfigResolver<Env> = ServerConfig | ((env: Env) => ServerConfig);
|
|
48
|
+
|
|
49
|
+
/** Read-path verifier: matches the `x-api-key` header against a shared secret in constant time. */
|
|
50
|
+
declare function apiKey(expected: string): Authenticator;
|
|
51
|
+
/**
|
|
52
|
+
* Admin-path verifier: matches an `Authorization: Bearer <token>` against a shared secret in
|
|
53
|
+
* constant time. The zero-config default — possession of the token is admin authorization.
|
|
54
|
+
*/
|
|
55
|
+
declare function bearerToken(expected: string): Authenticator;
|
|
56
|
+
interface OidcOptions {
|
|
57
|
+
/** Token issuer URL, validated against the `iss` claim. */
|
|
58
|
+
issuer: string;
|
|
59
|
+
/** Expected `aud` claim (your app/client id). */
|
|
60
|
+
audience: string;
|
|
61
|
+
/** JWKS URL. Defaults to `<issuer>/.well-known/jwks.json`. */
|
|
62
|
+
jwksUri?: string;
|
|
63
|
+
/** Allowed signature algorithms. Defaults to `['RS256']`. `alg: none` is always rejected. */
|
|
64
|
+
algorithms?: string[];
|
|
65
|
+
/** Optional `token_use` claim to require (e.g. `'id'` for Cognito id tokens). */
|
|
66
|
+
tokenUse?: string;
|
|
67
|
+
/** Claim holding the caller's groups. Defaults to `'groups'`. */
|
|
68
|
+
groupsClaim?: string;
|
|
69
|
+
/** If set, the caller must belong to at least one of these groups (authorization). */
|
|
70
|
+
allowedGroups?: string[];
|
|
71
|
+
/** Advanced/testing: supply a key resolver instead of fetching the remote JWKS. */
|
|
72
|
+
keyResolver?: JWTVerifyGetKey;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Admin-path verifier backed by an OIDC provider (Cognito, Auth0, Okta, Keycloak, Entra, …).
|
|
76
|
+
* Validates the JWT signature against the provider's JWKS with a pinned algorithm allowlist,
|
|
77
|
+
* checks `iss`/`aud`/`exp`/`nbf` (and optionally `token_use`), then enforces group membership.
|
|
78
|
+
* Signature/claim validation is delegated to the audited `jose` library.
|
|
79
|
+
*/
|
|
80
|
+
declare function oidc(options: OidcOptions): Authenticator;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* OpenAPI 3.1 description of the Flaghoist HTTP API. It is served at `GET /api/v1/openapi.json`
|
|
84
|
+
* and exported for tooling (`import { openApiDocument } from '@flaghoist/server'`).
|
|
85
|
+
*
|
|
86
|
+
* The admin API (`/api/v1/flags`) is Flaghoist's own, versioned surface for building dashboards
|
|
87
|
+
* and integrations. The read API (`/ofrep/v1/...`) follows the OpenFeature Remote Evaluation
|
|
88
|
+
* Protocol and is described here for completeness.
|
|
89
|
+
*/
|
|
90
|
+
declare const openApiDocument: Record<string, unknown>;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Build a Flaghoist server as a Hono app. Pass a config object, or a function that derives the
|
|
94
|
+
* config from the runtime environment (e.g. Cloudflare Workers bindings). The returned app has a
|
|
95
|
+
* `fetch` handler, so `export default createFlagServer(...)` works as a Worker entrypoint.
|
|
96
|
+
*/
|
|
97
|
+
declare function createFlagServer<Env extends object = Record<string, unknown>>(config: ConfigResolver<Env>): Hono<{
|
|
98
|
+
Bindings: Env;
|
|
99
|
+
}, hono_types.BlankSchema, "/">;
|
|
100
|
+
|
|
101
|
+
export { type AuthResult, type Authenticator, type ConfigResolver, type OidcOptions, type ServerConfig, apiKey, bearerToken, createFlagServer, oidc, openApiDocument };
|