Commit 539aae47 authored by Shawn Duncan's avatar Shawn Duncan
Browse files

Issue #3256272 by FatherShawn, zerbash: Additional parameters to get Access token

parent 89ea3656
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -53,6 +53,9 @@ class Oauth2Client extends Plugin {
  /**
   * The resource endpoint of the OAuth2 Server.
   *
   * @deprecated in oauth2_client:3.1 and is removed from oauth2_client:4.0. Use
   *   the new request options parameter in this annotation instead.
   *
   * @var string
   */
  public $resource_uri;
@@ -82,6 +85,20 @@ class Oauth2Client extends Plugin {
   */
  public $scope_separator = ',';

  /**
   * An optional set of additional parameters on the token request.
   *
   *  OPTIONAL.
   *  The array key will be used as the request parameter:
   *
   *   request_options = {
   *     "parameter" = "value",
   *   },
   *
   * @var array
   */
  public $request_options = [];

  /**
   * A flag that may be used by Oauth2ClientPluginInterface::storeAccessToken.
   *
+17 −8
Original line number Diff line number Diff line
@@ -392,11 +392,7 @@ abstract class Oauth2ClientPluginBase extends PluginBase implements Oauth2Client
   * {@inheritdoc}
   */
  public function getScopes() {
    if (!isset($this->pluginDefinition['scopes'])) {
      return [];
    }

    return $this->pluginDefinition['scopes'] ?: [];
    return $this->pluginDefinition['scopes'] ?? NULL;
  }

  /**
@@ -433,13 +429,26 @@ abstract class Oauth2ClientPluginBase extends PluginBase implements Oauth2Client
   * {@inheritdoc}
   */
  public function getScopeSeparator() {
    if (!isset($this->pluginDefinition['scope_separator'])) {
      return ',';
    }
    $this->checkKeyDefined('scope_separator');

    return $this->pluginDefinition['scope_separator'];
  }

  /**
   * {@inheritdoc}
   */
  public function getRequestOptions(array $additionalOptions = []) {
    try {
      $this->checkKeyDefined('request_options');
      $options = $this->pluginDefinition['request_options'];
    }
    catch (Oauth2ClientPluginMissingKeyException $e) {
      $options = [];
    }
    return array_merge($options, $additionalOptions);
  }


  /**
   * Check that a key is defined when requested. Throw an exception if not.
   *
+15 −1
Original line number Diff line number Diff line
@@ -82,13 +82,27 @@ interface Oauth2ClientPluginInterface extends PluginInspectionInterface, Contain
  public function getTokenUri();

  /**
   * Retrieves the resource_uri of the OAuth2 server.
   * Retrieves the resource owner uri of the OAuth2 server.
   *
   * @return string
   *   The resource_uri of the OAuth2 server.
   */
  public function getResourceUri();

  /**
   * Get an array of additional request parameters on the token request.
   *
   * Merges the request_options parameter from the plugin definition with
   * any passed in options, such as 'code', '`username' or 'password'.
   *
   * @param array $additionalOptions
   *   An array of additional options to merge with request options.
   *
   * @return array
   *   The associative array of parameters.
   */
  public function getRequestOptions(array $additionalOptions = []);

  /**
   * Get the set of scopes for the provider to use by default.
   *
+5 −3
Original line number Diff line number Diff line
@@ -86,12 +86,14 @@ class AuthorizationCodeGrantService extends Oauth2ClientGrantServiceBase {
   *   Exception thrown when trying to retrieve a non-existent OAuth2 Client.
   */
  public function requestAccessToken($pluginId, $code) {
    $client = $this->getClient($pluginId);
    $provider = $this->getProvider($pluginId);
    // There should not be a 'code' key in the options, ensure the parameter
    // value is used.
    $options = $client->getRequestOptions(['code' => $code]);
    // Try to get an access token using the authorization code grant.
    try {
      $accessToken = $provider->getAccessToken('authorization_code', [
        'code' => $code,
      ]);
      $accessToken = $provider->getAccessToken('authorization_code', $options);
      if ($accessToken instanceof AccessTokenInterface) {
        $this->storeAccessToken($pluginId, $accessToken);
        return TRUE;
+2 −1
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ class ClientCredentialsGrantService extends Oauth2ClientGrantServiceBase {
   * {@inheritdoc}
   */
  public function getAccessToken($pluginId) {
    $client = $this->getClient($pluginId);
    $provider = $this->getProvider($pluginId);
    $optionProvider = $provider->getOptionProvider();
    // If the provider was just created, our OptionProvder must be set.
@@ -23,7 +24,7 @@ class ClientCredentialsGrantService extends Oauth2ClientGrantServiceBase {
    }

    try {
      $accessToken = $provider->getAccessToken('client_credentials');
      $accessToken = $provider->getAccessToken('client_credentials', $client->getRequestOptions());

      $this->storeAccessToken($pluginId, $accessToken);
    }
Loading