@scalar/mock-server 0.17.5 → 0.18.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/CHANGELOG.md +21 -0
- package/dist/create-asyncapi-mock-server.d.ts.map +1 -1
- package/dist/create-asyncapi-mock-server.js +8 -0
- package/dist/routes/mock-handler-response.d.ts.map +1 -1
- package/dist/routes/mock-handler-response.js +7 -5
- package/dist/routes/respond-with-authorize-page.d.ts +1 -1
- package/dist/routes/respond-with-authorize-page.d.ts.map +1 -1
- package/dist/routes/respond-with-authorize-page.js +9 -6
- package/dist/routes/respond-with-token.d.ts +1 -10
- package/dist/routes/respond-with-token.d.ts.map +1 -1
- package/dist/routes/respond-with-token.js +8 -5
- package/dist/transports/sse.d.ts.map +1 -1
- package/dist/transports/sse.js +17 -13
- package/dist/transports/types.d.ts +10 -0
- package/dist/transports/types.d.ts.map +1 -1
- package/dist/transports/websocket.d.ts.map +1 -1
- package/dist/transports/websocket.js +38 -30
- package/dist/utils/authenticate-asyncapi-channel.d.ts +9 -0
- package/dist/utils/authenticate-asyncapi-channel.d.ts.map +1 -0
- package/dist/utils/authenticate-asyncapi-channel.js +49 -0
- package/dist/utils/build-handler-context.d.ts.map +1 -1
- package/dist/utils/build-handler-context.js +6 -5
- package/dist/utils/deserialize-parameter.d.ts.map +1 -1
- package/dist/utils/deserialize-parameter.js +7 -2
- package/dist/utils/digest-authentication.d.ts +6 -0
- package/dist/utils/digest-authentication.d.ts.map +1 -0
- package/dist/utils/digest-authentication.js +35 -0
- package/dist/utils/handle-authentication.d.ts +14 -0
- package/dist/utils/handle-authentication.d.ts.map +1 -1
- package/dist/utils/handle-authentication.js +28 -14
- package/dist/utils/log-authentication-instructions.d.ts +3 -1
- package/dist/utils/log-authentication-instructions.d.ts.map +1 -1
- package/dist/utils/log-authentication-instructions.js +10 -2
- package/dist/utils/querystring-parameter.d.ts.map +1 -1
- package/dist/utils/querystring-parameter.js +13 -11
- package/dist/utils/resolve-channels.d.ts.map +1 -1
- package/dist/utils/resolve-channels.js +16 -1
- package/dist/utils/set-up-asyncapi-authentication-routes.d.ts +5 -0
- package/dist/utils/set-up-asyncapi-authentication-routes.d.ts.map +1 -0
- package/dist/utils/set-up-asyncapi-authentication-routes.js +49 -0
- package/dist/utils/set-up-authentication-routes.d.ts.map +1 -1
- package/dist/utils/set-up-authentication-routes.js +9 -18
- package/dist/utils/set-up-device-authorization.js +1 -1
- package/dist/utils/set-up-openid-connect.d.ts +4 -0
- package/dist/utils/set-up-openid-connect.d.ts.map +1 -0
- package/dist/utils/set-up-openid-connect.js +196 -0
- package/dist/utils/streaming-response.js +1 -1
- package/package.json +7 -7
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
# @scalar/mock-server
|
|
2
2
|
|
|
3
|
+
## 0.18.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#10571](https://github.com/scalar/scalar/pull/10571): Enforce AsyncAPI WebSocket/SSE security per server and operation, accept case-insensitive HTTP authentication schemes, add Digest credential-shape checks and verified mutual TLS, and report unsupported broker schemes.
|
|
8
|
+
- [#10572](https://github.com/scalar/scalar/pull/10572): Add a development OpenID Connect code-flow provider with signed ID tokens, JWKS, PKCE, token expiry and refresh rotation, and scoped UserInfo.
|
|
9
|
+
|
|
10
|
+
### Patch Changes
|
|
11
|
+
|
|
12
|
+
- [#10605](https://github.com/scalar/scalar/pull/10605): Reject non-HTTP OAuth callback URLs and escape callback links in the mock authorization page.
|
|
13
|
+
- [#10614](https://github.com/scalar/scalar/pull/10614): Clean up lint warnings while preserving runtime behavior, and document intentional schema and type syntax.
|
|
14
|
+
- [#10560](https://github.com/scalar/scalar/pull/10560): Register OAuth authorization, token, and refresh endpoints for AsyncAPI mock servers and point the Galaxy AsyncAPI example at its existing token endpoint for refresh requests.
|
|
15
|
+
|
|
16
|
+
Support OpenID Connect discovery and token routes in AsyncAPI mocks, return discovery endpoints on the mock origin, and accept form-encoded token exchanges and refresh requests.
|
|
17
|
+
|
|
18
|
+
## 0.17.6
|
|
19
|
+
|
|
20
|
+
### Patch Changes
|
|
21
|
+
|
|
22
|
+
- [#10559](https://github.com/scalar/scalar/pull/10559): Clean up Biome warnings in package code and tests. Handle rejected debounced callbacks and await streaming writes.
|
|
23
|
+
|
|
3
24
|
## 0.17.5
|
|
4
25
|
|
|
5
26
|
## 0.17.4
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-asyncapi-mock-server.d.ts","sourceRoot":"","sources":["../src/create-asyncapi-mock-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,mBAAmB,EAAoB,MAAM,mBAAmB,CAAA;AAC9E,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AAK3B,OAAO,KAAK,EAAE,gBAAgB,EAAE,aAAa,EAAoB,MAAM,oBAAoB,CAAA;AAC3F,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"create-asyncapi-mock-server.d.ts","sourceRoot":"","sources":["../src/create-asyncapi-mock-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,mBAAmB,EAAoB,MAAM,mBAAmB,CAAA;AAC9E,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AAK3B,OAAO,KAAK,EAAE,gBAAgB,EAAE,aAAa,EAAoB,MAAM,oBAAoB,CAAA;AAC3F,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAQ/C,oDAAoD;AACpD,MAAM,MAAM,yBAAyB,GAAG;IACtC;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAEvC,sFAAsF;IACtF,MAAM,CAAC,EAAE,MAAM,CAAA;IAEf;;;;OAIG;IACH,UAAU,CAAC,EAAE,aAAa,EAAE,CAAA;IAE5B,yFAAyF;IACzF,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,gBAAgB,CAAC;QAAC,OAAO,EAAE,OAAO,CAAA;KAAE,KAAK,IAAI,CAAA;IAE/F;;;;;OAKG;IACH,MAAM,CAAC,EAAE,OAAO,GAAG,gBAAgB,CAAA;CACpC,CAAA;AAED,sDAAsD;AACtD,MAAM,MAAM,kBAAkB,GAAG;IAC/B,sEAAsE;IACtE,GAAG,EAAE,IAAI,CAAA;IACT,wFAAwF;IACxF,SAAS,EAAE;QAAE,MAAM,EAAE,mBAAmB,CAAA;KAAE,CAAA;CAC3C,CAAA;AAED;;;;;;;;;;;;GAYG;AACH,wBAAsB,wBAAwB,CAAC,OAAO,EAAE,yBAAyB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CA8C9G"}
|
|
@@ -3,10 +3,12 @@ import { Hono } from 'hono';
|
|
|
3
3
|
import { cors } from 'hono/cors';
|
|
4
4
|
import { WebSocketServer } from 'ws';
|
|
5
5
|
import { defaultTransports } from './transports/index.js';
|
|
6
|
+
import { authenticateAsyncApiChannel, warnUnsupportedAsyncApiSecurity } from './utils/authenticate-asyncapi-channel.js';
|
|
6
7
|
import { generateMessage } from './utils/generate-message.js';
|
|
7
8
|
import { processAsyncApiDocument } from './utils/process-asyncapi-document.js';
|
|
8
9
|
import { resolveChannels } from './utils/resolve-channels.js';
|
|
9
10
|
import { resolveLogger } from './utils/resolve-logger.js';
|
|
11
|
+
import { setUpAsyncApiAuthenticationRoutes } from './utils/set-up-asyncapi-authentication-routes.js';
|
|
10
12
|
/**
|
|
11
13
|
* Create a mock server for an AsyncAPI 3.1 document — the event-driven counterpart of
|
|
12
14
|
* {@link createMockServer}. Each channel is registered on a transport (WebSocket or SSE by
|
|
@@ -28,6 +30,7 @@ export async function createAsyncApiMockServer(options) {
|
|
|
28
30
|
const log = resolveLogger(options.logger, false);
|
|
29
31
|
// CORS for the SSE/HTTP routes (WebSocket upgrades are not subject to CORS).
|
|
30
32
|
app.use(cors());
|
|
33
|
+
setUpAsyncApiAuthenticationRoutes(app, document);
|
|
31
34
|
const context = {
|
|
32
35
|
app,
|
|
33
36
|
upgradeWebSocket,
|
|
@@ -36,6 +39,7 @@ export async function createAsyncApiMockServer(options) {
|
|
|
36
39
|
log,
|
|
37
40
|
};
|
|
38
41
|
for (const channel of channels) {
|
|
42
|
+
warnUnsupportedAsyncApiSecurity(channel);
|
|
39
43
|
const transport = transports.find((candidate) => candidate.supports(channel));
|
|
40
44
|
if (!transport) {
|
|
41
45
|
// A channel with no matching transport is a configuration problem, so surface it through the
|
|
@@ -43,6 +47,10 @@ export async function createAsyncApiMockServer(options) {
|
|
|
43
47
|
console.warn(`[asyncapi] no transport for channel "${channel.id}" (protocols: ${channel.protocols.join(', ') || 'none'})`);
|
|
44
48
|
continue;
|
|
45
49
|
}
|
|
50
|
+
// An unsecured server on a different protocol cannot authorize this transport.
|
|
51
|
+
const compatibleSecurity = channel.security?.filter(({ protocol }) => transport.supports({ ...channel, protocols: [protocol] }));
|
|
52
|
+
const securedChannel = compatibleSecurity?.length ? { ...channel, security: compatibleSecurity } : channel;
|
|
53
|
+
app.use(channel.route, authenticateAsyncApiChannel(securedChannel, defaultTransports.includes(transport)));
|
|
46
54
|
transport.register(channel, context);
|
|
47
55
|
log(`[asyncapi] ${transport.name} -> ${channel.route} (channel "${channel.id}")`);
|
|
48
56
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mock-handler-response.d.ts","sourceRoot":"","sources":["../../src/routes/mock-handler-response.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAIxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"mock-handler-response.d.ts","sourceRoot":"","sources":["../../src/routes/mock-handler-response.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAIxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAqJnC;;;GAGG;AACH,wBAAsB,mBAAmB,CACvC,CAAC,EAAE,OAAO,EACV,SAAS,EAAE,WAAW,CAAC,eAAe,EACtC,kBAAkB,CAAC,EAAE,WAAW,CAAC,cAAc,CAAC,YAAY,CAAC,EAC7D,cAAc,CAAC,EAAE,MAAM,qBA8ExB"}
|
|
@@ -61,11 +61,13 @@ function getExampleFromResponse(c, statusCode, responses, exampleName, openapiVe
|
|
|
61
61
|
c.header('Content-Type', acceptedContentType);
|
|
62
62
|
return serializeResponseBody(selectedExample.value, acceptedContentType, responseSchema, provenance) ?? null;
|
|
63
63
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
64
|
+
let value = null;
|
|
65
|
+
if (selectedExample) {
|
|
66
|
+
value = normalizeResponseBody(selectedExample.value, responseSchema);
|
|
67
|
+
}
|
|
68
|
+
else if (responseSchema) {
|
|
69
|
+
value = normalizeResponseBody(generateResponseExample(responseSchema, pathParameters(c)), responseSchema);
|
|
70
|
+
}
|
|
69
71
|
// Legacy examples retain the handler fallback's JSON encoding policy.
|
|
70
72
|
return JSON.stringify(value) ?? null;
|
|
71
73
|
}
|
|
@@ -2,5 +2,5 @@ import type { Context } from 'hono';
|
|
|
2
2
|
/**
|
|
3
3
|
* Responds with an HTML page that simulates an OAuth 2.0 authorization page.
|
|
4
4
|
*/
|
|
5
|
-
export declare function respondWithAuthorizePage(c: Context, title?: string): Response;
|
|
5
|
+
export declare function respondWithAuthorizePage(c: Context, title?: string, authorizationCode?: string): Response;
|
|
6
6
|
//# sourceMappingURL=respond-with-authorize-page.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"respond-with-authorize-page.d.ts","sourceRoot":"","sources":["../../src/routes/respond-with-authorize-page.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAgCnC;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,SAAK,
|
|
1
|
+
{"version":3,"file":"respond-with-authorize-page.d.ts","sourceRoot":"","sources":["../../src/routes/respond-with-authorize-page.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAgCnC;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,SAAK,EAAE,iBAAiB,SAA6B,YA+E9G"}
|
|
@@ -3,7 +3,7 @@ const EXAMPLE_AUTHORIZATION_CODE = 'super-secret-token';
|
|
|
3
3
|
/** Always responds with this token for implicit flow */
|
|
4
4
|
const EXAMPLE_ACCESS_TOKEN = 'super-secret-access-token';
|
|
5
5
|
/**
|
|
6
|
-
* Escapes
|
|
6
|
+
* Escapes user-controlled text and quoted HTML attribute values.
|
|
7
7
|
*/
|
|
8
8
|
function escapeHtml(s) {
|
|
9
9
|
return s
|
|
@@ -28,7 +28,7 @@ function parseScopeParam(scopeQuery) {
|
|
|
28
28
|
/**
|
|
29
29
|
* Responds with an HTML page that simulates an OAuth 2.0 authorization page.
|
|
30
30
|
*/
|
|
31
|
-
export function respondWithAuthorizePage(c, title = '') {
|
|
31
|
+
export function respondWithAuthorizePage(c, title = '', authorizationCode = EXAMPLE_AUTHORIZATION_CODE) {
|
|
32
32
|
const redirectUri = c.req.query('redirect_uri');
|
|
33
33
|
const responseType = c.req.query('response_type');
|
|
34
34
|
const scope = c.req.query('scope');
|
|
@@ -37,8 +37,11 @@ export function respondWithAuthorizePage(c, title = '') {
|
|
|
37
37
|
return c.html(generateErrorHtml('Missing redirect_uri parameter', 'This parameter is required for the OAuth 2.0 authorization flow to function correctly. Please provide a valid redirect URI in your request.'), 400);
|
|
38
38
|
}
|
|
39
39
|
try {
|
|
40
|
-
// Validate redirect URI against allowed domains
|
|
41
40
|
const redirectUrl = new URL(redirectUri);
|
|
41
|
+
// Mock callbacks can use any host, but must never execute code in this page.
|
|
42
|
+
if (redirectUrl.protocol !== 'https:' && redirectUrl.protocol !== 'http:') {
|
|
43
|
+
return c.html(generateErrorHtml('Invalid redirect_uri protocol', 'The redirect_uri must use HTTP or HTTPS.'), 400);
|
|
44
|
+
}
|
|
42
45
|
const isImplicitFlow = responseType === 'token';
|
|
43
46
|
if (isImplicitFlow) {
|
|
44
47
|
const fragmentParams = new URLSearchParams();
|
|
@@ -54,7 +57,7 @@ export function respondWithAuthorizePage(c, title = '') {
|
|
|
54
57
|
redirectUrl.hash = fragmentParams.toString();
|
|
55
58
|
}
|
|
56
59
|
else {
|
|
57
|
-
redirectUrl.searchParams.set('code',
|
|
60
|
+
redirectUrl.searchParams.set('code', authorizationCode);
|
|
58
61
|
if (state) {
|
|
59
62
|
redirectUrl.searchParams.set('state', state);
|
|
60
63
|
}
|
|
@@ -126,10 +129,10 @@ function generateAuthorizationHtml(redirectUrl, deniedUrl, title = '', scopes =
|
|
|
126
129
|
</p>
|
|
127
130
|
</div>
|
|
128
131
|
<div class="px-6 py-4 pt-0 flex justify-between">
|
|
129
|
-
<a href="${deniedUrl}" class="inline-block px-6 py-2 text-gray-600 rounded border" aria-label="Cancel authorization">
|
|
132
|
+
<a href="${escapeHtml(deniedUrl)}" class="inline-block px-6 py-2 text-gray-600 rounded border" aria-label="Cancel authorization">
|
|
130
133
|
Cancel
|
|
131
134
|
</a>
|
|
132
|
-
<a href="${redirectUrl}" class="inline-block px-6 py-2 bg-black text-white rounded transition-colors duration-300 hover:bg-gray-800" aria-label="Authorize application">
|
|
135
|
+
<a href="${escapeHtml(redirectUrl)}" class="inline-block px-6 py-2 bg-black text-white rounded transition-colors duration-300 hover:bg-gray-800" aria-label="Authorize application">
|
|
133
136
|
Authorize
|
|
134
137
|
</a>
|
|
135
138
|
</div>
|
|
@@ -2,14 +2,5 @@ import type { Context } from 'hono';
|
|
|
2
2
|
/**
|
|
3
3
|
* Responds with a JSON object simulating an OAuth 2.0 token response.
|
|
4
4
|
*/
|
|
5
|
-
export declare
|
|
6
|
-
error: string;
|
|
7
|
-
error_description: string;
|
|
8
|
-
}, 400, "json">) | (Response & import("hono").TypedResponse<{
|
|
9
|
-
access_token: string;
|
|
10
|
-
token_type: string;
|
|
11
|
-
expires_in: number;
|
|
12
|
-
refresh_token: string;
|
|
13
|
-
scope: string;
|
|
14
|
-
}, import("hono/utils/http-status").ContentfulStatusCode, "json">);
|
|
5
|
+
export declare const respondWithToken: (c: Context) => Promise<Response>;
|
|
15
6
|
//# sourceMappingURL=respond-with-token.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"respond-with-token.d.ts","sourceRoot":"","sources":["../../src/routes/respond-with-token.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAKnC;;GAEG;AACH,
|
|
1
|
+
{"version":3,"file":"respond-with-token.d.ts","sourceRoot":"","sources":["../../src/routes/respond-with-token.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAKnC;;GAEG;AACH,eAAO,MAAM,gBAAgB,GAAU,GAAG,OAAO,KAAG,OAAO,CAAC,QAAQ,CAuDnE,CAAA"}
|
|
@@ -3,8 +3,11 @@ const EXAMPLE_ACCESS_TOKEN = 'super-secret-access-token';
|
|
|
3
3
|
/**
|
|
4
4
|
* Responds with a JSON object simulating an OAuth 2.0 token response.
|
|
5
5
|
*/
|
|
6
|
-
export
|
|
7
|
-
const
|
|
6
|
+
export const respondWithToken = async (c) => {
|
|
7
|
+
const body = await c.req.parseBody();
|
|
8
|
+
// OAuth clients send form data; retain query parameters for existing mock consumers.
|
|
9
|
+
const parameter = (name) => typeof body[name] === 'string' ? body[name] : c.req.query(name);
|
|
10
|
+
const grantType = parameter('grant_type');
|
|
8
11
|
if (!grantType) {
|
|
9
12
|
return c.json({
|
|
10
13
|
error: 'invalid_request',
|
|
@@ -20,7 +23,7 @@ export function respondWithToken(c) {
|
|
|
20
23
|
}, 400);
|
|
21
24
|
}
|
|
22
25
|
// Validate required parameters for each grant type
|
|
23
|
-
if (grantType === 'authorization_code' && !
|
|
26
|
+
if (grantType === 'authorization_code' && !parameter('code')) {
|
|
24
27
|
return c.json({
|
|
25
28
|
error: 'invalid_request',
|
|
26
29
|
error_description: 'Missing code parameter',
|
|
@@ -32,10 +35,10 @@ export function respondWithToken(c) {
|
|
|
32
35
|
token_type: 'Bearer',
|
|
33
36
|
expires_in: 3600,
|
|
34
37
|
refresh_token: 'example-refresh-token',
|
|
35
|
-
scope:
|
|
38
|
+
scope: parameter('scope') ?? 'read write',
|
|
36
39
|
};
|
|
37
40
|
// Security headers
|
|
38
41
|
c.header('Cache-Control', 'no-store');
|
|
39
42
|
c.header('Pragma', 'no-cache');
|
|
40
43
|
return c.json(tokenResponse);
|
|
41
|
-
}
|
|
44
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../src/transports/sse.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAqB,MAAM,oBAAoB,CAAA;
|
|
1
|
+
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../src/transports/sse.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAqB,MAAM,oBAAoB,CAAA;AAG1E;;;;;;;GAOG;AACH,eAAO,MAAM,YAAY,EAAE,aAoC1B,CAAA"}
|
package/dist/transports/sse.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { streamSSE } from 'hono/streaming';
|
|
2
|
+
import { getAuthenticatedChannel } from '../utils/authenticate-asyncapi-channel.js';
|
|
2
3
|
/**
|
|
3
4
|
* Built-in Server-Sent Events transport. Serves one-way, server-push channels over HTTP: a `GET`
|
|
4
5
|
* on the channel route opens an SSE stream and emits a generated message per `receive` operation.
|
|
@@ -19,19 +20,22 @@ export const sseTransport = {
|
|
|
19
20
|
},
|
|
20
21
|
register: (channel, context) => {
|
|
21
22
|
const { app, generateMessage, onMessage, log } = context;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
23
|
+
app.get(channel.route, (c) => {
|
|
24
|
+
const authorized = getAuthenticatedChannel(c, channel);
|
|
25
|
+
const receiveOperations = authorized.operations.filter((operation) => operation.action === 'receive');
|
|
26
|
+
// Fall back to all channel messages when the channel has no explicit receive operation.
|
|
27
|
+
const operations = channel.operations.length > 0 ? receiveOperations : [{ messages: channel.messages }];
|
|
28
|
+
return streamSSE(c, async (stream) => {
|
|
29
|
+
log(`[sse] open ${channel.route}`);
|
|
30
|
+
for (const operation of operations) {
|
|
31
|
+
const message = generateMessage({ ...channel, messages: operation.messages });
|
|
32
|
+
if (message) {
|
|
33
|
+
await stream.writeSSE({ data: message.data, event: message.event });
|
|
34
|
+
onMessage?.({ channel: channel.id, direction: 'out', payload: message.data });
|
|
35
|
+
}
|
|
32
36
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
})
|
|
37
|
+
log(`[sse] close ${channel.route}`);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
36
40
|
},
|
|
37
41
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { OpenAPIV3_1 } from '@scalar/openapi-types';
|
|
2
|
+
import type { AsyncApiSecuritySchemeObject } from '@scalar/types/asyncapi/3.1';
|
|
2
3
|
import type { Hono } from 'hono';
|
|
3
4
|
import type { UpgradeWebSocket } from 'hono/ws';
|
|
4
5
|
/**
|
|
@@ -15,8 +16,12 @@ export type ResolvedMessage = {
|
|
|
15
16
|
/** Content type used to encode the payload (defaults to the document's `defaultContentType`). */
|
|
16
17
|
contentType?: string;
|
|
17
18
|
};
|
|
19
|
+
/** Alternatives in an AsyncAPI security array; unresolved references remain unsatisfied. */
|
|
20
|
+
export type AsyncApiSecurity = Array<AsyncApiSecuritySchemeObject | undefined>;
|
|
18
21
|
/** A single AsyncAPI operation (`send`/`receive`) bound to its channel. */
|
|
19
22
|
export type ResolvedOperation = {
|
|
23
|
+
/** Operation security, after merging traits. Empty or absent means no additional requirement. */
|
|
24
|
+
security?: AsyncApiSecurity;
|
|
20
25
|
/** The operation id (its key in the document's `operations` map). */
|
|
21
26
|
id: string;
|
|
22
27
|
/**
|
|
@@ -30,6 +35,11 @@ export type ResolvedOperation = {
|
|
|
30
35
|
};
|
|
31
36
|
/** A normalized, dereferenced view of an AsyncAPI channel that a transport can serve. */
|
|
32
37
|
export type ResolvedChannel = {
|
|
38
|
+
/** Alternative servers and their security alternatives. */
|
|
39
|
+
security?: Array<{
|
|
40
|
+
protocol: string;
|
|
41
|
+
schemes: AsyncApiSecurity;
|
|
42
|
+
}>;
|
|
33
43
|
/** The channel id (its key in the document's `channels` map). */
|
|
34
44
|
id: string;
|
|
35
45
|
/** The raw channel address, e.g. `user/signedup` or `rooms/{roomId}`. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/transports/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,MAAM,CAAA;AAChC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAE/C;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B,sFAAsF;IACtF,EAAE,EAAE,MAAM,CAAA;IACV,+FAA+F;IAC/F,OAAO,CAAC,EAAE,WAAW,CAAC,YAAY,CAAA;IAClC,uFAAuF;IACvF,QAAQ,EAAE,OAAO,EAAE,CAAA;IACnB,iGAAiG;IACjG,WAAW,CAAC,EAAE,MAAM,CAAA;CAC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AC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;CAC5B,CAAA;AAED;;;;GAIG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,oDAAoD;IACpD,IAAI,EAAE,MAAM,CAAA;IACZ,iFAAiF;IACjF,QAAQ,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,OAAO,CAAA;IAC/C,uFAAuF;IACvF,QAAQ,EAAE,CAAC,OAAO,EAAE,eAAe,EAAE,OAAO,EAAE,gBAAgB,KAAK,IAAI,CAAA;CACxE,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"websocket.d.ts","sourceRoot":"","sources":["../../src/transports/websocket.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;
|
|
1
|
+
{"version":3,"file":"websocket.d.ts","sourceRoot":"","sources":["../../src/transports/websocket.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAGvD;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,EAAE,aAmDhC,CAAA"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getAuthenticatedChannel } from '../utils/authenticate-asyncapi-channel.js';
|
|
1
2
|
/**
|
|
2
3
|
* Built-in WebSocket transport. Serves channels whose servers speak `ws`/`wss` by upgrading the
|
|
3
4
|
* channel route to a WebSocket connection.
|
|
@@ -10,36 +11,43 @@ export const websocketTransport = {
|
|
|
10
11
|
supports: (channel) => channel.protocols.includes('ws') || channel.protocols.includes('wss'),
|
|
11
12
|
register: (channel, context) => {
|
|
12
13
|
const { app, upgradeWebSocket, generateMessage, onMessage, log } = context;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
14
|
+
app.get(channel.route, upgradeWebSocket((c) => {
|
|
15
|
+
const authorized = getAuthenticatedChannel(c, channel);
|
|
16
|
+
const receiveOperations = authorized.operations.filter((operation) => operation.action === 'receive');
|
|
17
|
+
const sendOperation = authorized.operations.find((operation) => operation.action === 'send');
|
|
18
|
+
return {
|
|
19
|
+
onOpen: (_event, ws) => {
|
|
20
|
+
log(`[ws] open ${channel.route}`);
|
|
21
|
+
// Push a single message per receive operation on connect (quiet, deterministic default).
|
|
22
|
+
for (const operation of receiveOperations) {
|
|
23
|
+
const message = generateMessage({ ...channel, messages: operation.messages });
|
|
24
|
+
if (message) {
|
|
25
|
+
ws.send(message.data);
|
|
26
|
+
onMessage?.({ channel: channel.id, direction: 'out', payload: message.data });
|
|
27
|
+
}
|
|
24
28
|
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
29
|
+
},
|
|
30
|
+
onMessage: (event, ws) => {
|
|
31
|
+
if (!sendOperation && channel.operations.some((operation) => operation.action === 'send')) {
|
|
32
|
+
ws.close(1008, 'Not authorized to send messages');
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const incoming = typeof event.data === 'string' ? event.data : '[binary]';
|
|
36
|
+
log(`[ws] recv ${channel.route}: ${incoming}`);
|
|
37
|
+
onMessage?.({ channel: channel.id, direction: 'in', payload: incoming });
|
|
38
|
+
// Only echo a reply when the channel declares a `send` operation. Receive-only channels
|
|
39
|
+
// (server push) stay quiet on inbound frames instead of fabricating an unsolicited reply.
|
|
40
|
+
if (!sendOperation) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const reply = generateMessage({ ...channel, messages: sendOperation.messages });
|
|
44
|
+
if (reply) {
|
|
45
|
+
ws.send(reply.data);
|
|
46
|
+
onMessage?.({ channel: channel.id, direction: 'out', payload: reply.data });
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
onClose: () => log(`[ws] close ${channel.route}`),
|
|
50
|
+
};
|
|
51
|
+
}));
|
|
44
52
|
},
|
|
45
53
|
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Context, MiddlewareHandler } from 'hono';
|
|
2
|
+
import type { ResolvedChannel } from '../transports/types.js';
|
|
3
|
+
/** Return the operations permitted for this connection, without modifying the shared channel. */
|
|
4
|
+
export declare const getAuthenticatedChannel: (c: Context, channel: ResolvedChannel) => ResolvedChannel;
|
|
5
|
+
/** Check server security before SSE or WebSocket upgrade and hide unauthorized operations. */
|
|
6
|
+
export declare const authenticateAsyncApiChannel: (channel: ResolvedChannel, filterOperations?: boolean) => MiddlewareHandler;
|
|
7
|
+
/** Report schemes that HTTP transports cannot authenticate instead of silently ignoring them. */
|
|
8
|
+
export declare const warnUnsupportedAsyncApiSecurity: (channel: ResolvedChannel) => void;
|
|
9
|
+
//# sourceMappingURL=authenticate-asyncapi-channel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authenticate-asyncapi-channel.d.ts","sourceRoot":"","sources":["../../src/utils/authenticate-asyncapi-channel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAA;AAEtD,OAAO,KAAK,EAAoB,eAAe,EAAE,MAAM,oBAAoB,CAAA;AAM3E,iGAAiG;AACjG,eAAO,MAAM,uBAAuB,GAAI,GAAG,OAAO,EAAE,SAAS,eAAe,KAAG,eACf,CAAA;AAEhE,8FAA8F;AAC9F,eAAO,MAAM,2BAA2B,GACrC,SAAS,eAAe,EAAE,0BAAwB,KAAG,iBAyBrD,CAAA;AAEH,iGAAiG;AACjG,eAAO,MAAM,+BAA+B,GAAI,SAAS,eAAe,KAAG,IA0B1E,CAAA"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { getChallenge, isSchemeSatisfied } from './handle-authentication.js';
|
|
2
|
+
const CHANNEL_KEY = 'scalarAuthenticatedChannel';
|
|
3
|
+
/** Return the operations permitted for this connection, without modifying the shared channel. */
|
|
4
|
+
export const getAuthenticatedChannel = (c, channel) => c.get(CHANNEL_KEY) ?? channel;
|
|
5
|
+
/** Check server security before SSE or WebSocket upgrade and hide unauthorized operations. */
|
|
6
|
+
export const authenticateAsyncApiChannel = (channel, filterOperations = false) => async (c, next) => {
|
|
7
|
+
const satisfies = (alternatives = []) => alternatives.length === 0 || alternatives.some((scheme) => scheme !== undefined && isSchemeSatisfied(scheme, c));
|
|
8
|
+
const serverAllowed = !channel.security?.length || channel.security.some(({ schemes }) => satisfies(schemes));
|
|
9
|
+
const operations = channel.operations.filter((operation) => satisfies(operation.security));
|
|
10
|
+
if (!serverAllowed ||
|
|
11
|
+
(channel.operations.length > 0 && operations.length === 0) ||
|
|
12
|
+
(!filterOperations && operations.length !== channel.operations.length)) {
|
|
13
|
+
const schemes = [
|
|
14
|
+
...(channel.security ?? []).flatMap(({ schemes }) => schemes),
|
|
15
|
+
...channel.operations.flatMap((operation) => operation.security ?? []),
|
|
16
|
+
];
|
|
17
|
+
const challenges = new Set(schemes.flatMap((scheme) => (scheme ? [getChallenge(scheme)].filter((value) => value !== null) : [])));
|
|
18
|
+
for (const challenge of challenges) {
|
|
19
|
+
c.header('WWW-Authenticate', challenge, { append: true });
|
|
20
|
+
}
|
|
21
|
+
return c.json({ error: 'Unauthorized', message: 'Authentication is required to access this channel.' }, 401);
|
|
22
|
+
}
|
|
23
|
+
c.set(CHANNEL_KEY, { ...channel, operations });
|
|
24
|
+
return await next();
|
|
25
|
+
};
|
|
26
|
+
/** Report schemes that HTTP transports cannot authenticate instead of silently ignoring them. */
|
|
27
|
+
export const warnUnsupportedAsyncApiSecurity = (channel) => {
|
|
28
|
+
const schemes = [
|
|
29
|
+
...(channel.security ?? []).flatMap(({ schemes }) => schemes),
|
|
30
|
+
...channel.operations.flatMap((operation) => operation.security ?? []),
|
|
31
|
+
];
|
|
32
|
+
const unsupported = new Set(schemes.flatMap((scheme) => {
|
|
33
|
+
if (!scheme) {
|
|
34
|
+
return ['unresolved security reference'];
|
|
35
|
+
}
|
|
36
|
+
if (scheme.type === 'http') {
|
|
37
|
+
return ['basic', 'bearer', 'digest'].includes(scheme.scheme?.toLowerCase() ?? '')
|
|
38
|
+
? []
|
|
39
|
+
: [`http/${scheme.scheme}`];
|
|
40
|
+
}
|
|
41
|
+
if (['httpApiKey', 'oauth2', 'openIdConnect', 'X509'].includes(scheme.type)) {
|
|
42
|
+
return [];
|
|
43
|
+
}
|
|
44
|
+
return [scheme.type];
|
|
45
|
+
}));
|
|
46
|
+
for (const type of unsupported) {
|
|
47
|
+
console.warn(`[asyncapi] channel "${channel.id}" uses unsupported security: ${type}. WebSocket/SSE cannot authenticate this scheme; it will not satisfy a security requirement.`);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build-handler-context.d.ts","sourceRoot":"","sources":["../../src/utils/build-handler-context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAGxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AASnC,OAAO,EAAE,KAAK,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAA;AAGjF;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,KAAK,EAAE,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC,cAAc,CAAC,CAAA;IAC5D,GAAG,EAAE;QACH,IAAI,EAAE,GAAG,CAAA;QACT,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;QAC9B,oFAAoF;QACpF,KAAK,EAAE,OAAO,CAAA;QACd,OAAO,EAAE,MAAM,CAAC,MAAM,EA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
|
@@ -47,11 +47,12 @@ function getExampleFromResponse(c, statusCode, responses) {
|
|
|
47
47
|
}
|
|
48
48
|
const responseSchema = acceptedResponse.schema ? getResolvedRefDeep(acceptedResponse.schema) : undefined;
|
|
49
49
|
// Extract example from example property or generate from schema
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
if (acceptedResponse.example !== undefined) {
|
|
51
|
+
return normalizeResponseBody(acceptedResponse.example, responseSchema);
|
|
52
|
+
}
|
|
53
|
+
return responseSchema
|
|
54
|
+
? normalizeResponseBody(generateResponseExample(responseSchema, pathParameters(c)), responseSchema)
|
|
55
|
+
: null;
|
|
55
56
|
}
|
|
56
57
|
/**
|
|
57
58
|
* Build the handler context from a Hono context.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deserialize-parameter.d.ts","sourceRoot":"","sources":["../../src/utils/deserialize-parameter.ts"],"names":[],"mappings":"AACA;;;;;GAKG;AAEH,oFAAoF;AACpF,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,GAAG,QAAQ,CAAA;AAUtE;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,GAC/B,UAAU,iBAAiB,EAC3B,QAAQ,MAAM,EACd,UAAU,OAAO,KAChB;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAGnC,CAAA;AA2BD,8GAA8G;AAC9G,eAAO,MAAM,aAAa,GAAI,QAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,KAAG,OAU3E,CAAA;AAED,gHAAgH;AAChH,eAAO,MAAM,cAAc,GAAI,QAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,KAAG,OAU5E,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,GAAI,QAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAA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M,GAAG,SAAS,CAAA;IAC1B;;;;OAIG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,GAAG,SAAS,CAAA;IACnD,IAAI,EAAE,MAAM,CAAA;IACZ,mGAAmG;IACnG,aAAa,CAAC,EAAE,MAAM,EAAE,GAAG,SAAS,CAAA;IACpC;;;;OAIG;IACH,YAAY,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,SAAS,CAAA;CACvC,KAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,GAAG,SAoEvC,CAAA"}
|
|
@@ -95,7 +95,12 @@ export const getObjectPropertyNames = (schema) => {
|
|
|
95
95
|
const wrap = (value) => (value === undefined ? undefined : [value]);
|
|
96
96
|
// An empty value is an empty list, not a one-element list of the empty string. Otherwise `?ids=`
|
|
97
97
|
// would satisfy a `minItems: 1` array while failing the element type check on `''`.
|
|
98
|
-
const split = (value, delimiter) =>
|
|
98
|
+
const split = (value, delimiter) => {
|
|
99
|
+
if (value === undefined) {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
return value === '' ? [] : value.split(delimiter);
|
|
103
|
+
};
|
|
99
104
|
/**
|
|
100
105
|
* Build an exploded array from the repeated values (query) or the single value (other locations).
|
|
101
106
|
*
|
|
@@ -185,7 +190,7 @@ export const deserializeArrayParameter = ({ style, explode, single, multi, }) =>
|
|
|
185
190
|
case 'simple':
|
|
186
191
|
// Path and header arrays are comma-separated; `explode` does not change the delimiter. HTTP allows
|
|
187
192
|
// optional whitespace after the comma in header list values (`a, b, c`), so trim each element.
|
|
188
|
-
return single
|
|
193
|
+
return split(single, ',')?.map((element) => element.trim());
|
|
189
194
|
case 'label':
|
|
190
195
|
// Path `label` arrays are dot-prefixed. Non-exploded values are comma-separated (`.1,2,3`),
|
|
191
196
|
// exploded values are dot-separated (`.1.2.3`).
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Context } from 'hono';
|
|
2
|
+
/** A deterministic challenge lets HTTP clients exercise Digest without a credential database. */
|
|
3
|
+
export declare const DIGEST_CHALLENGE = "Digest realm=\"Scalar Mock Server\", nonce=\"scalar-mock-nonce\", algorithm=MD5, qop=\"auth\"";
|
|
4
|
+
/** Validate the Digest exchange shape, like the mock's Basic and Bearer credential checks. */
|
|
5
|
+
export declare const isValidDigestAuth: (c: Context) => boolean;
|
|
6
|
+
//# sourceMappingURL=digest-authentication.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"digest-authentication.d.ts","sourceRoot":"","sources":["../../src/utils/digest-authentication.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAKnC,iGAAiG;AACjG,eAAO,MAAM,gBAAgB,kGAAyE,CAAA;AAEtG,8FAA8F;AAC9F,eAAO,MAAM,iBAAiB,GAAI,GAAG,OAAO,KAAG,OA+B9C,CAAA"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const REALM = 'Scalar Mock Server';
|
|
2
|
+
const NONCE = 'scalar-mock-nonce';
|
|
3
|
+
/** A deterministic challenge lets HTTP clients exercise Digest without a credential database. */
|
|
4
|
+
export const DIGEST_CHALLENGE = `Digest realm="${REALM}", nonce="${NONCE}", algorithm=MD5, qop="auth"`;
|
|
5
|
+
/** Validate the Digest exchange shape, like the mock's Basic and Bearer credential checks. */
|
|
6
|
+
export const isValidDigestAuth = (c) => {
|
|
7
|
+
const header = c.req.header('Authorization');
|
|
8
|
+
if (!header || !/^Digest\s+/i.test(header)) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
const parameters = new Map();
|
|
12
|
+
const input = header.replace(/^Digest\s+/i, '');
|
|
13
|
+
const pattern = /\s*([\w-]+)\s*=\s*(?:"((?:[^"\\]|\\.)*)"|([^\s,]+))\s*(?:,|$)/gy;
|
|
14
|
+
let consumed = 0;
|
|
15
|
+
for (const match of input.matchAll(pattern)) {
|
|
16
|
+
const name = match[1].toLowerCase();
|
|
17
|
+
if (parameters.has(name)) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
parameters.set(name, (match[2] ?? match[3] ?? '').replace(/\\(.)/g, '$1'));
|
|
21
|
+
consumed = match.index + match[0].length;
|
|
22
|
+
}
|
|
23
|
+
const url = new URL(c.req.url);
|
|
24
|
+
return (consumed === input.length &&
|
|
25
|
+
Boolean(parameters.get('username')) &&
|
|
26
|
+
parameters.get('realm') === REALM &&
|
|
27
|
+
parameters.get('nonce') === NONCE &&
|
|
28
|
+
parameters.get('uri') === `${url.pathname}${url.search}` &&
|
|
29
|
+
(parameters.get('algorithm') ?? 'MD5').toUpperCase() === 'MD5' &&
|
|
30
|
+
parameters.get('qop') === 'auth' &&
|
|
31
|
+
Boolean(parameters.get('cnonce')) &&
|
|
32
|
+
/^[0-9a-f]{8}$/i.test(parameters.get('nc') ?? '') &&
|
|
33
|
+
parameters.get('nc') !== '00000000' &&
|
|
34
|
+
/^[0-9a-f]{32}$/i.test(parameters.get('response') ?? ''));
|
|
35
|
+
};
|
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
import type { OpenAPIV3_1, OpenAPIV3_2 } from '@scalar/openapi-types';
|
|
2
|
+
import type { AsyncApiSecuritySchemeObject } from '@scalar/types/asyncapi/3.1';
|
|
2
3
|
import type { Context } from 'hono';
|
|
4
|
+
/** Security shapes understood by HTTP and AsyncAPI transports. */
|
|
5
|
+
type SecurityScheme = OpenAPIV3_2.SecuritySchemeObject | AsyncApiSecuritySchemeObject | {
|
|
6
|
+
type: 'mutualTLS';
|
|
7
|
+
};
|
|
8
|
+
/** Check whether a single security scheme is satisfied by the request. */
|
|
9
|
+
export declare const isSchemeSatisfied: (scheme: SecurityScheme, c: Context) => boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Build the `WWW-Authenticate` challenge for a single security scheme.
|
|
12
|
+
*
|
|
13
|
+
* Returns `null` for schemes that do not map to an HTTP authentication challenge.
|
|
14
|
+
*/
|
|
15
|
+
export declare const getChallenge: (scheme: SecurityScheme) => string | null;
|
|
3
16
|
/**
|
|
4
17
|
* Handles authentication for incoming requests based on the OpenAPI document.
|
|
5
18
|
*
|
|
@@ -9,4 +22,5 @@ import type { Context } from 'hono';
|
|
|
9
22
|
* means authentication is optional and always passes.
|
|
10
23
|
*/
|
|
11
24
|
export declare function handleAuthentication(schema?: OpenAPIV3_1.Document | OpenAPIV3_2.Document, operation?: OpenAPIV3_2.OperationObject): (c: Context, next: () => Promise<void>) => Promise<Response | void>;
|
|
25
|
+
export {};
|
|
12
26
|
//# sourceMappingURL=handle-authentication.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handle-authentication.d.ts","sourceRoot":"","sources":["../../src/utils/handle-authentication.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"handle-authentication.d.ts","sourceRoot":"","sources":["../../src/utils/handle-authentication.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAa,WAAW,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAChF,OAAO,KAAK,EAAE,4BAA4B,EAAE,MAAM,4BAA4B,CAAA;AAE9E,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAKnC,kEAAkE;AAClE,KAAK,cAAc,GAAG,WAAW,CAAC,oBAAoB,GAAG,4BAA4B,GAAG;IAAE,IAAI,EAAE,WAAW,CAAA;CAAE,CAAA;AA8C7G,0EAA0E;AAC1E,eAAO,MAAM,iBAAiB,GAAI,QAAQ,cAAc,EAAE,GAAG,OAAO,KAAG,OAgDtE,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,YAAY,GAAI,QAAQ,cAAc,KAAG,MAAM,GAAG,IA6B9D,CAAA;AAYD;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,CAAC,EAAE,WAAW,CAAC,QAAQ,GAAG,WAAW,CAAC,QAAQ,EACpD,SAAS,CAAC,EAAE,WAAW,CAAC,eAAe,IAEzB,GAAG,OAAO,EAAE,MAAM,MAAM,OAAO,CAAC,IAAI,CAAC,KAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CA4D/E"}
|