JwtCirce Object

Installation

libraryDependencies += "com.github.jwt-scala" %% "jwt-circe" % "9.1.1"

Basic usage

import java.time.Instant
import pdi.jwt.{JwtCirce, JwtAlgorithm, JwtClaim}

val claim = JwtClaim(
    expiration = Some(Instant.now.plusSeconds(157784760).getEpochSecond)
  , issuedAt = Some(Instant.now.getEpochSecond)
)
// claim: JwtClaim = JwtClaim({}, None, None, None, Some(1819679589), None, Some(1661894829), None)
val key = "secretKey"
// key: String = "secretKey"
val algo = JwtAlgorithm.HS256
// algo: JwtAlgorithm.HS256.type = HS256

val token = JwtCirce.encode(claim, key, algo)
// token: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE4MTk2Nzk1ODksImlhdCI6MTY2MTg5NDgyOX0.nJ7kdsXOYdhqTr4-hBJfpshutammQMkFvJ1k49sbKzU"

JwtCirce.decodeJson(token, key, Seq(JwtAlgorithm.HS256))
// res1: util.Try[io.circe.Json] = Success(
//   value = JObject(value = object[exp -> 1819679589,iat -> 1661894829])
// )
JwtCirce.decode(token, key, Seq(JwtAlgorithm.HS256))
// res2: util.Try[JwtClaim] = Success(
//   value = JwtClaim({}, None, None, None, Some(1819679589), None, Some(1661894829), None)
// )

Encoding

import java.time.Instant
import io.circe._, jawn.{parse => jawnParse}
import pdi.jwt.{JwtCirce, JwtAlgorithm}

val key = "secretKey"
// key: String = "secretKey"
val algo = JwtAlgorithm.HS256
// algo: JwtAlgorithm.HS256.type = HS256

val Right(claimJson) = jawnParse(s"""{"expires":${Instant.now.getEpochSecond}}""")
// claimJson: Json = JObject(value = object[expires -> 1661894829])
val Right(header) = jawnParse( """{"typ":"JWT","alg":"HS256"}""")
// header: Json = JObject(value = object[typ -> "JWT",alg -> "HS256"])
// From just the claim to all possible attributes
JwtCirce.encode(claimJson)
// res4: String = "eyJhbGciOiJub25lIn0.eyJleHBpcmVzIjoxNjYxODk0ODI5fQ."
JwtCirce.encode(claimJson, key, algo)
// res5: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHBpcmVzIjoxNjYxODk0ODI5fQ.3a1bIinO8O6JhcuRU-5UM693GAj0-Gsb2ST2eyJjzGk"
JwtCirce.encode(header, claimJson, key)
// res6: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHBpcmVzIjoxNjYxODk0ODI5fQ.3a1bIinO8O6JhcuRU-5UM693GAj0-Gsb2ST2eyJjzGk"

Decoding

import java.time.Instant
import pdi.jwt.{JwtCirce, JwtAlgorithm, JwtClaim}

val claim = JwtClaim(
    expiration = Some(Instant.now.plusSeconds(157784760).getEpochSecond)
  , issuedAt = Some(Instant.now.getEpochSecond)
)
// claim: JwtClaim = JwtClaim({}, None, None, None, Some(1819679589), None, Some(1661894829), None)
val key = "secretKey"
// key: String = "secretKey"
val algo = JwtAlgorithm.HS256
// algo: JwtAlgorithm.HS256.type = HS256

val token = JwtCirce.encode(claim, key, algo)
// token: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE4MTk2Nzk1ODksImlhdCI6MTY2MTg5NDgyOX0.nJ7kdsXOYdhqTr4-hBJfpshutammQMkFvJ1k49sbKzU"

// You can decode to JsObject
JwtCirce.decodeJson(token, key, Seq(JwtAlgorithm.HS256))
// res8: util.Try[io.circe.Json] = Success(
//   value = JObject(value = object[exp -> 1819679589,iat -> 1661894829])
// )
JwtCirce.decodeJsonAll(token, key, Seq(JwtAlgorithm.HS256))
// res9: util.Try[(io.circe.Json, io.circe.Json, String)] = Success(
//   value = (
//     JObject(value = object[typ -> "JWT",alg -> "HS256"]),
//     JObject(value = object[exp -> 1819679589,iat -> 1661894829]),
//     "nJ7kdsXOYdhqTr4-hBJfpshutammQMkFvJ1k49sbKzU"
//   )
// )
// Or to case classes
JwtCirce.decode(token, key, Seq(JwtAlgorithm.HS256))
// res10: util.Try[JwtClaim] = Success(
//   value = JwtClaim({}, None, None, None, Some(1819679589), None, Some(1661894829), None)
// )
JwtCirce.decodeAll(token, key, Seq(JwtAlgorithm.HS256))
// res11: util.Try[(pdi.jwt.JwtHeader, JwtClaim, String)] = Success(
//   value = (
//     JwtHeader(Some(HS256), Some(JWT), None, None),
//     JwtClaim({}, None, None, None, Some(1819679589), None, Some(1661894829), None),
//     "nJ7kdsXOYdhqTr4-hBJfpshutammQMkFvJ1k49sbKzU"
//   )
// )