-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic-usage.ts
More file actions
70 lines (56 loc) · 2.49 KB
/
basic-usage.ts
File metadata and controls
70 lines (56 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// examples/basic-usage.ts
// Basic usage with traditional throwing functions
import { generateKey, generateKeyPair, signJWT, unsafeParseJOSEHeader, unsafeParseJWT, validateJWT } from "@cross/jwt";
async function basicUsage() {
console.log("=== Basic JWT Usage (Traditional Throwing Functions) ===\n");
// 1. HMAC with string secret
console.log("1. HMAC with string secret:");
const secret = "my_strong_secretmy_strong_secretmy_strong_secretmy_strong_secretmy_strong_secretmy_strong_HS256";
const payload = { userId: 123, name: "John Doe" };
try {
const key = await generateKey(secret, "HS256");
const jwt = await signJWT(payload, key, { algorithm: "HS256" });
console.log("JWT:", jwt);
const decoded = await validateJWT(jwt, key, { algorithm: "HS256" });
console.log("Decoded payload:", decoded);
} catch (error) {
console.error("Error:", (error as Error).message);
}
console.log("\n" + "=".repeat(50) + "\n");
// 2. RSA with key pair
console.log("2. RSA with key pair:");
try {
const { privateKey, publicKey } = await generateKeyPair("RS256");
const jwt = await signJWT(payload, privateKey, { algorithm: "RS256" });
console.log("JWT:", jwt);
const decoded = await validateJWT(jwt, publicKey, { algorithm: "RS256" });
console.log("Decoded payload:", decoded);
} catch (error) {
console.error("Error:", (error as Error).message);
}
console.log("\n" + "=".repeat(50) + "\n");
// 3. Unsafe parsing (no verification)
console.log("3. Unsafe parsing (no verification):");
try {
const key = await generateKey(secret, "HS256");
const jwt = await signJWT(payload, key);
const unsafePayload = unsafeParseJWT(jwt);
const unsafeHeader = unsafeParseJOSEHeader(jwt);
console.log("Unsafe payload:", unsafePayload);
console.log("Unsafe header:", unsafeHeader);
} catch (error) {
console.error("Error:", (error as Error).message);
}
console.log("\n" + "=".repeat(50) + "\n");
// 4. Error handling example
console.log("4. Error handling example:");
try {
const invalidJWT = "invalid.jwt.token";
const key = await generateKey(secret, "HS256");
await validateJWT(invalidJWT, key);
} catch (error) {
console.log("Caught expected error:", (error as Error).constructor.name, "-", (error as Error).message);
}
}
// Run the example
basicUsage().catch(console.error);