Skip to content

Commit b26d476

Browse files
authored
Enhance USER_GUIDE with JWT authentication examples
Added instructions for generating keys and tokens for JWT authentication using Node/Bun.
1 parent 61e04f5 commit b26d476

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

docs/USER_GUIDE.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,86 @@ curl -d '{"statements": ["SELECT * FROM users"]}' 127.0.0.1:8081
116116
You can configure client authentication by passing the `--auth-jwt-key-file FILENAME` command line option to `sqld`.
117117
The key is either a PKCS#8-encoded Ed25519 public key in PEM, or just plain bytes of the Ed25519 public key in URL-safe base64.
118118

119+
### Generate Keys using Node/Bun
120+
121+
Generate a private key and public key pair for JWT authentication:
122+
123+
```typescript
124+
import fs from "node:fs";
125+
import { generateKeyPairSync } from "crypto";
126+
127+
const { publicKey, privateKey } = generateKeyPairSync("ed25519");
128+
// Export private key in PKCS#8 PEM format
129+
const privPem = privateKey.export({ format: "pem", type: "pkcs8" });
130+
fs.writeFileSync("private.pem", privPem);
131+
132+
// Export public key in SPKI PEM format
133+
const pubPem = publicKey.export({ format: "pem", type: "spki" });
134+
fs.writeFileSync("public.pem", pubPem);
135+
```
136+
137+
The snippet code generate two files: `private.pem` and `public.pem`, use the `public.pem` in the configuration `--auth-jwt-key-file public.pem`.
138+
139+
> [!CAUTION]
140+
> Always keep in mind that the `private.pem` file must not be shared publicly; you should store it in a secure location, unlike the `public.pem` file, which can be shared without any risk.
141+
142+
### Generate a Token using Node/Bun
143+
144+
For generate the JWT you can install `jose` library and sign a JWT using the `private.pem`.
145+
146+
```console
147+
npm install jose
148+
```
149+
150+
```typescript
151+
import fs from "node:fs";
152+
import { importPKCS8, SignJWT } from "jose";
153+
154+
const privateKeyPem = fs.readFileSync("private.pem", "utf-8");
155+
const privateKey = await importPKCS8(privateKeyPem, "EdDSA");
156+
157+
const payload = {
158+
sub: "username",
159+
name: "Name User",
160+
};
161+
const jwt = await new SignJWT(payload)
162+
.setProtectedHeader({ alg: "EdDSA" })
163+
.setIssuedAt() // Issued At
164+
.setExpirationTime("1h") // Expiration time in 1 hour
165+
.sign(privateKey); // Pass the private key object
166+
167+
fs.writeFileSync("token.txt", jwt);
168+
```
169+
170+
The generated token could be used for authentication:
171+
172+
```console
173+
npm install @libsql/client
174+
```
175+
176+
```typescript
177+
import { createClient } from "@libsql/client";
178+
179+
const client = createClient({
180+
url: "https://example.domain/",
181+
authToken: jwt,
182+
});
183+
184+
await client.batch(
185+
[
186+
"CREATE TABLE IF NOT EXISTS users (email TEXT)",
187+
"INSERT INTO users VALUES ('a@example.com')",
188+
"INSERT INTO users VALUES ('b@example.com')",
189+
"INSERT INTO users VALUES ('c@example.com')",
190+
],
191+
"write",
192+
);
193+
194+
const result = await client.execute("SELECT * FROM users");
195+
196+
console.log("Users:", result.rows);
197+
```
198+
119199
## Deployment
120200

121201
### Deploying with Docker

0 commit comments

Comments
 (0)