Skip to content

Commit

Permalink
key resolver for a single key shouldn't need an execution context (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrktkt authored Mar 25, 2020
1 parent 1bac884 commit faf70d9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .mill-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.6
0.6.1
2 changes: 1 addition & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = 1.6.0-RC3
version = 2.4.2

maxColumn = 95

Expand Down
24 changes: 18 additions & 6 deletions jose/src/black/door/jose/jws/KeyResolver.scala
Original file line number Diff line number Diff line change
@@ -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}")
)
)
}

0 comments on commit faf70d9

Please sign in to comment.