-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.php
More file actions
254 lines (231 loc) · 7.1 KB
/
auth.php
File metadata and controls
254 lines (231 loc) · 7.1 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
/* Eregansu: Authentication
*
* Copyright 2009 Mo McRoberts.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @year 2009
* @include uses('auth');
* @since Available in Eregansu 1.0 and later.
*/
uses('uri');
URI::register('builtin', 'AuthEngine', array('file' => dirname(__FILE__) . '/auth/builtin.php', 'class' => 'BuiltinAuth'));
URI::register('http', 'AuthEngine', array('file' => dirname(__FILE__) . '/auth/openid.php', 'class' => 'OpenIDAuth'));
URI::register('https', 'AuthEngine', array('file' => dirname(__FILE__) . '/auth/openid.php', 'class' => 'OpenIDAuth'));
URI::register('posix', 'AuthEngine', array('file' => dirname(__FILE__) . '/auth/posix.php', 'class' => 'PosixAuth'));
if(!defined('DEFAULT_AUTH_SCHEME')) define('DEFAULT_AUTH_SCHEME', 'https');
/**
* Interface implemented by authentication engines.
*/
interface IAuthEngine
{
public function verifyAuth($request, $scheme, $remainder, $authData, $callbackIRI);
public function verifyToken($request, $scheme, $remainder, $token);
public function refreshUserData(&$data);
public function callback($request, $scheme, $remainder);
public function retrieveUserData($scheme, $remainder);
}
/**
* Exception class whose instances are thrown when an authentication exception
* occurs.
*
* @synopsis throw new AuthError($engine, $message, $reason);
*/
class AuthError extends Exception
{
public $reason;
public $engine;
public function __construct($engine, $message = null, $reason = null)
{
if(!strlen($message)) $message = 'Incorrect sign-in name or password';
if(!strlen($reason)) $reason = $message;
parent::__construct($message);
$this->reason = $reason;
$this->engine = $engine;
}
}
/* Base class for authentication engines */
abstract class Auth implements IAuthEngine
{
protected static $authEngines = array();
protected $id = null;
protected $builtinAuthScheme = false;
/** Create an instance of an authentication system given an IRI.
*
* The instance is returned by the call to \m{Auth::authEngineForScheme}.
* \p{$iri} will be modified to strip the scheme (if supplied), which will
* be stored in \p{$scheme}. Thus, upon successful return, a fully-qualified
* IRI can be constructed from \x{$scheme . ':' . $iri}
*
* @param[in,out] string $iri The IRI to match against
* @param[out] string $scheme The authentication IRI scheme that was
* determined
* @param[in] string $defaultScheme The default authentication scheme to
* use if none can be determined from \p{$iri}
*
*/
public static function authEngineForIRI(&$iri, &$scheme, $defaultScheme = null)
{
if(!strlen($defaultScheme)) $defaultScheme = DEFAULT_AUTH_SCHEME;
$c = strpos($iri, ':');
$s = strpos($iri, '/');
$a = strpos($iri, '@');
if($c !== false && $s !== false && $s < $c) $c = false;
if($c !== false && $a !== false && $a < $c) $c = false;
if($c !== false)
{
$scheme = substr($iri, 0, $c);
$iri = substr($iri, $c + 1);
}
else
{
$scheme = $defaultScheme;
}
return self::authEngineForScheme($scheme);
}
/* Return an instance of an authentication system given a token name
* Analogous to Auth::authEngineForIRI(), except operating on tokens rather
* than IRIs. Unlike IRIs, tokens use plings as a separator
* rather than colons, so upon successful return a fully-qualified
* token name can be constructed from $scheme . '!' . $tokenName
*/
public static function authEngineForToken(&$tokenName, &$scheme)
{
if(($p = strpos($tokenName, '!')) !== false)
{
$tokenName = substr($tokenName, 0, $p) . ':' . substr($tokenName, $p + 1);
}
return self::authEngineForIRI($tokenName, $scheme);
}
/* Return an instance of an authentication system for a given named scheme */
public static function authEngineForScheme($scheme)
{
global $AUTH_CLASSES;
if(!isset(self::$authEngines[$scheme]))
{
if(isset($AUTH_CLASSES[$scheme]))
{
$class = $AUTH_CLASSES[$scheme];
self::$authEngines[$scheme] = call_user_func(array($class, 'getInstance'));
return self::$authEngines[$scheme];
}
switch($scheme)
{
case 'builtin':
self::$authEngines[$scheme] = new BuiltinAuth();
break;
case 'http':
case 'https':
case 'openid':
require_once(dirname(__FILE__) . '/openid.php');
$scheme = 'openid';
self::$authEngines[$scheme] = new OpenIDAuth();
break;
case 'posix':
require_once(dirname(__FILE__) . '/posix.php');
self::$authEngines[$scheme] = new PosixAuth();
break;
default:
return null;
}
}
return self::$authEngines[$scheme];
}
public function __construct()
{
if(defined('IDENTITY_IRI'))
{
require_once(dirname(__FILE__) . '/id.php');
$this->id = Identity::getInstance();
}
}
/* Given a request, a scheme, the remainder of an IRI and some
* (authentication-system specific) data, return true or false
* depending upon whether authentication was successful or not.
*/
public function verifyAuth($request, $scheme, $remainder, $authData, $callbackIRI)
{
return new AuthError($this);
}
/* Attempt to validate an authentication token for a user */
public function verifyToken($request, $scheme, $tokenName, $token)
{
return new AuthError($this);
}
/* Continue a multi-stage login process */
public function callback($request, $scheme, $remainder)
{
return new AuthError($this);
}
/* Retrieve (creating if they don’t exist, if possible) user details given
* a particular IRI.
*
*/
protected function createRetrieveUserWithIRI($iri, $data = null)
{
if(!is_array($iri))
{
$iri = array($iri);
}
if($this->id)
{
foreach($iri as $i)
{
if(($uuid = $this->id->uuidFromIRI($i, $data)))
{
return $uuid;
}
}
foreach($iri as $i)
{
if(($uuid = $this->id->createIdentity($i, $data, true)))
{
return $uuid;
}
}
}
else if(isset($data['uuid']) && $this->builtinAuthScheme)
{
return $data['uuid'];
}
return null;
}
/* Called periodically by the session-initiated callback in platform.php
* to cause the user details in the session to be updated by the
* authentication and identity layers.
*/
public function refreshUserData(&$data)
{
if($this->id)
{
$this->id->refreshUserData($data);
}
if(!isset($data['ttl']))
{
$data['ttl'] = time(0) + 30;
}
}
/* Given a scheme/remainder pair, retrieve the user details known to
* the authentication layer.
*
* Note that this function will NOT invoke refreshUserData(), above.
* It is the caller’s responsibility to do this if manually associating
* a user with a session.
*/
public function retrieveUserData($scheme, $remainder)
{
return null;
}
}