🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

@scalar/mock-server

Package Overview
Dependencies
Maintainers
8
Versions
233
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@scalar/mock-server - npm Package Compare versions

Comparing version

to
0.1.70

6

CHANGELOG.md
# @scalar/mock-server
## 0.1.70
### Patch Changes
- 32b08ad: feat: http basic auth middleware
## 0.1.69

@@ -4,0 +10,0 @@

9

dist/index.d.ts
import * as hono_types from 'hono/types';
import * as hono from 'hono';
import { Context, Hono } from 'hono';
import { ResolvedOpenAPI } from '@scalar/openapi-parser';
import { ResolvedOpenAPI, OpenAPIV3 } from '@scalar/openapi-parser';

@@ -28,2 +28,7 @@ /**

export { createMockServer, findPreferredResponseKey, routeFromPath };
/**
* Check whether the given security scheme key is in the `security` configuration for this operation.
**/
declare function isAuthenticationRequired(security?: OpenAPIV3.SecurityRequirementObject[]): boolean;
export { createMockServer, findPreferredResponseKey, isAuthenticationRequired, routeFromPath };

@@ -5,2 +5,3 @@ import { getExampleFromSchema } from '@scalar/oas-utils';

import { cors } from 'hono/cors';
import { HTTPException } from 'hono/http-exception';

@@ -17,2 +18,46 @@ function findPreferredResponseKey(responses) {

function isAuthenticationRequired(security) {
if (!security) {
return false;
}
if (Array.isArray(security) && !security.length) {
return false;
}
if ((security ?? []).some(
(securityRequirement) => !Object.keys(securityRequirement).length
)) {
return false;
}
return true;
}
function anyBasicAuthentication() {
return async function(ctx, next) {
if (ctx.req.header("Authorization")?.startsWith("Basic ")) {
return await next();
}
throw new HTTPException(401, {
res: new Response("Unauthorized", {
status: 401,
headers: {
"WWW-Authenticate": 'Basic realm="Authentication Required"'
}
})
});
};
}
function isBasicAuthenticationRequired(operation, schema) {
const allowedSecuritySchemes = operation.security?.map(
(securityScheme) => {
return Object.keys(securityScheme)[0];
}
);
const httpBasicAuthIsRequired = allowedSecuritySchemes?.findIndex((securitySchemeKey) => {
const securityScheme = schema?.components?.securitySchemes?.[securitySchemeKey];
return securityScheme?.type === "http" && securityScheme?.scheme === "basic";
}) !== void 0;
return httpBasicAuthIsRequired;
}
async function createMockServer(options) {

@@ -37,2 +82,13 @@ const app = new Hono();

const route = routeFromPath(path);
const operation = result.schema?.paths?.[path]?.[method];
const requiresAuthentication = isAuthenticationRequired(
operation.security
);
const requiresBasicAuthentication = isBasicAuthenticationRequired(
operation,
result?.schema
);
if (requiresAuthentication && requiresBasicAuthentication) {
app[method](route, anyBasicAuthentication());
}
app[method](route, (c) => {

@@ -42,7 +98,5 @@ if (options?.onRequest) {

context: c,
// @ts-expect-error Needs a proper type
operation: result.schema.paths[path][method]
operation
});
}
const operation = result.schema?.paths?.[path]?.[method];
const preferredResponseKey = findPreferredResponseKey(

@@ -67,3 +121,3 @@ Object.keys(operation.responses ?? {})

export { createMockServer, findPreferredResponseKey, routeFromPath };
export { createMockServer, findPreferredResponseKey, isAuthenticationRequired, routeFromPath };
//# sourceMappingURL=index.js.map

@@ -19,3 +19,3 @@ {

],
"version": "0.1.69",
"version": "0.1.70",
"engines": {

@@ -51,3 +51,3 @@ "node": ">=18"

"vite-node": "^1.3.1",
"@scalar/galaxy": "0.1.3"
"@scalar/galaxy": "0.1.4"
},

@@ -54,0 +54,0 @@ "scripts": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet