Created
May 10, 2012 12:55
-
-
Save hamnis/2652851 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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