Skip to content

Commit 06aef60

Browse files
forgot about basic-auth component
1 parent d578f09 commit 06aef60

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<details>
2+
<summary>Basic Authentication</summary>
3+
4+
The simplest authorization scheme is [Basic Authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Authentication#basic_authentication_scheme) which transmits credentials as username/password pairs encoded using base64. Importantly, this scheme does not encrypt credentials. If used over an insecure connection, such as HTTP, they are susceptible to being compromised. Only ever use Basic Authentication over secured connections, such as HTTPS. Even then, its better to upgrade to an encryption based authentication scheme or certificates. Harper supports many different authentication mechanisms, and they will all be covered in later Learn guides.
5+
6+
Use the username and password values from the previous [Install and Connect](/learn/getting-started/install-and-connect-harper) guide to generate an authorization value. The important part is to combine the `username` and `password` using a colon `:` character, encode that using base64, and then append the result to `"Basic "`. Here are some efficient methodologies:
7+
8+
{/* Maybe consider using Tabs here with labels like "Node.js <=v24" and ("Web" or "Winter" or "W/MCA" if defined that abbreviation earlier somehow?) */}
9+
10+
In Node.js v24 or earlier use the [`Buffer`](https://nodejs.org/docs/latest/api/buffer.html) API:
11+
12+
```typescript
13+
// Ensure you replace these with your installation's values
14+
const username = 'HDB_ADMIN';
15+
const password = 'abc123!';
16+
const credentials = Buffer.from(`${username}:${password}`).toString('base64');
17+
const authorizationValue = `Basic ${credentials}`;
18+
```
19+
20+
For Node.js v25 or later, most web browser consoles, and any WinterTC Minimum Common API compatible runtime use the [`Uint8Array.prototype.toBase64()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toBase64) API:
21+
22+
```typescript
23+
// Ensure you replace these with your installation's values
24+
const username = 'HDB_ADMIN';
25+
const password = 'abc123!';
26+
const credentials = new TextEncoder().encode(`${username}:${password}`).toBase64();
27+
const authorizationValue = `Basic ${credentials}`;
28+
```
29+
30+
> Both of these options are preferred over [`btoa()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/btoa) do to its limitation to Latin-1 character set.
31+
32+
And finally, you would use the `authorizationValue` as the value for the `Authorization` header such as:
33+
34+
```typescript
35+
fetch('/', {
36+
// ...
37+
headers: {
38+
Authorization: authorizationValue,
39+
},
40+
// ...
41+
});
42+
```
43+
44+
</details>

0 commit comments

Comments
 (0)