Skip to content
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

Allow to pass options when fetching access token #230

Merged
merged 2 commits into from
Mar 19, 2020

Conversation

akondas
Copy link
Contributor

@akondas akondas commented Mar 6, 2020

This little change allow to pass options when fetching AccessToken.

Motivation

Some providers checks for redirect_uri when fetching token from code (last step). When in my app is more than one entry point with otuah (for example different scopes in different app region) I want to have ability to change redirect_uri. The redirect method allows for that, but then, when user returns from provider redirect_uri param is taken from main configuration, which causes auth error (i.e. GitLab). For now the only solution is:

        gitlab-register:
            type: gitlab
            client_id: '%env(OAUTH_GITLAB_CLIENT_ID)%'
            client_secret: '%env(OAUTH_GITLAB_CLIENT_SECRET)%'
            redirect_route: register_gitlab_check
        gitlab-auth:
            type: gitlab
            client_id: '%env(OAUTH_GITLAB_CLIENT_ID)%'
            client_secret: '%env(OAUTH_GITLAB_CLIENT_SECRET)%'
            redirect_route: login_gitlab_check

This PR allow to have one client and change only redirect_uri:

// Start -> redirect
$this->oauth->getClient('gitlab')->redirect(['read_user'], ['redirect_uri' => 'https://some.url'])

// Return -> check
$this->oauth->getClient('gitlab')->getAccessToken(['redirect_uri' => 'https://some.url']);

src/Client/OAuth2Client.php Outdated Show resolved Hide resolved
Copy link
Member

@weaverryan weaverryan left a comment

Choose a reason for hiding this comment

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

This PR makes a lot of sense. Unfortunately, the interface change is a backwards-compatibility break. Adding a BC-layer would take some work, and I think we would ultimately need to renamegetAccessToken() to something else in order to do it.

I've asked for one small change. Other than that,+1 for me. The request is legitimate... which means we'll just need to bump to 2.0 after this PR.

src/Client/OAuth2ClientInterface.php Show resolved Hide resolved
@akondas
Copy link
Contributor Author

akondas commented Mar 9, 2020

One question: adding new optional param to method definition is really a bc-break?

@weaverryan
Copy link
Member

Yea... unfortunately it is - you'll get stuff like this for classes that already implement it:

PHP Fatal error: Declaration of FooBar::getAccessToken() must be compatible with KnpU\OAuth2ClientBundle\Client\OAuth2ClientInterface::getAccessToken(array $options = Array) in /Users/weaverryan/Sites/os/KnpUOAuth2ClientBundle/play.php on line 20

And we mention it in the Symfony BC details here - https://symfony.com/doc/current/contributing/code/bc.html#changing-interfaces - even for me, I need to check this to be sure - BC is super complicated.

I'll review and hopefully merge this soon :)

@weaverryan weaverryan merged commit e0d24f5 into knpuniversity:master Mar 19, 2020
@weaverryan
Copy link
Member

Thank you @akondas!

I'm going to wait for #232 before merging - because I want to make sure that doesn't impact this.

Cheers!

@akondas akondas deleted the access-token branch March 19, 2020 13:22
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.

2 participants