From a823e71ea6df9ae0ddd233594bf355c2834b0120 Mon Sep 17 00:00:00 2001
From: Michal Prochazka <michalp@ics.muni.cz>
Date: Wed, 21 Sep 2016 14:00:43 +0200
Subject: [PATCH] Configurable LinkedIn attributes (#449)

- Attributes which can be get from LinkedIn are now configurable
- By default these attributes are requested: id,first-name,last-name,headline,summary,specialties,picture-url,email-address
- Added email-address attribute into the default set
- Fixed link to the documentation
---
 config-templates/authsources.php                  |  3 +++
 modules/authlinkedin/docs/oauthlinkedin.md        |  2 +-
 modules/authlinkedin/lib/Auth/Source/LinkedIn.php | 11 +++++++++--
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/config-templates/authsources.php b/config-templates/authsources.php
index 9f09ffdcb..049cad554 100644
--- a/config-templates/authsources.php
+++ b/config-templates/authsources.php
@@ -212,10 +212,13 @@ $config = array(
     // LinkedIn OAuth Authentication API.
     // Register your application to get an API key here:
     //  https://www.linkedin.com/secure/developer
+    // Attributes definition:
+    //  https://developer.linkedin.com/docs/fields
     'linkedin' => array(
         'authlinkedin:LinkedIn',
         'key' => 'xxxxxxxxxxxxxxxx',
         'secret' => 'xxxxxxxxxxxxxxxx',
+        'attributes' => 'id,first-name,last-name,headline,summary,specialties,picture-url,email-address',
     ),
     */
 
diff --git a/modules/authlinkedin/docs/oauthlinkedin.md b/modules/authlinkedin/docs/oauthlinkedin.md
index 835935f6a..df437006f 100644
--- a/modules/authlinkedin/docs/oauthlinkedin.md
+++ b/modules/authlinkedin/docs/oauthlinkedin.md
@@ -1,7 +1,7 @@
 Using the LinkedIn authentication source with SimpleSAMLphp
 ===========================================================
 
-Remember to configure `authsources.php`, with both Consumer key and secret.
+Remember to configure `authsources.php`, with both Consumer key and secret. Optionally you can set which attributes are requested from LinkedIn, the list of available attributes can be found here <https://developer.linkedin.com/docs/fields>.
 
 To get an API key and a secret, register the application at:
 
diff --git a/modules/authlinkedin/lib/Auth/Source/LinkedIn.php b/modules/authlinkedin/lib/Auth/Source/LinkedIn.php
index e8d78511a..9525d6969 100644
--- a/modules/authlinkedin/lib/Auth/Source/LinkedIn.php
+++ b/modules/authlinkedin/lib/Auth/Source/LinkedIn.php
@@ -22,6 +22,7 @@ class sspmod_authlinkedin_Auth_Source_LinkedIn extends SimpleSAML_Auth_Source {
 
 	private $key;
 	private $secret;
+	private $attributes;
 
 
 	/**
@@ -46,6 +47,13 @@ class sspmod_authlinkedin_Auth_Source_LinkedIn extends SimpleSAML_Auth_Source {
 			throw new Exception('LinkedIn authentication source is not properly configured: missing [secret]');
 
 		$this->secret = $config['secret'];
+
+		if (array_key_exists('attributes', $config)) {
+			$this->attributes = $config['attributes'];
+		} else {
+			// Default values if the attributes are not set in config (ref https://developer.linkedin.com/docs/fields)
+			$this->attributes = 'id,first-name,last-name,headline,summary,specialties,picture-url,email-address';
+		}
 	}
 
 
@@ -97,8 +105,7 @@ class sspmod_authlinkedin_Auth_Source_LinkedIn extends SimpleSAML_Auth_Source {
 		SimpleSAML\Logger::debug("Got an access token from the OAuth service provider [" .
 			$accessToken->key . "] with the secret [" . $accessToken->secret . "]");
 
-		// TODO: configure attributes (http://developer.linkedin.com/docs/DOC-1061) from config? Limited options via LinkedIn
-		$userdata = $consumer->getUserInfo('https://api.linkedin.com/v1/people/~:(id,first-name,last-name,headline,summary,specialties,picture-url)', $accessToken, array('http' => array('header' => 'x-li-format: json')));
+		$userdata = $consumer->getUserInfo('https://api.linkedin.com/v1/people/~:(' . $this->attributes . ')', $accessToken, array('http' => array('header' => 'x-li-format: json')));
 
 		$attributes = array();
 		foreach($userdata AS $key => $value) {
-- 
GitLab