profunktor / http4s-jwt-auth   1.2.2

Website GitHub

:lock: Opinionated JWT authentication library for Http4s

Scala versions: 3.x 2.13 2.12

http4s-jwt-auth

CI Status Gitter Chat Maven Central Cats friendly Scala Steward badge

Opinionated JWT authentication library for http4s.

Dependencies

jwt-scala is being used to encode and decode JWT tokens.

Add the following dependency to your build.sbt (check latest version on the badge):

"dev.profunktor" %% "http4s-jwt-auth" % Version

This library currently only supports the stable 0.23 series of http4s.

Usage

import cats.effect.IO
import cats.implicits._
import dev.profunktor.auth._
import dev.profunktor.auth.jwt._
import pdi.jwt._
import org.http4s._

case class AuthUser(id: Long, name: String)

// i.e. retrieve user from database
val authenticate: JwtToken => JwtClaim => IO[Option[AuthUser]] =
  token => claim => AuthUser(123L, "joe").some.pure[IO]

val jwtAuth    = JwtAuth.hmac("53cr3t", JwtAlgorithm.HS256)
val middleware = JwtAuthMiddleware[IO, AuthUser](jwtAuth, authenticate)

val routes: AuthedRoutes[AuthUser, IO] = ???
val securedRoutes: HttpRoutes[IO] = middleware(routes)

Build microsite

You need to have jekyll installed before running the following command:

$ sbt makeMicrosite
$ cd site/target/site 
$ jekyll serve

Or you can use nix develop by leveraging the flake.nix in this repo.

Notes

This library is quite opinionated, use with caution. Examples and docs coming soon!

If you would like to see support for any other functionality come have a chat in the Gitter channel!