JWTea
The goal of JWTea is to simplify authentication in web apps without coupling it with any specific strategy. It is a simple lightweight ruby wrapper around the JWT gem, which allows for storing jti claims so that tokens can be revoked and validated. If that sounds like your cup of tea, then this gem is for you.
Comes with a Redis store built-in, but allows for custom stores as well.
Installation
Add this line to your application's Gemfile:
gem 'jwtea-rb'
And then execute:
$ bundle
Or install it yourself as:
$ gem install jwtea
Usage
Brewing a JWTea token (encode)
kettle = JWTea::Kettle.new(
secret: 'MY_SECRET_KEY',
store: JWTea::Stores::RedisStore.new,
algorithm: 'HS256',
expires_in: 3600
)
data = { some: 'data' }
token = kettle.brew(data)
token.encoded
token.jti
token.exp
Or, if you don't really care about jti/exp and just want the encoded token
data = { some: 'data' }
encoded_token = kettle.encode(data)
Pouring a JWTea token (decoding)
token = kettle.pour(encoded_token)
token.jti
token.data
Again, if you just want the data you can do
data = kettle.decode(encoded_token)
If using a store, you can validate that the token hasn't been revoked (trying to pour/decode a revoked token will yield an error)
kettle.valid?(encoded_token)
Revoking a token
kettle.revoke(encoded_token)
kettle.valid?(encoded_token)
You can also define your own method of storage, which just needs to respond to save
, exists?
and delete
class MyCustomStore
def save(jti, exp, ttl_in_seconds)
end
def exists?(jti, exp)
end
def delete(jti)
end
end
JWTea::Kettle.new(
secret: 'MY_SECRET_KEY',
store: MyCustomStore.new,
)
That's pretty much it!
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jwtea. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the JWTea project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.