Skip to content

Instantly share code, notes, and snippets.

@jmkoni
Created May 2, 2018 14:53
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 jmkoni/63114562c67a371db1297877f47fe6fe to your computer and use it in GitHub Desktop.
Save jmkoni/63114562c67a371db1297877f47fe6fe to your computer and use it in GitHub Desktop.
import org.json4s.DefaultFormats
import org.json4s.jackson.JsonMethods.parse
import pdi.jwt.{Jwt, JwtAlgorithm}
import scala.util.Try
/*
* This object decodes JWTs that are created by another application.
* There are a few different options available for parsing JWTs and I went with:
* http://pauldijou.fr/jwt-scala/samples/jwt-core/
*/
object JWT {
implicit val formats = DefaultFormats
def parseUserJwt(token: String): Try[UserTokenData] = {
for {
decoded <- Jwt.decode(token,
Configuration.SecretKey,
Seq(JwtAlgorithm.HS256)) // this will return a string
userTokenData = parse(decoded).extract[Token].data // this parses the string to JSON and extracts to a token
} yield userTokenData
}
}
case class Token(data: UserTokenData, exp: Int, iat: Int, iss: String)
case class UserTokenData(email: String)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment