Skip to content
Browse files

Support a secure cookie spray directive

  • Loading branch information...
1 parent 5d45907 commit fdc0b35077b2988cd31b499ef29984db6b20ac5f @theatrus committed Aug 2, 2012
Showing with 20 additions and 1 deletion.
  1. +20 −1 src/main/scala/com/stackfoundry/spray_nozzle/HeaderFilterDirectives.scala
View
21 src/main/scala/com/stackfoundry/spray_nozzle/HeaderFilterDirectives.scala
@@ -1,10 +1,29 @@
package com.stackfoundry.spray_nozzle
-import cc.spray.{Reject, Directives, Pass}
+import cc.spray.{MissingCookieRejection, Reject, Directives, Pass}
+import cc.spray.directives.{SprayRoute2, SprayRoute1}
+import cc.spray.http.{HttpHeaders, HttpCookie}
trait HeaderFilterDirectives extends Directives {
def ajaxRequest = filter { ctx => ctx.request.headers.find(_.name.toLowerCase == "x-requested-with") match {
case Some(h) => if (h.value.toLowerCase.contains("xmlhttprequest")) Pass else Reject()
case None => Reject()
} }
+
+ def secureCookie(name: String, crypter: CookieCrypter): SprayRoute1[String] = {
+ val directive = headerValue {
+ case HttpHeaders.Cookie(cookies) => cookies.find(_.name == name) match {
+ case Some(cookie) => crypter.verify(cookie.value) match {
+ case Some(value) => Some(value)
+ case _ => None
+ }
+ case _ => None
+ }
+ case _ => None
+ }
+ filter1[String] {
+ directive.filter(_).mapRejections(_ => MissingCookieRejection(name))
+ }
+ }
+
}

0 comments on commit fdc0b35

Please sign in to comment.
Something went wrong with that request. Please try again.