Skip to content

[Security] Fix json_login default success/failure handling #22494

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

Merged
merged 1 commit into from
Apr 23, 2017

Conversation

chalasr
Copy link
Member

@chalasr chalasr commented Apr 21, 2017

Q A
Branch? 3.3
Bug fix? yes
New feature? no
BC breaks? no (master only)
Deprecations? no
Tests pass? yes
Fixed tickets #22483
License MIT
Doc PR n/a

This makes the json_login listener default configuration stateless oriented by:

  • Not using the default (redirect based) failure handler, it returns a 401 (json) response containing the failure reason instead
  • Not using the default (redirect based) success handler, just let the original request continue instead (reaching the targeted resource without being redirected).
  • Setting require_previous_session to false by default (I have to set it on form-login each time I want it to be stateless)
  • Removing the options related to redirections (default_target_path, login_path, ...) from the listener factory, if one wants redirections then one has to write its own handlers, not the inverse

@chalasr chalasr force-pushed the stateless-json-login branch from f481ea2 to 9749618 Compare April 21, 2017 08:18
@chalasr chalasr changed the title Fix json_login default success/failure handling [Security] Fix json_login default success/failure handling Apr 21, 2017
@chalasr
Copy link
Member Author

chalasr commented Apr 21, 2017

ping @lsmith77 @dunglas

{
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
return new JsonResponse(array('message' => sprintf('Good game @%s!', $token->getUsername())));
Copy link
Contributor

Choose a reason for hiding this comment

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

do we have a standard style how we word such defaults?

Copy link
Member Author

Choose a reason for hiding this comment

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

this is a custom success handler fixture :)

@@ -117,6 +118,10 @@ public function handle(GetResponseEvent $event)
$response = $this->onFailure($request, $e);
}

if (null === $response) {
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the reason this is needed?

Copy link
Member Author

@chalasr chalasr Apr 21, 2017

Choose a reason for hiding this comment

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

In case of success, the listener just lets the original request succeeds so it doesn't return any response. Should it return a default success response instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

ah of course

@lsmith77
Copy link
Contributor

great! thanks for implementing this.

@fabpot
Copy link
Member

fabpot commented Apr 23, 2017

Thank you @chalasr.

@fabpot fabpot merged commit 9749618 into symfony:master Apr 23, 2017
fabpot added a commit that referenced this pull request Apr 23, 2017
… (chalasr)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[Security] Fix json_login default success/failure handling

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no (master only)
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #22483
| License       | MIT
| Doc PR        | n/a

This makes the `json_login` listener default configuration stateless oriented by:

- Not using the default (redirect based) failure handler, it returns a 401 (json) response containing the failure reason instead
- Not using the default (redirect based) success handler, just let the original request continue instead (reaching the targeted resource without being redirected).
- Setting `require_previous_session` to `false` by default (I have to set it on `form-login` each time I want it to be stateless)
- Removing the options related to redirections (`default_target_path`, `login_path`, ...) from the listener factory, if one wants redirections then one has to write its own handlers, not the inverse

Commits
-------

9749618 Fix json_login default success/failure handling
@chalasr chalasr deleted the stateless-json-login branch April 24, 2017 00:05
@arthens
Copy link

arthens commented Dec 28, 2017

@chalasr does this mean that now success_handler (and maybe failure_handler) are required? I'm trying to enable json_login in Symfony 3.3.14 and I'm not sure if the docs are out of date, if there's a bug or if I'm misconfiguring something.

I'm copying the configuration from https://symfony.com/doc/current/security/json_login_setup.html and I'm getting the following error on successful logins:

CRITICAL: Uncaught PHP Exception LogicException: "The
controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?

Which I think is because my controller is empty (as suggested by the documentation) and:

Not using the default (redirect based) success handler, just let the original request continue instead (reaching the targeted resource without being redirected).

by default there is no success handler by default.

I'm happy to submit a bug report, but I'm not sure if the bug is in the docs or in the code

@chalasr
Copy link
Member Author

chalasr commented Dec 28, 2017

does this mean that now success_handler (and maybe failure_handler) are required?

@arthens depending on your need, yes. This made json_login suited for stateless authentication by default, meaning that you can pass credentials to each single request (not restricted to a given check_path) and, if credentials are valid, then the request continues for reaching the target controller (itself returning any resource).

To me the common workflow is to send username/password to a specific check_path only once, this check_path returning a token (e.g. bearer) itself passed to each subsequent request (validated by another security listener).
In that case, you may want to set check_path and success_handler (+ failure_handler if the default one doesn't fit, which is a 401 json response).

Docs need to be updated indeed, please submit an issue there and eventually ping me, I'll take care of the PR. Thanks!

javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Oct 10, 2018
This PR was squashed before being merged into the 3.4 branch (closes #10467).

Discussion
----------

Fix and improve JSON login docs

Since symfony/symfony#22494, it's mandatory to return a valid response in the controller to prevent a 500 error.

I've updated the docs accordingly, and added an example of what can be returned.

This PR is similar to #9611 (that was right, sorry I just noticed).

ping @vincentchalamon

Commits
-------

570dce3 Fix and improve JSON login docs
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