@andrew-pyle/basicauthheader
v0.3.0
Published
Create Authorization HTTP headers. Compatible with `Headers()`, `Request()`, and `fetch()`.
Downloads
7
Maintainers
Readme
Basic Authorization Header
Construct a Basic Authorization string for the Authorization
HTTP Header according to RFC 7617:
If the user agent wishes to send the user-id "Aladdin" and password "open sesame", it would use the following header field:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
Installation
$ npm i @andrew-pyle/basicauthheader
Usage
Uses only standard JavaScript. Nothing Node.js-specific. Code provided as ES module only.
import { BasicAuth } from '@andrew-pyle/basicauthheader';
// CommonJS environments can probably use import('@andrew-pyle/basicauthheader')
const username = "Aladdin";
const password = "open sesame";
// Authorization Header String
const basicAuthString = new BasicAuth({ username, password }).toString();
// => "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
// Directly Compatible with Web APIs: Headers, Request, fetch
const headers = new Headers({
Authorization: new BasicAuth({ username, password })
});
// => Headers { authorization: "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" }
const request = new Request("https://example.com", {
headers: {
Authorization: new BasicAuth({ username, password })
}
});
// => Response
const response = await fetch("https://example.com", {
headers: {
Authorization: new BasicAuth({ username, password })
}
});
// => Response
// Exposes Credentials Property
const base64EncodedCredentials = new BasicAuth({ username, password }).credentials;
// => "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
Base64 Encoding Strategy
Binary Encoding
Encodes strings into a binary Uint8Array
as UTF-8. For a discussion of the need for encoding as UTF-8, see "The Unicode Problem" on MDN. If another text encoding is necessary, Pull Requests are welcome.
Base64 Conversion
The UTF-8 binary data in the Uint8Array
(MDN) is then encoded as Base64 by Uint8Array.prototype.toBase64()
(TC39), currently a stage 2 TC39 Proposal, falling back to btoa()
if the environment doesn't support or polyfill Uint8Array.prototype.toBase64()
.
References
- See
Uint8Array
on MDN - See TC39 Proposal "Uint8Array to/from base64 and hex"
- Problems for
btoa()
and Unicode in "The Unicode Problem" on MDN - Sindre Sorhus: "It's time to move from [Node.js]
Buffer
toUint8Array
"
Prior Art
basic-authorization-header
(npm)- Node.js Only. Rely on non-standard, global
Buffer
to base64 encode. This package uses the standardUint8Array
or the standardbtoa()
function. - Use deprecated
new Buffer(string)
, instead of recommendedBuffer.from(string)
. This package does not.
- Node.js Only. Rely on non-standard, global
basic-auth-header
(npm)- Node.js Only. Rely on non-standard, global
Buffer
to base64 encode. This package uses the standardUint8Array
or the standardbtoa()
function. - Use deprecated
new Buffer(string)
, instead of recommendedBuffer.from(string)
. This package does not.
- Node.js Only. Rely on non-standard, global
http-auth-utils
(npm)- Larger package: 1.8 kB minified & gzip (Tested at https://bundlephobia.com/package/[email protected])
- Seems complicated.
@mitmaro/http-authorization-header
(npm)- Larger package: 1.9 kB minified & gzip (Tested at https://bundlephobia.com/package/@mitmaro/[email protected])
- Does not appear to perform the Base64 encoding of credentials—It is left to the caller to perform the encoding.