Tofu/app/tools/webdav/buildBasicAuthHeader.js

11 lines
606 B
JavaScript

// Builds the HTTP Basic credentials sent to Nextcloud.
// btoa() alone cannot encode a character above U+00FF and mangles accents, so the
// pair is turned into UTF-8 bytes first, then into one byte-per-character string
// that btoa() can consume. "tofu:sécret" therefore gives the same bytes as the
// server expects, not a latin-1 approximation.
export function buildBasicAuthHeader(username, password) {
const bytes = new TextEncoder().encode(`${username ?? ''}:${password ?? ''}`)
let binary = ''
for (const byte of bytes) binary += String.fromCharCode(byte)
return `Basic ${btoa(binary)}`
}