authorization icon indicating copy to clipboard operation
authorization copied to clipboard

RequestAuthorizationMiddleware config redirect url missing

Open wiifree opened this issue 5 years ago • 4 comments

I try to use to add the RequestAuthorizationMiddleware for some controller in my application in cake 4. In the Authorization Middleware i see a code block like this:

$middlewareQueue->add(new AuthorizationMiddleware($this, [
    'unauthorizedHandler' => [
        'className' => 'Authorization.Redirect',
        'url' => '/users/login',
        'queryParam' => 'redirectUrl',
        'exceptions' => [
            MissingIdentityException::class,
            OtherException::class,
        ],
    ],
]));

I can add a redirect url if the authorization failed. This work fine but how i can add a redirect url for the Request Authorization Middleware ?

wiifree avatar Jan 29 '20 22:01 wiifree

The RequestAuthorizationMiddleware doesn't support redirect options at this time. It could be added in a future release. Would you like to help add that option?

markstory avatar Jan 30 '20 04:01 markstory

Yeah. I would like to help to implement this feature and maybe other features in future.

wiifree avatar Jan 30 '20 13:01 wiifree

Can't you just throw a Authorization\Exception\Exception which AuthorizationMiddleware will catch? You can re-use the unauthorizedHandler config for requests.

othercorey avatar May 19 '21 13:05 othercorey

@othercorey that wouldn't work because, when throwing an exception in a RequestPolicy class (that is, a class that implements the RequestPolicyInterface interface), that exception needs to be caught in the RequestAuthorizationMiddleware, and not in the AuthorizationMiddleware.

In fact, the process method of AuthorizationMiddleware has a try/catch block, while the process method of RequestAuthorizationMiddleware doesn't.

I have opened a pull request to fix that. Please let me know.

matteorebeschi avatar Aug 01 '22 16:08 matteorebeschi