3
0
Fork 0
mirror of https://gitea.com/actions/setup-node.git synced 2024-11-25 03:29:33 +01:00
setup-node/node_modules/@octokit/rest/plugins/authentication-deprecated/before-request.js
Danny McCormick 78148dae50
Add auth support (#21)
* Updates

* Update

* Update

* Update

* Update

* Yarn sometimes prefers npmrc, so use same token

* Description

* Update readme

* Feedback

* Add type

* new toolkit and scoped registries

* npmrc in RUNNER_TEMP

* Dont always auth

* Try exporting blank token

* Get auth working for now pending runner changes

* Fix string interpolation for auth token.

* Don't export both userconfigs

* Update authutil.js

* Add single quotes for authString

* Fix the registry string.

* Use userconfig and append trailing slash

* Keep in root of repo

* Try just adding auth token

* Remove auth token

* Try changes again

* Add tests

* Npm and GPR samples

* Add types
2019-08-06 18:26:04 -04:00

40 lines
1.2 KiB
JavaScript

module.exports = authenticationBeforeRequest
const btoa = require('btoa-lite')
const uniq = require('lodash.uniq')
function authenticationBeforeRequest (state, options) {
if (!state.auth.type) {
return
}
if (state.auth.type === 'basic') {
const hash = btoa(`${state.auth.username}:${state.auth.password}`)
options.headers['authorization'] = `Basic ${hash}`
return
}
if (state.auth.type === 'token') {
options.headers['authorization'] = `token ${state.auth.token}`
return
}
if (state.auth.type === 'app') {
options.headers['authorization'] = `Bearer ${state.auth.token}`
const acceptHeaders = options.headers['accept'].split(',')
.concat('application/vnd.github.machine-man-preview+json')
options.headers['accept'] = uniq(acceptHeaders).filter(Boolean).join(',')
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}`
}