forked from opauth/linkedin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedInStrategy.php
238 lines (197 loc) · 7.68 KB
/
LinkedInStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
* LinkedIn strategy for Opauth
* based on https://developer.linkedin.com/documents/authentication
*
* More information on Opauth: http://opauth.org
*
* @copyright Copyright © 2012 U-Zyn Chua (http://uzyn.com)
* @link http://opauth.org
* @package Opauth.LinkedInStrategy
* @license MIT License
*/
/**
* LinkedIn strategy for Opauth
* based on https://developer.linkedin.com/documents/authentication
*
* @package Opauth.LinkedIn
*/
class LinkedInStrategy extends OpauthStrategy{
/**
* Compulsory config keys, listed as unassociative arrays
*/
public $expects = array('api_key', 'secret_key');
/**
* Optional config keys, without predefining any default values.
*/
public $optionals = array();
/**
* Optional config keys with respective default values, listed as associative arrays
* eg. array('scope' => 'email');
*/
public $defaults = array(
'method' => 'POST', // The HTTP method being used. e.g. POST, GET, HEAD etc
'oauth_callback' => '{complete_url_to_strategy}oauth_callback',
// For LinkedIn
'request_token_url' => 'https://api.linkedin.com/uas/oauth/requestToken',
'authorize_url' => 'https://www.linkedin.com/uas/oauth/authenticate', // or 'https://www.linkedin.com/uas/oauth/authorize'
'access_token_url' => 'https://api.linkedin.com/uas/oauth/accessToken',
'get_profile_url' => 'http://api.linkedin.com/v1/people/~',
'profile_fields' => array('id', 'first-name', 'last-name', 'formatted-name', 'headline', 'picture-url', 'summary', 'location', 'public-profile-url', 'site-standard-profile-request'),
// From tmhOAuth
'user_token' => '',
'user_secret' => '',
'use_ssl' => true,
'debug' => false,
'force_nonce' => false,
'nonce' => false, // used for checking signatures. leave as false for auto
'force_timestamp' => false,
'timestamp' => false, // used for checking signatures. leave as false for auto
'oauth_version' => '1.0',
'curl_connecttimeout' => 30,
'curl_timeout' => 10,
'curl_ssl_verifypeer' => false,
'curl_followlocation' => false, // whether to follow redirects or not
'curl_proxy' => false, // really you don't want to use this if you are using streaming
'curl_proxyuserpwd' => false, // format username:password for proxy, if required
'is_streaming' => false,
'streaming_eol' => "\r\n",
'streaming_metrics_interval' => 60,
'as_header' => true,
);
public function __construct($strategy, $env){
parent::__construct($strategy, $env);
$this->strategy['consumer_key'] = $this->strategy['api_key'];
$this->strategy['consumer_secret'] = $this->strategy['secret_key'];
require dirname(__FILE__).'/Vendor/tmhOAuth/tmhOAuth.php';
$this->tmhOAuth = new tmhOAuth($this->strategy);
}
/**
* Auth request
*/
public function request(){
$params = array(
'oauth_callback' => $this->strategy['oauth_callback'],
'scope' => $this->strategy['scope'],
);
$results = $this->_request('POST', $this->strategy['request_token_url'], $params);
if ($results !== false && !empty($results['oauth_token']) && !empty($results['oauth_token_secret'])){
session_start();
$_SESSION['_opauth_linkedin'] = $results;
$this->_authorize($results['oauth_token']);
}
}
/**
* Receives oauth_verifier, requests for access_token and redirect to callback
*/
public function oauth_callback(){
session_start();
$session = $_SESSION['_opauth_linkedin'];
unset($_SESSION['_opauth_linkedin']);
if ($_REQUEST['oauth_token'] == $session['oauth_token']){
$this->tmhOAuth->config['user_token'] = $session['oauth_token'];
$this->tmhOAuth->config['user_secret'] = $session['oauth_token_secret'];
$params = array(
'oauth_verifier' => $_REQUEST['oauth_verifier']
);
$results = $this->_request('POST', $this->strategy['access_token_url'], $params);
if ($results !== false && !empty($results['oauth_token']) && !empty($results['oauth_token_secret'])){
$profile = $this->_getProfile($results['oauth_token'], $results['oauth_token_secret']);
if (!empty($profile['id'])){
$this->auth = array(
'uid' => $profile['id'],
'info' => array(),
'credentials' => array(
'token' => $results['oauth_token'],
'secret' => $results['oauth_token_secret']
),
'raw' => $profile
);
$this->mapProfile($profile, 'formatted-name', 'info.name');
$this->mapProfile($profile, 'first-name', 'info.first_name');
$this->mapProfile($profile, 'last-name', 'info.last_name');
$this->mapProfile($profile, 'headline', 'info.headline');
$this->mapProfile($profile, 'summary', 'info.description');
$this->mapProfile($profile, 'location.name', 'info.location');
$this->mapProfile($profile, 'picture-url', 'info.image');
$this->mapProfile($profile, 'public-profile-url', 'info.urls.linkedin');
$this->mapProfile($profile, 'site-standard-profile-request.url', 'info.urls.linkedin_authenticated');
$this->callback();
}
}
else{
$error = array(
'code' => 'oauth_token_expected',
'message' => 'OAuth token and secret expected.',
'raw' => $results
);
$this->errorCallback($error);
}
}
else{
$error = array(
'code' => 'access_denied',
'message' => 'User denied access.',
'raw' => $_GET
);
$this->errorCallback($error);
}
}
private function _authorize($oauth_token){
$params = array(
'oauth_token' => $oauth_token
);
$this->clientGet($this->strategy['authorize_url'], $params);
}
private function _getProfile($user_token, $user_token_secret){
$this->tmhOAuth->config['user_token'] = $user_token;
$this->tmhOAuth->config['user_secret'] = $user_token_secret;
$url = $this->strategy['get_profile_url'];
if (!empty($this->strategy['profile_fields'])){
if (is_array($this->strategy['profile_fields']))
$url = $url.':('.implode(',',$this->strategy['profile_fields']).')';
else
$url = $url.':('.$this->strategy['profile_fields'].')';
}
$response = $this->_request('GET', $url, array(), true, false, 'xml');
return $this->recursiveGetObjectVars($response);
}
/**
* Wrapper of tmhOAuth's request() with Opauth's error handling.
*
* request():
* Make an HTTP request using this library. This method doesn't return anything.
* Instead the response should be inspected directly.
*
* @param string $method the HTTP method being used. e.g. POST, GET, HEAD etc
* @param string $url the request URL without query string parameters
* @param array $params the request parameters as an array of key=value pairs
* @param string $useauth whether to use authentication when making the request. Default true.
* @param string $multipart whether this request contains multipart data. Default false
* @param string $hander Set to 'json' or 'xml' to parse JSON or XML-based output.
*/
private function _request($method, $url, $params = array(), $useauth = true, $multipart = false, $handler = null){
$code = $this->tmhOAuth->request($method, $url, $params, $useauth, $multipart);
if (is_null($handler)){
if (strpos($url, '.json') !== false) $handler = 'json';
elseif (strpos($url, '.xml') !== false) $handler = 'xml';
}
if ($code == 200){
if ($handler == 'json')
$response = json_decode($this->tmhOAuth->response['response']);
elseif ($handler == 'xml')
$response = simplexml_load_string($this->tmhOAuth->response['response']);
else
$response = $this->tmhOAuth->extract_params($this->tmhOAuth->response['response']);
return $response;
}
else {
$error = array(
'code' => $code,
'raw' => $this->tmhOAuth->response['response']
);
$this->errorCallback($error);
return false;
}
}
}