jwt
Version, currently 0.1.23 versions
- 0.1.2latestMar 14, 2018
- 0.1.1not indexedAug 3, 2018
- 0.1.0not indexedAug 3, 2018
github.com/randomstate/jwt
General purpose JWT library for Crystal. Focus on RSA algorithms to close gap in community.
6 stars
0 dependents
License: MIT
Installation
# Add this to your shard.yml
dependencies:
jwt:
github: randomstate/jwt
version: ~> 0.1.2Then run:
shards installshard.yml
- Crystal
0.24.1- License
- MIT
- Author
- Connor Imrie <3436576+CImrie@users.noreply.github.com>
Dependencies
Runtime Dependencies
- openssl_ext0.1.1github: randomstate/openssl_ext
README
# jwt
General purpose JWT library for Crystal.
Current support for:
[x] RSA
[ ] Anything else
## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
jwt:
github: randomstate/jwt
```
## Usage
```crystal
require "jwt"
```
### Generating a Token
```crystal
builder = JWT::Builder.new
builder.issuer = "me"
builder.subject = "subject"
builder.audience = "my-app"
builder.header["src"] = "multisite-1"
builder.payload["username"] = "flyingkitty2019"
jwt = builder.generate
```
### Decoding a Token
```crystal
jwt = JWT::Token.decode("eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjE1MjEwNjI1MjMsIm5iZiI6MTUyMTA1ODkyMywiaWF0IjoxNTIxMDU4OTIzLCJlbWFpbCI6ImpvaG5AZXhhbXBsZS5jb20ifQ==.MhN4Yiq5Ivevp-XHmPUdecpLWuRu2-IcgMHHfj7hR_VXQtIPqe54uuSwd2")
issuer = jwt.headers["iss"]
username = jwt.payload["username"]
# etc
```
### Validating Token Claims
```crystal
jwt = JWT::Token.decode("eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjE1MjEwNjI1MjMsIm5iZiI6MTUyMTA1ODkyMywiaWF0IjoxNTIxMDU4OTIzLCJlbWFpbCI6ImpvaG5AZXhhbXBsZS5jb20ifQ==.MhN4Yiq5Ivevp-XHmPUdecpLWuRu2-IcgMHHfj7hR_VXQtIPqe54uuSwd2")
validator = JWT::Validator.new
validator.issuer = "must be me"
validator.custom "username" do | username |
next(username == "the_only_allowed_username")
end
validator.validate(token) # Returns true or false
validator.validate(token, true) # true or raises an exception because in strict mode - this will supply a reason for the failure
```
### Verifying a Token using an RSA Algorithm
```crystal
jwt = JWT::Token.decode("eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjE1MjEwNjI1MjMsIm5iZiI6MTUyMTA1ODkyMywiaWF0IjoxNTIxMDU4OTIzLCJlbWFpbCI6ImpvaG5AZXhhbXBsZS5jb20ifQ==.MhN4Yiq5Ivevp-XHmPUdecpLWuRu2-IcgMHHfj7hR_VXQtIPqe54uuSwd2")
pem = "-----BEGIN RSA PUBLIC KEY----- #..."
rsa = OpenSSL::RSA.new(pem, true)
JWT::Verifier::RSA.verify(token, rsa, JWT::Algorithm::RS256)
```
## Contributing
1. Fork it ( https://github.com/randomstate/jwt/fork )
2. Create your feature branch (git checkout -b feature/my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin feature/my-new-feature)
5. Create a new Pull Request
## Contributors
- [cimrie](https://github.com/cimrie) Connor Imrie - creator, maintainer
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.1.2- Tagged
- Mar 14, 2018
- Commit
fa9ae92b69e8- Crystal
0.24.1- Indexed
- yes
Dependents
No indexed shard depends on this one yet.
Repository
github.com/randomstate/jwt
Metadata
- Created
- Aug 12, 2026
- Updated
- Aug 13, 2026
- Synced
- Aug 13, 2026
- Versions
- 3