This repository was archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathparseSecurityRequirementObject.js
89 lines (70 loc) · 2.48 KB
/
parseSecurityRequirementObject.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
const R = require('ramda');
const { isString } = require('../../predicates');
const { createWarning } = require('../annotations');
const pipeParseResult = require('../../pipeParseResult');
const parseObject = require('../parseObject');
const parseArray = require('../parseArray');
const name = 'Security Requirement Object';
/**
* Parse Security Requirement Object
*
* @param namespace {Namespace}
* @param element {Element}
* @returns ParseResult
*
* @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#securityRequirementObject
* @private
*/
function parseSecurityRequirementObject(context, object) {
const { namespace } = context;
const parseScopes = (member) => {
const key = member.key.toValue();
const createScopeNotStringWarning = createWarning(namespace,
`'${name}' '${key}' array value is not a string`);
const parseScope = pipeParseResult(namespace,
R.unless(isString, createScopeNotStringWarning),
value => value);
return parseArray(context, `${name}' '${key}`, parseScope)(member.value);
};
const parseMember = R.cond([
[R.T, parseScopes],
]);
const parseSecurityRequirement = pipeParseResult(namespace,
parseObject(context, name, parseMember),
(securityRequirement) => {
const parseResult = new namespace.elements.ParseResult([]);
const array = new namespace.elements.Array([]);
securityRequirement.forEach((value, key) => {
let e;
const schemeName = key.toValue();
const scopes = value.map(scope => scope.toValue());
if (scopes.length) {
e = new namespace.elements.AuthScheme({ scopes });
} else {
e = new namespace.elements.AuthScheme({});
}
// Expand oauth2 flows
const hasFlows = context.state.oauthFlows[schemeName] || [];
if (hasFlows.length !== 0) {
hasFlows.forEach((flow) => {
const element = e.clone();
element.element = flow;
array.push(element);
});
return;
}
if (!context.hasScheme(schemeName)) {
parseResult.push(createWarning(namespace, `'${schemeName}' security scheme not found`, key));
} else {
e.element = schemeName;
array.push(e);
}
});
if (!array.isEmpty) {
parseResult.push(array);
}
return parseResult;
});
return parseSecurityRequirement(object);
}
module.exports = R.curry(parseSecurityRequirementObject);