1
0
Files

24 lines
516 B
JavaScript
Raw Permalink Normal View History

2020-05-15 15:25:57 -04:00
module.exports = withAuthorizationPrefix;
2019-08-06 17:37:58 -04:00
2020-05-15 15:25:57 -04:00
const atob = require("atob-lite");
2019-08-06 17:37:58 -04:00
2020-05-15 15:25:57 -04:00
const REGEX_IS_BASIC_AUTH = /^[\w-]+:/;
2019-08-06 17:37:58 -04:00
2020-05-15 15:25:57 -04:00
function withAuthorizationPrefix(authorization) {
2019-08-06 17:37:58 -04:00
if (/^(basic|bearer|token) /i.test(authorization)) {
2020-05-15 15:25:57 -04:00
return authorization;
2019-08-06 17:37:58 -04:00
}
try {
if (REGEX_IS_BASIC_AUTH.test(atob(authorization))) {
2020-05-15 15:25:57 -04:00
return `basic ${authorization}`;
2019-08-06 17:37:58 -04:00
}
2020-05-15 15:25:57 -04:00
} catch (error) {}
2019-08-06 17:37:58 -04:00
if (authorization.split(/\./).length === 3) {
2020-05-15 15:25:57 -04:00
return `bearer ${authorization}`;
2019-08-06 17:37:58 -04:00
}
2020-05-15 15:25:57 -04:00
return `token ${authorization}`;
2019-08-06 17:37:58 -04:00
}