crystal-community / Jwt

Licence: mit
JWT implementation in Crystal

Programming Languages

crystal
512 projects

Labels

Projects that are alternatives of or similar to Jwt

Spark Pac4j
Security library for Sparkjava: OAuth, CAS, SAML, OpenID Connect, LDAP, JWT...
Stars: ✭ 154 (-9.94%)
Mutual labels:  jwt
Branca Spec
Authenticated and encrypted API tokens using modern crypto
Stars: ✭ 163 (-4.68%)
Mutual labels:  jwt
Pac4j
Security engine for Java (authentication, authorization, multi frameworks): OAuth, CAS, SAML, OpenID Connect, LDAP, JWT...
Stars: ✭ 2,097 (+1126.32%)
Mutual labels:  jwt
Cakephp Jwt Auth
A CakePHP plugin for authenticating using JSON Web Tokens
Stars: ✭ 153 (-10.53%)
Mutual labels:  jwt
Api guard
JWT authentication solution for Rails APIs
Stars: ✭ 159 (-7.02%)
Mutual labels:  jwt
Spring Rest Ecommerce
Java Spring Boot - Ecommerce REST API
Stars: ✭ 164 (-4.09%)
Mutual labels:  jwt
Fake Api Jwt Json Server
A Fake API with JWT Authentication using json-server and jsonwebtoken
Stars: ✭ 151 (-11.7%)
Mutual labels:  jwt
React Jwt Authentication Example
React - JWT Authentication Tutorial & Example
Stars: ✭ 170 (-0.58%)
Mutual labels:  jwt
Django Jwt Auth
JSON Web Token Authentication support for Django
Stars: ✭ 160 (-6.43%)
Mutual labels:  jwt
Fastify Jwt
JWT utils for Fastify
Stars: ✭ 165 (-3.51%)
Mutual labels:  jwt
Spring Boot Examples
个人学习 SpringBoot2.x 写的一些示例程序,目前正在持续更新中.....
Stars: ✭ 159 (-7.02%)
Mutual labels:  jwt
Flask Restless Security
Concise skeleton for development of Flask, Flask-Restless, SQLAlchemy, JWT based REST APIs.
Stars: ✭ 159 (-7.02%)
Mutual labels:  jwt
Cognito Express
Authenticates API requests on a Node application by verifying the JWT signature of AccessToken or IDToken generated by Amazon Cognito.
Stars: ✭ 165 (-3.51%)
Mutual labels:  jwt
Twitter Clone With Graphql Reactnative
Stars: ✭ 155 (-9.36%)
Mutual labels:  jwt
Liugh Parent
SpringBoot+SpringCloud Oauth2+JWT+MybatisPlus实现Restful快速开发后端脚手架
Stars: ✭ 2,112 (+1135.09%)
Mutual labels:  jwt
Jwt Security Example
Spring Boot with Spring Security using JWT
Stars: ✭ 154 (-9.94%)
Mutual labels:  jwt
Security.identity
.NET DevPack Identity is a set of common implementations to help you implementing Identity, Jwt, claims validation and another facilities
Stars: ✭ 165 (-3.51%)
Mutual labels:  jwt
Lexikjwtauthenticationbundle
JWT authentication for your Symfony API
Stars: ✭ 2,184 (+1177.19%)
Mutual labels:  jwt
Jwt Pwn
Security Testing Scripts for JWT
Stars: ✭ 170 (-0.58%)
Mutual labels:  jwt
Spring Boot Security Jwt Spa
Spring Boot 2 + JWT + Spring Security 5的单页应用(SPA) Restful 解决方案
Stars: ✭ 166 (-2.92%)
Mutual labels:  jwt

Crystal JWT Build Status

An implementation of JSON Web Token (JWT) in Crystal programming language.

Installation

Add this to your application's shard.yml:

dependencies:
  jwt:
    github: crystal-community/jwt

Usage

# Encoding
payload = { "foo" => "bar" }
token = JWT.encode(payload, "SecretKey", JWT::Algorithm::HS256)
# => "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIifQ.Y3shN5Wh4FmOPM34biIm9QQmat373hJFKNxgSANQWJo"

# Custom headers
token = JWT.encode(payload, "SecretKey", JWT::Algorithm::HS256, custom: "header")

# Decoding
payload, header = JWT.decode(token, "$secretKey", JWT::Algorithm::HS256)
# payload = {"foo" => "bar"}
# header = {"typ" => "JWT", "alg" => "HS256"}

# You can optionally ignore verification and validation if you want to inspect the token
payload, header = JWT.decode(token, verify: false, validate: false)
# Verification checks the signature
# Validation is checking if the token has expired etc

Supported algorithms

  • [x] none
  • [x] HMAC (HS256, HS384, HS512)
  • [x] RSA (RS256, RS384, RS512)
  • [x] ECDSA (ES256, ES384, ES512)

Supported reserved claim names

JSON Web Token defines some reserved claim names and how they should be used.

Expiration Time (exp)

From RFC 7519:

The "exp" (expiration time) claim identifies the expiration time on or after which the JWT MUST NOT be accepted for processing. The processing of the "exp" claim requires that the current date/time MUST be before the expiration date/time listed in the "exp" claim. Implementers MAY provide for some small leeway, usually no more than a few minutes, to account for clock skew. Its value MUST be a number containing a NumericDate value. Use of this claim is OPTIONAL

Example:

# Create token that expires in 1 minute
exp = Time.utc.to_unix + 60
payload = { "foo" => "bar", "exp" => exp }
token = JWT.encode(payload, "SecretKey", JWT::Algorithm::HS256)

# At this moment token can be decoded
payload, header = JWT.decode(token, "SecretKey", JWT::Algorithm::HS256)

sleep 61
# Now token is expired, so JWT::ExpiredSignatureError will be raised
payload, header = JWT.decode(token, "SecretKey", JWT::Algorithm::HS256)

Not Before Time (nbf)

From RFC 7519:

MUST NOT be accepted for processing. The processing of the "nbf" The "nbf" (not before) claim identifies the time before which the JWT claim requires that the current date/time MUST be after or equal to the not-before date/time listed in the "nbf" claim. Implementers MAY provide for some small leeway, usually no more than a few minutes, to account for clock skew. Its value MUST be a number containing a NumericDate value. Use of this claim is OPTIONAL.

Example:

# Create token that will become acceptable in 1 minute
nbf = Time.utc.to_unix + 60
payload = { "foo" => "bar", "nbf" => nbf }
token = JWT.encode(payload, "SecretKey", JWT::Algorithm::HS256)

# Currently it's not acceptable, raises JWT::ImmatureSignatureError
JWT.decode(token, "SecretKey", JWT::Algorithm::HS256)

Issued At (iat)

From RFC 7519:

The "iat" (issued at) claim identifies the time at which the JWT was issued. This claim can be used to determine the age of the JWT. Its value MUST be a number containing a NumericDate value. Use of this claim is OPTIONAL.

Example:

payload = { "foo" => "bar", "iat" => Time.utc.to_unix }
token = JWT.encode(payload, "SecretKey", JWT::Algorithm::HS256)

Audience (aud)

From RFC 7519:

The aud (audience) claim identifies the recipients that the JWT is intended for. Each principal intended to process the JWT MUST identify itself with a value in the audience claim. If the principal processing the claim does not identify itself with a value in the aud claim when this claim is present, then the JWT MUST be rejected. In the general case, the aud value is an array of case-sensitive strings, each containing a StringOrURI value. In the special case when the JWT has one audience, the aud value MAY be a single case-sensitive string containing a StringOrURI value. The interpretation of audience values is generally application specific. Use of this claim is OPTIONAL.

Example:

payload = {"foo" => "bar", "aud" => ["sergey", "julia"]}
token = JWT.encode(payload, "key", JWT::Algorithm::HS256)

# OK, aud matches
payload, header = JWT.decode(token, "key", JWT::Algorithm::HS256, aud: "sergey")

# aud does not match, raises JWT::InvalidAudienceError
payload, header = JWT.decode(token, "key", JWT::Algorithm::HS256, aud: "max")

Issuer (iss)

From RFC 7519:

The iss (issuer) claim identifies the principal that issued the JWT. The processing of this claim is generally application specific. The iss value is a case-sensitive string containing a StringOrURI value. Use of this claim is OPTIONAL.

Example:

payload = { "foo" => "bar", "iss" => "me"}
token = JWT.encode(payload, "SecretKey", "HS256")

# OK, because iss matches
payload, header = JWT.decode(token, "SecretKey", JWT::Algorithm::HS256, iss: "me")

# iss does not match, raises JWT::InvalidIssuerError
payload, header = JWT.decode(token, "SecretKey", JWT::Algorithm::HS256, iss: "you")

Subject (sub)

From RFC 7519:

The sub (subject) claim identifies the principal that is the subject of the JWT. The Claims in a JWT are normally statements about the subject. The subject value MUST either be scoped to be locally unique in the context of the issuer or be globally unique. The processing of this claim is generally application specific. The sub value is a case-sensitive string containing a StringOrURI value. Use of this claim is OPTIONAL.

Example:

payload = { "nomo" => "Sergeo", "sub" => "Esperanto" }
token = JWT.encode(payload, "key", JWT::Algorithm::HS256)

# Raises JWT::InvalidSubjectError, because "sub" claim does not match
JWT.decode(token, "key", JWT::Algorithm::HS256, sub: "Junularo")

JWT ID (jti)

From RFC 7519:

The jti (JWT ID) claim provides a unique identifier for the JWT. The identifier value MUST be assigned in a manner that ensures that there is a negligible probability that the same value will be accidentally assigned to a different data object; if the application uses multiple issuers, collisions MUST be prevented among values produced by different issuers as well. The jti claim can be used to prevent the JWT from being replayed. The jti value is a case-sensitive string. Use of this claim is OPTIONAL.

Example:

require "secure_random"

jti = SecureRandom.urlsafe_base64
payload = { "foo" => "bar", "jti" => jti }
token = JWT.encode(payload, "SecretKey", JWT::Algorithm::HS256)

Exceptions

  • JWT::Error
    • JWT::DecodeError
      • JWT::VerificationError
      • JWT::ExpiredSignatureError
      • JWT::ImmatureSignatureError
      • JWT::InvalidAudienceError
      • JWT::InvalidIssuerError
      • JWT::InvalidSubjectError
    • UnsupportedAlgorithmError

Test

crystal spec

Contributors

  • greyblake Potapov Sergey - creator, maintainer
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].