-
Notifications
You must be signed in to change notification settings - Fork 0
/
EveSSO.php
194 lines (165 loc) · 5.39 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
<?php
namespace eve\sso;
/**
* Eve-sso main class and API
*
* @author Moiv
*
*/
class EveSSO
{
private $keychain = "";
private $generator = "";
private $tokenstorer = "";
private $sso_status;
/**
* Constructor
*
* @param array $options [Optional] An array containing options<br>
* Valid Options:<br>
* 'keychain'<br>
* 'requestgenerator'
*/
public function __construct(array $options = null)
{
if ($options == null) $options = array();
$this->data['hasValidAuthToken'] = false;
//Use KeyChain if supplied otherwise create a default one
if (array_key_exists('keychain', $options)) {
if (is_a($options['keychain'], 'eve\sso\KeyChain')) $this->keychain = $options['keychain'];
} else {
$this->keychain = new KeyChain(new FSTokenStorer(FS_TOKEN_PATH), CLIENT_ID, SECRET_KEY);
}
//Use RequestGenerator if supplied otherwise create a default one
if (array_key_exists('requestgenerator', $options)) {
if (is_a($options['requestgenerator'], 'eve\sso\RequestGenerator')) $this->generator = $options['requestgenerator'];
} else {
$this->generator = new RequestGenerator(CLIENT_ID, SECRET_KEY);
$this->generator->SetCallback(CALLBACK_URL);
$this->generator->SetState(UNIQUE_STATE);
if (defined('ESI_SCOPE')) $this->generator->SetESIScope(ESI_SCOPE);
}
}
/**
* Initialise SSO
* @return string Will return 1 of 3 responses:<br>
* 'code' An Authorise Request URL will be required to get an auth token<br>
* 'refresh' A new auth token will be required using the currently store refresh token<br>
* 'authd' The current auth token is still valid and no action is required
*/
public function Init()
{
$status = $this->GetSSOStatus();
// Automatically try to refresh once if required
if ($status == 'refresh')
{
$this->RefreshAuthToken();
$status = $this->GetSSOStatus();
}
return $status;
}
/**
* Get the current SSO Status
* @return string Will return 1 of 3 responses:<br>
* 'code' An Authorise Request URL will be required to get an auth token<br>
* 'refresh' A new auth token will be required using the currently store refresh token<br>
* 'authd' The current auth token is still valid and no action is required
*/
public function GetSSOStatus()
{
switch ($this->sso_status = $this->DecideOnAction()) {
case 'code':
//echo "We need an auth token using a request code<br>\n";
break;
case 'refresh':
//echo "We need a new auth token using the refresh token<br>\n";
break;
case 'authd':
//echo "We have a valid auth token, no action required<br>\n";
break;
}
return $this->sso_status;
}
/**
* Get the current auth token
* @return string Auth token string
*/
public function GetAuthTokenString()
{
if ($token = $this->keychain->GetAuthToken())
{
return $token->GetValue();
} else {
return null;
}
}
/**
* Get the auth token request URL
* @param string $uri [Optional] Callback URI
* @param string $state [Optional] A unique string of your choice
* @return string Auth token request URL
*/
public function GetRequestCode($uri = '', $state = '')
{
return $this->generator->GenerateAuthoriseRequest($uri, $state);
}
/**
* Output HTML Code to show the LOG IN with EVE Online button
* @param string $url URL for button to direct to
* @param int $size [optional] Size of button image:<br> 1 = Large Black (Default), 2 = Large White, 3 = Small Black, 4 = Small White
* @param bool $echo [optional] Set to false to suppress echoing the HTML code
* @return string containing HTML code
*/
public function HTMLLoginButton($url, $size=1, $echo = true)
{
switch ($size) {
case 2:
$img = 'eve-sso-login-white-large.png';
break;
case 3:
$img = 'eve-sso-login-black-small.png';
break;
case 4:
$img = 'eve-sso-login-white-small.png';
break;
default:
$img = 'eve-sso-login-black-large.png';
break;
}
$string = " <A HREF = '$url'><img src = 'https://web.ccpgamescdn.com/eveonlineassets/developers/$img'></a>";
if ($echo) echo $string;
return $string;
}
/**
* Refresh the auth token
* @return bool True on refresh success
*/
public function RefreshAuthToken()
{
$this->keychain->ReloadKeys();
$refreshtoken = $this->keychain->GetRefreshToken();
if ($refreshtoken != null)
{
$requester = new TokenRequester($this->keychain);
$success = $requester->RequestToken($this->generator->GenerateTokenRequest($refreshtoken));
}
return $success;
}
/**
* Decide which action is to be performed<br>code = Request code to begin auth<br>none = No action required<br>refresh = Refresh auth token
* @return string
*/
private function DecideOnAction()
{
$authToken = $this->keychain->GetAuthToken();
$refreshToken = $this->keychain->GetRefreshToken();
if ($authToken != null)
{
if ($authToken->IsExpired() == false) return 'authd'; // We have an auth token and it's valid
if ($authToken->IsExpired() == true && $refreshToken != null) return 'refresh'; // We have an auth token but it's expired
}
if ($refreshToken != null) return 'refresh'; // We didnt have an auth token but we do have a refresh token
return 'code'; // No other conditions met. We need to authorise using a code
}
}
?>