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

longmtran created an issue. See original summary.

longmtran’s picture

Attached patch adds return statement to ClientCredentialsGrantService->getAccessToken()

longmtran’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: oauth2_client-empty-access-token-for-client-credentials-on-first-call-3064062-0.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

longmtran’s picture

Assigned: Unassigned » longmtran
Status: Needs work » Needs review
sfuchsbe’s picture

Since 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.

ivan.chavarro’s picture

Patch #6 worked for me, thank you.

imclean’s picture

Status: Needs review » Reviewed & tested by the community

Thanks, #6 looks good and is pretty essential imo.

longmtran’s picture

Assigned: longmtran » Unassigned
jumoke’s picture

This 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).

fathershawn’s picture

Version: 8.x-2.x-dev » 8.x-3.x-dev
Assigned: Unassigned » fathershawn

Hi! @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.

fathershawn’s picture

Status: Reviewed & tested by the community » Closed (duplicate)
fathershawn’s picture