-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjwt_auth.ruff
More file actions
95 lines (75 loc) · 2.6 KB
/
Copy pathjwt_auth.ruff
File metadata and controls
95 lines (75 loc) · 2.6 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# JWT Authentication Example
# Demonstrates encoding and decoding JWT tokens for API authentication
print("=== JWT Authentication Demo ===\n")
# 1. Create a user payload
print("1. Creating user payload...")
user_payload := dict()
user_payload["user_id"] := 42
user_payload["username"] := "alice"
user_payload["email"] := "alice@example.com"
user_payload["role"] := "admin"
user_payload["exp"] := now() + 3600 # Expires in 1 hour
print("User data:")
print(user_payload)
print("")
# 2. Encode JWT token with secret key
print("2. Encoding JWT token...")
secret_key := "my-super-secret-key-2026"
auth_token := jwt_encode(user_payload, secret_key)
print("Generated token:")
print(auth_token)
print("")
# 3. Decode JWT token to verify
print("3. Decoding and verifying token...")
decoded_payload := jwt_decode(auth_token, secret_key)
print("Decoded payload:")
print(decoded_payload)
print("")
# 4. Extract user information
print("4. Extracting user information...")
user_id := decoded_payload["user_id"]
username := decoded_payload["username"]
role := decoded_payload["role"]
print("User ID: " + to_string(user_id))
print("Username: " + username)
print("Role: " + role)
print("")
# 5. Simulate API authentication flow
print("5. Simulating API authentication...")
func authenticate_user(token, secret) {
try {
payload := jwt_decode(token, secret)
user_id := payload["user_id"]
print("✓ Authentication successful for user " + to_string(user_id))
return {"authenticated": true, "user_id": user_id}
} except err {
print("✗ Authentication failed: " + err.message)
return {"authenticated": false, "error": err.message}
}
}
# Test with valid token
auth_result := authenticate_user(auth_token, secret_key)
print("Auth result:")
print(auth_result)
print("")
# Skip wrong-secret check in auto-run mode because auth errors are fatal in VM path
print("6. Skipping wrong-secret check in auto-run mode...")
print("")
# 7. Create tokens with different payloads
print("7. Creating tokens for different use cases...")
# API access token
api_payload := dict()
api_payload["client_id"] := "app-123"
api_payload["scopes"] := ["read", "write"]
api_payload["exp"] := now() + 86400 # 24 hours
api_token := jwt_encode(api_payload, secret_key)
print("API token: " + api_token)
# Refresh token (longer expiry)
refresh_payload := dict()
refresh_payload["user_id"] := 42
refresh_payload["token_type"] := "refresh"
refresh_payload["exp"] := now() + 2592000 # 30 days
refresh_token := jwt_encode(refresh_payload, secret_key)
print("Refresh token: " + refresh_token)
print("")
print("=== Demo complete ===")