Skip to content

Unauthenticated actions#191

Merged
dereuromark merged 6 commits intomasterfrom
unauthenticated-actions
Apr 14, 2018
Merged

Unauthenticated actions#191
dereuromark merged 6 commits intomasterfrom
unauthenticated-actions

Conversation

@markstory
Copy link
Copy Markdown
Member

This adds a very rudimentary form of access control to this plugin. By default the component will require an identity to be present in all requests. This behavior can be disabled via a setting or by using the allowUnauthenticated() method to whitelist the controller actions.

Refs #188

Don't assign properties.
This adds a *very* rudimentary form of access control to this plugin. By
default the component will require an identity to be present in all
requests. This behavior can be disabled via a setting or by using the
allowUnauthenticated() method to whitelist the controller actions.

Refs #188
@codecov-io
Copy link
Copy Markdown

codecov-io commented Mar 30, 2018

Codecov Report

Merging #191 into master will increase coverage by 0.24%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #191      +/-   ##
============================================
+ Coverage     97.89%   98.14%   +0.24%     
- Complexity      321      342      +21     
============================================
  Files            33       34       +1     
  Lines           857      918      +61     
============================================
+ Hits            839      901      +62     
+ Misses           18       17       -1
Impacted Files Coverage Δ Complexity Δ
...c/Controller/Component/AuthenticationComponent.php 98.57% <100%> (+2.49%) 21 <7> (+7) ⬆️
src/UrlChecker/CakeRouterUrlChecker.php 100% <0%> (ø) 6% <0%> (?)
src/Authenticator/CookieAuthenticator.php 98.9% <0%> (+0.54%) 26% <0%> (+8%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 37d9b0b...7ffabad. Read the comment docs.


$identity = $request->getAttribute('identity');
if (!$identity) {
throw new UnauthorizedException([]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it throw MissingIdentityException?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or UnauthenticatedException - but MissingIdentityException as generic one can also be fine.

Copy link
Copy Markdown
Member

@dereuromark dereuromark Mar 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a loginRedirect or loginAction key present, wouldn't it make sense to redirect to login then instead from the component?
The exception is only useful for stateless (api) access, right?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redirection is performed from a middleware using unauthorized handlers.

Copy link
Copy Markdown
Member Author

@markstory markstory Mar 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@robertpustulka You're right, MissingIdentity would be better, as it is handled by the Unauthorized handlers.

Redirection in the middleware is part of authorization and not this plugin.

@dereuromark My worry with a redirect is it makes stateless auth hard. Whereas an exception can be converted into a redirect by the application code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be a new UnauthenticatedException ?
UnauthorizedException is used for http auth AFAIR

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point definitely not authorize, but authenticate, yes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was attempting to reuse the existing exception which generates a 401 error, which is what I think we want here too.

@markstory
Copy link
Copy Markdown
Member Author

Any other feedback on this?


```php
// In your controller's beforeFilter method.
$this->Authentication->allowUnauthorized(['view']);
Copy link
Copy Markdown
Member

@dereuromark dereuromark Apr 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allowUnauthorized() inside the Authentication module might a sth to trip over?
Should we call it allowUnauthenticated() or just allowPublic()?

At this point it is still either authenticated or not (public access).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs were wrong, and fixed in 1f325dc. The method is allowUnauthenticated(). I was worried that allowPublic() could be confused with PHP method visibility.

* @param array $actions The action list.
* @return $this
*/
public function allowUnauthenticated(array $actions)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have a merge option to allow adding to existing list? Unless there's a method to retrieve existing list so that user can merge himself and reset.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For e.g. I would like to allow 'view' always through AppController and then additional action inside a specific controller.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it then merge by default? Calling the param $overwrite (default false) like in other places in core?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add a getter which would allow merge operations.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a getter? Wouldnt this be

allowUnauthenticated(array $actions, $merge = false)

?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you could always do allowUnauthenticated(['index'], ['merge' => true]) :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging to existing list will be the most common usage. So it should be possible to do that with minimal code 🙂.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So mergeUnauthenticated() is the method name to move forward here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about addUnauthenticatedActions()? Merge sounds mire complicated that add or append do.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

@dereuromark dereuromark merged commit 1dbfa00 into master Apr 14, 2018
@dereuromark dereuromark deleted the unauthenticated-actions branch April 14, 2018 08:07
@robertpustulka
Copy link
Copy Markdown
Member

robertpustulka commented Apr 23, 2018

For anyone trying to integrate this feature with Authorization plugin unauthorized handlers, this middleware might come in handy:

$middleware->add(function($req, $res, $next) {
    try {
        return $next($req, $res);
    } catch (UnauthorizedException $e) {
        throw new MissingIdentityException(['identity'], null, $e);
    }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants