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.
Arguments
- claim
a named list with fields to include in the jwt payload
- secret
string or raw vector with a secret passphrase
- size
bitsize of sha2 signature, i.e.
sha256,sha384orsha512. Only for HMAC/RSA, not applicable for ECDSA keys.- header
named list with additional parameter fields to include in the jwt header as defined in rfc7515 section 9.1.2
- jwt
string containing the JSON Web Token (JWT)
- key
path or object with RSA or EC private key, see openssl::read_key.
- pubkey
path or object with RSA or EC public key, see openssl::read_pubkey.
Examples
# 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] 1778517446
#>
#> $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] 1778517446
#>
#> $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] 1778517446
#>
#> $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] 7a 67 31 4d 54 63 30 4e 44 59 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] 0d 63 31 af 07 1c fb 47 2e 62 12 f4 ee 1b 24 41 3e 0d fc 7a 27 55 06 98 22
#> [26] 9b 4b 20 30 c3 16 ed
#>
#> $payload
#> $payload$iat
#> [1] 1778517446
#>
#> $payload$name
#> [1] "jeroen"
#>
#> $payload$session
#> [1] 123456
#>
#>
#> $header
#> $header$typ
#> [1] "JWT"
#>
#> $header$alg
#> [1] "HS256"
#>
#>