A Ruby implementation of JSON Web Token draft 06.
sudo gem install jwt
JWT.encode({"some" => "payload"}, "secret")
Note the resulting JWT will not be encrypted, but verifiable with a secret key.
JWT.decode("someJWTstring", "secret")
If the secret is wrong, it will raise a JWT::DecodeError
telling you as such. You can still get at the payload by setting the verify argument to false.
JWT.decode("someJWTstring", nil, false)
The JWT spec supports several algorithms for cryptographic signing. This library currently supports:
HMAC
- HS256 - HMAC using SHA-256 hash algorithm (default)
- HS384 - HMAC using SHA-384 hash algorithm
- HS512 - HMAC using SHA-512 hash algorithm
RSA
- RS256 - RSA using SHA-256 hash algorithm
- RS384 - RSA using SHA-384 hash algorithm
- RS512 - RSA using SHA-512 hash algorithm
Change the algorithm with by setting it in encode:
JWT.encode({"some" => "payload"}, "secret", "HS512")
Plaintext
We also support unsigned plaintext JWTs as introduced by draft 03 by explicitly specifying nil
as the key and algorithm:
jwt = JWT.encode({"some" => "payload"}, nil, nil)
JWT.decode(jwt, nil, nil)
We depend on Echoe for defining gemspec and performing releases to rubygems.org, which can be done with
rake release
The tests are written with rspec. Given you have rake and rspec, you can run tests with
rake test
- Jordan Brough [email protected]
- Ilya Zhitomirskiy [email protected]
- Daniel Grippi [email protected]
- Jeff Lindsay [email protected]
- Bob Aman [email protected]
- Micah Gates [email protected]
- Ariel Salomon (Oscil8)
MIT