1
|
|
|
<?php
|
2
|
|
|
/* zKillboard
|
3
|
|
|
* Copyright (C) 2012-2015 EVE-KILL Team and EVSCO.
|
4
|
|
|
*
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
8
|
|
|
* (at your option) any later version.
|
9
|
|
|
*
|
10
|
|
|
* This program is distributed in the hope that it will be useful,
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
|
|
* GNU Affero General Public License for more details.
|
14
|
|
|
*
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17
|
|
|
*/
|
18
|
|
|
|
19
|
|
|
class OAuth
|
20
|
|
|
{
|
21
|
|
|
public static function eveSSOLoginURL()
|
|
|
|
|
22
|
|
|
{
|
23
|
|
|
global $ssoServer, $ssoResponseType, $ssoRedirectURI, $ssoClientID, $ssoScope, $ssoState;
|
24
|
|
|
return "{$ssoServer}/oauth/authorize?response_type={$ssoResponseType}&redirect_uri={$ssoRedirectURI}&client_id={$ssoClientID}&scope={$ssoScope}&state={$ssoState}";
|
25
|
|
|
}
|
26
|
|
|
|
27
|
|
|
public static function eveSSOLoginToken($code, $state)
|
|
|
|
|
28
|
|
|
{
|
29
|
|
|
global $ssoServer, $ssoSecret, $ssoClientID;
|
30
|
|
|
|
31
|
|
|
$tokenURL = $ssoServer . "/oauth/token";
|
32
|
|
|
$b64 = $ssoClientID . ":" . $ssoSecret;
|
33
|
|
|
$base64 = base64_encode($b64);
|
34
|
|
|
|
35
|
|
|
$header = array();
|
36
|
|
|
$header[] = "Authorization: Basic {$base64}";
|
37
|
|
|
|
38
|
|
|
$fields = array(
|
39
|
|
|
"grant_type" => "authorization_code",
|
40
|
|
|
"code" => $code
|
41
|
|
|
);
|
42
|
|
|
|
43
|
|
|
$data = Util::postData($tokenURL, $fields, $header);
|
44
|
|
|
|
45
|
|
|
$data = json_decode($data);
|
46
|
|
|
$accessToken = $data->access_token;
|
47
|
|
|
|
48
|
|
|
self::eveSSOLoginVerify($accessToken);
|
49
|
|
|
|
50
|
|
|
}
|
51
|
|
|
|
52
|
|
|
public static function eveSSOLoginVerify($accessToken)
|
53
|
|
|
{
|
54
|
|
|
global $ssoServer;
|
55
|
|
|
|
56
|
|
|
$verifyURL = $ssoServer . "/oauth/verify";
|
57
|
|
|
|
58
|
|
|
$header = array();
|
59
|
|
|
$header[] = "Authorization: Bearer {$accessToken}";
|
60
|
|
|
|
61
|
|
|
$data = Util::postData($verifyURL, NULL, $header);
|
|
|
|
|
62
|
|
|
|
63
|
|
|
self::eveSSOLogin($data);
|
64
|
|
|
}
|
65
|
|
|
|
66
|
|
|
public static function eveSSOLogin($data = NULL)
|
67
|
|
|
{
|
68
|
|
|
global $cookie_name, $cookie_time, $cookie_ssl, $baseAddr, $app;
|
69
|
|
|
|
70
|
|
|
$data = json_decode($data);
|
71
|
|
|
$characterID = (int) $data->CharacterID;
|
72
|
|
|
$affiliationInfo = Info::getCharacterAffiliations($characterID);
|
73
|
|
|
|
74
|
|
|
$exists = Db::queryField("SELECT merged FROM zz_users WHERE characterID = :characterID", "merged", array(":characterID" => $characterID), 0);
|
75
|
|
|
if(!$exists || $exists == 0) // Exists should never be 0 actually, it should always be null or 1.. but lets catch it if it is for some strange reason..
|
76
|
|
|
{
|
77
|
|
|
// Insert the data to zz_users_crest
|
78
|
|
|
Db::execute("INSERT IGNORE INTO zz_users_crest (characterID, characterName, scopes, tokenType, characterOwnerHash, corporationID, corporationName, corporationTicker, allianceID, allianceName, allianceTicker) VALUES (:characterID, :characterName, :scopes, :tokenType, :characterOwnerHash, :corporationID, :corporationName, :corporationTicker, :allianceID, :allianceName, :allianceTicker)", array(":characterID" => $data->CharacterID, ":characterName" => $data->CharacterName, ":scopes" => $data->Scopes, ":tokenType" => $data->TokenType, ":characterOwnerHash" => $data->CharacterOwnerHash, ":corporationID" => $affiliationInfo["corporationID"], ":corporationName" => $affiliationInfo["corporationName"], ":corporationTicker" => $affiliationInfo["corporationTicker"], ":allianceID" => $affiliationInfo["allianceID"], ":allianceName" => $affiliationInfo["allianceName"], ":allianceTicker" => $affiliationInfo["allianceTicker"]));
|
79
|
|
|
|
80
|
|
|
// Send the user to the merge page
|
81
|
|
|
header("Location: /merge/{$characterID}/");
|
82
|
|
|
}
|
83
|
|
|
else
|
84
|
|
|
{
|
85
|
|
|
// User exists, and is already registered, merged etc. etc.. Just login
|
86
|
|
|
$password = Db::queryField("SELECT password FROM zz_users WHERE characterID = :characterID", "password", array(":characterID" => $characterID));
|
87
|
|
|
$username = Db::queryField("SELECT username FROM zz_users WHERE characterID = :characterID", "username", array(":characterID" => $characterID));
|
88
|
|
|
$userID = Db::queryField("SELECT id FROM zz_users WHERE characterID = :characterID", "id", array(":characterID" => $characterID));
|
89
|
|
|
$passwordHash = Password::genPassword($password);
|
90
|
|
|
$hash = $username . "/" . hash("sha256", $username . $passwordHash . time());
|
91
|
|
|
$app->setEncryptedCookie($cookie_name, $hash, time() + $cookie_time, "/", $baseAddr, $cookie_ssl, true);
|
92
|
|
|
$validTill = date("Y-m-d H:i:s", time() + $cookie_time);
|
93
|
|
|
$userAgent = $_SERVER["HTTP_USER_AGENT"];
|
94
|
|
|
$ip = IP::get();
|
95
|
|
|
Db::execute("INSERT INTO zz_users_sessions (userID, sessionHash, validTill, userAgent, ip) VALUES (:userID, :sessionHash, :validTill, :userAgent, :ip)",
|
96
|
|
|
array(":userID" => $userID, ":sessionHash" => $hash, ":validTill" => $validTill, ":userAgent" => $userAgent, ":ip" => $ip));
|
97
|
|
|
$_SESSION["loggedin"] = $data->CharacterName;
|
98
|
|
|
header("Location: /");
|
99
|
|
|
}
|
100
|
|
|
}
|
101
|
|
|
} |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.