You are here

class LinkedInAuthManager in Social Auth LinkedIn 8.2

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

Contains all the logic for LinkedIn OAuth2 authentication.

Hierarchy

  • class \Drupal\social_auth_linkedin\LinkedInAuthManager extends \Drupal\social_auth\AuthManager\OAuth2Manager

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 13

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.
   */
  public function __construct(ConfigFactory $configFactory, LoggerChannelFactoryInterface $logger_factory) {
    parent::__construct($configFactory
      ->get('social_auth_linkedin.settings'), $logger_factory);
  }

  /**
   * {@inheritdoc}
   */
  public function authenticate() {
    try {
      $this
        ->setAccessToken($this->client
        ->getAccessToken('authorization_code', [
        'code' => $_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