JwtArgonaut object

Installation

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

Basic usage

import java.time.Instant
import scala.util.Try

import pdi.jwt.{JwtAlgorithm, JwtArgonaut, JwtClaim}

import argonaut.Json

val claim = JwtClaim(
  expiration = Some(Instant.now().plusSeconds(157784760).getEpochSecond),
  issuedAt = Some(Instant.now.getEpochSecond)
)
// claim: JwtClaim = JwtClaim({}, None, None, None, Some(1819679586), None, Some(1661894826), None)

val key = "secretKey"
// key: String = "secretKey"
val alg = JwtAlgorithm.HS512
// alg: JwtAlgorithm.HS512.type = HS512

val token = JwtArgonaut.encode(claim, key, alg)
// token: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJleHAiOjE4MTk2Nzk1ODYsImlhdCI6MTY2MTg5NDgyNn0.04TkZuQb8uXIG_4aW4BvZwfP9D-YdRAJgGV3sMtFy4y0Kj9lMyeEcYUNFs0SHM3lsObMjjee8kQWVVvGcuazyA"
val decodedJson: Try[Json] = JwtArgonaut.decodeJson(token, key, Seq(alg))
// decodedJson: Try[Json] = Success(
//   value = JObject(
//     o = JsonObjectInstance(
//       fieldsMap = Map(
//         "exp" -> JNumber(n = JsonLong(value = 1819679586L)),
//         "iat" -> JNumber(n = JsonLong(value = 1661894826L))
//       ),
//       orderedFields = Vector("exp", "iat")
//     )
//   )
// )
val decodedClaim: Try[JwtClaim] = JwtArgonaut.decode(token, key, Seq(alg))
// decodedClaim: Try[JwtClaim] = Success(
//   value = JwtClaim({}, None, None, None, Some(1819679586), None, Some(1661894826), None)
// )

Encoding

import java.time.Instant
import argonaut.Parse
import pdi.jwt.{JwtAlgorithm, JwtArgonaut}

val key = "secretKey"
// key: String = "secretKey"
val alg = JwtAlgorithm.HS512
// alg: JwtAlgorithm.HS512.type = HS512

val jsonClaim = Parse.parseOption(s"""{"expires":${Instant.now().getEpochSecond}}""").get
// jsonClaim: argonaut.Json = JObject(
//   o = JsonObjectInstance(
//     fieldsMap = Map("expires" -> JNumber(n = JsonLong(value = 1661894826L))),
//     orderedFields = Vector("expires")
//   )
// )
val jsonHeader = Parse.parseOption("""{"typ":"JWT","alg":"HS512"}""").get
// jsonHeader: argonaut.Json = JObject(
//   o = JsonObjectInstance(
//     fieldsMap = Map("typ" -> JString(s = "JWT"), "alg" -> JString(s = "HS512")),
//     orderedFields = Vector("typ", "alg")
//   )
// )

val token1: String = JwtArgonaut.encode(jsonClaim)
// token1: String = "eyJhbGciOiJub25lIn0.eyJleHBpcmVzIjoxNjYxODk0ODI2fQ."
val token2: String = JwtArgonaut.encode(jsonClaim, key, alg)
// token2: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJleHBpcmVzIjoxNjYxODk0ODI2fQ.UoBsikNVNto_oD5RjacSmtPcjBEsN18S1gl1Jd6g91ZXU1zFlGxqY8wQ8NPjhJwfDMr28FPG_UEjJuCyC_rrYg"
val token3: String = JwtArgonaut.encode(jsonHeader, jsonClaim, key)
// token3: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJleHBpcmVzIjoxNjYxODk0ODI2fQ.UoBsikNVNto_oD5RjacSmtPcjBEsN18S1gl1Jd6g91ZXU1zFlGxqY8wQ8NPjhJwfDMr28FPG_UEjJuCyC_rrYg"

Decoding

import scala.util.Try
import java.time.Instant
import argonaut.Json
import pdi.jwt.{JwtAlgorithm, JwtArgonaut, JwtClaim, JwtHeader}

val claim = JwtClaim(
  expiration = Some(Instant.now.plusSeconds(157784760).getEpochSecond),
  issuedAt = Some(Instant.now.getEpochSecond)
)
// claim: JwtClaim = JwtClaim({}, None, None, None, Some(1819679586), None, Some(1661894826), None)
val key = "secretKey"
// key: String = "secretKey"
val alg = JwtAlgorithm.HS512
// alg: JwtAlgorithm.HS512.type = HS512

val token = JwtArgonaut.encode(claim, key, alg)
// token: String = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJleHAiOjE4MTk2Nzk1ODYsImlhdCI6MTY2MTg5NDgyNn0.04TkZuQb8uXIG_4aW4BvZwfP9D-YdRAJgGV3sMtFy4y0Kj9lMyeEcYUNFs0SHM3lsObMjjee8kQWVVvGcuazyA"

val decodedJsonClaim: Try[Json] = JwtArgonaut.decodeJson(token, key, Seq(alg))
// decodedJsonClaim: Try[Json] = Success(
//   value = JObject(
//     o = JsonObjectInstance(
//       fieldsMap = Map(
//         "exp" -> JNumber(n = JsonLong(value = 1819679586L)),
//         "iat" -> JNumber(n = JsonLong(value = 1661894826L))
//       ),
//       orderedFields = Vector("exp", "iat")
//     )
//   )
// )
val decodedJson: Try[(Json, Json, String)] = JwtArgonaut.decodeJsonAll(token, key, Seq(alg))
// decodedJson: Try[(Json, Json, String)] = Success(
//   value = (
//     JObject(
//       o = JsonObjectInstance(
//         fieldsMap = Map(
//           "typ" -> JString(s = "JWT"),
//           "alg" -> JString(s = "HS512")
//         ),
//         orderedFields = Vector("typ", "alg")
//       )
//     ),
//     JObject(
//       o = JsonObjectInstance(
//         fieldsMap = Map(
//           "exp" -> JNumber(n = JsonLong(value = 1819679586L)),
//           "iat" -> JNumber(n = JsonLong(value = 1661894826L))
//         ),
//         orderedFields = Vector("exp", "iat")
//       )
//     ),
//     "04TkZuQb8uXIG_4aW4BvZwfP9D-YdRAJgGV3sMtFy4y0Kj9lMyeEcYUNFs0SHM3lsObMjjee8kQWVVvGcuazyA"
//   )
// )

val decodedClaim: Try[JwtClaim]  = JwtArgonaut.decode(token, key, Seq(alg))
// decodedClaim: Try[JwtClaim] = Success(
//   value = JwtClaim({}, None, None, None, Some(1819679586), None, Some(1661894826), None)
// )
val decodedToken: Try[(JwtHeader, JwtClaim, String)] = JwtArgonaut.decodeAll(token, key, Seq(alg))
// decodedToken: Try[(JwtHeader, JwtClaim, String)] = Success(
//   value = (
//     JwtHeader(Some(HS512), Some(JWT), None, None),
//     JwtClaim({}, None, None, None, Some(1819679586), None, Some(1661894826), None),
//     "04TkZuQb8uXIG_4aW4BvZwfP9D-YdRAJgGV3sMtFy4y0Kj9lMyeEcYUNFs0SHM3lsObMjjee8kQWVVvGcuazyA"
//   )
// )