JwtJson4s Object

Installation

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

Basic usage

import pdi.jwt.{JwtJson4s, JwtAlgorithm}, org.json4s._, org.json4s.JsonDSL.WithBigDecimal._

val claim = JObject(("user", 1), ("nbf", 1431520421))
// claim: JObject = JObject(
//   obj = List(("user", JInt(num = 1)), ("nbf", JInt(num = 1431520421)))
// )
val key = "secretKey"
// key: String = "secretKey"
val algo = JwtAlgorithm.HS256
// algo: JwtAlgorithm.HS256.type = HS256

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

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

JwtJson4s.decodeJson(token, key, Seq(JwtAlgorithm.HS256))
// res1: util.Try[JObject] = Success(
//   value = JObject(
//     obj = List(("user", JInt(num = 1)), ("nbf", JInt(num = 1431520421)))
//   )
// )

JwtJson4s.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 = JObject(("typ", "JWT"), ("alg", "HS256"))
// header: JObject = JObject(
//   obj = List(("typ", JString(s = "JWT")), ("alg", JString(s = "HS256")))
// )

JwtJson4s.encode(claim)
// res3: String = "eyJhbGciOiJub25lIn0.eyJ1c2VyIjoxLCJuYmYiOjE0MzE1MjA0MjF9."
JwtJson4s.encode(claim, key, algo)
// res4: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoxLCJuYmYiOjE0MzE1MjA0MjF9.VmfmoqRbRvna9lfpCx4lXf96eD_X_woBM0twLjBGLlQ"
JwtJson4s.encode(header, claim, key)
// res5: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjoxLCJuYmYiOjE0MzE1MjA0MjF9.VmfmoqRbRvna9lfpCx4lXf96eD_X_woBM0twLjBGLlQ"

Decoding

// You can decode to JsObject
JwtJson4s.decodeJson(token, key, Seq(JwtAlgorithm.HS256))
// res6: util.Try[JObject] = Success(
//   value = JObject(
//     obj = List(("user", JInt(num = 1)), ("nbf", JInt(num = 1431520421)))
//   )
// )
JwtJson4s.decodeJsonAll(token, key, Seq(JwtAlgorithm.HS256))
// res7: util.Try[(JObject, JObject, String)] = Success(
//   value = (
//     JObject(
//       obj = List(("typ", JString(s = "JWT")), ("alg", JString(s = "HS256")))
//     ),
//     JObject(
//       obj = List(("user", JInt(num = 1)), ("nbf", JInt(num = 1431520421)))
//     ),
//     "VmfmoqRbRvna9lfpCx4lXf96eD_X_woBM0twLjBGLlQ"
//   )
// )
// Or to case classes
JwtJson4s.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)
// )
JwtJson4s.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"
//   )
// )