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

Add request object back to the error handler. #95

Open
tuupola opened this issue Jun 10, 2020 · 3 comments
Open

Add request object back to the error handler. #95

tuupola opened this issue Jun 10, 2020 · 3 comments
Assignees
Labels

Comments

@tuupola
Copy link
Owner

tuupola commented Jun 10, 2020

Add request object back as parameter to the error handler.

$app->add(new Tuupola\Middleware\HttpBasicAuthentication([
    "error" => function ($request, $response, $arguments) {
      ...
    }
]));

See tuupola/branca-middleware#13 for reference.

@tuupola tuupola self-assigned this Jun 10, 2020
@tuupola tuupola added the 4.x label Jun 10, 2020
@joelmora
Copy link

@tuupola FYI I did a workaround on v3.3.1 to be able to throw a Slim HTTP code.

use Slim\Exception\HttpUnauthorizedException;

  // 1st middleware to configure basic authentication
  $app->add(new HttpBasicAuthentication([
    "users" => [
      "root" => "secret",
    ],
    "error" => function ($response) {
      return $response->withStatus(401);
    }
  ]));

  // 2nd middleware to throw 401 with correct slim exception
  $app->add(function (Request $request, RequestHandler $handler) {
    $response = $handler->handle($request);
    $statusCode = $response->getStatusCode();

    if ($statusCode == 401) {
      throw new HttpUnauthorizedException($request);
    }

    return $response;
  });

any other way to get the request object?

@MarcHagen
Copy link

MarcHagen commented Jan 11, 2023

Changing the processError function in HttpBasicAuthentication would give the desired result.
Passing the $request through to the use to do whatever.

    private function processError(ServerRequestInterface $request, ResponseInterface $response, array $arguments): ResponseInterface
    {
        if (is_callable($this->options["error"])) {
            $handler_response = $this->options["error"]($request, $response, $arguments);
            if ($handler_response instanceof ResponseInterface) {
                return $handler_response;
            }
        }
        return $response;
    }

We can PR this, but that would be breaking for older installations.
Don't know if there is a major/minor coming out soon?

As intermit step, adding a option to throw an Exception instead would also give the desired effect.

@tuupola
Copy link
Owner Author

tuupola commented Jan 12, 2023

Yeah cannot break BC. A kludgish workaround would be to include the request object in the second parameter which is an array.

4.x version is planned but since I recently started freelancing paid work comes first atm.

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

No branches or pull requests

3 participants