|
8 | 8 | "io/ioutil" |
9 | 9 | "net/http" |
10 | 10 | "sort" |
11 | | - "sync" |
12 | 11 |
|
13 | 12 | "github.com/getkin/kin-openapi/openapi3" |
14 | 13 | ) |
@@ -204,57 +203,23 @@ func ValidateRequestBody(c context.Context, input *RequestValidationInput, reque |
204 | 203 | return nil |
205 | 204 | } |
206 | 205 |
|
207 | | -// ValidateSecurityRequirements validates a multiple OpenAPI 3 security requirements. |
208 | | -// Returns nil if one of them inputed. |
209 | | -// Otherwise returns an error describing the security failures. |
| 206 | +// ValidateSecurityRequirements goes through multiple OpenAPI 3 security |
| 207 | +// requirements in order and returns nil on the first valid requirement. |
| 208 | +// If no requirement is met, errors are returned in order. |
210 | 209 | func ValidateSecurityRequirements(c context.Context, input *RequestValidationInput, srs openapi3.SecurityRequirements) error { |
211 | | - // Alternative requirements |
212 | 210 | if len(srs) == 0 { |
213 | 211 | return nil |
214 | 212 | } |
215 | | - |
216 | | - var wg sync.WaitGroup |
217 | | - errs := make([]error /*zeros,here*/, len(srs)) |
218 | | - |
219 | | - // For each alternative security requirement |
220 | | - for i, securityRequirement := range srs { |
221 | | - // Capture index from iteration variable |
222 | | - currentIndex := i |
223 | | - currentSecurityRequirement := securityRequirement |
224 | | - |
225 | | - // Add a work item |
226 | | - wg.Add(1) |
227 | | - |
228 | | - go func() { |
229 | | - defer func() { |
230 | | - v := recover() |
231 | | - if v != nil { |
232 | | - if err, ok := v.(error); ok { |
233 | | - errs[currentIndex] = err |
234 | | - } else { |
235 | | - errs[currentIndex] = errors.New("Panicked") |
236 | | - } |
237 | | - } |
238 | | - |
239 | | - // Remove a work item |
240 | | - wg.Done() |
241 | | - }() |
242 | | - |
243 | | - if err := validateSecurityRequirement(c, input, currentSecurityRequirement); err != nil { |
244 | | - errs[currentIndex] = err |
| 213 | + var errs []error |
| 214 | + for _, sr := range srs { |
| 215 | + if err := validateSecurityRequirement(c, input, sr); err != nil { |
| 216 | + if len(errs) == 0 { |
| 217 | + errs = make([]error, 0, len(srs)) |
245 | 218 | } |
246 | | - }() |
247 | | - } |
248 | | - |
249 | | - // Wait for all |
250 | | - wg.Wait() |
251 | | - |
252 | | - // If any security requirement was met |
253 | | - for _, err := range errs { |
254 | | - if err == nil { |
255 | | - // Return no error |
256 | | - return nil |
| 219 | + errs = append(errs, err) |
| 220 | + continue |
257 | 221 | } |
| 222 | + return nil |
258 | 223 | } |
259 | 224 | return &SecurityRequirementsError{ |
260 | 225 | SecurityRequirements: srs, |
|
0 commit comments