Last active
September 5, 2024 15:43
-
-
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/b1ff274aae912b8a784841a886cc8e56ef6bdd58
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.4.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", "[email protected]"), // 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