2

I have a midleware that checks for a valid signature on a JWT token for GET requests to private routes. In it, I need to supply my JWT secret which is stored in settings.php at ['settings']['jwt']['secret']. I tried accessing this value using $this->settings->jwt->secret and get the error:

Using $this when not in object context

I switched to use $app->settings->jwt->secret and get the error

Uncaught InvalidArgumentException: Secret must be either a string or an array of "kid" => "secret" pairs



middleware.php

$app->add(new \Tuupola\Middleware\JwtAuthentication([
    "path" => "/api", /* or ["/api", "/admin"] */
    "attribute" => "decoded_token_data",
    "secret" => $this->settings->jwt->secret,
    "algorithm" => ["HS256"],
    "error" => function ($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));
    }, ]));

settings.php

return [
    'settings' => [
        'displayErrorDetails' => true, // set to false in production
        'addContentLengthHeader' => false, // Allow the web server to send the content-length header

        // Renderer settings
        'renderer' => [
            'template_path' => __DIR__ . '/../templates/',
        ],

        // Monolog settings
        'logger' => [
            'name' => 'slim-app',
            'path' => isset($_ENV['docker']) ? 'php://stdout' : __DIR__ . '/../logs/app.log',
            'level' => \Monolog\Logger::DEBUG,
        ],
        // database connection details
        "db" => [
            "host" => "127.0.0.1",
            "dbname" => "sity",
            "user" => "root",
            "pass" => "",
        ],

        // jwt settings
        "jwt" => [
            'secret' => 'jwtsecret',
        ],
    ],
];

What is the correct way to access that value (or anything in the settings object for that matter)?

Zamrony P. Juhara
  • 5,222
  • 2
  • 24
  • 40
Daveh0
  • 952
  • 9
  • 33

2 Answers2

1

According to the docs (http://www.slimframework.com/docs/v3/objects/application.html):

There are also a number of settings that are used by Slim. These are stored in the settings configuration key. You can also add your application-specific settings.

As the settings are stored in the DI container so you can access them via the settings key in container factories.

So, before you add the middleware, go ahead and grab the settings since, as the error indicates, you are not inside an object context.

$container = $app->getContainer();
$jwtSettings = $container->get('settings')['jwt'];

Then inside $app->add() you should be able to get the token like:

"secret" => $jwtSettings['secret'],
Community
  • 1
  • 1
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
  • Perfect - I knew I was missing something like that but am still a little foggy on the container concept. This will allow me to move forward and figure it out along the way. Thanks!! – Daveh0 Feb 07 '19 at 17:29
  • Lookup "dependency injection". It is a powerful concept for decoupling dependencies and allows code to be written that is more flexible. – Jeremy Harris Feb 07 '19 at 17:31
-1

something like this should work:

$modulesSettings = $this->get('settings')['jwt']['secret'];
Matt Goodis
  • 393
  • 1
  • 3
  • 11
  • it gives the error: "Using $this when not in object context". Changing `$this` to `$app` gives the error: "Too few arguments to function Slim\App::get()" – Daveh0 Feb 07 '19 at 17:24