Skip to content

clearToken exception is thrown at wrong place. #8830

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

Closed
wants to merge 5 commits into from
Closed

clearToken exception is thrown at wrong place. #8830

wants to merge 5 commits into from

Conversation

xkobal
Copy link
Contributor

@xkobal xkobal commented Aug 22, 2013

The PR #8528 has added a problem when logger is enabled.

The log message for clearToken exception throw actually a fatal error because $failed doesn't exist in clearToken method. I have moved the log message to the handle method.

@fabpot
Copy link
Member

fabpot commented Aug 22, 2013

I would have passed the exception to the clearToken() method as the method is called twice and the log should be enabled for both calls. Also, what about changing the visibility of the clearToken() to private as this is not used anywhere else.

ping @alcaeus

@xkobal
Copy link
Contributor Author

xkobal commented Aug 22, 2013

No problem to pass the Exception as argument of clearToken(), I can do the change.

Change the visibility of the method to private is not a good idea because we need to override this method.

For example, and it's my problem since this change (2.3.3).
Actually I have a custom provider who need to check at every request the status of the connection on a main server (SSO). If the session on the SSO server is expired, the authenticate method of the Authentication Manager throw an Exception, and the token must be cleared.
The problem is that if the user is already authenticated, the Token is stored in session and in my case, doesn't extends PreAuthenticatedToken, so the clearToken has no effect on my token and people can't be disconnected if the session expire on SSO server.
That's a major undocumented change for us of the 2.3.3 version.

Two solution:

  • override the clearToken method
  • my token has to extend PreAuthenticatedToken

I prefer the first choice, but I can't do it if the method clearToken is private.

@alcaeus
Copy link
Contributor

alcaeus commented Aug 22, 2013

Whoopsie. I agree with Fabien: pass the exception to the clearToken() method and have that call the logger. It's a bit weird to pass an exception to a method, but in this case it seems like the best choice.

@xkobal: Every listener only clears the token it sets. So, if a listener sets a PreAuthenticatedToken, it clears a PreAuthenticatedToken and nothing else. If your listener sets a different token, you must handle that. This was the main reason for the pull requests as it is necessary to keep the tokens well separated in order to use multiple authentication listeners at the same time (e.g. X509 authentication with form based login as fallback).

I agree with Fabien in this case as well: clearToken should be either private or final. Since the handle method is final and sets a PreAuthenticatedToken there can't be any other token class present in this listener. There should be no need to override the clearToken method to clear a different token. My bad for not seeing that when actually writing the code.

@xkobal
Copy link
Contributor Author

xkobal commented Aug 22, 2013

@alcaeus I'am totally agree with your pull request. But If the clearToken become final, that means override clearToken method need a complete rewrite of the handle() method. I find that very complicated for just a little change. The handle method of the AbstractPreAuthenticatedListener do correctly the job, why force people to rewrite the entire handle method.

Normally, the token is created by the authenticate method of the AuthenticationManagerInterface, it is two different classes.
In my case, unauthenticated token is different from authenticated token, but maybe I'm wrong on that point.

@alcaeus
Copy link
Contributor

alcaeus commented Aug 23, 2013

@xkobal What I don't understand is: The AbstractPreAuthenticatedListener creates an instance of PreAuthenticatedToken, which can't be changed (because it's written in handle() which again is declared final, so it can't be overridden). Why would you need to change the clearToken() method to clear a token of a different class?

@xkobal
Copy link
Contributor Author

xkobal commented Aug 23, 2013

@alcaeus You're right. But really this is the return of AuthenticationManager authenticate() method which create/update the token, and you can return whatever token you want, so the return of my method give me a different token.

$token = $this->authenticationManager->authenticate(new PreAuthenticatedToken($user, $credentials, $this->providerKey));

The reason ? It is because I wan't to check at every request the authentication, so I need to skip this line:

if ($token instanceof PreAuthenticatedToken && $token->isAuthenticated() && $token->getUsername() === $user) {
    return;
}

But when I read your point of view, I feel I have to rewrite the handle() method of AbstractPreAuthenticatedListener. This was something I wanted to avoid.

@xkobal
Copy link
Contributor Author

xkobal commented Aug 26, 2013

Tell me your point of view for the change of clearToken() method visibility.

@alcaeus
Copy link
Contributor

alcaeus commented Aug 26, 2013

My opinion: make it private or final. Since handle() is the method that specifies which token is set and it is marked final, I fail to see why clearToken() should be more open than handle(). You'd need to write your own Listener to set a different token anyways, so you're also in charge of clearing the token.

@fabpot
Copy link
Member

fabpot commented Aug 26, 2013

private looks good to me.

@xkobal
Copy link
Contributor Author

xkobal commented Aug 26, 2013

Done

{
$token = $this->securityContext->getToken();
if ($token instanceof PreAuthenticatedToken && $this->providerKey === $token->getProviderKey()) {
$this->securityContext->setToken(null);

Copy link
Member

Choose a reason for hiding this comment

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

Can you remove the blanks?

@xkobal
Copy link
Contributor Author

xkobal commented Aug 26, 2013

I have removed blank spaces

fabpot added a commit that referenced this pull request Aug 26, 2013
This PR was submitted for the master branch but it was merged into the 2.2 branch instead (closes #8830).

Discussion
----------

clearToken exception is thrown at wrong place.

The PR #8528 has added a problem when logger is enabled.

The log message for clearToken exception throw actually a fatal error because $failed doesn't exist in clearToken method. I have moved the log message to the handle method.

Commits
-------

701c25b clearToken exception is thrown at wrong place.
@fabpot fabpot closed this Aug 26, 2013
@xkobal
Copy link
Contributor Author

xkobal commented Aug 27, 2013

@fabpot Do you plan to rebase this PR in 2.3 branch too ?

@fabpot
Copy link
Member

fabpot commented Aug 27, 2013

Done and a new version of 2.3 is on its way.

@xkobal
Copy link
Contributor Author

xkobal commented Aug 27, 2013

Thanks

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

Successfully merging this pull request may close these issues.

3 participants