veryfront 0.1.321 → 0.1.322
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/esm/deno.js +1 -1
- package/esm/extensions/ext-jwt/src/index.d.ts +39 -0
- package/esm/extensions/ext-jwt/src/index.d.ts.map +1 -0
- package/esm/extensions/ext-jwt/src/index.js +103 -0
- package/esm/extensions/ext-openai/src/openai-provider.d.ts +29 -0
- package/esm/extensions/ext-openai/src/openai-provider.d.ts.map +1 -0
- package/esm/extensions/ext-openai/src/openai-provider.js +1095 -0
- package/esm/src/embedding/veryfront-cloud/provider.d.ts.map +1 -1
- package/esm/src/embedding/veryfront-cloud/provider.js +6 -1
- package/esm/src/provider/shared/index.d.ts +16 -0
- package/esm/src/provider/shared/index.d.ts.map +1 -0
- package/esm/src/provider/shared/index.js +18 -0
- package/esm/src/provider/veryfront-cloud/openai.d.ts +10 -0
- package/esm/src/provider/veryfront-cloud/openai.d.ts.map +1 -0
- package/esm/src/provider/veryfront-cloud/openai.js +18 -0
- package/esm/src/provider/veryfront-cloud/provider.d.ts.map +1 -1
- package/esm/src/provider/veryfront-cloud/provider.js +6 -1
- package/esm/src/proxy/main.js +3 -0
- package/esm/src/utils/version-constant.d.ts +1 -1
- package/esm/src/utils/version-constant.js +1 -1
- package/package.json +2 -1
- package/src/deno.js +1 -1
- package/src/extensions/ext-jwt/src/index.ts +173 -0
- package/src/extensions/ext-openai/src/openai-provider.ts +1481 -0
- package/src/src/embedding/veryfront-cloud/provider.ts +6 -3
- package/src/src/provider/shared/index.ts +62 -0
- package/src/src/provider/veryfront-cloud/openai.ts +34 -0
- package/src/src/provider/veryfront-cloud/provider.ts +6 -3
- package/src/src/proxy/main.ts +4 -0
- package/src/src/utils/version-constant.ts +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../../../src/src/embedding/veryfront-cloud/provider.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../../../src/src/embedding/veryfront-cloud/provider.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAShE,wBAAgB,kCAAkC,CAAC,OAAO,EAAE,MAAM,GAAG,gBAAgB,CA6BpF"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createError, toError } from "../../errors/veryfront-error.js";
|
|
2
2
|
import { createGoogleEmbeddingRuntime } from "../../provider/runtime-loader.js";
|
|
3
3
|
import { createVeryfrontCloudFetch, getVeryfrontCloudGatewayBaseUrl, parseVeryfrontCloudModelId, requireVeryfrontCloudBootstrap, } from "../../provider/veryfront-cloud/shared.js";
|
|
4
|
+
import { createVeryfrontCloudOpenAIEmbeddingModel } from "../../provider/veryfront-cloud/openai.js";
|
|
4
5
|
export function createVeryfrontCloudEmbeddingModel(modelId) {
|
|
5
6
|
const { provider, modelId: upstreamModelId } = parseVeryfrontCloudModelId(modelId, "embedding");
|
|
6
7
|
const { apiBaseUrl, apiToken } = requireVeryfrontCloudBootstrap();
|
|
@@ -8,7 +9,11 @@ export function createVeryfrontCloudEmbeddingModel(modelId) {
|
|
|
8
9
|
const fetch = createVeryfrontCloudFetch(apiToken);
|
|
9
10
|
switch (provider) {
|
|
10
11
|
case "openai":
|
|
11
|
-
|
|
12
|
+
return createVeryfrontCloudOpenAIEmbeddingModel(upstreamModelId, {
|
|
13
|
+
apiToken,
|
|
14
|
+
baseURL,
|
|
15
|
+
fetch,
|
|
16
|
+
});
|
|
12
17
|
case "google":
|
|
13
18
|
return createGoogleEmbeddingRuntime({
|
|
14
19
|
apiKey: apiToken,
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared plumbing consumed by the `@veryfront/ext-*` provider extensions.
|
|
3
|
+
*
|
|
4
|
+
* This barrel is the stable public surface: implementations currently live
|
|
5
|
+
* in `runtime-loader.ts` and `runtime-loader/` subdirectory. Future PRs
|
|
6
|
+
* (post ext-anthropic / ext-google extraction) may move the implementations
|
|
7
|
+
* into this directory; extensions keep importing from here unchanged.
|
|
8
|
+
*
|
|
9
|
+
* @module provider/shared
|
|
10
|
+
*/
|
|
11
|
+
export { getAnthropicMessagesUrl, getGoogleEmbeddingUrl, getGoogleGenerateContentUrl, getGoogleStreamGenerateContentUrl, getOpenAIChatCompletionsUrl, getOpenAIEmbeddingUrl, getOpenAIResponsesUrl, } from "../runtime-loader/provider-endpoints.js";
|
|
12
|
+
export { createAnthropicRequestInit, createGoogleRequestInit, createOpenAIRequestInit, } from "../runtime-loader/provider-request-init.js";
|
|
13
|
+
export { TOOL_INPUT_PENDING_THRESHOLD_MS, withToolInputStatusTransitions, } from "../runtime-loader/tool-input-status.js";
|
|
14
|
+
export { buildProviderError, createWarningCollector, isNumberArray, mergeUsage, parseRetryAfterMs, ProviderError, ProviderOverloadedError, ProviderQuotaError, ProviderRateLimitError, ProviderRequestError, readProviderOptions, readRecord, readTextParts, requestJson, requestStream, stringifyJsonValue, toOpenAICompatibleMessages, toOpenAICompatibleTools, } from "../runtime-loader.js";
|
|
15
|
+
export type { OpenAICompatibleChatMessage, OpenAICompatibleChatRequest, RuntimePromptMessage, } from "../runtime-loader.js";
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/src/provider/shared/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,EACL,uBAAuB,EACvB,qBAAqB,EACrB,2BAA2B,EAC3B,iCAAiC,EACjC,2BAA2B,EAC3B,qBAAqB,EACrB,qBAAqB,GACtB,MAAM,yCAAyC,CAAC;AAGjD,OAAO,EACL,0BAA0B,EAC1B,uBAAuB,EACvB,uBAAuB,GACxB,MAAM,4CAA4C,CAAC;AAGpD,OAAO,EACL,+BAA+B,EAC/B,8BAA8B,GAC/B,MAAM,wCAAwC,CAAC;AAGhD,OAAO,EACL,kBAAkB,EAClB,sBAAsB,EACtB,aAAa,EACb,UAAU,EACV,iBAAiB,EACjB,aAAa,EACb,uBAAuB,EACvB,kBAAkB,EAClB,sBAAsB,EACtB,oBAAoB,EACpB,mBAAmB,EACnB,UAAU,EACV,aAAa,EACb,WAAW,EACX,aAAa,EACb,kBAAkB,EAClB,0BAA0B,EAC1B,uBAAuB,GACxB,MAAM,sBAAsB,CAAC;AAE9B,YAAY,EACV,2BAA2B,EAC3B,2BAA2B,EAC3B,oBAAoB,GACrB,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared plumbing consumed by the `@veryfront/ext-*` provider extensions.
|
|
3
|
+
*
|
|
4
|
+
* This barrel is the stable public surface: implementations currently live
|
|
5
|
+
* in `runtime-loader.ts` and `runtime-loader/` subdirectory. Future PRs
|
|
6
|
+
* (post ext-anthropic / ext-google extraction) may move the implementations
|
|
7
|
+
* into this directory; extensions keep importing from here unchanged.
|
|
8
|
+
*
|
|
9
|
+
* @module provider/shared
|
|
10
|
+
*/
|
|
11
|
+
// URL builders
|
|
12
|
+
export { getAnthropicMessagesUrl, getGoogleEmbeddingUrl, getGoogleGenerateContentUrl, getGoogleStreamGenerateContentUrl, getOpenAIChatCompletionsUrl, getOpenAIEmbeddingUrl, getOpenAIResponsesUrl, } from "../runtime-loader/provider-endpoints.js";
|
|
13
|
+
// Request init builders
|
|
14
|
+
export { createAnthropicRequestInit, createGoogleRequestInit, createOpenAIRequestInit, } from "../runtime-loader/provider-request-init.js";
|
|
15
|
+
// Tool-input status transitions
|
|
16
|
+
export { TOOL_INPUT_PENDING_THRESHOLD_MS, withToolInputStatusTransitions, } from "../runtime-loader/tool-input-status.js";
|
|
17
|
+
// Retry / error / HTTP plumbing (currently in runtime-loader.ts).
|
|
18
|
+
export { buildProviderError, createWarningCollector, isNumberArray, mergeUsage, parseRetryAfterMs, ProviderError, ProviderOverloadedError, ProviderQuotaError, ProviderRateLimitError, ProviderRequestError, readProviderOptions, readRecord, readTextParts, requestJson, requestStream, stringifyJsonValue, toOpenAICompatibleMessages, toOpenAICompatibleTools, } from "../runtime-loader.js";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { EmbeddingRuntime, ModelRuntime } from "../types.js";
|
|
2
|
+
interface VeryfrontCloudOpenAIConfig {
|
|
3
|
+
apiToken: string;
|
|
4
|
+
baseURL: string;
|
|
5
|
+
fetch: typeof globalThis.fetch;
|
|
6
|
+
}
|
|
7
|
+
export declare function createVeryfrontCloudOpenAIModel(modelId: string, config: VeryfrontCloudOpenAIConfig): ModelRuntime;
|
|
8
|
+
export declare function createVeryfrontCloudOpenAIEmbeddingModel(modelId: string, config: VeryfrontCloudOpenAIConfig): EmbeddingRuntime;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=openai.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../../../src/src/provider/veryfront-cloud/openai.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAKlE,UAAU,0BAA0B;IAClC,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;CAChC;AAED,wBAAgB,+BAA+B,CAC7C,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,0BAA0B,GACjC,YAAY,CAOd;AAED,wBAAgB,wCAAwC,CACtD,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,0BAA0B,GACjC,gBAAgB,CAOlB"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { OpenAIProvider } from "../../../extensions/ext-openai/src/openai-provider.js";
|
|
2
|
+
const openAIProvider = new OpenAIProvider();
|
|
3
|
+
export function createVeryfrontCloudOpenAIModel(modelId, config) {
|
|
4
|
+
return openAIProvider.createModel(modelId, {
|
|
5
|
+
credential: config.apiToken,
|
|
6
|
+
baseURL: config.baseURL,
|
|
7
|
+
name: "veryfront-cloud",
|
|
8
|
+
fetch: config.fetch,
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
export function createVeryfrontCloudOpenAIEmbeddingModel(modelId, config) {
|
|
12
|
+
return openAIProvider.createEmbedding(modelId, {
|
|
13
|
+
credential: config.apiToken,
|
|
14
|
+
baseURL: config.baseURL,
|
|
15
|
+
name: "veryfront-cloud",
|
|
16
|
+
fetch: config.fetch,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../../../src/src/provider/veryfront-cloud/provider.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../../../src/src/provider/veryfront-cloud/provider.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAShD,wBAAgB,yBAAyB,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,CAmEvE"}
|
|
@@ -3,6 +3,7 @@ import { createAnthropicModelRuntime, createGoogleModelRuntime } from "../runtim
|
|
|
3
3
|
import { tryResolve } from "../../extensions/contracts.js";
|
|
4
4
|
import { AIProviderRegistryName } from "../../extensions/interfaces/index.js";
|
|
5
5
|
import { createVeryfrontCloudFetch, getVeryfrontCloudGatewayBaseUrl, parseVeryfrontCloudModelId, requireVeryfrontCloudBootstrap, } from "./shared.js";
|
|
6
|
+
import { createVeryfrontCloudOpenAIModel } from "./openai.js";
|
|
6
7
|
export function createVeryfrontCloudModel(modelId) {
|
|
7
8
|
const { provider, modelId: upstreamModelId } = parseVeryfrontCloudModelId(modelId, "language");
|
|
8
9
|
const { apiBaseUrl, apiToken, projectSlug } = requireVeryfrontCloudBootstrap();
|
|
@@ -50,7 +51,11 @@ export function createVeryfrontCloudModel(modelId) {
|
|
|
50
51
|
fetch,
|
|
51
52
|
});
|
|
52
53
|
}
|
|
53
|
-
|
|
54
|
+
return createVeryfrontCloudOpenAIModel(upstreamModelId, {
|
|
55
|
+
apiToken,
|
|
56
|
+
baseURL,
|
|
57
|
+
fetch,
|
|
58
|
+
});
|
|
54
59
|
}
|
|
55
60
|
default: {
|
|
56
61
|
const _exhaustive = provider;
|
package/esm/src/proxy/main.js
CHANGED
|
@@ -23,6 +23,8 @@ import * as dntShim from "../../_dnt.shims.js";
|
|
|
23
23
|
import { createProxyHandler, INTERNAL_PROXY_HEADERS } from "./handler.js";
|
|
24
24
|
import { createCacheFromEnv } from "./cache/index.js";
|
|
25
25
|
import { isRetryableConnectionError } from "./retry.js";
|
|
26
|
+
import { register } from "../extensions/contracts.js";
|
|
27
|
+
import { createAuthProvider } from "../../extensions/ext-jwt/src/index.js";
|
|
26
28
|
import { endSpan, extractContext, initializeOTLPWithApis, injectContext, ProxySpanNames, shutdownOTLP, startServerSpan, withContext, withSpan, } from "./tracing.js";
|
|
27
29
|
import { proxyLogger, runWithProxyRequestContext } from "./logger.js";
|
|
28
30
|
import { getProxyFailureLogLevel } from "./log-noise.js";
|
|
@@ -80,6 +82,7 @@ const DEFAULT_SERVER_RETRY_COUNT = 1;
|
|
|
80
82
|
const DEFAULT_SERVER_RETRY_DELAY_MS = 100;
|
|
81
83
|
const VERYFRONT_SERVER_RETRY_COUNT = parseInt(getEnv("VERYFRONT_SERVER_RETRY_COUNT") || String(DEFAULT_SERVER_RETRY_COUNT));
|
|
82
84
|
const VERYFRONT_SERVER_RETRY_DELAY_MS = parseInt(getEnv("VERYFRONT_SERVER_RETRY_DELAY_MS") || String(DEFAULT_SERVER_RETRY_DELAY_MS));
|
|
85
|
+
register("AuthProvider", createAuthProvider({}));
|
|
83
86
|
// Initialize cache and proxy handler
|
|
84
87
|
const cache = await createCacheFromEnv();
|
|
85
88
|
const proxyHandler = createProxyHandler({
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const VERSION = "0.1.
|
|
1
|
+
export declare const VERSION = "0.1.322";
|
|
2
2
|
//# sourceMappingURL=version-constant.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "veryfront",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.322",
|
|
4
4
|
"description": "The simplest way to build AI-powered apps",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -225,6 +225,7 @@
|
|
|
225
225
|
"esbuild": "0.27.4",
|
|
226
226
|
"github-slugger": "2.0.0",
|
|
227
227
|
"gray-matter": "4.0.3",
|
|
228
|
+
"jose": "5.9.6",
|
|
228
229
|
"mdast-util-to-string": "4.0.0",
|
|
229
230
|
"react": "19.2.4",
|
|
230
231
|
"react-dom": "19.2.4",
|
package/src/deno.js
CHANGED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ext-jwt — AuthProvider implementation backed by `jose`.
|
|
3
|
+
*
|
|
4
|
+
* Provides the `AuthProvider` contract: sign / verify (HS256 by default),
|
|
5
|
+
* verify-with-remote-JWKS, and decode-header.
|
|
6
|
+
*
|
|
7
|
+
* @module extensions/ext-jwt
|
|
8
|
+
*/
|
|
9
|
+
import * as dntShim from "../../../_dnt.shims.js";
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
createRemoteJWKSet,
|
|
14
|
+
decodeProtectedHeader,
|
|
15
|
+
type JWTPayload,
|
|
16
|
+
jwtVerify,
|
|
17
|
+
type KeyLike,
|
|
18
|
+
SignJWT,
|
|
19
|
+
} from "jose";
|
|
20
|
+
|
|
21
|
+
import type { ExtensionFactory } from "../../../src/extensions/index.js";
|
|
22
|
+
import type {
|
|
23
|
+
AuthProvider,
|
|
24
|
+
SignOptions,
|
|
25
|
+
TokenHeader,
|
|
26
|
+
TokenPayload,
|
|
27
|
+
VerifyOptions,
|
|
28
|
+
} from "../../../src/extensions/interfaces/index.js";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Signature used by jose's verify step to resolve a key for a given header.
|
|
32
|
+
*
|
|
33
|
+
* Matches the shape returned by `createRemoteJWKSet` / `createLocalJWKSet`
|
|
34
|
+
* and lets tests inject an in-memory key set without reaching the network.
|
|
35
|
+
*/
|
|
36
|
+
export type JwksResolver = Parameters<typeof jwtVerify>[1] & object;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Factory for building a JWKS resolver from a URL.
|
|
40
|
+
*
|
|
41
|
+
* The default uses `createRemoteJWKSet`; tests can inject a stub that returns
|
|
42
|
+
* a local resolver bound to an in-memory key set.
|
|
43
|
+
*/
|
|
44
|
+
export type JwksResolverFactory = (jwksUrl: string) => JwksResolver;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Optional configuration for the ext-jwt factory.
|
|
48
|
+
*
|
|
49
|
+
* - `secret`: HMAC secret for `sign`/`verify`. Falls back to the `JWT_SECRET`
|
|
50
|
+
* environment variable. Without one, `sign`/`verify` throw.
|
|
51
|
+
* - `jwksResolverFactory`: test seam that overrides JWKS resolution.
|
|
52
|
+
*/
|
|
53
|
+
export interface ExtJwtConfig {
|
|
54
|
+
secret?: string | Uint8Array;
|
|
55
|
+
jwksResolverFactory?: JwksResolverFactory;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function defaultJwksResolverFactory(jwksUrl: string): JwksResolver {
|
|
59
|
+
return createRemoteJWKSet(new URL(jwksUrl)) as unknown as JwksResolver;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function toUint8Array(secret: string | Uint8Array): Uint8Array {
|
|
63
|
+
return typeof secret === "string" ? new TextEncoder().encode(secret) : secret;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function getSecret(configSecret?: string | Uint8Array): Uint8Array {
|
|
67
|
+
if (configSecret !== undefined) return toUint8Array(configSecret);
|
|
68
|
+
const env = typeof dntShim.Deno !== "undefined" ? dntShim.Deno.env.get("JWT_SECRET") : undefined;
|
|
69
|
+
if (!env) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
"ext-jwt: no HMAC secret configured. Pass `secret` to the extension " +
|
|
72
|
+
"factory or set the JWT_SECRET environment variable.",
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
return new TextEncoder().encode(env);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function createAuthProvider(config: ExtJwtConfig): AuthProvider {
|
|
79
|
+
const jwksResolverFactory = config.jwksResolverFactory ??
|
|
80
|
+
defaultJwksResolverFactory;
|
|
81
|
+
|
|
82
|
+
// Cache one resolver per JWKS URL; `createRemoteJWKSet` maintains its own
|
|
83
|
+
// internal key cache with cooldown/rotation semantics, so reusing the
|
|
84
|
+
// same resolver is required for the cache to be effective.
|
|
85
|
+
const jwksResolvers = new Map<string, JwksResolver>();
|
|
86
|
+
|
|
87
|
+
function getJwksResolver(jwksUrl: string): JwksResolver {
|
|
88
|
+
const existing = jwksResolvers.get(jwksUrl);
|
|
89
|
+
if (existing) return existing;
|
|
90
|
+
const created = jwksResolverFactory(jwksUrl);
|
|
91
|
+
jwksResolvers.set(jwksUrl, created);
|
|
92
|
+
return created;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
async sign(payload: TokenPayload, options?: SignOptions): Promise<string> {
|
|
97
|
+
const secret = getSecret(config.secret);
|
|
98
|
+
const algorithm = options?.algorithm ?? "HS256";
|
|
99
|
+
const { sub, ...rest } = payload;
|
|
100
|
+
const builder = new SignJWT(rest as JWTPayload)
|
|
101
|
+
.setProtectedHeader({ alg: algorithm })
|
|
102
|
+
.setSubject(sub);
|
|
103
|
+
if (options?.expiresIn !== undefined) {
|
|
104
|
+
// jose's setExpirationTime accepts `string | number | Date`.
|
|
105
|
+
builder.setExpirationTime(
|
|
106
|
+
options.expiresIn as string | number,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
return await builder.sign(secret);
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
async verify(
|
|
113
|
+
token: string,
|
|
114
|
+
options?: VerifyOptions,
|
|
115
|
+
): Promise<TokenPayload> {
|
|
116
|
+
const secret = getSecret(config.secret);
|
|
117
|
+
const algorithms = options?.algorithms ?? ["HS256"];
|
|
118
|
+
const { payload } = await jwtVerify(token, secret, { algorithms });
|
|
119
|
+
return payload as TokenPayload;
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
async verifyWithJwks(
|
|
123
|
+
token: string,
|
|
124
|
+
jwksUrl: string,
|
|
125
|
+
options?: VerifyOptions,
|
|
126
|
+
): Promise<TokenPayload> {
|
|
127
|
+
const resolver = getJwksResolver(jwksUrl);
|
|
128
|
+
const { algorithms, ...rest } = options ?? {};
|
|
129
|
+
const verifyOpts: Record<string, unknown> = { ...rest };
|
|
130
|
+
if (algorithms) verifyOpts.algorithms = algorithms;
|
|
131
|
+
const { payload } = await jwtVerify(
|
|
132
|
+
token,
|
|
133
|
+
resolver as unknown as KeyLike,
|
|
134
|
+
verifyOpts,
|
|
135
|
+
);
|
|
136
|
+
return payload as TokenPayload;
|
|
137
|
+
},
|
|
138
|
+
|
|
139
|
+
decode(token: string): TokenHeader | undefined {
|
|
140
|
+
try {
|
|
141
|
+
return decodeProtectedHeader(token) as TokenHeader;
|
|
142
|
+
} catch {
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Default export — the ext-jwt extension factory.
|
|
151
|
+
*
|
|
152
|
+
* Produces an extension that registers an `AuthProvider` contract
|
|
153
|
+
* implementation backed by `jose`.
|
|
154
|
+
*/
|
|
155
|
+
const extJwt: ExtensionFactory = (config?: unknown) => {
|
|
156
|
+
const cfg = (config ?? {}) as ExtJwtConfig;
|
|
157
|
+
const provider = createAuthProvider(cfg);
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
name: "ext-jwt",
|
|
161
|
+
version: "0.1.0",
|
|
162
|
+
capabilities: [
|
|
163
|
+
{ type: "contract", name: "AuthProvider" },
|
|
164
|
+
{ type: "network", host: "*" },
|
|
165
|
+
],
|
|
166
|
+
provides: {
|
|
167
|
+
AuthProvider: provider,
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
export default extJwt;
|
|
173
|
+
export { createAuthProvider };
|