@@ -7,8 +7,8 @@ const HTTPError = sUtil.HTTPError;
7
7
requestLib . debug = true ;
8
8
9
9
const TokenType = {
10
- CSRF : 'csrf' ,
11
- LOGIN : 'login'
10
+ CSRF : 'csrf' ,
11
+ LOGIN : 'login'
12
12
} ;
13
13
14
14
/**
@@ -18,12 +18,12 @@ const TokenType = {
18
18
* @return {!string } return host header if it exists
19
19
*/
20
20
function extractCookieDomain ( request ) {
21
- const hostname = request . headers ?. host ;
22
- if ( ! hostname ) {
23
- return request . uri ;
24
- }
25
- // The cookie domain needs to be an URL formatted with a protocol
26
- return `http://${ hostname } ` ;
21
+ const hostname = request . headers ?. host ;
22
+ if ( ! hostname ) {
23
+ return request . uri ;
24
+ }
25
+ // The cookie domain needs to be an URL formatted with a protocol
26
+ return `http://${ hostname } ` ;
27
27
}
28
28
29
29
/**
@@ -35,49 +35,49 @@ function extractCookieDomain(request) {
35
35
* @return {!Promise } a promise resolving as the response object from the MW API
36
36
*/
37
37
function mwApiPost ( app , query , headers = { } ) {
38
- const request = app . mwapi_tpl . expand ( {
39
- request : {
40
- headers : {
41
- 'user-agent' : app . conf . user_agent ,
42
- 'X-Forwarded-Proto' : 'https'
43
- } ,
44
- query
45
- }
46
- } ) ;
47
- Object . assign ( request . headers , headers , app . cookies ?. headers || { } ) ;
48
- // Use custom cookie jar if it exists
49
- request . jar = app . cookies ?. jar || true ;
50
- return preq ( request ) . then ( ( response ) => {
51
- if ( app . cookies && response . headers [ 'set-cookie' ] ) {
52
- const cookieDomain = extractCookieDomain ( request ) ;
53
- if ( cookieDomain ) {
54
- const cookie = response . headers [ 'set-cookie' ] . map ( ( c ) => {
55
- return app . cookies . jar . setCookie ( c , cookieDomain ) ;
56
- } ) ;
57
- // the cookies need to be passed as request headers too
58
- app . cookies . headers = { cookie } ;
59
- }
60
- }
61
- // Server error
62
- if ( response . status < 200 || response . status > 399 ) {
63
- throw new HTTPError ( {
64
- status : response . status ,
65
- type : 'api_error' ,
66
- title : 'MW API error' ,
67
- detail : response . body
68
- } ) ;
69
- }
70
- // MW API Error
71
- if ( response . body . error ) {
72
- throw new HTTPError ( {
73
- status : response . status ,
74
- type : 'api_error' ,
75
- title : response . body . error . code ,
76
- detail : response . body . error . info
77
- } ) ;
78
- }
79
- return response ;
80
- } ) ;
38
+ const request = app . mwapi_tpl . expand ( {
39
+ request : {
40
+ headers : {
41
+ 'user-agent' : app . conf . user_agent ,
42
+ 'X-Forwarded-Proto' : 'https'
43
+ } ,
44
+ query
45
+ }
46
+ } ) ;
47
+ Object . assign ( request . headers , headers , app . cookies ?. headers || { } ) ;
48
+ // Use custom cookie jar if it exists
49
+ request . jar = app . cookies ?. jar || true ;
50
+ return preq ( request ) . then ( ( response ) => {
51
+ if ( app . cookies && response . headers [ 'set-cookie' ] ) {
52
+ const cookieDomain = extractCookieDomain ( request ) ;
53
+ if ( cookieDomain ) {
54
+ const cookie = response . headers [ 'set-cookie' ] . map ( ( c ) => {
55
+ return app . cookies . jar . setCookie ( c , cookieDomain ) ;
56
+ } ) ;
57
+ // the cookies need to be passed as request headers too
58
+ app . cookies . headers = { cookie } ;
59
+ }
60
+ }
61
+ // Server error
62
+ if ( response . status < 200 || response . status > 399 ) {
63
+ throw new HTTPError ( {
64
+ status : response . status ,
65
+ type : 'api_error' ,
66
+ title : 'MW API error' ,
67
+ detail : response . body
68
+ } ) ;
69
+ }
70
+ // MW API Error
71
+ if ( response . body . error ) {
72
+ throw new HTTPError ( {
73
+ status : response . status ,
74
+ type : 'api_error' ,
75
+ title : response . body . error . code ,
76
+ detail : response . body . error . info
77
+ } ) ;
78
+ }
79
+ return response ;
80
+ } ) ;
81
81
}
82
82
83
83
/**
@@ -88,15 +88,15 @@ function mwApiPost(app, query, headers = {}) {
88
88
* @return {!Promise }
89
89
*/
90
90
function mwApiGetToken ( app , type = TokenType . CSRF ) {
91
- return mwApiPost ( app , { action : 'query' , format : 'json' , meta : 'tokens' , type } )
92
- . then ( ( rsp ) => {
93
- return rsp && rsp . body && rsp . body . query && rsp . body . query . tokens &&
91
+ return mwApiPost ( app , { action : 'query' , format : 'json' , meta : 'tokens' , type } )
92
+ . then ( ( rsp ) => {
93
+ return rsp && rsp . body && rsp . body . query && rsp . body . query . tokens &&
94
94
rsp . body . query . tokens [ `${ type } token` ] ;
95
- } )
96
- . catch ( ( err ) => {
97
- app . logger . log ( 'error/mwapi' , err ) ;
98
- throw err ;
99
- } ) ;
95
+ } )
96
+ . catch ( ( err ) => {
97
+ app . logger . log ( 'error/mwapi' , err ) ;
98
+ throw err ;
99
+ } ) ;
100
100
}
101
101
102
102
/**
@@ -106,31 +106,31 @@ function mwApiGetToken(app, type = TokenType.CSRF) {
106
106
* @return {!Promise }
107
107
*/
108
108
function mwApiLogin ( app ) {
109
- if ( ! app . conf . mw_subscription_manager_username || ! app . conf . mw_subscription_manager_password ) {
110
- throw new Error ( 'mw_subscription_manager_username and mw_subscription_manager_password' +
109
+ if ( ! app . conf . mw_subscription_manager_username || ! app . conf . mw_subscription_manager_password ) {
110
+ throw new Error ( 'mw_subscription_manager_username and mw_subscription_manager_password' +
111
111
' must be defined in the app configuration!' ) ;
112
- }
113
- // Everytime login is called, recreate cookie object and expose in the app object
114
- // The cookie jar and headers will only be used when executing mwApiLogin and config is enabled
115
- if ( app . conf . enable_custom_cookie_jar ) {
116
- app . cookies = {
117
- jar : requestLib . jar ( ) ,
118
- headers : { }
119
- } ;
120
- }
121
- return mwApiGetToken ( app , TokenType . LOGIN ) . then ( ( logintoken ) => mwApiPost (
122
- app , {
123
- action : 'clientlogin' ,
124
- format : 'json' ,
125
- username : app . conf . mw_subscription_manager_username ,
126
- password : app . conf . mw_subscription_manager_password ,
127
- loginreturnurl : 'https://example.com' ,
128
- logintoken
129
- }
130
- ) ) . catch ( ( err ) => {
131
- app . logger . log ( 'error/login' , err ) ;
132
- throw err ;
133
- } ) ;
112
+ }
113
+ // Everytime login is called, recreate cookie object and expose in the app object
114
+ // The cookie jar and headers will only be used when executing mwApiLogin and config is enabled
115
+ if ( app . conf . enable_custom_cookie_jar ) {
116
+ app . cookies = {
117
+ jar : requestLib . jar ( ) ,
118
+ headers : { }
119
+ } ;
120
+ }
121
+ return mwApiGetToken ( app , TokenType . LOGIN ) . then ( ( logintoken ) => mwApiPost (
122
+ app , {
123
+ action : 'clientlogin' ,
124
+ format : 'json' ,
125
+ username : app . conf . mw_subscription_manager_username ,
126
+ password : app . conf . mw_subscription_manager_password ,
127
+ loginreturnurl : 'https://example.com' ,
128
+ logintoken
129
+ }
130
+ ) ) . catch ( ( err ) => {
131
+ app . logger . log ( 'error/login' , err ) ;
132
+ throw err ;
133
+ } ) ;
134
134
}
135
135
136
136
/**
@@ -139,21 +139,21 @@ function mwApiLogin(app) {
139
139
* @param {!Application } app the application object
140
140
*/
141
141
function setupApiTemplates ( app ) {
142
- if ( ! app . conf . mwapi_req ) {
143
- app . conf . mwapi_req = {
144
- method : 'post' ,
145
- uri : 'https://meta.wikimedia.org/w/api.php' ,
146
- headers : '{{request.headers}}' ,
147
- body : '{{ default(request.query, {}) }}'
148
- } ;
149
- }
150
- app . mwapi_tpl = new Template ( app . conf . mwapi_req ) ;
142
+ if ( ! app . conf . mwapi_req ) {
143
+ app . conf . mwapi_req = {
144
+ method : 'post' ,
145
+ uri : 'https://meta.wikimedia.org/w/api.php' ,
146
+ headers : '{{request.headers}}' ,
147
+ body : '{{ default(request.query, {}) }}'
148
+ } ;
149
+ }
150
+ app . mwapi_tpl = new Template ( app . conf . mwapi_req ) ;
151
151
}
152
152
153
153
module . exports = {
154
- mwApiGetToken,
155
- mwApiLogin,
156
- mwApiPost,
157
- setupApiTemplates,
158
- extractCookieDomain
154
+ mwApiGetToken,
155
+ mwApiLogin,
156
+ mwApiPost,
157
+ setupApiTemplates,
158
+ extractCookieDomain
159
159
} ;
0 commit comments