JwtUpickle Object
Installation
libraryDependencies += "com.github.jwt-scala" %% "jwt-upickle" % "9.2.0"
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(1834278804), None, Some(1676494044), 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.eyJleHAiOjE4MzQyNzg4MDQsImlhdCI6MTY3NjQ5NDA0NH0.8zIShJO9Aen6uVEF0g_ti3STi8VnlNZlyHuyCTYwzxY"
JwtUpickle.decodeJson(token, key, Seq(JwtAlgorithm.HS256))
// res1: util.Try[ujson.Value] = Success(
// value = Obj(
// value = LinkedHashMap(
// "exp" -> Num(value = 1.834278804E9),
// "iat" -> Num(value = 1.676494044E9)
// )
// )
// )
JwtUpickle.decode(token, key, Seq(JwtAlgorithm.HS256))
// res2: util.Try[JwtClaim] = Success(
// value = JwtClaim({}, None, None, None, Some(1834278804), None, Some(1676494044), 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.676494044E9))
// )
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.eyJleHBpcmVzIjoxNjc2NDk0MDQ0fQ."
JwtUpickle.encode(claimJson, key, algo)
// res5: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHBpcmVzIjoxNjc2NDk0MDQ0fQ.SX81GrAnY08cUxOARki9S6KN-edITL2njasvHLGucMA"
JwtUpickle.encode(header, claimJson, key)
// res6: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHBpcmVzIjoxNjc2NDk0MDQ0fQ.SX81GrAnY08cUxOARki9S6KN-edITL2njasvHLGucMA"
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(1834278804), None, Some(1676494044), 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.eyJleHAiOjE4MzQyNzg4MDQsImlhdCI6MTY3NjQ5NDA0NH0.8zIShJO9Aen6uVEF0g_ti3STi8VnlNZlyHuyCTYwzxY"
// 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.834278804E9),
// "iat" -> Num(value = 1.676494044E9)
// )
// )
// )
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.834278804E9),
// "iat" -> Num(value = 1.676494044E9)
// )
// ),
// "8zIShJO9Aen6uVEF0g_ti3STi8VnlNZlyHuyCTYwzxY"
// )
// )
// Or to case classes
JwtUpickle.decode(token, key, Seq(JwtAlgorithm.HS256))
// res10: util.Try[JwtClaim] = Success(
// value = JwtClaim({}, None, None, None, Some(1834278804), None, Some(1676494044), 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(1834278804), None, Some(1676494044), None),
// "8zIShJO9Aen6uVEF0g_ti3STi8VnlNZlyHuyCTYwzxY"
// )
// )