1
+ /* eslint-disable no-unused-vars */
1
2
const R = require ( 'ramda' ) ;
2
3
const {
3
- isAnnotation , isExtension, hasKey,
4
+ isExtension, hasKey, getValue ,
4
5
} = require ( '../../predicates' ) ;
5
6
const {
6
7
createWarning,
@@ -12,7 +13,7 @@ const parseOauthFlowObject = require('./parseOauthFlowObject');
12
13
13
14
const name = 'Oauth Flows Object' ;
14
15
15
- const validFlow = R . anyPass ( R . map ( hasKey , [ 'implicit' , 'password' , 'clientCredentials' , 'authorizationCode' ] ) ) ;
16
+ const isValidFlow = R . anyPass ( R . map ( hasKey , [ 'implicit' , 'password' , 'clientCredentials' , 'authorizationCode' ] ) ) ;
16
17
const grantTypes = {
17
18
implicit : 'implicit' ,
18
19
password : 'resource owner password credentials' ,
@@ -35,27 +36,25 @@ function parseOauthFlowsObject(context, object) {
35
36
36
37
const parseFlow = ( member ) => {
37
38
const key = member . key . toValue ( ) ;
38
- const flow = parseOauthFlowObject ( context , member . value ) ;
39
39
40
- if ( flow . length === 0 || isAnnotation ( flow . get ( 0 ) ) ) {
41
- return flow ;
42
- }
40
+ const needAuthorizationUrl = flow => R . includes ( key , [ 'implicit' , 'authorizationCode' ] ) ;
41
+ const needTokenUrl = flow => R . includes ( key , [ 'password' , 'clientCredentials' , 'authorizationCode' ] ) ;
43
42
44
- if ( [ 'implicit' , 'authorizationCode' ] . includes ( key ) && ! flow . get ( 0 ) . get ( 'authorizationUrl' ) ) {
45
- return createWarning ( namespace ,
46
- `'${ name } ' '${ key } ' is missing required property 'authorizationUrl'` , member ) ;
47
- }
43
+ const hasAuthorizationUrl = flow => flow . get ( 'authorizationUrl' ) ;
44
+ const hasTokenUrl = flow => flow . get ( 'tokenUrl' ) ;
48
45
49
- if ( [ 'password' , 'clientCredentials' , 'authorizationCode' ] . includes ( key ) && ! flow . get ( 0 ) . get ( 'tokenUrl' ) ) {
50
- return createWarning ( namespace ,
51
- `'${ name } ' '${ key } ' is missing required property 'tokenUrl'` , member ) ;
52
- }
46
+ const parse = pipeParseResult ( namespace ,
47
+ R . compose ( parseOauthFlowObject ( context ) , getValue ) ,
48
+ R . when ( R . allPass ( [ R . complement ( hasAuthorizationUrl ) , needAuthorizationUrl ] ) , flow => createWarning ( namespace ,
49
+ `'${ name } ' '${ key } ' is missing required property 'authorizationUrl'` , member ) ) ,
50
+ R . when ( R . allPass ( [ R . complement ( hasTokenUrl ) , needTokenUrl ] ) , flow => createWarning ( namespace ,
51
+ `'${ name } ' '${ key } ' is missing required property 'tokenUrl'` , member ) ) ) ;
53
52
54
- return flow ;
53
+ return parse ( member ) ;
55
54
} ;
56
55
57
56
const parseMember = R . cond ( [
58
- [ validFlow , parseFlow ] ,
57
+ [ isValidFlow , parseFlow ] ,
59
58
60
59
// FIXME Support exposing extensions into parse result
61
60
[ isExtension , ( ) => new namespace . elements . ParseResult ( ) ] ,
@@ -66,21 +65,16 @@ function parseOauthFlowsObject(context, object) {
66
65
67
66
const parseOauthFlows = pipeParseResult ( namespace ,
68
67
parseObject ( context , name , parseMember ) ,
69
- ( oauthFlows ) => {
70
- const arr = new namespace . elements . Array ( ) ;
68
+ flows => new namespace . elements . Array ( flows . content ) ,
69
+ R . map ( ( member ) => {
70
+ const authScheme = new namespace . elements . AuthScheme ( ) ;
71
71
72
- oauthFlows . forEach ( ( value , key ) => {
73
- const authScheme = new namespace . elements . AuthScheme ( ) ;
72
+ authScheme . element = 'Oauth2 Scheme' ;
73
+ authScheme . push ( new namespace . elements . Member ( 'grantType' , grantTypes [ member . key . toValue ( ) ] ) ) ;
74
+ authScheme . push ( member . value . getMember ( 'scopes' ) ) ;
74
75
75
- authScheme . element = 'Oauth2 Scheme' ;
76
- authScheme . push ( new namespace . elements . Member ( 'grantType' , grantTypes [ key . toValue ( ) ] ) ) ;
77
- authScheme . push ( value . getMember ( 'scopes' ) ) ;
78
-
79
- arr . push ( authScheme ) ;
80
- } ) ;
81
-
82
- return arr ;
83
- } ) ;
76
+ return authScheme ;
77
+ } ) ) ;
84
78
85
79
return parseOauthFlows ( object ) ;
86
80
}
0 commit comments