https://github.com/anakinj/jwk-loader
Gem to manage JWK caches
https://github.com/anakinj/jwk-loader
hacktoberfest jwk jwt
Last synced: about 2 months ago
JSON representation
Gem to manage JWK caches
- Host: GitHub
- URL: https://github.com/anakinj/jwk-loader
- Owner: anakinj
- License: mit
- Created: 2022-07-06T19:48:31.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-07-02T12:26:28.000Z (3 months ago)
- Last Synced: 2025-07-28T00:22:07.599Z (2 months ago)
- Topics: hacktoberfest, jwk, jwt
- Language: Ruby
- Homepage:
- Size: 44.9 KB
- Stars: 3
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# jwk-loader
[](https://badge.fury.io/rb/jwk-loader)
[](https://github.com/anakinj/jwk-loader/actions/workflows/test.yml)This gem can be used in combination with the [ruby-jwt](https://rubygems.org/gems/jwt) gem as the mechanism to load and cache the JWKs.
## Installation
Install the gem and add to the application's Gemfile by executing:
$ bundle add jwk-loader
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install jwk-loader
## Usage
### Using as a jwks loader when decoding JWT tokens
```ruby
require "jwt"
require "jwk-loader"JWT.decode(token, nil, true, algorithm: "RS512", jwks: JwkLoader.for_uri(uri: "https://url/to/public/jwks") )
```### Testing endpoints protected by JWT tokens
When testing HTTP endpoints protected by asymmetric JWT keys the mechanism in `jwk_loader/test` can be used to simplify the process.
```ruby
require 'jwk_loader/test'RSpec.describe 'GET /protected' do
include JwkLoader::Testcontext 'when called with a valid token' do
let(:token) { sign_test_token(token_payload: { user_id: "user" }, jwk_endpoint: "https://url/to/public/jwks") }
subject(:response) { get('/protected', { 'HTTP_AUTHORIZATION' => "Bearer #{token}" }) }it 'is a success' do
expect(response.status).to eq(200)
end
end
end
```### Configuring the gem
```ruby
require "jwt-loader"JwkLoader.configure do |config|
config[:cache] = YetAnotherCache.new
config[:cache_grace_period] = 999
end
```or in alternative
```ruby
require "jwt-loader"JwkLoader.configure do |config|
config.cache = YetAnotherCache.new
config.cache_grace_period = 999
end
```## Development
After checking out the repo, run `bundle install` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/anakinj/jwk-loader. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/anakinj/jwk-loader/blob/main/CODE_OF_CONDUCT.md).
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the Jwk::Loader project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/abajubh/jwk-loader/blob/main/CODE_OF_CONDUCT.md).