Skip to content

How to encode/decode jwt token in Oldes/Rebol3 #72

Answered by Oldes
fvanzeveren asked this question in Q&A
Discussion options

You must be logged in to vote

Here is code which you can use for a simple encoding:

jwt: context [
    base64UrlEncode: func[data [string! binary! map!]][
        if map? data [data: to-json data]
        data: enbase/flat/url data 64
        trim/with data #"=" ;@@ https://github.com/Oldes/Rebol-issues/issues/2548
    ]
    header: #(
        alg: "HS256"
        typ: "JWT"
    )

    set 'encode-jwt func[
        payload [map! string!]
        secret [string!]
        /local data signature
    ][
        data: ajoin [base64UrlEncode header #"." base64UrlEncode payload]
        signature: checksum/with data 'SHA256 :secret
        ajoin [data #"." base64UrlEncode signature]
    ]
]


payload: #(
    sub: "1234567890"

Replies: 6 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by fvanzeveren
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@Oldes
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants