Conversation
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 Report
@@ 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
Continue to review full report at Codecov.
|
|
|
||
| $identity = $request->getAttribute('identity'); | ||
| if (!$identity) { | ||
| throw new UnauthorizedException([]); |
There was a problem hiding this comment.
Should it throw MissingIdentityException?
There was a problem hiding this comment.
Or UnauthenticatedException - but MissingIdentityException as generic one can also be fine.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Redirection is performed from a middleware using unauthorized handlers.
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
Should it be a new UnauthenticatedException ?
UnauthorizedException is used for http auth AFAIR
There was a problem hiding this comment.
At this point definitely not authorize, but authenticate, yes.
There was a problem hiding this comment.
I was attempting to reuse the existing exception which generates a 401 error, which is what I think we want here too.
|
Any other feedback on this? |
docs/Quick-start-and-introduction.md
Outdated
|
|
||
| ```php | ||
| // In your controller's beforeFilter method. | ||
| $this->Authentication->allowUnauthorized(['view']); |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
For e.g. I would like to allow 'view' always through AppController and then additional action inside a specific controller.
There was a problem hiding this comment.
Would it then merge by default? Calling the param $overwrite (default false) like in other places in core?
There was a problem hiding this comment.
We could add a getter which would allow merge operations.
There was a problem hiding this comment.
Why a getter? Wouldnt this be
allowUnauthenticated(array $actions, $merge = false)
?
There was a problem hiding this comment.
Well, you could always do allowUnauthenticated(['index'], ['merge' => true]) :)
There was a problem hiding this comment.
Merging to existing list will be the most common usage. So it should be possible to do that with minimal code 🙂.
There was a problem hiding this comment.
So mergeUnauthenticated() is the method name to move forward here?
There was a problem hiding this comment.
What about addUnauthenticatedActions()? Merge sounds mire complicated that add or append do.
|
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);
}
}); |
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