-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathsecurity-defined.ts
More file actions
84 lines (79 loc) · 2.7 KB
/
Copy pathsecurity-defined.ts
File metadata and controls
84 lines (79 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { isRef } from '../../ref-utils.js';
import type { Location } from '../../ref-utils.js';
import type { Async3Rule } from '../../visitors.js';
import type { UserContext } from '../../walk.js';
type SecurityReference = {
location: Location;
name: string;
resolvedAbsolutePointer?: string;
resolved: boolean;
};
export const SecurityDefined: Async3Rule = () => {
const definedSchemeAbsolutePointers = new Set<string>();
const references: SecurityReference[] = [];
const operationsWithoutSecurity: Location[] = [];
let eachOperationHasSecurity = true;
return {
Root: {
leave(_root: unknown, { report }: UserContext) {
for (const reference of references) {
if (
reference.resolved &&
reference.resolvedAbsolutePointer &&
definedSchemeAbsolutePointers.has(reference.resolvedAbsolutePointer)
) {
continue;
}
if (!reference.resolved) {
report({
message: `There is no \`${reference.name}\` security scheme defined.`,
location: reference.location.key(),
});
} else {
report({
message: `Security scheme \`$ref\` must point to \`#/components/securitySchemes\`.`,
location: reference.location.key(),
});
}
}
if (!eachOperationHasSecurity) {
for (const operationLocation of operationsWithoutSecurity) {
report({
message: `Every operation should have security defined on it.`,
location: operationLocation.key(),
});
}
}
},
},
NamedSecuritySchemes: {
SecurityScheme(_scheme: unknown, { location }: UserContext) {
definedSchemeAbsolutePointers.add(location.absolutePointer.toString());
},
},
SecuritySchemeList: {
enter(list: unknown[] | undefined, { location, resolve }: UserContext) {
if (!list) return;
for (let i = 0; i < list.length; i++) {
const item = list[i];
if (!isRef(item)) continue;
const itemLocation = location.child([i]);
const resolved = resolve(item);
const name = item.$ref.split('/').pop() ?? item.$ref;
references.push({
location: itemLocation,
name,
resolvedAbsolutePointer: resolved.location?.absolutePointer.toString(),
resolved: resolved.node !== undefined,
});
}
},
},
Operation(operation: { security?: unknown }, { location }: UserContext) {
if (!operation?.security) {
eachOperationHasSecurity = false;
operationsWithoutSecurity.push(location);
}
},
};
};