Sign or verify a JSON web token. The jwt_encode_hmac
, jwt_encode_rsa
,
and jwt_encode_ec
default to HS256
, RS256
, and ES256
respectively. See jwt.io or
RFC7519 for more details.
a named list with fields to include in the jwt payload
string or raw vector with a secret passphrase
bitsize of sha2 signature, i.e. sha256
, sha384
or sha512
.
Only for HMAC/RSA, not applicable for ECDSA keys.
named list with additional parameter fields to include in the jwt header as defined in rfc7515 section 9.1.2
string containing the JSON Web Token (JWT)
path or object with RSA or EC private key, see openssl::read_key.
path or object with RSA or EC public key, see openssl::read_pubkey.
# HMAC signing
mysecret <- "This is super secret"
token <- jwt_claim(name = "jeroen", session = 123456)
sig <- jwt_encode_hmac(token, mysecret)
jwt_decode_hmac(sig, mysecret)
#> $iat
#> [1] 1748548087
#>
#> $name
#> [1] "jeroen"
#>
#> $session
#> [1] 123456
#>
# RSA encoding
mykey <- openssl::rsa_keygen()
pubkey <- as.list(mykey)$pubkey
sig <- jwt_encode_sig(token, mykey)
jwt_decode_sig(sig, pubkey)
#> $iat
#> [1] 1748548087
#>
#> $name
#> [1] "jeroen"
#>
#> $session
#> [1] 123456
#>
# Same with EC
mykey <- openssl::ec_keygen()
pubkey <- as.list(mykey)$pubkey
sig <- jwt_encode_sig(token, mykey)
jwt_decode_sig(sig, pubkey)
#> $iat
#> [1] 1748548087
#>
#> $name
#> [1] "jeroen"
#>
#> $session
#> [1] 123456
#>
# Get elements of the key
mysecret <- "This is super secret"
token <- jwt_claim(name = "jeroen", session = 123456)
jwt <- jwt_encode_hmac(token, mysecret)
jwt_split(jwt)
#> $type
#> [1] "HMAC"
#>
#> $keysize
#> [1] 256
#>
#> $data
#> [1] 65 79 4a 30 65 58 41 69 4f 69 4a 4b 56 31 51 69 4c 43 4a 68 62 47 63 69 4f
#> [26] 69 4a 49 55 7a 49 31 4e 69 4a 39 2e 65 79 4a 70 59 58 51 69 4f 6a 45 33 4e
#> [51] 44 67 31 4e 44 67 77 4f 44 63 73 49 6d 35 68 62 57 55 69 4f 69 4a 71 5a 58
#> [76] 4a 76 5a 57 34 69 4c 43 4a 7a 5a 58 4e 7a 61 57 39 75 49 6a 6f 78 4d 6a 4d
#> [101] 30 4e 54 5a 39
#>
#> $sig
#> [1] 5c d2 48 ec 45 50 72 e7 fa d5 80 cc ae 42 96 d3 e5 f3 0a 79 14 88 51 a8 d8
#> [26] 60 28 99 76 35 75 55
#>
#> $payload
#> $payload$iat
#> [1] 1748548087
#>
#> $payload$name
#> [1] "jeroen"
#>
#> $payload$session
#> [1] 123456
#>
#>
#> $header
#> $header$typ
#> [1] "JWT"
#>
#> $header$alg
#> [1] "HS256"
#>
#>