Problem/Motivation
Method getAccessToken($client_id) in Oauth2ClientService.php for client_credentials grant type does not return the access token on first call to create token. On first call to create token (no token stored or token expired), the token is only stored into state but not returned directly. This is the current code for client_credentials in Oauth2ClientService.php:
private function getClientCredentialsAccessToken($clientId) {
$access_token = $this->retrieveAccessToken($clientId);
if (!$access_token) {
$access_token = $this->grantServices['client_credentials']->getAccessToken($clientId);
}
return $access_token;
}
$this->grantServices['client_credentials']->getAccessToken($clientId); does not return anything, see ClientCredentialsGrantService.php
This might be intentional but it will be helpful to also return the access token there directly after it is stored into State. This will allow some integration work to seamlessly obtain and renew token efficiently.
This could also be useful on other grant type too but at the moment, I'm only doing some work on client_credentials
Proposed resolution
Return access token in getAccessToken in ClientCredentialsGrantService.php
Comments
Comment #2
longmtran commentedAttached patch adds return statement to
ClientCredentialsGrantService->getAccessToken()Comment #3
longmtran commentedComment #5
longmtran commentedComment #6
sfuchsbe commentedSince all grant services are implementing the getAccessToken() function this patch should make shure that all implementation are following the definition in the interface. Additionally @longmtran is right - it is useful for the others as well.
Comment #7
ivan.chavarro commentedPatch #6 worked for me, thank you.
Comment #8
imclean commentedThanks, #6 looks good and is pretty essential imo.
Comment #9
longmtran commentedComment #10
jumoke commentedThis looks good and works for me.
I recently added a patch to include Scopes as part of the $options for a getAccessToken request (if scopes is provided) as part of the Client Credentials grant_type. Scopes was being ignored. https://www.drupal.org/project/oauth2_client/issues/3157501
It coincides with this patch, so I am re-rolling this. You need this patch in addition to the one linked above (for scopes).
Comment #11
fathershawnHi! @perelesnyk and I have stepped up as maintainers, officially for D9 compatible and forward. I've done a quick review of the patch and may propose an alternative as part of some refactoring.
Comment #12
fathershawnAddressed in #3072062: Refactor Authorization Token Flow
Comment #13
fathershawn