• Stars
    star
    120
  • Rank 295,983 (Top 6 %)
  • Language
    Scala
  • Created about 5 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ”’ Opinionated JWT authentication library for Http4s

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

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!