Skip to content

Instantly share code, notes, and snippets.

@solidsnack
Created January 5, 2014 19:17
Show Gist options
  • Save solidsnack/8272556 to your computer and use it in GitHub Desktop.
Save solidsnack/8272556 to your computer and use it in GitHub Desktop.
Basic Auth helper for Play Framework
// 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)
}
@yakamoto69
Copy link

Very neat, simple. Nice work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment