ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
LinkedIn.php
Go to the documentation of this file.
1 <?php
2 
3 require_once(dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/oauth/libextinc/OAuth.php');
4 
12 {
13 
17  const STAGE_INIT = 'authlinkedin:init';
18 
22  const AUTHID = 'authlinkedin:AuthId';
23 
24  private $key;
25  private $secret;
26  private $attributes;
27 
28 
35  public function __construct($info, $config)
36  {
37  assert(is_array($info));
38  assert(is_array($config));
39 
40  // Call the parent constructor first, as required by the interface
41  parent::__construct($info, $config);
42 
43  if (!array_key_exists('key', $config))
44  throw new Exception('LinkedIn authentication source is not properly configured: missing [key]');
45 
46  $this->key = $config['key'];
47 
48  if (!array_key_exists('secret', $config))
49  throw new Exception('LinkedIn authentication source is not properly configured: missing [secret]');
50 
51  $this->secret = $config['secret'];
52 
53  if (array_key_exists('attributes', $config)) {
54  $this->attributes = $config['attributes'];
55  } else {
56  // Default values if the attributes are not set in config (ref https://developer.linkedin.com/docs/fields)
57  $this->attributes = 'id,first-name,last-name,headline,summary,specialties,picture-url,email-address';
58  }
59  }
60 
61 
68  public function authenticate(&$state)
69  {
70  assert(is_array($state));
71 
72  // We are going to need the authId in order to retrieve this authentication source later
73  $state[self::AUTHID] = $this->authId;
74 
76  SimpleSAML\Logger::debug('authlinkedin auth state id = ' . $stateID);
77 
78  $consumer = new sspmod_oauth_Consumer($this->key, $this->secret);
79 
80  // Get the request token
81  $requestToken = $consumer->getRequestToken(
82  'https://api.linkedin.com/uas/oauth/requestToken',
83  array('oauth_callback' => SimpleSAML\Module::getModuleUrl('authlinkedin') . '/linkback.php?stateid=' . $stateID)
84  );
85 
87  "Got a request token from the OAuth service provider [" .
88  $requestToken->key . "] with the secret [" . $requestToken->secret . "]"
89  );
90 
91  $state['authlinkedin:requestToken'] = $requestToken;
92 
93  // Update the state
94  SimpleSAML_Auth_State::saveState($state, self::STAGE_INIT);
95 
96  // Authorize the request token
97  $consumer->getAuthorizeRequest('https://www.linkedin.com/uas/oauth/authenticate', $requestToken);
98  }
99 
100 
101  public function finalStep(&$state)
102  {
103  $requestToken = $state['authlinkedin:requestToken'];
104 
105  $consumer = new sspmod_oauth_Consumer($this->key, $this->secret);
106 
108  "oauth: Using this request token [" .
109  $requestToken->key . "] with the secret [" . $requestToken->secret . "]"
110  );
111 
112  // Replace the request token with an access token (via GET method)
113  $accessToken = $consumer->getAccessToken(
114  'https://api.linkedin.com/uas/oauth/accessToken', $requestToken,
115  array('oauth_verifier' => $state['authlinkedin:oauth_verifier'])
116  );
117 
119  "Got an access token from the OAuth service provider [" .
120  $accessToken->key . "] with the secret [" . $accessToken->secret . "]"
121  );
122 
123  $userdata = $consumer->getUserInfo(
124  'https://api.linkedin.com/v1/people/~:(' . $this->attributes . ')',
125  $accessToken,
126  array('http' => array('header' => 'x-li-format: json'))
127  );
128 
129  $attributes = $this->flatten($userdata, 'linkedin.');
130 
131  // TODO: pass accessToken: key, secret + expiry as attributes?
132 
133  if (array_key_exists('id', $userdata)) {
134  $attributes['linkedin_targetedID'] = array('http://linkedin.com!' . $userdata['id']);
135  $attributes['linkedin_user'] = array($userdata['id'] . '@linkedin.com');
136  }
137 
138  SimpleSAML\Logger::debug('LinkedIn Returned Attributes: '. implode(", ",array_keys($attributes)));
139 
140  $state['Attributes'] = $attributes;
141  }
142 
170  protected function flatten($array, $prefix = '')
171  {
172  $result = array();
173  foreach ($array as $key => $value) {
174  if (is_array($value)) {
175  $result = $result + $this->flatten($value, $prefix . $key . '.');
176  } else {
177  $result[$prefix . $key] = array($value);
178  }
179  }
180  return $result;
181  }
182 }
static getStateId(&$state, $rawId=false)
Retrieve the ID of a state array.
Definition: State.php:145
$config
Definition: bootstrap.php:15
$result
static debug($string)
Definition: Logger.php:211
__construct($info, $config)
Constructor for this authentication source.
Definition: LinkedIn.php:35
if(!array_key_exists('stateid', $_REQUEST)) $state
Handle linkback() response from LinkedIn.
Definition: linkback.php:10
Attribute-related utility methods.
flatten($array, $prefix='')
takes an associative array, traverses it and returns the keys concatenated with a dot ...
Definition: LinkedIn.php:170
const AUTHID
The key of the AuthId field in the state.
Definition: LinkedIn.php:22
authenticate(&$state)
Log-in using LinkedIn platform Documentation at: http://developer.linkedin.com/docs/DOC-1008.
Definition: LinkedIn.php:68
$info
Definition: index.php:5
static saveState(&$state, $stage, $rawId=false)
Save the state.
Definition: State.php:194
const STAGE_INIT
The string used to identify our states.
Definition: LinkedIn.php:17