Skip to content

Instantly share code, notes, and snippets.

@Carlos-Augusto
Last active February 5, 2021 19:53
Show Gist options
  • Save Carlos-Augusto/d8122161cee20af6219aa00b744ec60e to your computer and use it in GitHub Desktop.
Save Carlos-Augusto/d8122161cee20af6219aa00b744ec60e to your computer and use it in GitHub Desktop.
import java.time.Clock
import java.util.UUID
import org.json4s._
import org.json4s.jackson.Serialization
import pdi.jwt.{Jwt, JwtAlgorithm, JwtClaim}
implicit lazy val formats: Formats = Serialization.formats(NoTypeHints) ++
org.json4s.ext.JavaTypesSerializers.all
implicit val clock: Clock = Clock.systemUTC
case class OtherClaims(role: Symbol)
val jwtID = UUID.fromString("2881e5f8-3d4b-4a1d-be78-b67837a383fa")
val tenantId = UUID.fromString("7d6018c0-f10b-4330-99fb-cd29f52ae9fa")
val deviceId = UUID.fromString("bb505d32-b5ec-4f8d-816d-761ce82f3657")
val otherClaims = OtherClaims('verifier)
val expiresInSecs = 60 * 60 * 60
val token = Jwt.encode(
JwtClaim()
.withContent(jackson.compactJson(Extraction.decompose(otherClaims).snakizeKeys))
.by(tenantId.toString)
.to(deviceId.toString)
.withId(jwtID.toString)
.issuedNow
.expiresIn(expiresInSecs),
"secretKey",
JwtAlgorithm.HS256
)
//token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI3ZDYwMThjMC1mMTBiLTQzMzAtOTlmYi1jZDI5ZjUyYWU5ZmEiLCJhdWQiOiJiYjUwNWQzMi1iNWVjLTRmOGQtODE2ZC03NjFjZTgyZjM2NTciLCJleHAiOjE2MDI0NjM4MzcsImlhdCI6MTYwMjI0NzgzNywianRpIjoiMjg4MWU1ZjgtM2Q0Yi00YTFkLWJlNzgtYjY3ODM3YTM4M2ZhIiwicm9sZSI6InZlcmlmaWVyIn0.H5BKlNZtgvKBrq8AKABtpwAoRhDTBHPimX-QOuRNmRY
//header:
//{
// "typ": "JWT",
// "alg": "HS256"
//}
//payload:
//{
// "iss": "7d6018c0-f10b-4330-99fb-cd29f52ae9fa",
// "aud": "bb505d32-b5ec-4f8d-816d-761ce82f3657",
// "exp": 1602463837,
// "iat": 1602247837,
// "jti": "2881e5f8-3d4b-4a1d-be78-b67837a383fa",
// "role": "verifier"
//}
Jwt.decodeRawAll(token, "secretKey", Seq(JwtAlgorithm.HS256))
//scala.util.Try[(String, String, String)] = Success(({"typ":"JWT","alg":"HS256"},{"iss":"7d6018c0-f10b-4330-99fb-cd29f52ae9fa","aud":"bb505d32-b5ec-4f8d-816d-761ce82f3657","exp":1602463837,"iat":1602247837,"jti":"2881e5f8-3d4b-4a1d-be78-b67837a383fa","role":"verifier"},H5BKlNZtgvKBrq8AKABtpwAoRhDTBHPimX-QOuRNmRY))
@Carlos-Augusto
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment