-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-security-scheme-if.js
166 lines (141 loc) · 4.78 KB
/
remove-security-scheme-if.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* @copyright Copyright 2021 Kevin Locke <kevin@kevinlocke.name>
* @license MIT
*/
import OpenApiTransformerBase from 'openapi-transformer-base';
const emptyRequirement = {};
const predicateSymbol = Symbol('predicate');
const removedSchemesSymbol = Symbol('removedSchemes');
/**
* Transformer to remove security schemes matching a given predicate.
*/
export default class RemoveSecuritySchemeIfTransformer
extends OpenApiTransformerBase {
/** Constructs a new RemoveSecuritySchemeIfTransformer with a given
* predicate.
*
* @param {function(!object)} predicate Predicate which returns true if
* the given Security Scheme should be removed and false otherwise.
*/
constructor(predicate) {
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
super();
this[predicateSymbol] = predicate;
this[removedSchemesSymbol] = new Set();
}
transformSecuritySchemesEarly(securitySchemes) {
const removedSchemes = this[removedSchemesSymbol];
let isEmpty = true;
const newSecuritySchemes = {};
for (const [schemeName, securityScheme]
of Object.entries(securitySchemes)) {
if (securityScheme !== undefined) {
if (this[predicateSymbol](securityScheme)) {
removedSchemes.add(schemeName);
} else {
isEmpty = false;
newSecuritySchemes[schemeName] = securityScheme;
}
}
}
return isEmpty ? undefined : newSecuritySchemes;
}
transformSecurityRequirement(securityRequirement) {
if (typeof securityRequirement !== 'object'
|| securityRequirement === null) {
this.warn(
'Ignoring non-object Security Requirement',
securityRequirement,
);
return securityRequirement;
}
if (Array.isArray(securityRequirement)) {
this.warn(
'Ignoring non-object Security Requirement',
securityRequirement,
);
return securityRequirement;
}
const removedSchemes = this[removedSchemesSymbol];
let isEmpty = true;
let removedAny = false;
const newSecurityRequirement = {};
for (const [schemeName, securityScheme]
of Object.entries(securityRequirement)) {
if (securityScheme !== undefined) {
if (removedSchemes.has(schemeName)) {
removedAny = true;
} else {
isEmpty = false;
newSecurityRequirement[schemeName] = securityScheme;
}
}
}
// Return empty marker only if it was made empty by removing scheme(s).
// Leave already (effectively) empty Security Requirements as-is.
return removedAny && isEmpty ? emptyRequirement : newSecurityRequirement;
}
transformOperation(operation) {
const newOperation = super.transformOperation(operation);
if (newOperation
&& Array.isArray(newOperation.security)
&& newOperation.security.length > 0) {
// Remove any empty requirements which were created.
newOperation.security =
newOperation.security.filter((sr) => sr !== emptyRequirement);
// Remove the whole array if there are no requirements remaining.
if (newOperation.security.length === 0) {
delete newOperation.security;
}
}
return newOperation;
}
transformOpenApi(openApi) {
let newSecuritySchemes;
if (openApi
&& openApi.components
&& openApi.components.securitySchemes) {
newSecuritySchemes =
this.transformSecuritySchemesEarly(openApi.components.securitySchemes);
}
let newSecurityDefinitions;
if (openApi && openApi.securityDefinitions) {
newSecurityDefinitions =
this.transformSecuritySchemesEarly(openApi.securityDefinitions);
}
if (this[removedSchemesSymbol].size === 0) {
return openApi;
}
let newOpenApi = super.transformOpenApi(openApi);
if (newOpenApi === openApi) {
newOpenApi = { ...newOpenApi };
}
if (Array.isArray(newOpenApi.security)
&& newOpenApi.security.length > 0) {
// Remove any empty requirements which were created.
newOpenApi.security =
newOpenApi.security.filter((sr) => sr !== emptyRequirement);
// Remove the whole array if there are no requirements remaining.
if (newOpenApi.security.length === 0) {
delete newOpenApi.security;
}
}
// Remove securitySchemes where appropriate
if (newOpenApi.components) {
if (newSecuritySchemes === undefined) {
delete newOpenApi.components.securitySchemes;
} else {
newOpenApi.components.securitySchemes = newSecuritySchemes;
}
}
// Remove securityDefinitions where appropriate
if (newSecurityDefinitions === undefined) {
delete newOpenApi.securityDefinitions;
} else {
newOpenApi.securityDefinitions = newSecurityDefinitions;
}
return newOpenApi;
}
}