Modifying the response for Auth failure using LiftRules.responseTransformers
I am using the below code to have HTTP Basic Auth in my app and transform the response in case of Unauthorised access. It is not working. Whats wrong here?
// Http Basic Authentication
LiftRules.authentication = HttpBasicAuthentication("xxx") {
case (userName, userPass, _) => {
Console.println("Authenticating: " + userName)
User.find("userName", userName).map {
user =>
if (user.password.isMatch(userPass)) {
Account.authenticatedUser.setFieldsFromDBObject(user.asDBObject)
userRoles(AuthRole("authorisedUser"))
Console.println("Success: " + userName)
true
}
else {
Console.println("Failed: " + userName)
false
}
} openOr false
}
}
LiftRules.responseTransformers.append {
resp => resp match {
case UnauthorizedResponse("xxx") =>
Console.println("Responding modified...");
JsonUnauthorizedResponse(("error" -> "Incorrect username or password"))
case x => Console.println("Responding..."); x
}
}
I am not getting the modified response for the URLs in RestApiHelper object. Can somebody please point me to how I can modify the response for the Rest Api Urls in case of unauthorizedResponse. I do not intend to do it using Status Codes. Since I am creating an API to be consumed by mobile apps with server controlled error responses for consistency and a lot of other factors.
Solution UPDATE: This following overridden case class based on the answer b开发者_如何学Pythony CheatEx worked for me:
case class JsonHttpBasicAuthentication(realmName: String)(func: PartialFunction[(String, String, Req), Boolean]) extends HttpAuthentication {
def credentials(r: Req): Box[(String, String)] = {
header(r).flatMap(auth => {
val decoded = new String(Base64.decodeBase64(auth.substring(6, auth.length).getBytes)).split(":").toList
decoded match {
case userName :: password :: _ => Full((userName, password))
case userName :: Nil => Full((userName, ""))
case _ => Empty
}
}
)
}
override def realm = realmName
def verified_? = {
case (req) => {
credentials(req) match {
case Full((user, pwd)) if (func.isDefinedAt(user, pwd, req)) =>
func(user, pwd, req)
case _ => false
}
}
}
override def unauthorizedResponse: UnauthorizedResponse = new UnauthorizedResponse(realm) {
override def toResponse = {
val errResp: JValue = ("error" -> "Incorrect username or password")
InMemoryResponse(errResp.toString.getBytes("UTF-8"),
S.getHeaders(Nil), S.responseCookies, 200)
}
}
}
I had the same problem with the Lift's default unauthorised response. My solution was to override unauthorizedResponse
method of the HttpAuthentication
trait.
Here is a code sample:
private object MyHttpAuth extends HttpAuthentication {
override def realm: String = "MyApp"
def verified_? : PartialFunction[Req, Boolean] = {
case req: Req => getUser(req) match {
case Some(userId) => {
userRoles(AuthRole(userId)::Nil)
true
}
case None => false
}
}
override def unauthorizedResponse: UnauthorizedResponse = new UnauthorizedResponse(realm) {
override def toResponse = InMemoryResponse(Array(), Nil, Nil, 401)
}
}
精彩评论