Skip to content

Instantly share code, notes, and snippets.

@hamnis
Created May 10, 2012 12:55
Show Gist options
  • Save hamnis/2652851 to your computer and use it in GitHub Desktop.
Save hamnis/2652851 to your computer and use it in GitHub Desktop.
package com.example.secure
import unfiltered.request.{UserAgent, HttpRequest}
import unfiltered.response.{ResponseString, ContentType, Forbidden, ResponseFunction}
trait Identification {
type User
type RF = ResponseFunction[Any]
protected def identify(userAgent: ApiKey): Either[SecurityException, User]
object Identified {
def unapply[A](req: HttpRequest[A]): Option[(User => RF) => RF] = {
Some(identified(req))
}
}
object identified {
val defaultFail: (SecurityException) => RF =
fail => Forbidden ~> ContentType("text/plain") ~> ResponseString(fail.getMessage + "\n")
def apply[A](req: HttpRequest[A])(f: User => RF): RF = {
req match {
case UserAgent(ua) => identify(ApiKey(ua)).fold(defaultFail, f)
case _ => Forbidden
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment