Created
January 5, 2014 19:17
-
-
Save solidsnack/8272556 to your computer and use it in GitHub Desktop.
Basic Auth helper for Play Framework
This file contains hidden or 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
// Call in the scope of a request handler to retrieve basic auth parameters | |
// if they are present. | |
def basicAuth()(implicit request: Request[_]): Option[(String, String)] = { | |
val Basic = "^ *Basic +([^ ].+)$".r | |
for { | |
auth <- request.headers.get("Authorization") | |
Basic(base64) <- Basic.findFirstIn(auth) | |
decoded <- Option(new BASE64Decoder().decodeBuffer(base64)) | |
plain <- Option(new String(decoded, "UTF-8")) | |
(a, b) <- plain.split(':') match { | |
case Array(a, b) => Some((a, b)) | |
case _ => None | |
} | |
} yield (a, b) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very neat, simple. Nice work.