/** Will try to decode a JSON Web Token to raw strings using a HMAC algorithm
*
* @return if successful, a tuple of 3 strings, the header, the claim and the signature
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def decodeRawAll(token: String, key: String, algorithms: Seq[JwtHmacAlgorithm], options: JwtOptions): Try[(String, String, String)] = Try {
val (header64, header, claim64, claim, signature) = splitToken(token)
validate(header64, parseHeader(header), claim64, parseClaim(claim), signature, key, algorithms, options)
(header, claim, signature)
}
def decodeRawAll(token: String, key: String, algorithms: Seq[JwtHmacAlgorithm]): Try[(String, String, String)] =
decodeRawAll(token, key, algorithms, JwtOptions.DEFAULT)
/** An alias for `isValid` if you want to directly pass a string as the key for HMAC algorithms
*
* @return a boolean value indicating if the token is valid or not
* @param token $token
* @param key $key
* @param algorithms $algos
*/
def isValid(token: String, key: String, algorithms: Seq[JwtHmacAlgorithm], options: JwtOptions): Boolean =
try {
validate(token, key, algorithms, options)
true
} catch {
case _ : Throwable => false
}
def isValid(token: String, key: String, algorithms: Seq[JwtHmacAlgorithm]): Boolean = isValid(token, key, algorithms, JwtOptions.DEFAULT)
import pdi.jwt._
import org.json4s._
import org.json4s.jackson.JsonMethods._
object JwtDemo extends App{
import scala.util._
var clms = JwtClaim() ++ ("shopid" -> "1101") ++ ("userid" -> "102") ++ ("vchnum" -> 23)
val token = Jwt.encode(clms,"OpenSesame", JwtAlgorithm.HS256)
println(token)
println(Jwt.isValid(token,"OpenSesame",Seq(JwtAlgorithm.HS256)))
val claims = Jwt.decodeRawAll(token,"OpenSesame",Seq(JwtAlgorithm.HS256))
println(claims)
claims match {
case Success(json) => println(((parse(json._2).asInstanceOf[JObject]) \ "shopid").values)
case Failure(err) => println(s"Error: ${err.getMessage}")
}
}
import akka.actor._
import akka.stream._
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.directives.Credentials
import pdi.jwt._
import org.json4s._
import org.json4s.jackson.JsonMethods._
import scala.util._
//import akka.http.scaladsl.marshallers.sprayjson._
//import spray.json._
object JsonMarshaller { // extends SprayJsonS