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 pathparseOauthFlowsObject.js
85 lines (68 loc) · 2.85 KB
/
parseOauthFlowsObject.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
const R = require('ramda');
const {
isExtension, hasKey, getValue,
} = require('../../predicates');
const {
createWarning,
createInvalidMemberWarning,
} = require('../annotations');
const pipeParseResult = require('../../pipeParseResult');
const parseObject = require('../parseObject');
const parseOauthFlowObject = require('./parseOauthFlowObject');
const name = 'Oauth Flows Object';
const isValidFlow = R.anyPass(R.map(hasKey, ['implicit', 'password', 'clientCredentials', 'authorizationCode']));
const grantTypes = {
implicit: 'implicit',
password: 'resource owner password credentials',
clientCredentials: 'client credentials',
authorizationCode: 'authorization code',
};
/**
* Parse Oauth Flows Object
*
* @param namespace {Namespace}
* @param element {Element}
* @returns ParseResult
*
* @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#oauthFlowsObject
* @private
*/
function parseOauthFlowsObject(context, object) {
const { namespace } = context;
const parseFlow = (member) => {
const key = member.key.toValue();
const needAuthorizationUrl = () => R.includes(key, ['implicit', 'authorizationCode']);
const needTokenUrl = () => R.includes(key, ['password', 'clientCredentials', 'authorizationCode']);
const hasAuthorizationUrl = flow => flow.get('authorizationUrl');
const hasTokenUrl = flow => flow.get('tokenUrl');
const parse = pipeParseResult(namespace,
R.compose(parseOauthFlowObject(context), getValue),
R.when(R.allPass([R.complement(hasAuthorizationUrl), needAuthorizationUrl]), () => createWarning(namespace,
`'${name}' '${key}' is missing required property 'authorizationUrl'`, member)),
R.when(R.allPass([R.complement(hasTokenUrl), needTokenUrl]), () => createWarning(namespace,
`'${name}' '${key}' is missing required property 'tokenUrl'`, member)));
return parse(member);
};
const parseMember = R.cond([
[isValidFlow, parseFlow],
// FIXME Support exposing extensions into parse result
[isExtension, () => new namespace.elements.ParseResult()],
// Return a warning for additional properties
[R.T, createInvalidMemberWarning(namespace, name)],
]);
const parseOauthFlows = pipeParseResult(namespace,
parseObject(context, name, parseMember),
flows => new namespace.elements.Array(flows.content),
R.map((member) => {
const authScheme = new namespace.elements.AuthScheme();
authScheme.element = 'Oauth2 Scheme';
authScheme.push(new namespace.elements.Member('grantType', grantTypes[member.key.toValue()]));
authScheme.push(member.value.getMember('scopes'));
R.filter(R.is(namespace.elements.Transition), member.value).forEach((item) => {
authScheme.push(item);
});
return authScheme;
}));
return parseOauthFlows(object);
}
module.exports = R.curry(parseOauthFlowsObject);