1
0
Fork 0
mirror of https://github.com/actions/setup-node synced 2024-12-23 13:42:42 +00: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}`
}