Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Token not found in Error Callback instead of notFoundHandler #127

Open
ludufre opened this issue Jul 13, 2018 · 2 comments
Open

Token not found in Error Callback instead of notFoundHandler #127

ludufre opened this issue Jul 13, 2018 · 2 comments

Comments

@ludufre
Copy link

ludufre commented Jul 13, 2018

If call a page that are not maped instead of notFoundHandler is called the error callback of this library.

I.e.: if call https://foo.bar/public/pageitenrionalynotmaped
I receive: {"status": "error", "message": "Token not found"}
I expected: Slim 404 Handler to be called

PHP 7.2.7
Slim 3.1
JWT-Auth: 3.0

@tuupola tuupola self-assigned this Aug 14, 2018
@tuupola tuupola added bug and removed bug labels Aug 14, 2018
@tuupola
Copy link
Owner

tuupola commented Aug 14, 2018

Confirmed, I do not remember why this is the case. Seems to date back to 2.x branch. Changing this behavior by default would be a BC break, so maybe add a configuration switch to honor 404.

Will investigate a bit.

@tuupola
Copy link
Owner

tuupola commented Aug 14, 2018

Now I remember.

By default middleware does not have any information about current route. Middleware is also executed for all requests, even those which are made against nonexistent route. As a workaround you could set determineRouteBeforeAppMiddleware => true and throw a NotFoundException when route does not exist. This will exit the middleware stack early.

For example:

<?php

require __DIR__ . "/vendor/autoload.php";

$config = [
    "settings" => [
        "determineRouteBeforeAppMiddleware" => true,
    ],
];
$app = new Slim\App($config);

$app->add(new Tuupola\Middleware\JwtAuthentication([
    "path" => "/",
    "secret" => "supersecretkeyyoushouldnotcommittogithub",
    "error" => function ($response, $arguments) {
        $data["status"] = "error";
        $data["message"] = $arguments["message"];
        return $response
            ->withHeader("Content-Type", "application/json")
            ->getBody()->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
    }
]));

$app->add(function ($request, $response, $next) {
    $route = $request->getAttribute("route");

    if (empty($route)) {
        throw new Slim\Exception\NotFoundException($request, $response);
    }

    return $next($request, $response);
});

$app->get("/test", function ($request, $response, $arguments) {
    print "Brawndo!";
});

$app->run();
$ curl --include http://localhost:8080/test

HTTP/1.1 401 Unauthorized
Host: localhost:8080
Date: Tue, 14 Aug 2018 09:50:40 +0000
Connection: close
X-Powered-By: PHP/7.1.14
Content-Length: 60

{
    "status": "error",
    "message": "Token not found."
}
$ curl --include http://localhost:8080/nosuch

HTTP/1.1 404 Not Found
Host: localhost:8080
Date: Tue, 14 Aug 2018 09:50:46 +0000
Connection: close
X-Powered-By: PHP/7.1.14
Content-type: text/html;charset=UTF-8
Content-Length: 879

...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants