Created
June 1, 2018 00:50
-
-
Save brianv0/dc9a91c54085d5d7599daf71bed6e5b5 to your computer and use it in GitHub Desktop.
JWT Test
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
package foo | |
import com.auth0.jwt.JWT | |
import com.auth0.jwt.algorithms.Algorithm | |
import com.auth0.jwt.exceptions.JWTVerificationException | |
import org.apache.commons.codec.binary.Base64 | |
import org.junit.Test | |
import java.security.interfaces.RSAPublicKey | |
import java.security.spec.RSAPublicKeySpec | |
import java.math.BigInteger | |
import java.security.KeyFactory | |
class AuthTest { | |
@Test | |
fun testAuth() { | |
val token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzY3AiOlsid3JpdGU6L3N0dWZmIiwicmVhZDpteXNxbDovL2xzc3QtcX" + | |
"NlcnY6NDA0MC8iXSwiYXVkIjoiIiwic3ViIjoiYnZhbiIsImlzcyI6Imh0dHBzOi8vZGVtby5zY2l0b2tlbnMub3JnIiwiZXhwIjoxN" + | |
"TI3ODAyODE1LCJpYXQiOjE1Mjc4MDIyMTUsIm5iZiI6MTUyNzgwMjIxNSwianRpIjoiZDVkNDA2MGUtOGQ3MS00OThjLTgxYWItZTVk" + | |
"ODQyYjVlNWM1In0.oQnL_tRjsu2t3mrNaIyKHIqie7vOyoUQP-drqbEpfUe_nA5RlbWchdo13k07IE-_z-tuaELDzkKJhVY2lYaqc7s" + | |
"UiREpa1fakHCh0v9skgvuq_hTf7R3UPcO7AN0D0du1tMx34VbUfMUGJ4nIGUsK6NJnlBzADjxpe5tLU_hJzcnMp2ObETiXqnhMFCoWU" + | |
"EDxA4lZl-GeSnQ6k9qKzNXAo2DxOCsMNWszUTKeLgWZgqvTAGerTYwTMzfXfKt6Ul5Ok_TO51Ivwao88tkGuufV2GwsHVsvbK3fDz7W" + | |
"BPLpKAo9ofYi28pCfe6cUWAir8j0WfEEL3ggoMfQ0_xWCl9eQ" | |
var authenticated = false | |
try { | |
val publicKey = getDemoPublicKey() | |
println(JWT.decode(token).issuer) | |
val verifier = JWT | |
.require(Algorithm.RSA256(publicKey)) | |
.withIssuer("https://demo.scitokens.org") | |
.acceptLeeway(50000) | |
.build() | |
val jwt = verifier.verify(token) | |
val subject = jwt.subject | |
authenticated = true | |
} catch (exception: JWTVerificationException) { | |
//Invalid signature/claims | |
exception.printStackTrace() | |
} | |
} | |
fun getDemoPublicKey(): RSAPublicKey { | |
val keyE = "AQAB" | |
val keyN = "uGDGTLXnqh3mfopjys6sFUBvFl3F4Qt6NEYphq_u_aBhtN1X9NEyb78uB_I1KjciJNGLIQU0ECsJiFx6qV1hR9xE1dPyrS3bU" + | |
"92AVtnBrvzUtTU-aUZAmZQiuAC_rC0-z_TOQr6qJkkUgZtxR9n9op55ZBpRfZD5dzhkW4Dm146vfTKt0D4cIMoMNJS5xQx9nibeB4E8h" + | |
"ryZDW_fPeD0XZDcpByNyP0jFDYkxdUtQFvyRpz4WMZ4ejUfvW3gf4LRAfGZJtMnsZ7ZW4RfoQbhiXKMfWeBEjQDiXh0r-KuZLykxhYJt" + | |
"pf7fTnPna753IzMgRMmW3F69iQn2LQN3LoSMw==" | |
val kf = KeyFactory.getInstance("RSA") | |
val modulus = BigInteger(1, Base64.decodeBase64(keyN)) | |
val exponent = BigInteger(1, Base64.decodeBase64(keyE)) | |
val key = kf.generatePublic(RSAPublicKeySpec(modulus, exponent)) | |
return key as RSAPublicKey | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment