Skip to content

Commit

Permalink
add support for HTTP Basic Auth authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
sqs committed Jun 21, 2012
1 parent bce6aa2 commit d094a1f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/controllers/Application.scala
Expand Up @@ -51,6 +51,7 @@ trait StatelessSecurity extends StatelessSecurityBase {

def lookupUser(userId: String) = User.findByEmail(userId)
def getUserId(user: User) = user.email
def authenticateUserAndReturnUserId(userId: String, password: String) = User.authenticate(userId, password).map(_.email)

def onUnauthorized(request: RequestHeader) = Redirect(routes.Application.login)
def onLoginSucceeded(request: RequestHeader) = Redirect(routes.Application.home)
Expand Down
3 changes: 2 additions & 1 deletion module/build.sbt
Expand Up @@ -7,7 +7,8 @@ resolvers ++= Seq(
)

libraryDependencies ++= Seq(
"play" %% "play" % "2.0.1"
"play" %% "play" % "2.0.1",
"commons-codec" % "commons-codec" % "1.2"
)

organization := "com.blendlabsinc"
Expand Down
@@ -1,16 +1,31 @@
package com.blendlabsinc.play20.auth

import play.api.mvc._
import org.apache.commons.codec.binary.Base64.decodeBase64

trait StatelessSecurityBase {

type User

def getUserIdFromRequest(request: RequestHeader): Option[String] =
Seq(getUserIdFromCookie _, getUserIdFromHTTPBasicAuth _).flatMap(_.apply(request)).headOption

def getUserIdFromCookie(request: RequestHeader): Option[String] =
AuthData.decodeFromCookie(request.cookies.get(AuthData.COOKIE_NAME)).get(AuthData.UserIdKey)

def getUserIdFromHTTPBasicAuth(request: RequestHeader): Option[String] =
request.headers.get("Authorization").flatMap { authorization =>
authorization.split(" ").drop(1).headOption.flatMap { encoded =>
new String(decodeBase64(encoded.getBytes)).split(":").toList match {
case userId :: password :: Nil => authenticateUserAndReturnUserId(userId, password)
case _ => None
}
}
}

def lookupUser(userId: String): Option[User]
def getUserId(user: User): String
def authenticateUserAndReturnUserId(userId: String, password: String): Option[String]

def onUnauthorized(request: RequestHeader): Result
def onLoginSucceeded(request: RequestHeader): PlainResult
Expand Down

0 comments on commit d094a1f

Please sign in to comment.