phonic
Version:
[](https://buildwithfern.com?utm_source=github&utm_medium=github&utm_campaign=readme&utm_source=https%3A%2F%2Fgithub.com%2FPhonic-Co%2Fphonic-node) [ • 914 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasicAuth = void 0;
const base64_js_1 = require("../base64.js");
const BASIC_AUTH_HEADER_PREFIX = /^Basic /i;
exports.BasicAuth = {
toAuthorizationHeader: (basicAuth) => {
if (basicAuth == null) {
return undefined;
}
const token = (0, base64_js_1.base64Encode)(`${basicAuth.username}:${basicAuth.password}`);
return `Basic ${token}`;
},
fromAuthorizationHeader: (header) => {
const credentials = header.replace(BASIC_AUTH_HEADER_PREFIX, "");
const decoded = (0, base64_js_1.base64Decode)(credentials);
const [username, password] = decoded.split(":", 2);
if (username == null || password == null) {
throw new Error("Invalid basic auth");
}
return {
username,
password,
};
},
};