JwtJson Object

Installation

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

Basic usage

import java.time.Clock
import pdi.jwt.{JwtJson, JwtAlgorithm}
import play.api.libs.json.Json

implicit val clock: Clock = Clock.systemUTC
// clock: Clock = SystemClock[Z]

val claim = Json.obj(("user", 1), ("nbf", 1431520421))
// claim: play.api.libs.json.JsObject = JsObject(
//   underlying = Map(
//     "user" -> JsNumber(value = 1),
//     "nbf" -> JsNumber(value = 1431520421)
//   )
// )
val key = "secretKey"
// key: String = "secretKey"
val algo = JwtAlgorithm.HS256
// algo: JwtAlgorithm.HS256.type = HS256

JwtJson.encode(claim)
// res0: String = "eyJhbGciOiJub25lIn0.eyJ1c2VyIjoxLCJuYmYiOjE0MzE1MjA0MjF9."

val token = JwtJson.encode(claim, key, algo)
// token: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoxLCJuYmYiOjE0MzE1MjA0MjF9.VmfmoqRbRvna9lfpCx4lXf96eD_X_woBM0twLjBGLlQ"

JwtJson.decodeJson(token, key, Seq(JwtAlgorithm.HS256))
// res1: util.Try[play.api.libs.json.JsObject] = Success(
//   value = JsObject(
//     underlying = Map(
//       "user" -> JsNumber(value = 1),
//       "nbf" -> JsNumber(value = 1431520421)
//     )
//   )
// )

JwtJson.decode(token, key, Seq(JwtAlgorithm.HS256))
// res2: util.Try[pdi.jwt.JwtClaim] = Success(
//   value = JwtClaim({"user":1}, None, None, None, None, Some(1431520421), None, None)
// )

Encoding

val header = Json.obj(("typ", "JWT"), ("alg", "HS256"))
// header: play.api.libs.json.JsObject = JsObject(
//   underlying = Map(
//     "typ" -> JsString(value = "JWT"),
//     "alg" -> JsString(value = "HS256")
//   )
// )
// From just the claim to all possible attributes
JwtJson.encode(claim)
// res3: String = "eyJhbGciOiJub25lIn0.eyJ1c2VyIjoxLCJuYmYiOjE0MzE1MjA0MjF9."
JwtJson.encode(claim, key, algo)
// res4: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoxLCJuYmYiOjE0MzE1MjA0MjF9.VmfmoqRbRvna9lfpCx4lXf96eD_X_woBM0twLjBGLlQ"
JwtJson.encode(header, claim, key)
// res5: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoxLCJuYmYiOjE0MzE1MjA0MjF9.VmfmoqRbRvna9lfpCx4lXf96eD_X_woBM0twLjBGLlQ"

Decoding

// You can decode to JsObject
JwtJson.decodeJson(token, key, Seq(JwtAlgorithm.HS256))
// res6: util.Try[play.api.libs.json.JsObject] = Success(
//   value = JsObject(
//     underlying = Map(
//       "user" -> JsNumber(value = 1),
//       "nbf" -> JsNumber(value = 1431520421)
//     )
//   )
// )
JwtJson.decodeJsonAll(token, key, Seq(JwtAlgorithm.HS256))
// res7: util.Try[(play.api.libs.json.JsObject, play.api.libs.json.JsObject, String)] = Success(
//   value = (
//     JsObject(
//       underlying = Map(
//         "typ" -> JsString(value = "JWT"),
//         "alg" -> JsString(value = "HS256")
//       )
//     ),
//     JsObject(
//       underlying = Map(
//         "user" -> JsNumber(value = 1),
//         "nbf" -> JsNumber(value = 1431520421)
//       )
//     ),
//     "VmfmoqRbRvna9lfpCx4lXf96eD_X_woBM0twLjBGLlQ"
//   )
// )
// Or to case classes
JwtJson.decode(token, key, Seq(JwtAlgorithm.HS256))
// res8: util.Try[pdi.jwt.JwtClaim] = Success(
//   value = JwtClaim({"user":1}, None, None, None, None, Some(1431520421), None, None)
// )
JwtJson.decodeAll(token, key, Seq(JwtAlgorithm.HS256))
// res9: util.Try[(pdi.jwt.JwtHeader, pdi.jwt.JwtClaim, String)] = Success(
//   value = (
//     JwtHeader(Some(HS256), Some(JWT), None, None),
//     JwtClaim({"user":1}, None, None, None, None, Some(1431520421), None, None),
//     "VmfmoqRbRvna9lfpCx4lXf96eD_X_woBM0twLjBGLlQ"
//   )
// )

Formating

The project provides implicit reader and writer for both JwtHeader and JwtClaim

import pdi.jwt._
import pdi.jwt.JwtJson._

// Reads
Json.fromJson[JwtHeader](header)
// res10: play.api.libs.json.JsResult[JwtHeader] = JsSuccess(
//   value = JwtHeader(Some(HS256), Some(JWT), None, None),
//   path = JsPath(path = List())
// )
Json.fromJson[JwtClaim](claim)
// res11: play.api.libs.json.JsResult[JwtClaim] = JsSuccess(
//   value = JwtClaim({"user":1}, None, None, None, None, Some(1431520421), None, None),
//   path = JsPath(path = List())
// )

// Writes
Json.toJson(JwtHeader(JwtAlgorithm.HS256))
// res12: play.api.libs.json.JsValue = JsObject(
//   underlying = Map(
//     "typ" -> JsString(value = "JWT"),
//     "alg" -> JsString(value = "HS256")
//   )
// )
Json.toJson(JwtClaim("""{"user":1}""").issuedNow.expiresIn(10))
// res13: play.api.libs.json.JsValue = JsObject(
//   underlying = Map(
//     "exp" -> JsNumber(value = 1661894845),
//     "iat" -> JsNumber(value = 1661894835),
//     "user" -> JsNumber(value = 1)
//   )
// )
// Or
JwtHeader(JwtAlgorithm.HS256).toJsValue()
// res14: play.api.libs.json.JsValue = JsObject(
//   underlying = Map(
//     "typ" -> JsString(value = "JWT"),
//     "alg" -> JsString(value = "HS256")
//   )
// )
JwtClaim("""{"user":1}""").issuedNow.expiresIn(10).toJsValue()
// res15: play.api.libs.json.JsValue = JsObject(
//   underlying = Map(
//     "exp" -> JsNumber(value = 1661894845),
//     "iat" -> JsNumber(value = 1661894835),
//     "user" -> JsNumber(value = 1)
//   )
// )