-
Notifications
You must be signed in to change notification settings - Fork 73
Exception thrown in login_middleware.php #68
Comments
It is an array, or better: it has the array methods, although that seems now to be deprecated and is removed from the documentation. So the code works, try using The Now it seems that Thanks for digging so deep. |
Ok, updating Slim 2.3.1 to 2.3.4 changed the behaviour back to normal. Now the 401 response code comes through again, the exception is gone. I'll check in the new version today. |
I just updated to the latest trunk and i still get the same exception thrown |
As also explained in this posting http://stackoverflow.com/questions/20270340/slim-php-route-in-middleware, it seems that indeed halt() cannot be used inside a middleware call. I just changed login_middleware.php, so that halt() fires inside a hook attached to 'slim.before.dispatch'. That makes it work again. |
I changed login_middleware.php to this: public function call() {
$this->app->hook('slim.before.dispatch', array($this, 'authBeforeDispatch'));
$this->next->call();
}
public function authBeforeDispatch() {
$app = $this->app;
$request = $app->request;
$resource = $request->getResourceUri();
$accept = $request->headers('ACCEPT');
$app->getLog()->debug('login resource: '.$resource);
$app->getLog()->debug('login accept: '.$accept);
if (!$this->is_static_resource($resource) && !$this->is_authorized()) {
if ($resource === '/login/') {
// special case login page
$app->getLog()->debug('login: login page authorized');
return;
} elseif (stripos($resource, '/opds') === 0) {
$app->getLog()->debug('login: unauthorized OPDS request');
$app->response->headers->set('WWW-Authenticate', sprintf('Basic realm="%s"', $this->realm));
$app->halt(401,'Please authenticate');
} elseif ($accept === 'application/json') {
$app->getLog()->debug('login: unauthorized JSON request');
$app->response->headers->set('WWW-Authenticate', sprintf('Basic realm="%s"', $this->realm));
$app->halt(401,'Please authenticate');
} else {
$app->getLog()->debug('login: redirecting to login');
// now we can also use the native app->redirect method!
$this->app->redirect($app->request->getRootUri().'/login/');
}
}
} |
Yes, the behavior was reproducible. Your solution works fine, thanks. |
When calling an OPDS/JSON route, the app throws an exception:
First of all there is typo in
login_middleware.php
:won't work - the $response object is not an array/dict. But even after changing to
$app->response->headers->set
, the error persists. So i think another problem is thehalt()
function.As far as i understand,
halt()
completely ignores the$response
object, so the WWW-authenticate header is never sent to the client.From slim docs: http://docs.slimframework.com/
After i changed the code like this, the error went away:
in: login_middleware.php
replace with
The text was updated successfully, but these errors were encountered: