├── .gitignore
├── LICENSE
├── README.md
├── auth_functions.php
├── db.inc.php
├── devauthcallback.php
├── devlogin.php
├── logout.php
└── whoami.php
/.gitignore:
--------------------------------------------------------------------------------
1 | composer.phar
2 | vendor/
3 |
4 | # Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
5 | # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
6 | # composer.lock
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 fuzzysteve
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | eve-sso-auth
2 | ============
3 |
4 | Basic PHP code for setting up a session and database entry for users
5 |
6 | requires a database and the CURL extension.
7 |
8 | Right now, it makes the assumption that people don't change which alliance/corporation they're in. Long term I'll change that, so it checks every time. Kind of requires a fix-this cronjob for the moment
9 |
10 |
11 | You'll need to have a file called secret.php, for the $clientid and $secret.
12 |
13 | Please change the $useragent in the authcallback.
14 |
15 | CREATE TABLE `alliance` (
16 | `id` int(11) NOT NULL AUTO_INCREMENT,
17 | `allianceid` int(11) DEFAULT NULL,
18 | `alliancename` varchar(255) DEFAULT NULL,
19 | `allianceticker` varchar(10) DEFAULT NULL,
20 | PRIMARY KEY (`id`)
21 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8
22 |
23 | CREATE TABLE `corporation` (
24 | `id` int(11) NOT NULL AUTO_INCREMENT,
25 | `corporationid` int(11) DEFAULT NULL,
26 | `corporationname` varchar(255) DEFAULT NULL,
27 | `corporationticker` varchar(10) DEFAULT NULL,
28 | `allianceid` int(11) DEFAULT NULL,
29 | PRIMARY KEY (`id`)
30 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8
31 |
32 | CREATE TABLE `user` (
33 | `id` int(11) NOT NULL AUTO_INCREMENT,
34 | `characterid` int(11) DEFAULT NULL,
35 | `characterownerhash` varchar(255) DEFAULT NULL,
36 | `character_name` varchar(255) DEFAULT NULL,
37 | `corporationid` int(11) DEFAULT NULL,
38 | PRIMARY KEY (`id`)
39 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8
40 |
--------------------------------------------------------------------------------
/auth_functions.php:
--------------------------------------------------------------------------------
1 | 'authorization_code',
21 | 'code' => $code
22 | );
23 | foreach ($fields as $key => $value) {
24 | $fields_string .= $key.'='.$value.'&';
25 | }
26 | rtrim($fields_string, '&');
27 | $ch = curl_init();
28 | curl_setopt($ch, CURLOPT_URL, $url);
29 | curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
30 | curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
31 | curl_setopt($ch, CURLOPT_POST, count($fields));
32 | curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
33 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
34 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
35 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
36 | $result = curl_exec($ch);
37 |
38 | if ($result===false) {
39 | auth_error(curl_error($ch));
40 | }
41 | curl_close($ch);
42 | $response=json_decode($result);
43 | $auth_token=$response->access_token;
44 | $ch = curl_init();
45 |
46 | // Get the Character details from SSO
47 |
48 | # $header='Authorization: Bearer '.$auth_token;
49 | # curl_setopt($ch, CURLOPT_URL, $verify_url);
50 | # curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
51 | # curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
52 | # curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
53 | # curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
54 | # curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
55 | # $result = curl_exec($ch);
56 | # if ($result===false) {
57 | # auth_error(curl_error($ch));
58 | # }
59 | # curl_close($ch);
60 | # $response=json_decode($result);
61 |
62 |
63 |
64 | # if (!isset($characterid)) {
65 | # auth_error('No character ID returned');
66 | # }
67 |
68 | $jwtexplode=json_decode(base64_decode(str_replace('_', '/', str_replace('-','+',explode('.',$auth_token )[1]))));
69 | $charactername=$jwtexplode->name;
70 | $owner=$jwtexplode->owner;
71 | $characterid=explode(":",$jwtexplode->sub)[2];
72 |
73 | // Lookup the character details in the DB.
74 | require_once('db.inc.php');
75 | $sql="select corporationname,corporationticker,user.corporationid,
76 | alliancename,allianceticker,corporation.allianceid,characterid,characterownerhash,
77 | user.id
78 | from user
79 | join corporation on user.corporationid=corporation.corporationid
80 | join alliance on corporation.allianceid=alliance.allianceid
81 | where
82 | user.characterid=:characterid
83 | and characterownerhash=:characterhash
84 | ";
85 |
86 | $stmt = $dbh->prepare($sql);
87 | $stmt->execute(array(':characterid'=>$characterid,':characterhash'=>$owner));
88 |
89 | while ($row = $stmt->fetchObject()) {
90 | $userdetails=$row;
91 | $userid=$row->id;
92 | }
93 |
94 | // Fill in character details, if they're not in the DB
95 |
96 | if (!isset($userdetails)) {
97 | // No database entry for the user. lookup time.
98 | error_log('Creating user details');
99 | $ch = curl_init();
100 | $lookup_url="https://esi.evetech.net/latest/characters/".$characterid."/";
101 | curl_setopt($ch, CURLOPT_URL, $lookup_url);
102 | curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
103 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
104 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
105 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
106 | $result = curl_exec($ch);
107 | curl_close($ch);
108 | if ($result===false) {
109 | auth_error('No such character on the API');
110 | }
111 | $chardetails=json_decode($result);
112 | $corporationID=$chardetails->corporation_id;
113 | $allianceID=$chardetails->alliance_id;
114 | //Alliance
115 | if ($allianceID!=0) {
116 | $alliancesql='select allianceid,allianceticker,alliancename from alliance where allianceid=:allianceid';
117 | $stmt = $dbh->prepare($alliancesql);
118 | $stmt->execute(array(':allianceid'=>$allianceID));
119 | while ($row = $stmt->fetchObject()) {
120 | $allianceticker=$row->allianceticker;
121 | $allianceName=$row->alliancename;
122 | }
123 | if (!isset($allianceticker)) {
124 | error_log('Getting alliance details');
125 | $alliance_url='https://esi.evetech.net/latest/alliances/'.$allianceID.'/';
126 | $ch = curl_init();
127 | curl_setopt($ch, CURLOPT_URL, $alliance_url);
128 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
129 | curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
130 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
131 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
132 | $result = curl_exec($ch);
133 | curl_close($ch);
134 | $alliance_data=json_decode($result);
135 | $allianceticker=$alliance_data->ticker;
136 | $allianceName=$alliance_data->name;
137 | $alliance_insert_sql="insert into alliance (allianceid,alliancename,allianceticker)
138 | values (:allianceid,:alliancename,:allianceticker)";
139 | $stmt = $dbh->prepare($alliance_insert_sql);
140 | $stmt->execute(
141 | array(
142 | ':allianceid'=>$allianceID,
143 | ':alliancename'=>$allianceName,
144 | ':allianceticker'=>$allianceticker)
145 | );
146 | }
147 |
148 | } else {
149 | $allianceName="No Alliance";
150 | $allianceTicker="";
151 | }
152 | $userdetails['allianceid']=$allianceID;
153 | $userdetails['alliancename']=$allianceName;
154 | $userdetails['allianceticker']=$allianceticker;
155 |
156 | // Corporation
157 | $corporationsql='select corporationid,corporationticker,corporationname from corporation where corporationid=:corporationid';
158 | $stmt = $dbh->prepare($corporationsql);
159 | $stmt->execute(array(':corporationid'=>$corporationID));
160 | while ($row = $stmt->fetchObject()) {
161 | $corporationticker=$row->corporationid;
162 | $corporationName=$row->corporationname;
163 | }
164 | if (!isset($corporationticker)) {
165 | error_log('Getting corporation details');
166 | $corporation_url="https://esi.evetech.net/latest/corporations/".$corporationID."/";
167 | $ch = curl_init();
168 | curl_setopt($ch, CURLOPT_URL, $corporation_url);
169 | curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
170 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
171 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
172 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
173 | $result = curl_exec($ch);
174 | curl_close($ch);
175 | $corpjson=json_decode($result);
176 | $corporationticker=$corpjson->ticker;
177 | $corporationName=$corpjson->name;
178 | $corporation_insert_sql="insert into corporation
179 | (corporationid,corporationname,corporationticker,allianceid)
180 | values (:corporationid,:corporationname,:corporationticker,:allianceid)";
181 | $stmt = $dbh->prepare($corporation_insert_sql);
182 | $stmt->execute(
183 | array(
184 | ':corporationid'=>$corporationID,
185 | ':corporationname'=>$corporationName,
186 | ':corporationticker'=>$corporationticker,
187 | ':allianceid'=>$allianceID
188 | )
189 | );
190 | }
191 | $userdetails['corporationid']=$corporationID;
192 | $userdetails['corporationname']=$corporationName;
193 | $userdetails['corporationticker']=$corporationticker;
194 | $user_creation_sql='insert into user (characterid,characterownerhash,character_name,corporationid)
195 | values (:characterid,:characterownerhash,:character_name,:corporationid)';
196 | $stmt = $dbh->prepare($user_creation_sql);
197 | $stmt->execute(
198 | array(
199 | ':characterid'=>$characterid,
200 | ':characterownerhash'=>$owner,
201 | ':character_name'=>$charactername,
202 | ':corporationid'=>$corporationID
203 | )
204 | );
205 | $userid=$dbh->lastInsertId();
206 | $userdetails['id']=$userid;
207 |
208 | error_log("user added to db");
209 | }
210 |
211 | $_SESSION['auth_characterid']=$characterid;
212 | $_SESSION['auth_id']=$userid;
213 | $_SESSION['auth_charactername']=$charactername;
214 | $_SESSION['auth_userdetails']=json_encode($userdetails);
215 | $_SESSION['auth_characterhash']=$owner;
216 | session_write_close();
217 | setcookie('fuzzworkauth', $_SESSION['auth_characterid'], 0, '/', 'www.fuzzwork.co.uk', true, true);
218 | header('Location:'. $_SESSION['auth_redirect']);
219 |
220 | exit;
221 |
222 | } else {
223 | echo "State is wrong. Did you make sure to actually hit the login url first?";
224 | error_log($_SESSION['auth_state']);
225 | error_log($_GET['state']);
226 | }
227 |
--------------------------------------------------------------------------------
/devlogin.php:
--------------------------------------------------------------------------------
1 | ';
4 | echo $_SESSION['auth_charactername'].'
';
5 | echo $_SESSION['auth_characterhash'].'
';
6 | echo $_SESSION['auth_userdetails'].'
';
7 |
--------------------------------------------------------------------------------