persona-community-5/.pnpm-store/v3/files/3d/f07a0f3f7db8e602162c0441004765f49ceab4f3d9def27ec5430254f913ab588c167b64e11d9b6ed63764dc67b7becd807a78d48855d1fa9f1b47851db4ec
rdev-worker a1d0d1bf1c
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
build: /implement-feature community-ui --requirements 'Build the React commu...
2026-02-24 08:22:30 +00:00

90 lines
2.8 KiB
Plaintext

import type { Oas3Rule, Oas2Rule } from '../../visitors';
import type { Location } from '../../ref-utils';
import type { UserContext } from '../../walk';
import type {
Oas2Definition,
Oas2Operation,
Oas2PathItem,
Oas2SecurityScheme,
} from '../../typings/swagger';
import type {
Oas3Definition,
Oas3_1Definition,
Oas3Operation,
Oas3PathItem,
Oas3SecurityScheme,
} from '../../typings/openapi';
export const SecurityDefined: Oas3Rule | Oas2Rule = (opts: {
exceptions?: { path: string; methods?: string[] }[];
}) => {
const referencedSchemes = new Map<
string,
{
defined?: boolean;
from: Location[];
}
>();
const operationsWithoutSecurity: Location[] = [];
let eachOperationHasSecurity: boolean = true;
let path: string | undefined;
return {
Root: {
leave(root: Oas2Definition | Oas3Definition | Oas3_1Definition, { report }: UserContext) {
for (const [name, scheme] of referencedSchemes.entries()) {
if (scheme.defined) continue;
for (const reportedFromLocation of scheme.from) {
report({
message: `There is no \`${name}\` security scheme defined.`,
location: reportedFromLocation.key(),
});
}
}
if (root.security || eachOperationHasSecurity) {
return;
} else {
for (const operationLocation of operationsWithoutSecurity) {
report({
message: `Every operation should have security defined on it or on the root level.`,
location: operationLocation.key(),
});
}
}
},
},
SecurityScheme(_securityScheme: Oas2SecurityScheme | Oas3SecurityScheme, { key }: UserContext) {
referencedSchemes.set(key.toString(), { defined: true, from: [] });
},
SecurityRequirement(requirements, { location }) {
for (const requirement of Object.keys(requirements)) {
const authScheme = referencedSchemes.get(requirement);
const requirementLocation = location.child([requirement]);
if (!authScheme) {
referencedSchemes.set(requirement, { from: [requirementLocation] });
} else {
authScheme.from.push(requirementLocation);
}
}
},
PathItem: {
enter(pathItem: Oas2PathItem | Oas3PathItem, { key }: UserContext) {
path = key as string;
},
Operation(operation: Oas2Operation | Oas3Operation, { location, key }: UserContext) {
const isException = opts.exceptions?.some(
(item) =>
item.path === path &&
(!item.methods || item.methods?.some((method) => method.toLowerCase() === key))
);
if (!operation?.security && !isException) {
eachOperationHasSecurity = false;
operationsWithoutSecurity.push(location);
}
},
},
};
};