From faf70d94866326b1ee68a036ba1f8d4fe75e32c5 Mon Sep 17 00:00:00 2001 From: Nathaniel Fischer Date: Wed, 25 Mar 2020 13:51:59 -0700 Subject: [PATCH] key resolver for a single key shouldn't need an execution context (#30) --- .mill-version | 2 +- .scalafmt.conf | 2 +- .../src/black/door/jose/jws/KeyResolver.scala | 24 ++++++++++++++----- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/.mill-version b/.mill-version index b49b253..ee6cdce 100644 --- a/.mill-version +++ b/.mill-version @@ -1 +1 @@ -0.5.6 +0.6.1 diff --git a/.scalafmt.conf b/.scalafmt.conf index ce3eaa0..202e39c 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -1,4 +1,4 @@ -version = 1.6.0-RC3 +version = 2.4.2 maxColumn = 95 diff --git a/jose/src/black/door/jose/jws/KeyResolver.scala b/jose/src/black/door/jose/jws/KeyResolver.scala index 7adafde..f8d4d0a 100644 --- a/jose/src/black/door/jose/jws/KeyResolver.scala +++ b/jose/src/black/door/jose/jws/KeyResolver.scala @@ -1,17 +1,29 @@ package black.door.jose.jws -import cats.data.EitherT -import cats.implicits._ import black.door.jose.jwk.Jwk -import scala.language.implicitConversions +import cats.data.EitherT -import scala.concurrent.{ExecutionContext, Future} +import scala.concurrent.Future +import scala.language.implicitConversions trait KeyResolver[A] { def resolve(header: JwsHeader, payload: A): EitherT[Future, String, Jwk] } object KeyResolver { - implicit def fromSingleKey[A](key: Jwk)(implicit ex: ExecutionContext): KeyResolver[A] = - (header: JwsHeader, payload: A) => EitherT.rightT(key) + + implicit def fromSingleKey[A](key: Jwk): KeyResolver[A] = + (_, _) => EitherT[Future, String, Jwk](Future.successful(Right(key))) + + implicit def fromKeys[A](keys: Seq[Jwk]): KeyResolver[A] = + (header, _) => + EitherT( + Future.successful( + keys + .collectFirst { + case key if key.kid == header.kid => key + } + .toRight(s"no key found in set for kid ${header.kid}") + ) + ) }