jwt

General purpose JWT library for Crystal. Focus on RSA algorithms to close gap in community.

jwt

General purpose JWT library for Crystal.

Current support for: [x] RSA [ ] Anything else

Installation

Add this to your application's shard.yml:

dependencies:
  jwt:
    github: randomstate/jwt

Usage

require "jwt"

Generating a Token

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

jwt = JWT::Token.decode("eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJleHAiOjE1MjEwNjI1MjMsIm5iZiI6MTUyMTA1ODkyMywiaWF0IjoxNTIxMDU4OTIzLCJlbWFpbCI6ImpvaG5AZXhhbXBsZS5jb20ifQ==.MhN4Yiq5Ivevp-XHmPUdecpLWuRu2-IcgMHHfj7hR_VXQtIPqe54uuSwd2")

issuer = jwt.headers["iss"]
username = jwt.payload["username"]
# etc

Validating Token Claims

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

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 Connor Imrie - creator, maintainer
Repository

jwt

Owner
Statistic
  • 5
  • 0
  • 0
  • 1
  • 1
  • over 6 years ago
  • March 12, 2018
License

MIT License

Links
Synced at

Thu, 07 Nov 2024 13:33:23 GMT

Languages