io.fintrospect.parameters.Security.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fintrospect_2.10 Show documentation
Show all versions of fintrospect_2.10 Show documentation
Library that adds self-documentation to Finagle server endpoint services
package io.fintrospect.parameters
import com.twitter.finagle.Filter
import com.twitter.finagle.http.Status.Unauthorized
import com.twitter.finagle.http.{Request, Response}
import com.twitter.util.Future
import io.fintrospect.parameters.ApiKey.ValidateKey
import scala.util.{Failure, Success, Try}
/**
* Endpoint security. Provides filter to be applied to endpoints for all requests.
*/
sealed trait Security {
val filter: Filter[Request, Response, Request, Response]
}
/**
* Checks the presence of the named Api Key parameter. Filter returns 401 if Api-Key is not found in request.
*/
case class ApiKey[T, K >: Request](param: Parameter with Mandatory[K, T], validateKey: ValidateKey[T]) extends Security {
val filter = Filter.mk[Request, Response, Request, Response] {
(request, svc) => Try(param <-- request) match {
case Success(apiKey) => validateKey(apiKey)
.flatMap(result => if (result) svc(request) else Future.value(Response(Unauthorized)))
case Failure(e) => Future.value(Response(Unauthorized))
}
}
}
object ApiKey {
type ValidateKey[T] = T => Future[Boolean]
}
/**
* Default NoOp security filter. Filter allows all traffic through.
*/
object NoSecurity extends Security {
val filter = Filter.identity[Request, Response]
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy