class OpenIDConnectLinkedinClient in OpenID Connect / OAuth client 2.x
Same name and namespace in other branches
- 8 src/Plugin/OpenIDConnectClient/OpenIDConnectLinkedinClient.php \Drupal\openid_connect\Plugin\OpenIDConnectClient\OpenIDConnectLinkedinClient
LinkedIn OpenID Connect client.
Implements OpenID Connect Client plugin for LinkedIn.
Plugin annotation
@OpenIDConnectClient(
id = "linkedin",
label = @Translation("LinkedIn")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\openid_connect\Plugin\OpenIDConnectClientBase implements ContainerFactoryPluginInterface, OpenIDConnectClientInterface uses PluginWithFormsTrait, StringTranslationTrait
- class \Drupal\openid_connect\Plugin\OpenIDConnectClient\OpenIDConnectLinkedinClient
- class \Drupal\openid_connect\Plugin\OpenIDConnectClientBase implements ContainerFactoryPluginInterface, OpenIDConnectClientInterface uses PluginWithFormsTrait, StringTranslationTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of OpenIDConnectLinkedinClient
File
- src/
Plugin/ OpenIDConnectClient/ OpenIDConnectLinkedinClient.php, line 20
Namespace
Drupal\openid_connect\Plugin\OpenIDConnectClientView source
class OpenIDConnectLinkedinClient extends OpenIDConnectClientBase {
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) : array {
$form = parent::buildConfigurationForm($form, $form_state);
$url = 'https://www.linkedin.com/developer/apps';
$form['description'] = [
'#markup' => '<div class="description">' . $this
->t('Set up your app in <a href="@url" target="_blank">my apps</a> on LinkedIn.', [
'@url' => $url,
]) . '</div>',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function getEndpoints() : array {
return [
'authorization' => 'https://www.linkedin.com/oauth/v2/authorization',
'token' => 'https://www.linkedin.com/oauth/v2/accessToken',
'userinfo' => 'https://api.linkedin.com/v2/me?projection=(id,localizedFirstName,localizedLastName,profilePicture(displayImage~:playableStreams))',
'useremail' => 'https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))',
];
}
/**
* {@inheritdoc}
*/
public function authorize(string $scope = 'openid email') : Response {
// Use LinkedIn specific authorisations.
return parent::authorize('r_liteprofile r_emailaddress');
}
/**
* {@inheritdoc}
*/
public function retrieveUserInfo(string $access_token) : ?array {
$userinfo = [];
$info = parent::retrieveUserInfo($access_token);
if ($info) {
$userinfo['sub'] = isset($info['id']) ? $info['id'] : '';
$userinfo['first_name'] = isset($info['localizedFirstName']) ? $info['localizedFirstName'] : '';
$userinfo['last_name'] = isset($info['localizedLastName']) ? $info['localizedLastName'] : '';
$userinfo['name'] = $userinfo['first_name'] . ' ' . $userinfo['last_name'];
if (isset($info['profilePicture']['displayImage~']['elements'])) {
// The picture was provided.
$pictures = $info['profilePicture']['displayImage~']['elements'];
// The last picture should have the largest picture of size 800x800 px.
$last_picture = end($pictures);
if (isset($last_picture['identifiers'][0]['identifier'])) {
$userinfo['picture'] = $last_picture['identifiers'][0]['identifier'];
}
}
else {
// The picture was not provided.
$userinfo['picture'] = '';
}
}
// Get the email. It should always be provided.
if ($email = $this
->retrieveUserEmail($access_token)) {
$userinfo['email'] = $email;
}
return $userinfo;
}
/**
* Get user email.
*
* @param string $access_token
* An access token string.
*
* @return string|null
* An email or null.
*/
protected function retrieveUserEmail(string $access_token) : ?string {
$request_options = [
'headers' => [
'Authorization' => 'Bearer ' . $access_token,
'Accept' => 'application/json',
],
];
$endpoints = $this
->getEndpoints();
try {
$response = $this->httpClient
->get($endpoints['useremail'], $request_options);
$object = Json::decode((string) $response
->getBody());
if (isset($object['elements'])) {
foreach ($object['elements'] as $element) {
if (isset($element['handle~']['emailAddress'])) {
// The email address was found.
return $element['handle~']['emailAddress'];
}
}
}
} catch (\Exception $e) {
$variables = [
'@message' => 'Could not retrieve user email information',
'@error_message' => $e
->getMessage(),
];
$this->loggerFactory
->get('openid_connect_' . $this->pluginId)
->error('@message. Details: @error_message', $variables);
}
// No email address was provided.
return NULL;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
OpenIDConnectClientBase:: |
protected | property | The OpenID well-known discovery service. | |
OpenIDConnectClientBase:: |
protected | property | The datetime.time service. | |
OpenIDConnectClientBase:: |
protected | property | The HTTP client to fetch the feed data with. | |
OpenIDConnectClientBase:: |
protected | property | The language manager. | |
OpenIDConnectClientBase:: |
protected | property | The logger factory used for logging. | |
OpenIDConnectClientBase:: |
protected | property | Page cache kill switch. | |
OpenIDConnectClientBase:: |
protected | property | The parent entity identifier. | |
OpenIDConnectClientBase:: |
protected | property | The request stack used to access request globals. | |
OpenIDConnectClientBase:: |
protected | property | The OpenID state token service. | |
OpenIDConnectClientBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
OpenIDConnectClientBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
OpenIDConnectClientBase:: |
public | function |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
3 |
OpenIDConnectClientBase:: |
public | function |
Gets an array of of scopes. Overrides OpenIDConnectClientInterface:: |
2 |
OpenIDConnectClientBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
OpenIDConnectClientBase:: |
public | function |
Return the plugin label as defined in the annotation. Overrides OpenIDConnectClientInterface:: |
|
OpenIDConnectClientBase:: |
public | function |
Returns the parent entity ID. Overrides OpenIDConnectClientInterface:: |
|
OpenIDConnectClientBase:: |
protected | function | Returns the redirect URL. | |
OpenIDConnectClientBase:: |
protected | function | Helper function for request options. | |
OpenIDConnectClientBase:: |
protected | function | Helper function for URL options. | |
OpenIDConnectClientBase:: |
public | function |
Retrieve access token and ID token. Overrides OpenIDConnectClientInterface:: |
|
OpenIDConnectClientBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
OpenIDConnectClientBase:: |
public | function |
Sets the parent entity ID. Overrides OpenIDConnectClientInterface:: |
|
OpenIDConnectClientBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
2 |
OpenIDConnectClientBase:: |
protected | function | Unsets some elements of the configuration. | |
OpenIDConnectClientBase:: |
public | function |
Check if the client uses the userinfo endpoint. Overrides OpenIDConnectClientInterface:: |
|
OpenIDConnectClientBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
1 |
OpenIDConnectClientBase:: |
public | function |
The constructor. Overrides PluginBase:: |
|
OpenIDConnectLinkedinClient:: |
public | function |
Redirects the user to the authorization endpoint. Overrides OpenIDConnectClientBase:: |
|
OpenIDConnectLinkedinClient:: |
public | function |
Form constructor. Overrides OpenIDConnectClientBase:: |
|
OpenIDConnectLinkedinClient:: |
public | function |
Returns an array of endpoints. Overrides OpenIDConnectClientInterface:: |
|
OpenIDConnectLinkedinClient:: |
protected | function | Get user email. | |
OpenIDConnectLinkedinClient:: |
public | function |
Retrieves user info: additional user profile data. Overrides OpenIDConnectClientBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginWithFormsTrait:: |
public | function | Implements \Drupal\Core\Plugin\PluginWithFormsInterface::getFormClass(). | |
PluginWithFormsTrait:: |
public | function | Implements \Drupal\Core\Plugin\PluginWithFormsInterface::hasFormClass(). | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |