forked from fuzzysteve/eve-sso-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
devauthcallback.php
221 lines (201 loc) · 8.92 KB
/
devauthcallback.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
<?php
require_once('auth_functions.php');
require_once('secret.php');
session_start();
$useragent="Fuzzwork Auth agent.";
// Make sure that the secret matches the one set before the redirect.
if (isset($_SESSION['auth_state']) and isset($_GET['state']) and $_SESSION['auth_state']==$_GET['state']) {
$code=$_GET['code'];
$state=$_GET['state'];
//Do the initial check.
$url='https://sisilogin.testeveonline.com/oauth/token';
$verify_url='https://sisilogin.testeveonline.com/oauth/verify';
$header='Authorization: Basic '.base64_encode($clientid.':'.$secret);
$fields_string='';
$fields=array(
'grant_type' => 'authorization_code',
'code' => $code
);
foreach ($fields as $key => $value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$result = curl_exec($ch);
if ($result===false) {
auth_error(curl_error($ch));
}
curl_close($ch);
$response=json_decode($result);
$auth_token=$response->access_token;
$ch = curl_init();
// Get the Character details from SSO
$header='Authorization: Bearer '.$auth_token;
curl_setopt($ch, CURLOPT_URL, $verify_url);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$result = curl_exec($ch);
if ($result===false) {
auth_error(curl_error($ch));
}
curl_close($ch);
$response=json_decode($result);
if (!isset($response->CharacterID)) {
auth_error('No character ID returned');
}
// Lookup the character details in the DB.
require_once('db.inc.php');
$sql="select corporationname,corporationticker,user.corporationid,
alliancename,allianceticker,corporation.allianceid,characterid,characterownerhash,
user.id
from user
join corporation on user.corporationid=corporation.corporationid
join alliance on corporation.allianceid=alliance.allianceid
where
user.characterid=:characterid
and characterownerhash=:characterhash
";
$stmt = $dbh->prepare($sql);
$stmt->execute(array(':characterid'=>$response->CharacterID,':characterhash'=>$response->CharacterOwnerHash));
while ($row = $stmt->fetchObject()) {
$userdetails=$row;
$userid=$row->id;
}
// Fill in character details, if they're not in the DB
if (!isset($userdetails)) {
// No database entry for the user. lookup time.
error_log('Creating user details');
$ch = curl_init();
$lookup_url="https://api.eveonline.com/eve/CharacterAffiliation.xml.aspx?ids=".$response->CharacterID;
curl_setopt($ch, CURLOPT_URL, $lookup_url);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$result = curl_exec($ch);
curl_close($ch);
if ($result===false) {
auth_error('No such character on the API');
}
$xml=simplexml_load_string($result);
if (isset($xml->result->rowset->row->attributes()["characterID"])) {
$corporationID=(string)$xml->result->rowset->row->attributes()["corporationID"];
$corporationName=(string)$xml->result->rowset->row->attributes()["corporationName"];
$allianceID=(string)$xml->result->rowset->row->attributes()["allianceID"];
$allianceName=(string)$xml->result->rowset->row->attributes()["allianceName"];
} else {
auth_error("No character details returned from API");
}
//Alliance
if ($allianceID!=0) {
$alliancesql='select allianceid,allianceticker from alliance where allianceid=:allianceid';
$stmt = $dbh->prepare($alliancesql);
$stmt->execute(array(':allianceid'=>$allianceID));
while ($row = $stmt->fetchObject()) {
$allianceticker=$row->allianceticker;
}
if (!isset($allianceticker)) {
error_log('Getting alliance details');
$alliance_url='http://public-crest.eveonline.com/alliances/'.$allianceID.'/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $alliance_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$result = curl_exec($ch);
curl_close($ch);
$alliance_data=json_decode($result);
$allianceticker=$alliance_data->shortName;
$alliance_insert_sql="insert into alliance (allianceid,alliancename,allianceticker)
values (:allianceid,:alliancename,:allianceticker)";
$stmt = $dbh->prepare($alliance_insert_sql);
$stmt->execute(
array(
':allianceid'=>$allianceID,
':alliancename'=>$allianceName,
':allianceticker'=>$allianceticker)
);
}
} else {
$allianceName="No Alliance";
$allianceTicker="";
}
$userdetails['allianceid']=$allianceID;
$userdetails['alliancename']=$allianceName;
$userdetails['allianceticker']=$allianceticker;
// Corporation
$corporationsql='select corporationid,corporationticker from corporation where corporationid=:corporationid';
$stmt = $dbh->prepare($corporationsql);
$stmt->execute(array(':corporationid'=>$corporationID));
while ($row = $stmt->fetchObject()) {
$corporationticker=$row->corporationid;
}
if (!isset($corporationticker)) {
error_log('Getting corporation details');
$corporation_url="https://api.eveonline.com/corp/CorporationSheet.xml.aspx?corporationid=".$corporationID;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $corporation_url);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$result = curl_exec($ch);
curl_close($ch);
$corpxml=simplexml_load_string($result);
$corporationticker=$corpxml->result->ticker;
$corporation_insert_sql="insert into corporation
(corporationid,corporationname,corporationticker,allianceid)
values (:corporationid,:corporationname,:corporationticker,:allianceid)";
$stmt = $dbh->prepare($corporation_insert_sql);
$stmt->execute(
array(
':corporationid'=>$corporationID,
':corporationname'=>$corporationName,
':corporationticker'=>$corporationticker,
':allianceid'=>$allianceID
)
);
}
$userdetails['corporationid']=$corporationID;
$userdetails['corporationname']=$corporationName;
$userdetails['corporationticker']=$corporationticker;
$user_creation_sql='insert into user (characterid,characterownerhash,character_name,corporationid)
values (:characterid,:characterownerhash,:character_name,:corporationid)';
$stmt = $dbh->prepare($user_creation_sql);
$stmt->execute(
array(
':characterid'=>$response->CharacterID,
':characterownerhash'=>$response->CharacterOwnerHash,
':character_name'=>$response->CharacterName,
':corporationid'=>$corporationID
)
);
$userid=$dbh->lastInsertId();
$userdetails['id']=$userid;
error_log("user added to db");
}
$_SESSION['auth_characterid']=$response->CharacterID;
$_SESSION['auth_id']=$userid;
$_SESSION['auth_charactername']=$response->CharacterName;
$_SESSION['auth_userdetails']=json_encode($userdetails);
$_SESSION['auth_characterhash']=$response->CharacterOwnerHash;
session_write_close();
header('Location:'. $_SESSION['auth_redirect']);
exit;
} else {
echo "State is wrong. Did you make sure to actually hit the login url first?";
error_log($_SESSION['auth_state']);
error_log($_GET['state']);
}