Advertisement
Guest User

Untitled

a guest
Mar 13th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.82 KB | None | 0 0
  1.   def AuthAction(user_pw: (String, String))(fun: Request[AnyContent] => SimpleResult) = Action{ r =>
  2.     val (us, pw) = user_pw
  3.     Logger.debug(r.headers.get("Authorization").toString)
  4.     r.headers.get("Authorization").flatMap{
  5.       case BasicAuth(`us`, `pw`) => Some(fun(r))
  6.       case _ => None
  7.     }.getOrElse(Unauthorized("Plz auth").withHeaders("WWW-Authenticate" -> """Basic realm="Secured""""))
  8.   }
  9.  
  10. object BasicAuth {
  11.   object UsPw {
  12.     def unapply(str: String) = str.fromBase64.split(":") match{
  13.       case Array(us, pw) => Some(us -> pw)
  14.       case _ => None
  15.     }
  16.   }
  17.  
  18.   def unapply(str: String): Option[(String, String)] = str.split(" ") match {
  19.     case Array("Basic", UsPw(us, pw)) => Some(us -> pw)
  20.     case _ => None
  21.   }
  22. }
  23.  
  24.   def runka = AuthAction("abc"->"123"){ r =>
  25.     Ok("Inloggning ok")
  26.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement