JwtUpickle Object

Installation

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

Basic usage

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

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

val token = JwtUpickle.encode(claim, key, algo)
// token: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE4MTk2Nzk1OTcsImlhdCI6MTY2MTg5NDgzN30.d7RVBTcvRY7O06pAYXdqdbEraWC-8lv3c9h_Lm4MTgs"

JwtUpickle.decodeJson(token, key, Seq(JwtAlgorithm.HS256))
// res1: util.Try[ujson.Value] = Success(
//   value = Obj(
//     value = LinkedHashMap(
//       "exp" -> Num(value = 1.819679597E9),
//       "iat" -> Num(value = 1.661894837E9)
//     )
//   )
// )
JwtUpickle.decode(token, key, Seq(JwtAlgorithm.HS256))
// res2: util.Try[JwtClaim] = Success(
//   value = JwtClaim({}, None, None, None, Some(1819679597), None, Some(1661894837), None)
// )

Encoding

import java.time.Instant
import upickle.default._
import pdi.jwt.{JwtUpickle, JwtAlgorithm}

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

val claimJson = read[ujson.Value](s"""{"expires":${Instant.now.getEpochSecond}}""")
// claimJson: ujson.Value = Obj(
//   value = LinkedHashMap("expires" -> Num(value = 1.661894837E9))
// )
val header = read[ujson.Value]( """{"typ":"JWT","alg":"HS256"}""")
// header: ujson.Value = Obj(
//   value = LinkedHashMap(
//     "typ" -> Str(value = "JWT"),
//     "alg" -> Str(value = "HS256")
//   )
// )
// From just the claim to all possible attributes
JwtUpickle.encode(claimJson)
// res4: String = "eyJhbGciOiJub25lIn0.eyJleHBpcmVzIjoxNjYxODk0ODM3fQ."
JwtUpickle.encode(claimJson, key, algo)
// res5: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHBpcmVzIjoxNjYxODk0ODM3fQ.uacl8uZg3Bvp9L-pZRvEgqbIOkx8Erafcbl1hF1zWnc"
JwtUpickle.encode(header, claimJson, key)
// res6: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHBpcmVzIjoxNjYxODk0ODM3fQ.uacl8uZg3Bvp9L-pZRvEgqbIOkx8Erafcbl1hF1zWnc"

Decoding

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

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

val token = JwtUpickle.encode(claim, key, algo)
// token: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE4MTk2Nzk1OTcsImlhdCI6MTY2MTg5NDgzN30.d7RVBTcvRY7O06pAYXdqdbEraWC-8lv3c9h_Lm4MTgs"

// You can decode to JsObject
JwtUpickle.decodeJson(token, key, Seq(JwtAlgorithm.HS256))
// res8: util.Try[ujson.Value] = Success(
//   value = Obj(
//     value = LinkedHashMap(
//       "exp" -> Num(value = 1.819679597E9),
//       "iat" -> Num(value = 1.661894837E9)
//     )
//   )
// )
JwtUpickle.decodeJsonAll(token, key, Seq(JwtAlgorithm.HS256))
// res9: util.Try[(ujson.Value, ujson.Value, String)] = Success(
//   value = (
//     Obj(
//       value = LinkedHashMap(
//         "typ" -> Str(value = "JWT"),
//         "alg" -> Str(value = "HS256")
//       )
//     ),
//     Obj(
//       value = LinkedHashMap(
//         "exp" -> Num(value = 1.819679597E9),
//         "iat" -> Num(value = 1.661894837E9)
//       )
//     ),
//     "d7RVBTcvRY7O06pAYXdqdbEraWC-8lv3c9h_Lm4MTgs"
//   )
// )
// Or to case classes
JwtUpickle.decode(token, key, Seq(JwtAlgorithm.HS256))
// res10: util.Try[JwtClaim] = Success(
//   value = JwtClaim({}, None, None, None, Some(1819679597), None, Some(1661894837), None)
// )
JwtUpickle.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(1819679597), None, Some(1661894837), None),
//     "d7RVBTcvRY7O06pAYXdqdbEraWC-8lv3c9h_Lm4MTgs"
//   )
// )