Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 6, 2023 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/7b1ef40ff64a9e53e497ade332f61271 to your computer and use it in GitHub Desktop.
Save dacr/7b1ef40ff64a9e53e497ade332f61271 to your computer and use it in GitHub Desktop.
decode/encode/validate JWT token with secret key / published by https://github.com/dacr/code-examples-manager #16c7b9b9-5869-42ee-82f9-a8a53bba42db/ff8ac8c1d9046d247410d9109a469547378b842a
// summary : decode/encode/validate JWT token with secret key
// keywords : scala, token, api, jwt, authentication, secretkey, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : 16c7b9b9-5869-42ee-82f9-a8a53bba42db
// created-on : 2022-01-24T18:29:59+01:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.2.2"
//> using dep "org.json4s::json4s-jackson:4.0.6"
//> using dep "org.json4s::json4s-ext:4.0.6"
//> using dep "com.github.jwt-scala::jwt-json4s-jackson:9.2.0"
//> using dep "com.github.jwt-scala::jwt-core:9.2.0"
// ---------------------
import org.json4s.*
import org.json4s.jackson.Serialization
import org.json4s.JsonDSL.WithBigDecimal.*
import pdi.jwt.{JwtJson4s, JwtAlgorithm}
import java.time.Instant
import java.security._
import java.security.spec._
import java.util.UUID
import scala.util.{Try, Success, Failure}
val nowEpochSeconds: Long = Instant.now().getEpochSecond
// https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
val claim = JObject(
("jti", UUID.randomUUID().toString), // JTW ID
("iss", "this-app"), // Issuer
("iat", nowEpochSeconds), // Issued at
("exp", nowEpochSeconds + 60), // Expiration time
("nbf", nowEpochSeconds + 2), // Not before
("sub", "userlogin@example.com"), // The subject
("user", 1)
)
val secretKey = "secretKey"
val algo = JwtAlgorithm.HS256
val token = JwtJson4s.encode(claim, secretKey, algo)
val decodedJson = JwtJson4s.decodeJson(token, secretKey, Seq(algo))
val decodedClaim = JwtJson4s.decode(token, secretKey, Seq(algo))
// -----------------------------------------------------------------------------
val validatingAlgorithms = Seq(JwtAlgorithm.HS224, JwtAlgorithm.HS256, JwtAlgorithm.HS512)
// -----------------------------------------------------------------------------
println("Validating before nbf")
assert(Try(JwtJson4s.validate(token, secretKey, validatingAlgorithms)).isFailure)
assert(!JwtJson4s.isValid(token, secretKey, validatingAlgorithms))
println("TOKEN INVALID")
// -----------------------------------------------------------------------------
Thread.sleep(2100)
println("Validating after nbf")
assert(Try(JwtJson4s.validate(token, secretKey, validatingAlgorithms)).isSuccess)
assert(JwtJson4s.isValid(token, secretKey, validatingAlgorithms))
println("TOKEN VALID !")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment