38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
module.exports = authenticationBeforeRequest
|
|
|
|
function authenticationBeforeRequest (state, options) {
|
|
if (!state.auth.type) {
|
|
return
|
|
}
|
|
|
|
if (state.auth.type === 'basic') {
|
|
const hash = Buffer.from(
|
|
`${state.auth.username}:${state.auth.password}`, 'ascii'
|
|
).toString('base64')
|
|
options.headers['authorization'] = `Basic ${hash}`
|
|
return
|
|
}
|
|
|
|
if (state.auth.type === 'token') {
|
|
options.headers['authorization'] = `token ${state.auth.token}`
|
|
return
|
|
}
|
|
|
|
if (state.auth.type === 'integration') {
|
|
options.headers['authorization'] = `Bearer ${state.auth.token}`
|
|
options.headers['accept'] = 'application/vnd.github.machine-man-preview+json'
|
|
return
|
|
}
|
|
|
|
options.url += options.url.indexOf('?') === -1 ? '?' : '&'
|
|
|
|
if (state.auth.token) {
|
|
options.url += `access_token=${encodeURIComponent(state.auth.token)}`
|
|
return
|
|
}
|
|
|
|
const key = encodeURIComponent(state.auth.key)
|
|
const secret = encodeURIComponent(state.auth.secret)
|
|
options.url += `client_id=${key}&client_secret=${secret}`
|
|
}
|