/** * test jwt authentication */ public function testAuthenticate() { $this->app["test.controller"] = function (Container $container) { return new TestController($container["command.bus"]); }; $jwt_encoder = $this->app["security.jwt.encoder"]; $this->app->registerCommands([LoginCommand::class], function () use($jwt_encoder) { return new LoginHandler($jwt_encoder); }); $this->app->get("/login", "test.controller:loginAction"); $this->app->get("/vip", "test.controller:indexAction"); $request = Request::create("/login"); $app = $this->app->builder->resolve($this->app); $token = $app->handle($request)->getContent(); // Ensure return token $this->assertContains("data", $token); $token = json_decode($token); // Create request to restricted area $request = Request::create("/vip"); // Fail if no auth header $this->assertEquals("401", $app->handle($request)->getStatusCode()); $request->headers->add(["AUTH-HEADER-TOKEN" => $token->data->token]); $response = $app->handle($request)->getContent(); // return ok if auth header if present and valid $this->assertEquals("ok", $response); }
/** * {@inheritdoc} */ public function command(Singo $application) { /** * register login command and handler */ $application->registerCommands([LoginCommand::class], function () use($application) { return new UserHandler($application["security.jwt.encoder"], $application["dispatcher"]); }); }