setup-node/node_modules/@octokit/rest/plugins/authentication/with-authorization-prefix.js

24 lines
516 B
JavaScript
Raw Normal View History

2020-01-24 12:20:19 -05:00
module.exports = withAuthorizationPrefix;
2020-01-24 12:20:19 -05:00
const atob = require("atob-lite");
2020-01-24 12:20:19 -05:00
const REGEX_IS_BASIC_AUTH = /^[\w-]+:/;
2020-01-24 12:20:19 -05:00
function withAuthorizationPrefix(authorization) {
if (/^(basic|bearer|token) /i.test(authorization)) {
2020-01-24 12:20:19 -05:00
return authorization;
}
try {
if (REGEX_IS_BASIC_AUTH.test(atob(authorization))) {
2020-01-24 12:20:19 -05:00
return `basic ${authorization}`;
}
2020-01-24 12:20:19 -05:00
} catch (error) {}
if (authorization.split(/\./).length === 3) {
2020-01-24 12:20:19 -05:00
return `bearer ${authorization}`;
}
2020-01-24 12:20:19 -05:00
return `token ${authorization}`;
}