Created
October 13, 2020 14:37
-
-
Save Carlos-Augusto/86a2162840809aa2b4e4890ec2ba7100 to your computer and use it in GitHub Desktop.
This file contains 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
import java.security.PublicKey | |
import java.time.Clock | |
import java.util.UUID | |
import com.ubirch.crypto.GeneratorKeyFactory | |
import com.ubirch.crypto.utils.Curve | |
import org.bouncycastle.util.encoders.Hex | |
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, env: Symbol) | |
val jwtID = UUID.fromString("2881e5f8-3d4b-4a1d-be78-b67837a383fa") // Should be unique per JWT | |
val by = "ubirch" | |
val to = Set( | |
UUID.fromString("7d6018c0-f10b-4330-99fb-cd29f52ae9fa"), | |
UUID.fromString("bb505d32-b5ec-4f8d-816d-761ce82f3657") | |
) // The verifier | |
val otherClaims = OtherClaims('verifier, 'prod) | |
val expiresInSecs = 631139040 | |
//We generate a keypair | |
val privKey = GeneratorKeyFactory.getPrivKey("00d1772d7e8f157964dabe54bbdf36122dca421499d161dd302041159cd3cf8586",Curve.PRIME256V1) | |
val privateKey = privKey.getPrivateKey | |
val publicKey: PublicKey = privKey.getPublicKey | |
val oc = jackson.compactJson(Extraction.decompose(otherClaims).snakizeKeys) | |
//We create the token | |
val token = Jwt.encode( | |
JwtClaim(oc) | |
.by(by) | |
.to(to.map(_.toString)) | |
.about("King Dude - Concert") | |
.withId(jwtID.toString) | |
.issuedNow | |
.expiresIn(expiresInSecs), | |
privateKey, | |
JwtAlgorithm.ES256 | |
) | |
//eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJpc3MiOiJ1YmlyY2giLCJzdWIiOiJLaW5nIER1ZGUgLSBDb25jZXJ0IiwiYXVkIjpbIjdkNjAxOGMwLWYxMGItNDMzMC05OWZiLWNkMjlmNTJhZTlmYSIsImJiNTA1ZDMyLWI1ZWMtNGY4ZC04MTZkLTc2MWNlODJmMzY1NyJdLCJleHAiOjIyMzM3Mzg3ODUsImlhdCI6MTYwMjU5OTc0NSwianRpIjoiMjg4MWU1ZjgtM2Q0Yi00YTFkLWJlNzgtYjY3ODM3YTM4M2ZhIiwicm9sZSI6InZlcmlmaWVyIiwiZW52IjoicHJvZCJ9.FdlORYWyctbuf6PertT-mr2bgl_hhQcyac12yleZ-N_X7qgBCkWq4ozyTuSKV6GwAhtfT8tBGXbC0Eu8F6dAUQ | |
//{ | |
// "iss": "ubirch", | |
// "sub": "King Dude - Concert", | |
// "aud": [ | |
// "7d6018c0-f10b-4330-99fb-cd29f52ae9fa", | |
// "bb505d32-b5ec-4f8d-816d-761ce82f3657" | |
// ], | |
// "exp": 2233738785, | |
// "iat": 1602599745, | |
// "jti": "2881e5f8-3d4b-4a1d-be78-b67837a383fa", | |
// "role": "verifier", | |
// "env": "prod" | |
//} | |
//We decode the token | |
Jwt.decodeRawAll(token, publicKey, Seq(JwtAlgorithm.ES256)) | |
//res0: scala.util.Try[(String, String, String)] = Success(({"typ":"JWT","alg":"ES256"},{"iss":"7d6018c0-f10b-4330-99fb-cd29f52ae9fa","aud":"bb505d32-b5ec-4f8d-816d-761ce82f3657","exp":1602708919,"iat":1602492919,"jti":"2881e5f8-3d4b-4a1d-be78-b67837a383fa","role":"verifier"},MGRDTFwaI5lVD442bgwPyFKszX06pbXSUcY_cftARPJW7dgbDfB0lTPqrQKeYVeP-Cqgkh24djsh7DfOCYmXhg)) |
Author
Carlos-Augusto
commented
Oct 13, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment