Newer
Older
<?php
require(INCLUDE_DIR.'class.ostsession.php');
require(INCLUDE_DIR.'class.usersession.php');
abstract class AuthenticatedUser {
//Authorization key returned by the backend used to authorize the user
private $authkey;
abstract function getId();
abstract function getUsername();
abstract function getRole();
//Backend used to authenticate the user
abstract function getAuthBackend();
//Authentication key
function setAuthKey($key) {
$this->authkey = $key;
}
function getAuthKey() {
return $this->authkey;
}
// logOut the user
function logOut() {
if ($bk = $this->getAuthBackend())
return $bk->signOut($this);
return false;
}
interface AuthDirectorySearch {
/**
* Indicates if the backend can be used to search for user information.
* Lookup is performed to find user information based on a unique
* identifier.
*/
function lookup($id);
/**
* Indicates if the backend supports searching for usernames. This is
* distinct from information lookup in that lookup is intended to lookup
* information based on a unique identifier
*/
function search($query);
}
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
/**
* Class: ClientCreateRequest
*
* Simple container to represent a remote authentication success for a
* client which should be imported into the local database. The class will
* provide access to the backend that authenticated the user, the username
* that the user entered when logging in, and any other information about
* the user that the backend was able to lookup. Generally, this extra
* information would be the same information retrieved from calling the
* AuthDirectorySearch::lookup() method.
*/
class ClientCreateRequest {
var $backend;
var $username;
var $info;
function __construct($backend, $username, $info=array()) {
$this->backend = $backend;
$this->username = $username;
$this->info = $info;
}
function getBackend() {
return $this->backend;
}
function setBackend($what) {
$this->backend = $what;
}
function getUsername() {
return $this->username;
}
function getInfo() {
return $this->info;
}
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
function attemptAutoRegister() {
global $cfg;
if (!$cfg)
return false;
// Attempt to automatically register
$this_form = UserForm::getUserForm()->getForm($this->getInfo());
$bk = $this->getBackend();
$defaults = array(
'timezone_id' => $cfg->getDefaultTimezoneId(),
'dst' => $cfg->observeDaylightSaving(),
'username' => $this->getUsername(),
);
if ($bk->supportsInteractiveAuthentication())
// User can only be authenticated against this backend
$defaults['backend'] = $bk::$id;
if ($this_form->isValid(function($f) { return !$f->get('private'); })
&& ($U = User::fromVars($this_form->getClean()))
&& ($acct = ClientAccount::createForUser($U, $defaults))
// Confirm and save the account
&& $acct->confirm()
// Login, since `tickets.php` will not attempt SSO
&& ($cl = new ClientSession(new EndUser($U)))
&& ($bk->login($cl, $bk)))
return $cl;
}
/**
* Authentication backend
*
* Authentication provides the basis of abstracting the link between the
* login page with a username and password and the staff member,
* administrator, or client using the system.
*
* The system works by allowing the AUTH_BACKENDS setting from
* ost-config.php to determine the list of authentication backends or
* providers and also specify the order they should be evaluated in.
*
* The authentication backend should define a authenticate() method which
* receives a username and optional password. If the authentication
* succeeds, an instance deriving from <User> should be returned.
*/
abstract class AuthenticationBackend {
static protected $registry = array();
/* static */
static function register($class) {
if (is_string($class) && class_exists($class))
if (!is_object($class)
|| !($class instanceof AuthenticationBackend))
return false;
return static::_register($class);
}
static function _register($class) {
// XXX: Raise error if $class::id is already in the registry
static::$registry[$class::$id] = $class;
}
static function allRegistered() {
return static::$registry;
}
static function getBackend($id) {
if ($id
&& ($backends = static::allRegistered())
&& isset($backends[$id]))
return $backends[$id];
static function getSearchDirectoryBackend($id) {
if ($id
&& ($backends = static::getSearchDirectories())
&& isset($backends[$id]))
return $backends[$id];
}
/*
* Allow the backend to do login audit depending on the result
* This is mainly used to track failed login attempts
*/
static function authAudit($result, $credentials=null) {
if (!$result) return;
foreach (static::allRegistered() as $bk)
$bk->audit($result, $credentials);
}
static function process($username, $password=null, &$errors) {
if (!$username)
return false;
$backends = static::getAllowedBackends($username);
foreach (static::allRegistered() as $bk) {
if ($backends //Allowed backends
&& $bk->supportsInteractiveAuthentication()
&& !in_array($bk::$id, $backends))
// User cannot be authenticated against this backend
continue;
Loading
Loading full blame...