You are here

class OpenIDConnectFacebookClient in OpenID Connect / OAuth client 8

Same name and namespace in other branches
  1. 2.x src/Plugin/OpenIDConnectClient/OpenIDConnectFacebookClient.php \Drupal\openid_connect\Plugin\OpenIDConnectClient\OpenIDConnectFacebookClient

Facebook OpenID Connect client.

Implements OpenID Connect Client plugin for Facebook.

Plugin annotation


@OpenIDConnectClient(
  id = "facebook",
  label = @Translation("Facebook")
)

Hierarchy

Expanded class hierarchy of OpenIDConnectFacebookClient

File

src/Plugin/OpenIDConnectClient/OpenIDConnectFacebookClient.php, line 18

Namespace

Drupal\openid_connect\Plugin\OpenIDConnectClient
View source
class OpenIDConnectFacebookClient extends OpenIDConnectClientBase {

  /**
   * Facebook API versions.
   *
   * @var array
   */
  protected $versions = [
    'v2.12',
    'v2.11',
    'v2.10',
    'v2.9',
    'v2.8',
    'v2.7',
    'v2.6',
    'v2.5',
    'v2.4',
    'v2.3',
  ];

  /**
   * Facebook fields.
   *
   * @var array
   */
  protected $fields = [
    'id',
    'name',
    'email',
    'first_name',
    'last_name',
    'gender',
    'locale',
    'timezone',
    'picture',
  ];

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'api_version' => '',
    ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['api_version'] = [
      '#title' => $this
        ->t('API Version'),
      '#type' => 'select',
      '#options' => array_combine($this->versions, $this->versions),
      '#default_value' => $this->configuration['api_version'],
    ];
    $url = 'https://developers.facebook.com/apps/';
    $form['description'] = [
      '#markup' => '<div class="description">' . $this
        ->t('Set up your app in <a href="@url" target="_blank">my apps</a> on Facebook.', [
        '@url' => $url,
      ]) . '</div>',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getEndpoints() {
    return [
      'authorization' => 'https://www.facebook.com/' . $this->configuration['api_version'] . '/dialog/oauth',
      'token' => 'https://graph.facebook.com/' . $this->configuration['api_version'] . '/oauth/access_token',
      'userinfo' => 'https://graph.facebook.com/' . $this->configuration['api_version'] . '/me',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function authorize($scope = 'openid email') {

    // Use Facebook specific authorisations.
    return parent::authorize('public_profile email');
  }

  /**
   * {@inheritdoc}
   */
  public function decodeIdToken($id_token) {
    return [];
  }

  /**
   * Implements OpenIDConnectClientInterface::retrieveUserInfo().
   *
   * @param string $access_token
   *   An access token string.
   *
   * @return array|bool
   *   A result array or false.
   */
  public function retrieveUserInfo($access_token) {
    $request_options = [
      'query' => [
        'access_token' => $access_token,
        'fields' => implode(',', $this->fields),
      ],
      'headers' => [
        'Accept' => 'application/json',
      ],
    ];
    $endpoints = $this
      ->getEndpoints();

    /** @var \GuzzleHttp\Client $client */
    $client = $this->httpClient;
    try {
      $response = $client
        ->get($endpoints['userinfo'], $request_options);
      $response_data = (string) $response
        ->getBody();
      $userinfo = json_decode($response_data, TRUE);
      $userinfo['sub'] = $userinfo['id'];
      if (!empty($userinfo['picture']['data']['url'])) {
        $userinfo['picture'] = $userinfo['picture']['data']['url'];
      }
      return $userinfo;
    } catch (\Exception $e) {
      $variables = [
        '@message' => 'Could not retrieve user profile information',
        '@error_message' => $e
          ->getMessage(),
      ];
      $this->loggerFactory
        ->get('openid_connect_' . $this->pluginId)
        ->error('@message. Details: @error_message', $variables);
      return FALSE;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OpenIDConnectClientBase::$clientScopes protected property The minimum set of scopes for this client.
OpenIDConnectClientBase::$dateTime protected property The datetime.time service.
OpenIDConnectClientBase::$httpClient protected property The HTTP client to fetch the feed data with.
OpenIDConnectClientBase::$languageManager protected property The language manager.
OpenIDConnectClientBase::$loggerFactory protected property The logger factory used for logging.
OpenIDConnectClientBase::$pageCacheKillSwitch protected property Page cache kill switch.
OpenIDConnectClientBase::$requestStack protected property The request stack used to access request globals.
OpenIDConnectClientBase::$stateToken protected property The OpenID state token service.
OpenIDConnectClientBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
OpenIDConnectClientBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
OpenIDConnectClientBase::getClientScopes public function Gets an array of of scopes. Overrides OpenIDConnectClientInterface::getClientScopes
OpenIDConnectClientBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
OpenIDConnectClientBase::getRedirectUrl protected function Returns the redirect URL.
OpenIDConnectClientBase::getRequestOptions protected function Helper function for request options.
OpenIDConnectClientBase::getUrlOptions protected function Helper function for URL options.
OpenIDConnectClientBase::retrieveTokens public function Retrieve access token and ID token. Overrides OpenIDConnectClientInterface::retrieveTokens
OpenIDConnectClientBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
OpenIDConnectClientBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
OpenIDConnectClientBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
OpenIDConnectClientBase::__construct public function The constructor. Overrides PluginBase::__construct
OpenIDConnectFacebookClient::$fields protected property Facebook fields.
OpenIDConnectFacebookClient::$versions protected property Facebook API versions.
OpenIDConnectFacebookClient::authorize public function Redirects the user to the authorization endpoint. Overrides OpenIDConnectClientBase::authorize
OpenIDConnectFacebookClient::buildConfigurationForm public function Form constructor. Overrides OpenIDConnectClientBase::buildConfigurationForm
OpenIDConnectFacebookClient::decodeIdToken public function Decodes ID token to access user data. Overrides OpenIDConnectClientBase::decodeIdToken
OpenIDConnectFacebookClient::defaultConfiguration public function Gets default configuration for this plugin. Overrides OpenIDConnectClientBase::defaultConfiguration
OpenIDConnectFacebookClient::getEndpoints public function Returns an array of endpoints. Overrides OpenIDConnectClientInterface::getEndpoints
OpenIDConnectFacebookClient::retrieveUserInfo public function Implements OpenIDConnectClientInterface::retrieveUserInfo(). Overrides OpenIDConnectClientBase::retrieveUserInfo
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.