How to create a Json Web Token (JWT) using OpenSSL shell commands?

preview_player
Показать описание
Create JWT Token:

# Construct the header
jwt_header=$(echo -n '{"alg":"HS256","typ":"JWT"}' | base64 | sed s/\+/-/g | sed 's/\//_/g' | sed -E s/=+$//)

# Construct the payload

# Store the raw user secret (with example of newline at end)
secret=$'s3cretalsieurokwitqu5lcmsbrhtiy2gsbbcgotoensop\n'

# Convert secret to hex (not base64)
hexsecret=$(echo -n "$secret" | xxd -p | paste -sd "")

# Calculate hmac signature -- note option to pass in the key as hex bytes
hmac_signature=$(echo -n "${jwt_header}.${payload}" | openssl dgst -sha256 -mac HMAC -macopt hexkey:$hexsecret -binary | base64 | sed s/\+/-/g | sed 's/\//_/g' | sed -E s/=+$//)

# Create the full token
jwt="${jwt_header}.${payload}.${hmac_signature}"

Decode JWT Token:

# Install jwt-cli
npm install -g jwt-cli

# Decode
Рекомендации по теме
welcome to shbcf.ru