@coinbase/cdp-sdk
Version:
SDK for interacting with the Coinbase Developer Platform Wallet API
30 lines • 1.1 kB
JavaScript
import { base64Decode, base64Encode } from "../base64.js";
const BASIC_AUTH_HEADER_PREFIX = /^Basic /i;
export const BasicAuth = {
toAuthorizationHeader: (basicAuth) => {
if (basicAuth == null) {
return undefined;
}
const username = basicAuth.username ?? "";
const password = basicAuth.password ?? "";
if (username === "" && password === "") {
return undefined;
}
const token = base64Encode(`${username}:${password}`);
return `Basic ${token}`;
},
fromAuthorizationHeader: (header) => {
const credentials = header.replace(BASIC_AUTH_HEADER_PREFIX, "");
const decoded = base64Decode(credentials);
const [username, ...passwordParts] = decoded.split(":");
const password = passwordParts.length > 0 ? passwordParts.join(":") : undefined;
if (username == null || password == null) {
throw new Error("Invalid basic auth");
}
return {
username,
password,
};
},
};
//# sourceMappingURL=BasicAuth.js.map