Hello, I can’t seem to get the JWT authentication to work.
My middleware.php contains this entry. I am trying to use a custom header: “stoken”
$app->add(new \Tuupola\Middleware\JwtAuthentication([
“path” => “/api”, /* or ["/api", “/admin”] */
“secret” => “secretkey”,
“header” => “stoken”,
“algorithm” => [“HS256”],
“callback” => function ($request, $response, $arguments) use ($container) {
$container[“jwt”] = $arguments[“decoded”];
},
“error” => function ($request, $response, $arguments) {
$data[“status”] = “error”;
$data[“message”] = $arguments[“message”];
return $response
->withHeader(“Content-Type”, “application/json”)
->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
}
]));
In settings.php - I added this entry at the bottom:
// jwt settings
“jwt” => [
‘secret’ => ‘secretkey’
]
And in the routes.php, for successful login I added:
$settings = $this->get(‘settings’);
$token = JWT::encode([‘id’ => $usr->user_id, ‘email’ => $usr->email_address], $settings[‘jwt’][‘secret’], “HS256”);
return $this->response->withJson([‘token’ => $token]);
So the login returns a 200 response, and dumps the token:
200
{“token”:“eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjEwMDAxMyIsImVtYWlsIjoiam9lMTFAeWFob28uY29tIn0.6I9GMf2tX9YzMiaAIXptcDqUfYssqqB5ZMb7w9AWEhw”}
Login Success
So that’s good. Then in the next call (a Java program) I am attempting to set the token (hard coding it for now):
conn.setRequestProperty(“stoken”, “eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjEwMDAxMyIsImVtYWlsIjoiam9lMTFAeWFob28uY29tIn0.6I9GMf2tX9YzMiaAIXptcDqUfYssqqB5ZMb7w9AWEhw”);
But I get a 500 error.
In this slim php there is no Error log information.
As per the documentation I have an entry for displayErrorDetails:
$configuration = [
‘settings’ => [
‘displayErrorDetails’ => true,
//
// OTHER STUFF HERE
],
];
I don’t see any ERROR in app.log or know why it is failing. Any ideas?
John.