66 lines
1.4 KiB
Bash
Executable File
66 lines
1.4 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
|
|
#
|
|
# JWT Encoder Bash Script
|
|
#
|
|
|
|
secret='woo1jagh3um1yee9ong8aeM4Uedies0JaizeimaeV0Pha'
|
|
|
|
# Static header fields.
|
|
header='{
|
|
"typ": "JWT",
|
|
"alg": "HS256",
|
|
"iss": "VNC internal"
|
|
}'
|
|
|
|
# Use jq to set the dynamic `iat` and `exp`
|
|
# fields on the header using the current time.
|
|
# `iat` is set to now, and `exp` is now + 1 second.
|
|
header=$(
|
|
echo "${header}" | jq --arg time_str "$(date +%s)" \
|
|
'
|
|
($time_str | tonumber) as $time_num
|
|
| .iat=$time_num
|
|
'
|
|
)
|
|
|
|
shouldBeArray=$(echo "$1" | grep -c ",")
|
|
|
|
echo "should be array: $shouldBeArray"
|
|
|
|
if [ "$shouldBeArray" = "0" ]; then
|
|
payload="{
|
|
\"vncdomain\": \"$1\"
|
|
}"
|
|
else
|
|
splitted=$(echo $1 | tr ',' '\n' | awk '{print "\"" $0 "\","}' | tr -d '\n' | sed 's/,$//g')
|
|
payload="{\"vncdomain\": [ $splitted ] }"
|
|
fi
|
|
|
|
base64_encode()
|
|
{
|
|
declare input=${1:-$(</dev/stdin)}
|
|
# Use `tr` to URL encode the output from base64.
|
|
printf '%s' "${input}" | base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n'
|
|
}
|
|
|
|
json() {
|
|
declare input=${1:-$(</dev/stdin)}
|
|
printf '%s' "${input}" | jq -c .
|
|
}
|
|
|
|
hmacsha256_sign()
|
|
{
|
|
declare input=${1:-$(</dev/stdin)}
|
|
printf '%s' "${input}" | openssl dgst -binary -sha256 -hmac "${secret}"
|
|
}
|
|
|
|
header_base64=$(echo "${header}" | json | base64_encode)
|
|
payload_base64=$(echo "${payload}" | json | base64_encode)
|
|
|
|
header_payload=$(echo "${header_base64}.${payload_base64}")
|
|
signature=$(echo "${header_payload}" | hmacsha256_sign | base64_encode)
|
|
|
|
echo "${header_payload}.${signature}"
|