jwt_encode: JSON Web Token

Description Usage Arguments Examples

Description

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.

Usage

1
2
3
4
5
6
7
8
9
jwt_encode_hmac(claim = jwt_claim(), secret, size = 256, header = NULL)

jwt_decode_hmac(jwt, secret)

jwt_encode_sig(claim = jwt_claim(), key, size = 256, header = NULL)

jwt_decode_sig(jwt, pubkey)

jwt_split(jwt)

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, sha384 or sha512. 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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 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)

# RSA encoding
mykey <- openssl::rsa_keygen()
pubkey <- as.list(mykey)$pubkey
sig <- jwt_encode_sig(token, mykey)
jwt_decode_sig(sig, pubkey)

# Same with EC
mykey <- openssl::ec_keygen()
pubkey <- as.list(mykey)$pubkey
sig <- jwt_encode_sig(token, mykey)
jwt_decode_sig(sig, pubkey)

# 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)

jose documentation built on Nov. 6, 2021, 5:07 p.m.