Caballero
Caballero

Reputation: 12101

Scala/Play http authentication issue - getting user name and password from request

I'm using Firefox "REST Easy" plugin to test HTTP authentication in my Play application. This is how the request URL looks like in Firebug:

http://username:password@localhost:9001/test

The method in my Play controller looks like this:

def test = Action { implicit request =>

    request.headers.get("Authorization") match {
        case Some(header) => println(header)
        case None => {
            println("send user name and password")
            Unauthorized.withHeaders("WWW-Authenticate" -> "Basic realm=\"myrealm\"")
        }           
    }

    Ok("")

}

I'm getting "send user name and password" in my console and nothing else happens. What am I doing wrong and how do I get the user name and password from the request?

Upvotes: 3

Views: 1386

Answers (1)

Redmar Kerkhoff
Redmar Kerkhoff

Reputation: 182

Your code works fine, the problem is in your used browser plugin. Somehow it doesn't work with basic auth because if you add a println(request.headers) call as the first line of your action body you can see it's not in there, while if you use curl it's there!

I used the following commind with your given code:

curl --user name:password http://localhost:9000/test

and it results in:

Basic bmFtZTpwYXNzd29yZA==

So it works!

Upvotes: 1

Related Questions