Skip to content

Commit

Permalink
Use parasitic EC for sync JWT validation (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrktkt authored May 20, 2021
1 parent 5d1f66a commit ae806b6
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions jose/src/black/door/jose/jwt/Jwt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import cats.data.{EitherT, OptionT}
import cats.implicits._

import java.security.KeyException
import java.util.concurrent.{Executors, TimeUnit}
import java.util.concurrent.TimeUnit
import scala.collection.immutable.Seq
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext, Future}
Expand Down Expand Up @@ -67,15 +67,6 @@ object Jwt {
OptionT(jwtValidator.orElse(fallbackJwtValidator).apply(jwt)).toLeft(jwt)
}.value

private val threadFactory = Executors.defaultThreadFactory()

private val sadSpasticLittleEc =
ExecutionContext.fromExecutor(Executors.newCachedThreadPool { r =>
val t = threadFactory.newThread(r)
t.setDaemon(true)
t
})

def validate(compact: String) = new UnitValidation(compact)

protected class UnitValidation(protected val compact: String) extends TypedValidation[Unit] {
Expand All @@ -86,6 +77,11 @@ object Jwt {
}
}

private val sameThreadExecutionContext = new ExecutionContext {
def execute(runnable: Runnable) = runnable.run()
def reportFailure(cause: Throwable) = cause.printStackTrace()
}

sealed trait TypedValidation[C] {
protected def compact: String

Expand Down Expand Up @@ -113,9 +109,9 @@ object Jwt {
validate(compact, keyResolver, jwtValidator, fallbackJwtValidator, algorithms)(
payloadDeserializer,
headerDeserializer,
sadSpasticLittleEc
sameThreadExecutionContext
),
Duration(5, TimeUnit.SECONDS)
Duration(30, TimeUnit.SECONDS)
)

def async(implicit ec: ExecutionContext) =
Expand Down

0 comments on commit ae806b6

Please sign in to comment.