diff options
author | zachdriver <zach@driver.xyz> | 2017-10-17 14:51:21 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-17 14:51:21 -0700 |
commit | 2af63e7fb8b15568adaf2d9f0a6b395d6719fd71 (patch) | |
tree | bb66e54c3f7ff26af279df255452566df057223b /src/main/scala/xyz/driver/core/rest/auth/CachedTokenAuthorization.scala | |
parent | b00892d723f6dedf50dc1c1fde7d443e9c3f9497 (diff) | |
parent | 1aaaf7a5ecf2cd28350fff872e334f9f6186966a (diff) | |
download | driver-core-2af63e7fb8b15568adaf2d9f0a6b395d6719fd71.tar.gz driver-core-2af63e7fb8b15568adaf2d9f0a6b395d6719fd71.tar.bz2 driver-core-2af63e7fb8b15568adaf2d9f0a6b395d6719fd71.zip |
Merge pull request #74 from drivergroup/zsmith/rest-app-packages
Split app and rest packages into separate files
Diffstat (limited to 'src/main/scala/xyz/driver/core/rest/auth/CachedTokenAuthorization.scala')
-rw-r--r-- | src/main/scala/xyz/driver/core/rest/auth/CachedTokenAuthorization.scala | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/core/rest/auth/CachedTokenAuthorization.scala b/src/main/scala/xyz/driver/core/rest/auth/CachedTokenAuthorization.scala new file mode 100644 index 0000000..4f4c811 --- /dev/null +++ b/src/main/scala/xyz/driver/core/rest/auth/CachedTokenAuthorization.scala @@ -0,0 +1,55 @@ +package xyz.driver.core.rest.auth + +import java.nio.file.{Files, Path} +import java.security.{KeyFactory, PublicKey} +import java.security.spec.X509EncodedKeySpec + +import pdi.jwt.{Jwt, JwtAlgorithm} +import xyz.driver.core.auth.{Permission, User} +import xyz.driver.core.rest.ServiceRequestContext + +import scala.concurrent.Future +import scalaz.syntax.std.boolean._ + +class CachedTokenAuthorization[U <: User](publicKey: => PublicKey, issuer: String) extends Authorization[U] { + override def userHasPermissions(user: U, permissions: Seq[Permission])( + implicit ctx: ServiceRequestContext): Future[AuthorizationResult] = { + import spray.json._ + + def extractPermissionsFromTokenJSON(tokenObject: JsObject): Option[Map[String, Boolean]] = + tokenObject.fields.get("permissions").collect { + case JsObject(fields) => + fields.collect { + case (key, JsBoolean(value)) => key -> value + } + } + + val result = for { + token <- ctx.permissionsToken + jwt <- Jwt.decode(token.value, publicKey, Seq(JwtAlgorithm.RS256)).toOption + jwtJson = jwt.parseJson.asJsObject + + // Ensure jwt is for the currently authenticated user and the correct issuer, otherwise return None + _ <- jwtJson.fields.get("sub").contains(JsString(user.id.value)).option(()) + _ <- jwtJson.fields.get("iss").contains(JsString(issuer)).option(()) + + permissionsMap <- extractPermissionsFromTokenJSON(jwtJson) + + authorized = permissions.map(p => p -> permissionsMap.getOrElse(p.toString, false)).toMap + } yield AuthorizationResult(authorized, Some(token)) + + Future.successful(result.getOrElse(AuthorizationResult.unauthorized)) + } +} + +object CachedTokenAuthorization { + def apply[U <: User](publicKeyFile: Path, issuer: String): CachedTokenAuthorization[U] = { + lazy val publicKey: PublicKey = { + val publicKeyBase64Encoded = new String(Files.readAllBytes(publicKeyFile)).trim + val publicKeyBase64Decoded = java.util.Base64.getDecoder.decode(publicKeyBase64Encoded) + val spec = new X509EncodedKeySpec(publicKeyBase64Decoded) + KeyFactory.getInstance("RSA").generatePublic(spec) + } + new CachedTokenAuthorization[U](publicKey, issuer) + } +} |