@@ -6,6 +6,35 @@ const colorized = require(`./output-color`)
6
6
const httpExceptionHandler = require ( `./http-exception-handler` )
7
7
const requestInQueue = require ( `./request-in-queue` )
8
8
9
+ /**
10
+ * Check auth object to see if we should fetch JWT access token
11
+ */
12
+ const shouldUseJwt = auth => auth && ( auth . jwt_user || auth . jwt_pass )
13
+
14
+ /**
15
+ * Check auth object to see if we should use HTTP Basic Auth
16
+ */
17
+ const shouldUseHtaccess = auth =>
18
+ auth && ( auth . htaccess_user || auth . htaccess_pass )
19
+
20
+ /**
21
+ * Format Auth settings for verbose output
22
+ */
23
+ const formatAuthSettings = auth => {
24
+ let authOutputLines = [ ]
25
+ if ( shouldUseJwt ( auth ) ) {
26
+ authOutputLines . push ( ` JWT Auth: ${ auth . jwt_user } :${ auth . jwt_pass } ` )
27
+ }
28
+
29
+ if ( shouldUseHtaccess ( auth ) ) {
30
+ authOutputLines . push (
31
+ ` HTTP Basic Auth: ${ auth . htaccess_user } :${ auth . htaccess_pass } `
32
+ )
33
+ }
34
+
35
+ return authOutputLines . join ( `\n` )
36
+ }
37
+
9
38
/**
10
39
* High-level function to coordinate fetching data from a WordPress
11
40
* site.
@@ -35,11 +64,16 @@ async function fetch({
35
64
_accessToken = await getWPCOMAccessToken ( _auth )
36
65
} else {
37
66
url = `${ _siteURL } /wp-json`
67
+ if ( shouldUseJwt ( _auth ) ) {
68
+ _accessToken = await getJWToken ( _auth , url )
69
+ }
38
70
}
39
71
40
72
if ( _verbose ) {
41
73
console . time ( `=END PLUGIN=====================================` )
42
74
75
+ const authOutput = formatAuthSettings ( _auth )
76
+
43
77
console . log (
44
78
colorized . out (
45
79
`
@@ -48,7 +82,7 @@ async function fetch({
48
82
Site URL: ${ _siteURL }
49
83
Site hosted on Wordpress.com: ${ _hostingWPCOM }
50
84
Using ACF: ${ _useACF }
51
- Using Auth: ${ _auth . htaccess_user } ${ _auth . htaccess_pass }
85
+ Auth: ${ authOutput ? `\n ${ authOutput } ` : `false` }
52
86
Verbose output: ${ _verbose }
53
87
54
88
Mama Route URL: ${ url }
@@ -65,14 +99,14 @@ Mama Route URL: ${url}
65
99
method : `get` ,
66
100
url : url ,
67
101
}
68
- if ( _auth && ( _auth . htaccess_user || _auth . htaccess_pass ) ) {
102
+ if ( shouldUseHtaccess ( _auth ) ) {
69
103
options . auth = {
70
104
username : _auth . htaccess_user ,
71
105
password : _auth . htaccess_pass ,
72
106
}
73
107
}
74
108
75
- if ( _hostingWPCOM && _accessToken ) {
109
+ if ( _accessToken ) {
76
110
options . headers = {
77
111
Authorization : `Bearer ${ _accessToken } ` ,
78
112
}
@@ -100,7 +134,6 @@ Mama Route URL: ${url}
100
134
_verbose,
101
135
_useACF,
102
136
_acfOptionPageIds,
103
- _hostingWPCOM,
104
137
_includedRoutes,
105
138
_excludedRoutes,
106
139
typePrefix,
@@ -124,7 +157,6 @@ Fetching the JSON data from ${validRoutes.length} valid API Routes...
124
157
route,
125
158
_verbose,
126
159
_perPage,
127
- _hostingWPCOM,
128
160
_auth,
129
161
_accessToken,
130
162
_concurrentRequests,
@@ -173,6 +205,32 @@ async function getWPCOMAccessToken(_auth) {
173
205
return result
174
206
}
175
207
208
+ /**
209
+ * Gets JSON Web Token so it can fetch private data
210
+ *
211
+ * @returns
212
+ */
213
+ async function getJWToken ( _auth , url ) {
214
+ let result
215
+ let authUrl = `${ url } /jwt-auth/v1/token`
216
+ try {
217
+ const options = {
218
+ url : authUrl ,
219
+ method : `post` ,
220
+ data : {
221
+ username : _auth . jwt_user ,
222
+ password : _auth . jwt_pass ,
223
+ } ,
224
+ }
225
+ result = await axios ( options )
226
+ result = result . data . token
227
+ } catch ( e ) {
228
+ httpExceptionHandler ( e )
229
+ }
230
+
231
+ return result
232
+ }
233
+
176
234
/**
177
235
* Fetch the data from specified route url, using the auth provided.
178
236
*
@@ -183,7 +241,6 @@ async function fetchData({
183
241
route,
184
242
_verbose,
185
243
_perPage,
186
- _hostingWPCOM,
187
244
_auth,
188
245
_accessToken,
189
246
_concurrentRequests,
@@ -202,18 +259,14 @@ async function fetchData({
202
259
console . time ( `Fetching the ${ type } took` )
203
260
}
204
261
205
- let routeResponse = await getPages (
206
- {
207
- url,
208
- _perPage,
209
- _hostingWPCOM,
210
- _auth,
211
- _accessToken,
212
- _verbose,
213
- _concurrentRequests,
214
- } ,
215
- 1
216
- )
262
+ let routeResponse = await getPages ( {
263
+ url,
264
+ _perPage,
265
+ _auth,
266
+ _accessToken,
267
+ _verbose,
268
+ _concurrentRequests,
269
+ } )
217
270
218
271
let entities = [ ]
219
272
if ( routeResponse ) {
@@ -244,7 +297,6 @@ async function fetchData({
244
297
route : { url : menu . meta . links . self , type : `${ type } _items` } ,
245
298
_verbose,
246
299
_perPage,
247
- _hostingWPCOM,
248
300
_auth,
249
301
_accessToken,
250
302
} )
@@ -282,15 +334,7 @@ async function fetchData({
282
334
* @returns
283
335
*/
284
336
async function getPages (
285
- {
286
- url,
287
- _perPage,
288
- _hostingWPCOM,
289
- _auth,
290
- _accessToken,
291
- _concurrentRequests,
292
- _verbose,
293
- } ,
337
+ { url, _perPage, _auth, _accessToken, _concurrentRequests, _verbose } ,
294
338
page = 1
295
339
) {
296
340
try {
@@ -304,15 +348,20 @@ async function getPages(
304
348
page : page ,
305
349
} ) } `,
306
350
}
307
- if ( _hostingWPCOM ) {
351
+
352
+ if ( _accessToken ) {
308
353
o . headers = {
309
354
Authorization : `Bearer ${ _accessToken } ` ,
310
355
}
311
- } else {
312
- o . auth = _auth
313
- ? { username : _auth . htaccess_user , password : _auth . htaccess_pass }
314
- : null
315
356
}
357
+
358
+ if ( shouldUseHtaccess ( _auth ) ) {
359
+ o . auth = {
360
+ username : _auth . htaccess_user ,
361
+ password : _auth . htaccess_pass ,
362
+ }
363
+ }
364
+
316
365
return o
317
366
}
318
367
@@ -465,6 +514,7 @@ function getValidRoutes({
465
514
`**/embed` ,
466
515
`**/proxy` ,
467
516
`/` ,
517
+ `/jwt-auth/**` ,
468
518
]
469
519
470
520
const routePath = getRoutePath ( url , route . _links . self )
0 commit comments