2014-09-25 05:41:07 +08:00
|
|
|
module.exports = authify
|
|
|
|
|
2015-01-09 06:37:26 +08:00
|
|
|
function authify (authed, parsed, headers, credentials) {
|
|
|
|
if (credentials && credentials.token) {
|
2014-09-25 05:41:07 +08:00
|
|
|
this.log.verbose("request", "using bearer token for auth")
|
2015-01-09 06:37:26 +08:00
|
|
|
headers.authorization = "Bearer " + credentials.token
|
2014-09-25 05:41:07 +08:00
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
if (authed) {
|
2015-01-09 06:37:26 +08:00
|
|
|
if (credentials && credentials.username && credentials.password) {
|
|
|
|
var username = encodeURIComponent(credentials.username)
|
|
|
|
var password = encodeURIComponent(credentials.password)
|
2014-09-25 05:41:07 +08:00
|
|
|
parsed.auth = username + ":" + password
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return new Error(
|
|
|
|
"This request requires auth credentials. Run `npm login` and repeat the request."
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|