Giter VIP home page Giter VIP logo

zio-jwt-validator's Introduction

zio-jwt-validator

CI/CD

A ZIO based library for validating JWT tokens. Includes fetching keys from JWKS and claim validation.

What this is

You want to protect your Scala backend API using signed JWT tokens?
You don't want to hardcode public keys needed for token signature verification?
You want to use JWKS over HTTPS to retrieve a matching public key?
You want to perform additional validation of JWT claims, e.g. audience and issuer?
You want a Scala library that implements all of this?
Then zio-jwt-validator might be for you.

It is based on zio, zio-json, zio-http (for the http client) and jwt-scala for JWT token parsing and signature verification.

Installation

libraryDependencies += "io.github.janlisse" %% "zio-jwt-validator" % "0.1.0"

Status

  • Only validation of RSA signed tokens is currently supported, which works fine with an Authentication Provider like Auth0
  • Validation of elliptic curve signatures will be added soon
  • Symmetric keys are not supported for the time being

Usage

Here is a simple program to validate a JWT token using JwtValidator:

  val program = for {
    _ <- JwtValidator.validate(jwtToken)
    _ <- ZIO.logInfo("Successfully validated token.")
  } yield ()

All sorts of possible validation errors will be returned through the error type of the returned ZIO. There is a hierarchy of error types derived from JwtValidationError.

In order to run this program you need to provide a ZLayer for a JwtValidator and a JwksFetcher. Note: It is recommended to use the cached version of JwksFetcher which caches the retrieved JWKS, via: JwksFetcherLive.cached(...). It allows to configure a custom cacheTTL, the default is 10 minutes. If you don't want any chaching at all you can still use: JwksFetcherLive.uncached(...)

  val run = program.provide(
    JwksFetcherLive.cached(
      "/Users/jan/.sdkman/candidates/java/current/lib/security/cacerts",
      "changeit"
    ),
    JwtValidatorLive.layer("https://your.auth.provider/.well-known/jwks.json",)
  )

zio-jwt-validator's People

Contributors

github-actions[bot] avatar janlisse avatar

Stargazers

 avatar

Watchers

 avatar  avatar

zio-jwt-validator's Issues

Cache fetched JWSK response

ATM on every validate() call the JWSK is retrieved. For efficiency reasons this should be cached for a configurable amount of time.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.