Skip to content

Instantly share code, notes, and snippets.

@atomicsamurai
Forked from thomasdarimont/readme.md
Last active April 25, 2022 15:08
Show Gist options
  • Select an option

  • Save atomicsamurai/26d8c6073470adeb91495706c49f0165 to your computer and use it in GitHub Desktop.

Select an option

Save atomicsamurai/26d8c6073470adeb91495706c49f0165 to your computer and use it in GitHub Desktop.
Example for decoding a JWT Payload with your Shell (bash, zsh...)

Setup

Add this to your .profile, .bashrc, .zshrc...

decode_base64_url() {
    local len=$((${#1} % 4))
    local result="$1"
    if [ $len -eq 2 ]; then result="$1"'=='
    elif [ $len -eq 3 ]; then result="$1"'=' 
    fi
    echo "$result" | tr '_-' '/+' | openssl enc -d -base64
}

decode_jwt(){
    local in
    if [ $# -eq 0 ]; then
        read in
    else
        in="$1"
    fi
    decode_base64_url $(echo -n $in | cut -d "." -f 2) | jq '.exp |= todateiso8601 | .iat |= todateiso8601 | .nbf |= todateiso8601'
}

# Decode JWT Payload
alias jwt="decode_jwt"

Usage

Command (token as stdin)

curl --request POST \
--data "grant_type=password" \
--data "username=_journeyuser" \
--data "password=<password>" \
--data "scope=test1 test2" \
--data "client_id=_journeyclient" \
--data "client_secret=<secret>" \
--data "auth_chain=PasswordGrant" \
"https://openam-bootcamp-tc2.forgeblocks.com/am/oauth2/realms/root/realms/alpha/access_token" | jq -r .access_token | jwt

Output

    {
    "sub": "e65020d2-1dff-455f-9770-3862863bb8c5",
    "cts": "OAUTH2_STATELESS_GRANT",
    "auth_level": 0,
    "auditTrackingId": "4e5a35d3-3de1-47ae-a6d9-09ef09574f9b-3589911",
    "subname": "e65020d2-1dff-455f-9770-3862863bb8c5",
    "iss": "https://openam-bootcamp-tc2.forgeblocks.com:443/am/oauth2/realms/root/realms/alpha",
    "tokenName": "access_token",
    "token_type": "Bearer",
    "authGrantId": "3E26MKsE5BoibmiTGqEsQ5pxPTU",
    "aud": "_journeyclient",
    "nbf": "2022-04-25T15:01:59Z",
    "grant_type": "password",
    "scope": "test2 test1",
    "auth_time": 1650898919,
    "realm": "/alpha",
    "exp": "2022-04-25T15:02:29Z",
    "iat": "2022-04-25T15:01:59Z",
    "expires_in": 30,
    "jti": "-yXP6FFdimZjLwumc2Bd5BvAtSo"
}

Alternate invokation (token as an argument)

jwt eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJlNjUwMjBkMi0xZGZmLTQ1NWYtOTc3MC0zODYyODYzYmI4YzUiLCJjdHMiOiJPQVVUSDJfU1RBVEVMRVNTX0dSQU5UIiwiYXV0aF9sZXZlbCI6MCwiYXVkaXRUcmFja2luZ0lkIjoiNGU1YTM1ZDMtM2RlMS00N2FlLWE2ZDktMDllZjA5NTc0ZjliLTM1ODgzNTYiLCJzdWJuYW1lIjoiZTY1MDIwZDItMWRmZi00NTVmLTk3NzAtMzg2Mjg2M2JiOGM1IiwiaXNzIjoiaHR0cHM6Ly9vcGVuYW0tYm9vdGNhbXAtdGMyLmZvcmdlYmxvY2tzLmNvbTo0NDMvYW0vb2F1dGgyL3JlYWxtcy9yb290L3JlYWxtcy9hbHBoYSIsInRva2VuTmFtZSI6ImFjY2Vzc190b2tlbiIsInRva2VuX3R5cGUiOiJCZWFyZXIiLCJhdXRoR3JhbnRJZCI6ImNWQ2J4eE9oVkc2YWRYaldieHl6M1h4TTh0OCIsImF1ZCI6Il9qb3VybmV5Y2xpZW50IiwibmJmIjoxNjUwODk4ODE5LCJncmFudF90eXBlIjoicGFzc3dvcmQiLCJzY29wZSI6InRlc3QyIHRlc3QxIiwiYXV0aF90aW1lIjoxNjUwODk4ODE5LCJyZWFsbSI6Ii9hbHBoYSIsImV4cCI6MTY1MDg5ODg0OSwiaWF0IjoxNjUwODk4ODE5LCJleHBpcmVzX2luIjozMCwianRpIjoiNzRKLTdaRkJDb3VZakdya0RJRDFLUk9PMUtVIn0.JN33NvitSwbw6NYhg4pFpAZDSqkXSBga5zL1stIUsLU

Output

{
    "sub": "e65020d2-1dff-455f-9770-3862863bb8c5",
    "cts": "OAUTH2_STATELESS_GRANT",
    "auth_level": 0,
    "auditTrackingId": "4e5a35d3-3de1-47ae-a6d9-09ef09574f9b-3588356",
    "subname": "e65020d2-1dff-455f-9770-3862863bb8c5",
    "iss": "https://openam-bootcamp-tc2.forgeblocks.com:443/am/oauth2/realms/root/realms/alpha",
    "tokenName": "access_token",
    "token_type": "Bearer",
    "authGrantId": "cVCbxxOhVG6adXjWbxyz3XxM8t8",
    "aud": "_journeyclient",
    "nbf": "2022-04-25T15:00:19Z",
    "grant_type": "password",
    "scope": "test2 test1",
    "auth_time": 1650898819,
    "realm": "/alpha",
    "exp": "2022-04-25T15:00:49Z",
    "iat": "2022-04-25T15:00:19Z",
    "expires_in": 30,
    "jti": "74J-7ZFBCouYjGrkDID1KROO1KU"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment