-
Notifications
You must be signed in to change notification settings - Fork 2
/
EveSSO.php
227 lines (180 loc) · 9.56 KB
/
EveSSO.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
<?php
/*!
* HybridAuth
* http://hybridauth.sourceforge.net | https://github.com/hybridauth/hybridauth
* (c) 2009-2012 HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html
*/
/**
* Hybrid_Providers_EveSSO (By Steve Anderson)
*/
class Hybrid_Providers_EveSSO extends Hybrid_Provider_Model_OAuth2
{
// default permissions
public $scope = "";
/**
* IDP wrappers initializer
*/
function initialize()
{
parent::initialize();
// Provider api end-points
$this->api->api_base_url = "https://login.eveonline.com/oauth/";
$this->api->authorize_url = "https://login.eveonline.com/oauth/authorize";
$this->api->token_url = "https://login.eveonline.com/oauth/token";
}
function loginFinish()
{
$error = (array_key_exists('error',$_REQUEST))?$_REQUEST['error']:"";
// check for errors
if ( $error ){
throw new Exception( "Authentication failed! {$this->providerId} returned an error: " . htmlentities( $error ), 5 );
}
// try to authenticate user
$code = (array_key_exists('code',$_REQUEST))?$_REQUEST['code']:"";
try{
$this->authenticate( $code );
}
catch( Exception $e ){
throw new Exception( "Authentication failed! {$this->providerId} returned an error", 5 );
}
// check if authenticated
if ( ! $this->api->access_token ){
throw new Exception( "Authentication failed! {$this->providerId} returned an invalid access_token", 5 );
}
// store tokens
$this->token( "access_token" , $this->api->access_token );
$this->token( "refresh_token", $this->api->refresh_token );
$this->token( "expires_in" , $this->api->access_token_expires_in );
$this->token( "expires_at" , $this->api->access_token_expires_at );
// set user connected locally
$this->setUserConnected();
}
function authenticate( $code )
{
error_log(getcwd());
require( '../../../../wp-load.php' );
$params = array(
"client_id" => $this->api->client_id,
"grant_type" => "authorization_code",
"redirect_uri" => $this->api->redirect_uri,
"code" => $code
);
$http_headers = array();
$http_headers['Authorization'] = 'Basic ' . base64_encode( $this->api->client_id . ':' . $this->api->client_secret);
$response = $this->request( $this->api->token_url, http_build_query($params, '', '&'), 'POST', $http_headers );
$response = $this->parseRequestResult( $response );
if( ! $response || ! isset( $response->access_token ) ){
throw new Exception( "The Authorization Service has return: " . $response->error );
}
if( isset( $response->access_token ) ) $this->api->access_token = $response->access_token;
if( isset( $response->refresh_token ) ) $this->api->refresh_token = $response->refresh_token;
if( isset( $response->expires_in ) ) $this->api->access_token_expires_in = $response->expires_in;
// calculate when the access token expire
if( isset( $response->expires_in ) ) {
$this->api->access_token_expires_at = time() + $response->expires_in;
}
else {
$this->api->access_token_expires_at = time() + 3600;
}
// Get the various character details.
$http_headers = array();
$http_headers['Authorization'] = 'Bearer ' . $this->api->access_token;
$restriction = $this->request( $this->api->api_base_url."/verify", array(), 'GET', $http_headers );
$restriction = $this->parseRequestResult( $restriction );
$characterid = $restriction->CharacterID;
$restriction = $this->request( "https://esi.evetech.net/latest/characters/".$characterid."/", array(), 'GET', $http_headers );
$restriction = $this->parseRequestResult( $restriction );
$allowed_corporations = str_replace(' ','',get_option("allowed_corporations",""));
$allowed_alliances = str_replace(' ','',get_option("allowed_alliances",""));
$allowed_characters = str_replace(' ','',get_option("allowed_characters",""));
$allallowed=get_option("allowed_all",true);
$allowed_corporations=explode(",",$allowed_corporations);
$allowed_alliances=explode(",",$allowed_alliances);
$allowed_characters=explode(",",$allowed_characters);
$allowed=0;
if ($allallowed) {
$allowed=1;
} elseif (in_array($characterid, $allowed_characters)) {
$allowed=2;
} elseif (isset($restriction->alliance_id) && in_array($restriction->alliance_id, $allowed_alliances)) {
$allowed=3;
} elseif (in_array($restriction->corporation_id, $allowed_corporations)) {
$allowed=4;
}
if ($allowed == 0) {
throw new Exception( "The Character is not in a permitted list" );
}
return $response;
}
private function request( $url, $params = array(), $type="GET", $http_headers = null )
{
if( $type == "GET" ){
$url = $url . ( strpos( $url, '?' ) ? '&' : '?' ) . http_build_query($params, '', '&');
}
$this->http_info = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL , $url );
curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1 );
curl_setopt($ch, CURLOPT_TIMEOUT , $this->api->curl_time_out );
curl_setopt($ch, CURLOPT_USERAGENT , $this->api->curl_useragent );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , $this->api->curl_connect_time_out );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , $this->api->curl_ssl_verifypeer );
curl_setopt($ch, CURLOPT_HTTPHEADER , $this->api->curl_header );
if (is_array($http_headers)) {
$header = array();
foreach($http_headers as $key => $parsed_urlvalue) {
$header[] = "$key: $parsed_urlvalue";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $header );
}
else{
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->api->curl_header );
}
if($this->api->curl_proxy){
curl_setopt( $ch, CURLOPT_PROXY , $this->api->curl_proxy);
}
if( $type == "POST" ){
curl_setopt($ch, CURLOPT_POST, 1);
if($params) curl_setopt( $ch, CURLOPT_POSTFIELDS, $params );
}
$response = curl_exec($ch);
$this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->http_info = array_merge($this->http_info, curl_getinfo($ch));
curl_close ($ch);
return $response;
}
private function parseRequestResult( $result )
{
if( json_decode( $result ) ) return json_decode( $result );
parse_str( $result, $output );
$result = new StdClass();
foreach( $output as $k => $v )
$result->$k = $v;
return $result;
}
/**
* load the user profile from the IDP api client
*/
function getUserProfile(){
$http_headers = array();
$http_headers['Authorization'] = 'Bearer ' . $this->api->access_token;
$response = $this->request( $this->api->api_base_url."verify", array(), 'GET', $http_headers );
$response = $this->parseRequestResult( $response );
if ( ! $response || ! isset( $response->CharacterName ) ){
throw new Exception( "User profile request failed! {$this->providerId} returned an invalid response.", 6 );
}
$names=explode(" ",$response->CharacterName);
$lastname=array_pop($names);
$firstname=join(" ",$names);
$this->user->profile->identifier = @ $response->CharacterID;
$this->user->profile->displayName = @ $response->CharacterName;
$this->user->profile->firstName = $firstname;
$this->user->profile->lastName = $lastname;
$this->user->profile->displayName = @ $response->CharacterName;
$this->user->profile->profileURL = "https://forums.eveonline.com/profile/".urlencode($response->CharacterName);
$this->user->profile->photoURL = "https://image.eveonline.com/character/".$response->CharacterID."_128.jpg";
if( $this->user->profile->identifier ){
return $this->user->profile;
}
}
}