Commit 24d9475b authored by Günter Hipler's avatar Günter Hipler
Browse files

first implementation resumption token

parent 7c6b8973
package org.swissbib.memobase.oai.common.util
import java.time.{Clock, ZoneId}
import pdi.jwt.{Jwt, JwtAlgorithm, JwtClaim, JwtHeader}
import play.api.libs.json.Json
import play.api.libs.json._
import play.api.libs.json.Reads._
class ResumptionToken {
import scala.util.{Failure, Success, Try}
case class ResumptionToken (issuer: String, issuedAt: Long, expiration: Long, subject: String, claim: JwtClaim)
object ESResumptionTokenHelper {
implicit val clock: Clock = Clock.system(ZoneId.of("Europe/Zurich"))
def apply(esCursor: String, expiresInSeconds: Long = 180): ResumptionToken = {
val jwtClaim = JwtClaim(
subject = Option(esCursor),
issuer = Option("memobase OAI server")
).issuedNow.expiresIn(expiresInSeconds)
ResumptionToken( jwtClaim.issuer.get,jwtClaim.issuedAt.get,jwtClaim.expiration.get,jwtClaim.subject.get,jwtClaim)
}
def createOaiToken(rt: ResumptionToken, key: String="oaimemobase"): String = {
Jwt.encode(JwtHeader(JwtAlgorithm.HS384), rt.claim, key)
}
def createResumptionToken(oaiToken: String, key:String="oaimemobase"): Try[ResumptionToken] = {
val decoded = Jwt.decodeRaw(oaiToken, key, Seq(JwtAlgorithm.HS384))
decoded match {
case Success(value) =>
val jsnonNode: JsValue = Json.parse(value)
//val issuer = (jsnonNode \ "iss").as[String]
//val subject = (jsnonNode \ "sub").as[String]
val expiration = (jsnonNode \ "exp").as[Long]
val issuedAt = (jsnonNode \ "iat").as[Long]
Success(ResumptionToken("lll",expiration,issuedAt,"mySubject",JwtClaim("lll").issuedNow.expiresIn(10)))
case Failure(value) => Failure(value)
}
}
}
object Test extends App {
val rt = ESResumptionTokenHelper("1234",5)
val token = ESResumptionTokenHelper.createOaiToken(rt)
//Thread.sleep(6000)
ESResumptionTokenHelper.createResumptionToken(token) match {
case Success(value) => println(value)
case Failure(value) => println(value)
}
}
package org.swissbib.memobase.oai.runner
import scala.xml.Node
case class ResultList(repositoryToken:Option[String],result:Option[Seq[(String,Node)]])
case class Result()
import java.time.{Clock, ZoneId}
import com.fasterxml.jackson.databind.JsonNode
import pdi.jwt.{Jwt, JwtAlgorithm, JwtClaim, JwtHeader, JwtOptions}
import org.joda.time.{DateTime, Duration, Instant, LocalDateTime}
import play.api.libs.json.Json
import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._
import scala.util.{Failure, Success}
val token = Jwt.encode("""{"user":1}""", "secretKey", JwtAlgorithm.HS256)
implicit val clock: Clock = Clock.system(ZoneId.of("Europe/Zurich"))
val jwtClaim = JwtClaim(
subject = Option("DnF1ZXJ5VGhlbkZldGNoAwAAAAAAAShoFlVKdDRxRWdGUk5XVjRtblYzMXd5V0EAAAAAAAYJKRZuWVYyeWFBWlNDQzhsQ3RBd0dDZ1BBAAAAAAAGCSgWbllWMnlhQVpTQ0M4bEN0QXdHQ2dQQQ=="),
issuer = Option("memobase OAI server")
).issuedNow.expiresIn(10)
val key = "swortfish"
//val tokenWithClaim = Jwt.encode(jwtClaim,key,JwtAlgorithm.HS384)
val tokenWithClaim = Jwt.encode(JwtHeader(JwtAlgorithm.HS384), jwtClaim, key)
//Thread.sleep(3000)
//Jwt.decode(tokenWithClaim,key)
//val decoded = Jwt.decodeAll(tokenWithClaim, "swortfish", Seq(JwtAlgorithm.HS384))
val decoded = Jwt.decodeRaw(tokenWithClaim, "swortfish", Seq(JwtAlgorithm.HS384))
decoded match {
case Success(value) =>
//val j = JwtClaim(value)
//println(j.subject)
//val (header,claim,string) = decoded.get
//println("is valid: " + Jwt.isValid(tokenWithClaim))
val jsnonNode: JsValue = Json.parse(value)
val issuer = (jsnonNode \ "iss").as[String]
val subject = (jsnonNode \ "sub").as[String]
println(issuer)
println(subject)
//println(decoded)
println(jsnonNode)
case Failure(exception) => println(exception)
}
......@@ -35,7 +35,8 @@ lazy val root = (project in file("."))
json2xml,
scala_xml_module,
upickl,
httprequests
httprequests,
jwtcore
),
scalacOptions ++= Seq(
"-feature",
......
Nutzung von Kibana
docker run --network host -e elasticsearch.hosts:http://localhost:8080 -p 5601:5601 docker.elastic.co/kibana/kibana:7.6.1
\ No newline at end of file
......@@ -29,3 +29,26 @@ http://localhost:9000/?verb=ListSets
http://localhost:9000/?verb=ListSets&resumptionToken=1234
http://localhost:9000/?verb=Identify
Aufrufe ES scroll api
POST documents-v8/_search?scroll=1m
{
"size": 100,
"query": {
"match_all": {}
}
}
POST /_search/scroll
{
"scroll" : "1m",
"scroll_id" : "DnF1ZXJ5VGhlbkZldGNoAwAAAAAAAB5dFlp2eUpoaWZzUURTMGRteTl6bHN0ckEAAAAAAAYITRZuWVYyeWFBWlNDQzhsQ3RBd0dDZ1BBAAAAAAAAHl4WWnZ5SmhpZnNRRFMwZG15OXpsc3RyQQ=="
}
_scroll_id in response
{
"_scroll_id" : "DnF1ZXJ5VGhlbkZldGNoAwAAAAAAAB5dFlp2eUpoaWZzUURTMGRteTl6bHN0ckEAAAAAAAYITRZuWVYyeWFBWlNDQzhsQ3RBd0dDZ1BBAAAAAAAAHl4WWnZ5SmhpZnNRRFMwZG15OXpsc3RyQQ==",
"took" : 9,
......@@ -40,5 +40,7 @@ object Dependencies {
lazy val scala_xml_module = "org.scala-lang.modules" %% "scala-xml" % "1.3.0"
lazy val httprequests = "com.lihaoyi" %% "requests" % "0.6.5"
lazy val jwtcore = "com.pauldijou" %% "jwt-core" % "4.3.0"
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment