http4s-jwt-auth

CircleCI Gitter Chat Maven Central Cats friendly MergifyStatus

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

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)

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!