-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt.sh
executable file
·92 lines (78 loc) · 1.95 KB
/
jwt.sh
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
#!/bin/bash
set -e
print_usage() {
echo "Usage: $0 [-k key_path] [--rsa-key key_path] [--ttl duration] [-s subject] [--subject subject]"
echo " -k, --rsa-key key_path Path to the RSA key file (default: ./rsa.pem)"
echo " -s, --subject subject Subject of the token (default: sarumaj)"
echo " --ttl duration Duration in seconds for which the token will be valid (default: 600)"
}
# Function to URL-safe base64 encode
base64_url_encode() {
openssl enc -base64 -A | tr '+/' '-_' | tr -d '='
}
# Default values
KEY_PATH="./rsa.pem"
TTL="600"
SUBJECT="sarumaj"
# Parse command line options
while [[ "$#" -gt 0 ]]; do
case $1 in
-k | --rsa-key)
KEY_PATH="$2"
shift 2
;;
-s | --subject)
SUBJECT="$2"
shift 2
;;
--ttl)
TTL="$2"
shift 2
;;
*)
echo "Unknown option: $1"
print_usage
exit 1
;;
esac
done
# Check if the RSA key file exists
if [ ! -f "$KEY_PATH" ]; then
echo "RSA key file not found: $KEY_PATH"
print_usage
exit 1
fi
# Validate TTL
if ! [[ "$TTL" =~ ^[0-9]+$ ]]; then
echo "Invalid TTL: $TTL"
print_usage
exit 1
fi
# Get the current timestamp for 'iat' claim
iat=$(date +%s)
# Define header and payload as variables
header='{
"alg": "RS256",
"typ": "JWT"
}'
payload=$(jq -n --arg iat "$iat" --arg ttl "$TTL" --arg sub "$SUBJECT" '
.iat = ($iat | tonumber) |
.iss = "space-invaders" |
.sub = $sub |
.aud = ["space-invaders"] |
if ($ttl | tonumber) > 0 then
.exp = (($iat | tonumber) + ($ttl | tonumber))
else
.
end
')
# Base64 URL encode the header and payload
header_base64=$(echo -n "${header}" | base64_url_encode)
payload_base64=$(echo -n "${payload}" | base64_url_encode)
# Create unsigned token
unsigned_token="${header_base64}.${payload_base64}"
# Sign the token
signature=$(echo -n "${unsigned_token}" | openssl dgst -sha256 -sign "$KEY_PATH" | base64_url_encode)
# Combine to form the final JWT
jwt="${unsigned_token}.${signature}"
echo "${jwt}"