-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinkedin.php
135 lines (95 loc) · 4.36 KB
/
linkedin.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
<?php
namespace aw2\linkedin;
\aw2_library::add_service('linkedin','linkedin api support',['namespace'=>__NAMESPACE__]);
\aw2_library::add_service('linkedin.login_url','returns the login URL for linkedin',['namespace'=>__NAMESPACE__]);
function login_url($atts,$content=null,$shortcode){
if(\aw2_library::pre_actions('all',$atts,$content)==false)return;
extract(\aw2_library::shortcode_atts( array(
'ticket_id'=>null,
'scope'=>null,
'app_id'=>null,
'app_secret'=>null
), $atts) );
$redirect_url = SITE_URL.'?social_auth=linkedin';
if(empty($ticket_id)) return '';
if(empty($app_id) || empty($app_secret)) return '';
$return_value='';
$provider = new \League\OAuth2\Client\Provider\LinkedIn([
'clientId' => $app_id,
'clientSecret' => $app_secret,
'redirectUri' => $redirect_url,
]);
$scope = \aw2\session_ticket\get(["main"=>$ticket_id,"field"=>'scope'],null,null);
//$scopes= explode(',',$scope);
$options = [
'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE',
'scope' => $scope // array or string
];
$authorizationUrl = $provider->getAuthorizationUrl($options);
$return_value = $provider->getAuthorizationUrl($options);
\aw2\session_ticket\set(["main"=>$ticket_id,"field"=>'state',"value"=>$provider->getState()],null,null); // save state for future validation
\aw2\session_ticket\set(["main"=>$ticket_id,"field"=>'redirect_url',"value"=>$redirect_url],null,null); // save state for future validation
$return_value=\aw2_library::post_actions('all',$return_value,$atts);
return $return_value;
}
\aw2_library::add_service('linkedin.auth','Check the auth for linkedin',['namespace'=>__NAMESPACE__]);
function auth($atts,$content=null,$shortcode){
if(\aw2_library::pre_actions('all',$atts,$content)==false)return;
extract(\aw2_library::shortcode_atts( array(
'ticket_id'=>null,
'scope'=>null,
'app_id'=>null,
'app_secret'=>null
), $atts) );
$redirect_url = SITE_URL.'?social_auth=linkedin';
if(empty($ticket_id)) return '';
if(empty($app_id) || empty($app_secret)) return '';
if (isset($_GET['error']) || !isset($_GET['code'])) {
\aw2\session_ticket\set(["main"=>$ticket_id,"field"=>'status',"value"=>'error'],null,null);
\aw2\session_ticket\set(["main"=>$ticket_id,"field"=>'description',"value"=>$_REQUEST['error_description']],null,null);
return;
}
$provider = new \League\OAuth2\Client\Provider\LinkedIn([
'clientId' => $app_id,
'clientSecret' => $app_secret,
'redirectUri' => $redirect_url,
]);
try {
// you have to set initially used redirect url to be able
// to retrieve access token
// retrieve access token using code provided by LinkedIn
$accessToken = $provider->getAccessToken('authorization_code', [
'code' => $_GET['code']
]);
\aw2\session_ticket\set(["main"=>$ticket_id,"field"=>'access_token',"value"=>$accessToken],null,null);
$user = $provider->getResourceOwner($accessToken);
$return_value['firstName'] = $user->getFirstName();
$return_value['lastName'] = $user->getLastName();
$return_value['id'] = $user->getId();
$return_value['emailAddress'] = $user->getEmail();
$return_value['publicProfileUrl'] = $user->getUrl();
$return_value['pictureUrl'] = $user->getImageUrl();
} catch (Exception $exception) {
// in case of failure, provide with details
\aw2\session_ticket\set(["main"=>$ticket_id,"field"=>'status',"value"=>'error'],null,null);
\aw2\session_ticket\set(["main"=>$ticket_id,"field"=>'description',"value"=>$exception->getMessage()],null,null);
return;
}
\aw2\session_ticket\set(["main"=>$ticket_id,"field"=>'status',"value"=>'success'],null,null);
$return_value=\aw2_library::post_actions('all',$return_value,$atts);
return $return_value;
}
if(IS_WP){
\add_action( 'wp', 'aw2\linkedin\auth_check', 11 );
function auth_check(){
if(!isset($_REQUEST['social_auth'])) return;
if($_REQUEST['social_auth'] !== 'linkedin') return;
$ticket_id = $_COOKIE['linkedin_login'];
$query_string=explode('&',$_SERVER["QUERY_STRING"]);
array_shift($query_string);
$query_string = implode('&',$query_string);
$app_path = \aw2\session_ticket\get(["main"=>$ticket_id,"field"=>'app_path'],null,null);
wp_redirect($app_path.'/t/'.$ticket_id.'?'.$query_string);
die();
}
}