Even when Scopes are declared in the annotations, they are ignored and not sent during Token Request.
So authorization request with grant_type='client_credentials' fails.
I am creating a patch that includes Scopes in the getAccessToken function.
Also, Scopes format of array (in the README file) do not work. Scopes should be declared as comma delimited string as shown below. Conversation around this can also be found on thephpleague php library being used for this module: https://github.com/thephpleague/oauth2-client/issues/464
Patch coming. Here are the scopes declaration that work below. Read me also updated.
namespace Drupal\my_module\Plugin\Oauth2Client;
use Drupal\oauth2_client\Plugin\Oauth2Client\Oauth2ClientPluginBase;
/**
* OAuth2 Client to authenticate with ExampleClient
*
* @Oauth2Client(
* id = "example",
* name = @Translation("Example"),
* grant_type = "client_credentials",
* client_id = "###",
* client_secret = "###",
* authorization_uri = "",
* token_uri = "https://example.com/oauth2/token",
* redirect_uri = "",
* resource_owner_uri = "https://example.com/JobRequisitionDetails",
* scopes = "jobrequisition:read, jobstuff:read",
* scope_separator = ",",
* )
*/
class ExampleClient extends Oauth2ClientPluginBase {}
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | Add_Scopes_to_ClientCredentials-3157501-1.patch | 1.48 KB | jumoke |
Issue fork oauth2_client-3157501
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
jumoke commentedComment #3
jumoke commentedHere is the patch to add scope correctly to your token request.
Comment #4
jumoke commentedComment #5
imclean commentedThere is a current GitLab issue addressing this problem: https://github.com/thephpleague/oauth2-client/issues/774#issuecomment-64...
Comment #6
imclean commentedScopes can be defined using object notation.
Use this with the change I suggested in the GitLab issue linked to in #5.
Also see: #3046879: Change optionprovider to get the token
Comment #7
imclean commentedComment #8
imclean commentedThis is not correct. It should be a json object, see #3150757: Fix scopes documentation in README.md. The module just needs to be fixed to use it properly. See the other issues.
I think this is a duplicate of other issues, unless you can point out what this does which is different.
Comment #9
zerbash commentedSince Oauth2ClientPluginBase is designed to handle scopes, it should work with the declared scopes, rather than having to #3046879: Change optionprovider to get the token or use #3256272: Additional parameters to get Access token.
Something along the lines of the second half of jumoke's patch:
This MR provides a workaround:
...but it sure would be cleaner to just use the 'scopes' declared in the plugin annotation.
Comment #10
fathershawnComment #11
fathershawnI'm not clear on how we are not already using scopes with the code as it is:
They are a named parameter in the upstream GenericProvider.
We populate that parameter from our plugin definition
Most of which is code inherited from previous maintainers, but if I add
scopes = {"foo", "bar"}to a plugin annotation and inspect the instantiated provider at the return above, the scopes property in the GenericProvder is properly initialized with the expected array.Comment #12
fathershawnI found an upstream issue: [Question] Client Credentials Grant with scope which lead to a deeper exploration of the Oauth2 Client library code.
Our
scopesproperty in the plugin definition gets passed to the GenericProvider as shown in #11 and becomes\League\OAuth2\Client\Provider\GenericProvider::$scopes. Within the library, that property is accessed viaGenericProvider::getDefaultScopeswhich is only called in the library by\League\OAuth2\Client\Provider\AbstractProvider::getAuthorizationParameterswhich is only used to build the url for\Drupal\oauth2_client\Service\Grant\AuthorizationCodeGrantService::getAccessTokenFollowing the guidance of that issue, I propose to create a ClientCredentialsOptionProvider and inject it before the access token is requested our Client Credentials service
Comment #14
fathershawnSuccessfully tested on my current project
Comment #16
fathershawn