OAuth   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5
Metric Value
wmc 6
lcom 0
cbo 5
dl 0
loc 83
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A eveSSOLoginURL() 0 5 1
B eveSSOLoginToken() 0 24 1
A eveSSOLoginVerify() 0 13 1
B eveSSOLogin() 0 35 3
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()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $state is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
NULL is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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
}