Skip to content

Commit

Permalink
infer useJWT
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Nov 2, 2018
1 parent 83bd85e commit 2e71260
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
66 changes: 45 additions & 21 deletions packages/gatsby-source-wordpress/src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,35 @@ const colorized = require(`./output-color`)
const httpExceptionHandler = require(`./http-exception-handler`)
const requestInQueue = require(`./request-in-queue`)

/**
* Check auth object to see if we should fetch JWT access token
*/
const shouldUseJwt = auth => auth && (auth.jwt_user || auth.jwt_pass)

/**
* Check auth object to see if we should use HTTP Basic Auth
*/
const shouldUseHtaccess = auth =>
auth && (auth.htaccess_user || auth.htaccess_pass)

/**
* Format Auth settings for verbose output
*/
const formatAuthSettings = auth => {
let authOutputLines = []
if (shouldUseJwt(auth)) {
authOutputLines.push(` JWT Auth: ${auth.jwt_user}:${auth.jwt_pass}`)
}

if (shouldUseHtaccess(auth)) {
authOutputLines.push(
` HTTP Basic Auth: ${auth.htaccess_user}:${auth.htaccess_pass}`
)
}

return authOutputLines.join(`\n`)
}

/**
* High-level function to coordinate fetching data from a WordPress
* site.
Expand All @@ -17,7 +46,6 @@ async function fetch({
_useACF,
_acfOptionPageIds,
_hostingWPCOM,
_useJWT,
_auth,
_perPage,
_concurrentRequests,
Expand All @@ -34,16 +62,18 @@ async function fetch({
if (_hostingWPCOM) {
url = `https://public-api.wordpress.com/wp/v2/sites/${baseUrl}`
_accessToken = await getWPCOMAccessToken(_auth)
} else if (_useJWT) {
url = `${_siteURL}/wp-json`
_accessToken = await getJWToken(_auth, url)
} else {
url = `${_siteURL}/wp-json`
if (shouldUseJwt(_auth)) {
_accessToken = await getJWToken(_auth, url)
}
}

if (_verbose) {
console.time(`=END PLUGIN=====================================`)

const authOutput = formatAuthSettings(_auth)

console.log(
colorized.out(
`
Expand All @@ -52,11 +82,7 @@ async function fetch({
Site URL: ${_siteURL}
Site hosted on Wordpress.com: ${_hostingWPCOM}
Using ACF: ${_useACF}
Using Auth: ${
_useJWT
? _auth.jwt_user + ` ` + _auth.jwt_pass
: _auth.htaccess_user + ` ` + _auth.htaccess_pass
}
Auth: ${authOutput ? `\n${authOutput}` : `false`}
Verbose output: ${_verbose}
Mama Route URL: ${url}
Expand All @@ -73,7 +99,7 @@ Mama Route URL: ${url}
method: `get`,
url: url,
}
if (_auth && (_auth.htaccess_user || _auth.htaccess_pass)) {
if (shouldUseHtaccess(_auth)) {
options.auth = {
username: _auth.htaccess_user,
password: _auth.htaccess_pass,
Expand Down Expand Up @@ -108,8 +134,6 @@ Mama Route URL: ${url}
_verbose,
_useACF,
_acfOptionPageIds,
_hostingWPCOM,
_useJWT,
_includedRoutes,
_excludedRoutes,
typePrefix,
Expand All @@ -133,8 +157,6 @@ Fetching the JSON data from ${validRoutes.length} valid API Routes...
route,
_verbose,
_perPage,
_hostingWPCOM,
_useJWT,
_auth,
_accessToken,
_concurrentRequests,
Expand Down Expand Up @@ -219,8 +241,6 @@ async function fetchData({
route,
_verbose,
_perPage,
_hostingWPCOM,
_useJWT,
_auth,
_accessToken,
_concurrentRequests,
Expand Down Expand Up @@ -277,7 +297,6 @@ async function fetchData({
route: { url: menu.meta.links.self, type: `${type}_items` },
_verbose,
_perPage,
_hostingWPCOM,
_auth,
_accessToken,
})
Expand Down Expand Up @@ -329,15 +348,20 @@ async function getPages(
page: page,
})}`,
}

if (_accessToken) {
o.headers = {
Authorization: `Bearer ${_accessToken}`,
}
} else {
o.auth = _auth
? { username: _auth.htaccess_user, password: _auth.htaccess_pass }
: null
}

if (shouldUseHtaccess(_auth)) {
o.auth = {
username: _auth.htaccess_user,
password: _auth.htaccess_pass,
}
}

return o
}

Expand Down
4 changes: 0 additions & 4 deletions packages/gatsby-source-wordpress/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ let _siteURL
let _useACF = true
let _acfOptionPageIds
let _hostingWPCOM
let _useJWT
let _auth
let _perPage
let _concurrentRequests
Expand All @@ -30,7 +29,6 @@ exports.sourceNodes = async (
baseUrl,
protocol,
hostingWPCOM,
useJWT,
useACF = true,
acfOptionPageIds = [],
auth = {},
Expand All @@ -51,7 +49,6 @@ exports.sourceNodes = async (
_useACF = useACF
_acfOptionPageIds = acfOptionPageIds
_hostingWPCOM = hostingWPCOM
_useJWT = useJWT
_auth = auth
_perPage = perPage
_concurrentRequests = concurrentRequests
Expand All @@ -66,7 +63,6 @@ exports.sourceNodes = async (
_useACF,
_acfOptionPageIds,
_hostingWPCOM,
_useJWT,
_auth,
_perPage,
_concurrentRequests,
Expand Down

0 comments on commit 2e71260

Please sign in to comment.