You are here

class LinkedInAuthManager in Social Auth LinkedIn 3.x

Same name and namespace in other branches
  1. 8.2 src/LinkedInAuthManager.php \Drupal\social_auth_linkedin\LinkedInAuthManager
  2. 8 src/LinkedinAuthManager.php \Drupal\social_auth_linkedin\LinkedinAuthManager

Contains all the logic for LinkedIn OAuth2 authentication.

Hierarchy

Expanded class hierarchy of LinkedInAuthManager

1 file declares its use of LinkedInAuthManager
LinkedInAuthController.php in src/Controller/LinkedInAuthController.php
1 string reference to 'LinkedInAuthManager'
social_auth_linkedin.services.yml in ./social_auth_linkedin.services.yml
social_auth_linkedin.services.yml
1 service uses LinkedInAuthManager
social_auth_linkedin.manager in ./social_auth_linkedin.services.yml
Drupal\social_auth_linkedin\LinkedInAuthManager

File

src/LinkedInAuthManager.php, line 14

Namespace

Drupal\social_auth_linkedin
View source
class LinkedInAuthManager extends OAuth2Manager {

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactory $configFactory
   *   Used for accessing configuration object factory.
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
   *   The logger factory.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   Used to get the authorization code from the callback request.
   */
  public function __construct(ConfigFactory $configFactory, LoggerChannelFactoryInterface $logger_factory, RequestStack $request_stack) {
    parent::__construct($configFactory
      ->get('social_auth_linkedin.settings'), $logger_factory, $this->request = $request_stack
      ->getCurrentRequest());
  }

  /**
   * {@inheritdoc}
   */
  public function authenticate() {
    try {
      $this
        ->setAccessToken($this->client
        ->getAccessToken('authorization_code', [
        'code' => $this->request->query
          ->get('code'),
      ]));
    } catch (IdentityProviderException $e) {
      $this->loggerFactory
        ->get('social_auth_linkedin')
        ->error('There was an error during authentication. Exception: ' . $e
        ->getMessage());
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getUserInfo() {
    if (!$this->user) {
      $this->user = $this->client
        ->getResourceOwner($this
        ->getAccessToken());
    }
    return $this->user;
  }

  /**
   * {@inheritdoc}
   */
  public function getAuthorizationUrl() {
    $scopes = [
      'r_liteprofile',
      'r_emailaddress',
    ];
    $extra_scopes = $this
      ->getScopes();
    if ($extra_scopes) {
      $scopes = array_merge($scopes, explode(',', $extra_scopes));
    }

    // Returns the URL where user will be redirected.
    return $this->client
      ->getAuthorizationUrl([
      'scope' => $scopes,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function requestEndPoint($method, $path, $domain = NULL, array $options = []) {
    if (!$domain) {
      $domain = 'https://api.linkedin.com';
    }
    $url = $domain . $path;
    $request = $this->client
      ->getAuthenticatedRequest($method, $url, $this
      ->getAccessToken(), $options);
    try {
      return $this->client
        ->getParsedResponse($request);
    } catch (IdentityProviderException $e) {
      $this->loggerFactory
        ->get('social_auth_linkedin')
        ->error('There was an error when requesting ' . $url . '. Exception: ' . $e
        ->getMessage());
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getState() {
    return $this->client
      ->getState();
  }

  /**
   * Gets the email address.
   *
   * @return string
   *   The user email address.
   */
  public function getEmail() {
    return $this->client
      ->getResourceOwnerEmail($this
      ->getAccessToken());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LinkedInAuthManager::authenticate public function Authenticates the user. Overrides OAuth2ManagerInterface::authenticate
LinkedInAuthManager::getAuthorizationUrl public function Returns the authorization URL where user will be redirected. Overrides OAuth2ManagerInterface::getAuthorizationUrl
LinkedInAuthManager::getEmail public function Gets the email address.
LinkedInAuthManager::getState public function Returns OAuth2 state. Overrides OAuth2ManagerInterface::getState
LinkedInAuthManager::getUserInfo public function Gets data about the user. Overrides OAuth2ManagerInterface::getUserInfo
LinkedInAuthManager::requestEndPoint public function Request and end point. Overrides OAuth2ManagerInterface::requestEndPoint
LinkedInAuthManager::__construct public function Constructor. Overrides OAuth2Manager::__construct
OAuth2Manager::$accessToken protected property Access token for OAuth2 authentication.
OAuth2Manager::$client protected property The service client.
OAuth2Manager::$endPoints protected property The end points to be requested.
OAuth2Manager::$loggerFactory protected property The logger factory.
OAuth2Manager::$request protected property The current request.
OAuth2Manager::$scopes protected property The scopes to be requested.
OAuth2Manager::$settings protected property Social Auth implementer settings.
OAuth2Manager::$user protected property The user returned by the provider. Overrides OAuth2Manager::$user
OAuth2Manager::getAccessToken public function Gets the access token after authentication. Overrides OAuth2ManagerInterface::getAccessToken
OAuth2Manager::getClient public function Gets the service client object. Overrides OAuth2ManagerInterface::getClient
OAuth2Manager::getEndPoints public function Gets the API endpoints to be requested. Overrides OAuth2ManagerInterface::getEndPoints
OAuth2Manager::getExtraDetails public function Request data from the declared endpoints. Overrides OAuth2ManagerInterface::getExtraDetails
OAuth2Manager::getScopes public function Gets the scopes defined in the settings form. Overrides OAuth2ManagerInterface::getScopes
OAuth2Manager::setAccessToken public function Sets the access token. Overrides OAuth2ManagerInterface::setAccessToken
OAuth2Manager::setClient public function Sets the provider client. Overrides OAuth2ManagerInterface::setClient