phpkohanahttp-status-code-401kohana-3.3www-authenticate

Throw a custom 401 HTTP Exception in Kohana


When I throw a HTTP_Exception_401 in Kohana 3.3 I get the following error:

Kohana_Exception [ 0 ]: A 'www-authenticate' header must be specified for a HTTP 401 Unauthorized

Now the obvious problem here is that the www-authenticate header hasn't been set.

What I'm struggling to figure out is how and where to set it.
I have tried the following but the same error is thrown:

Using native PHP header():

header('WWW-Authenticate: realm="My Website"');
throw new HTTP_Exception_401('Authorisation Required');

Adding header to request object:

$this->request->header('WWW-Authenticate', 'realm="My Website"');
throw new HTTP_Exception_401('Authorisation Required');

I'm trying to throw the exception from a controller.
Any help would be appreciated.


Solution

  • As the upgrage guide says:

    // Unauthorized / Login Requied
    throw HTTP_Exception::factory(401)->authenticate('Basic realm="MySite"');