Skip to content

Commit

Permalink
dev: cleanup auth plugins with a new authpluginbase
Browse files Browse the repository at this point in the history
  • Loading branch information
mennodekker committed Apr 2, 2013
1 parent dee0c38 commit 33e4632
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 41 deletions.
84 changes: 43 additions & 41 deletions application/core/plugins/Authdb/Authdb.php
@@ -1,7 +1,9 @@
<?php
class Authdb extends PluginBase
class Authdb extends AuthPluginBase
{
protected $storage = 'DbStorage';
protected $storage = 'DbStorage';

protected $_onepass = null;

static protected $description = 'Core: Database authentication';

Expand All @@ -28,22 +30,29 @@ public function beforeDeactivate(PluginEvent $event)

public function beforeLogin(PluginEvent $event)
{
$event->set('default', get_class($this)); // This is the default login method, should be configurable from plugin settings
$this->getEvent()->set('default', get_class($this)); // This is the default login method, should be configurable from plugin settings

// We can skip the login form here and set username/password etc.

/* @var $identity LSUserIdentity */
$identity = $event->get('identity');

$request = $this->api->getRequest();
if ($request->getIsPostRequest() && !is_null($request->getQuery('onepass'))) {
// We have a one time password, skip the login form
$identity->setConfig(array('onepass'=>$request()->getQuery('onepass')));
$identity->username = $request()->getQuery('user');
$event->stop(); // Skip the login form
$this->setOnePass($request()->getQuery('onepass'));
$this->setUsername($request()->getQuery('user'));
$this->getEvent()->stop(); // Skip the login form
}
}

/**
* Get the onetime password (if set)
*
* @return string|null
*/
protected function getOnePass()
{
return $this->_onepass;
}

public function newLoginForm(PluginEvent $event)
{
$event->getContent($this)
Expand All @@ -53,31 +62,22 @@ public function newLoginForm(PluginEvent $event)

public function afterLoginFormSubmit(PluginEvent $event)
{
// Here we handle moving post data to the identity
/* @var $identity LSUserIdentity */
$identity = $event->get('identity');

// Here we handle post data
$request = $this->api->getRequest();
if ($request->getIsPostRequest()) {
$identity->username = $request->getPost('user');
$identity->password = $request->getPost('password');
$this->setUsername( $request->getPost('user'));
$this->setPassword($request->getPost('password'));
}

$event->set('identity', $identity);
}

public function newUserSession(PluginEvent $event)
{
// Here we do the actual authentication
/* @var $identity LSUserIdentity */
$identity = $event->getSender();
// Here we do the actual authentication
$username = $this->getUsername();
$password = $this->getPassword();
$onepass = $this->getOnePass();

$username = $identity->username;
$password = $identity->password;
$config = $identity->getConfig();
$onepass = isset($config['onepass']) ? $config['onepass'] : '';

$user = User::model()->findByAttributes(array('users_name' => $username));
$user = $this->getUserByName($username);

if ($user !== null)
{
Expand All @@ -92,35 +92,37 @@ public function newUserSession(PluginEvent $event)
}
else
{
$event->set('result', new LSAuthResult(LSUserIdentity::ERROR_USERNAME_INVALID));
$this->setAuthFailure(self::ERROR_USERNAME_INVALID);
return;
}

if ($onepass != '' && $this->api->getConfigKey('use_one_time_passwords') && md5($onepass) == $user->one_time_pw)
{
$user->one_time_pw='';
$user->save();
$identity->id = $user->uid;
$identity->user = $user;
$event->set('result', new LSAuthResult(LSUserIdentity::ERROR_NONE));
$this->setAuthSuccess($user);
return;
}

}

if ($sStoredPassword !== hash('sha256', $password))
{
$event->set('result', new LSAuthResult(LSUserIdentity::ERROR_PASSWORD_INVALID));
return;
}
else
{
$identity->id = $user->uid;
$identity->user = $user;
$event->set('result', new LSAuthResult(LSUserIdentity::ERROR_NONE));
$this->setAuthFailure(self::ERROR_PASSWORD_INVALID);
return;
}

$this->setAuthSuccess($user);
}


/**
* Set the onetime password
*
* @param type $onepass
* @return Authdb
*/
protected function setOnePass($onepass)
{
$this->_onepass = $onepass;

return $this;
}
}
120 changes: 120 additions & 0 deletions application/libraries/PluginManager/AuthPluginBase.php
@@ -0,0 +1,120 @@
<?php
abstract class AuthPluginBase extends PluginBase {

/**
* These constants reflect the error codes to be used by the identity, they
* are copied from LSUserIdentity and CBaseUserIdentity for easier access.
*/
const ERROR_NONE = 0;
const ERROR_USERNAME_INVALID = 1;
const ERROR_PASSWORD_INVALID = 2;
const ERROR_IP_LOCKED_OUT = 98;
const ERROR_UNKNOWN_HANDLER = 99;
const ERROR_UNKNOWN_IDENTITY = 100;

protected $_username = null;
protected $_password = null;

/**
* Get the password (if set)
*
* @return string|null
*/
protected function getPassword()
{
return $this->_password;
}

/**
* Get the user object for a given username
*
* @param string $username
* @return User|null Returns the user, or null when not found
*/
protected function getUserByName($username)
{
$user = User::model()->findByAttributes(array('users_name' => $username));

return $user;
}

/**
* Get the username (if set)
*
* @return string|null
*/
protected function getUserName()
{
return $this->_username;
}

/**
* Set authentication result to success for the given user object.
*
* @param User $user
* @return AuthPluginBase
*/
public function setAuthSuccess(User $user)
{
$event = $this->getEvent();
$identity = $this->getEvent()->get('identity');
$identity->id = $user->uid;
$identity->user = $user;
$event->set('result', new LSAuthResult(self::ERROR_NONE));

return $this;
}

/**
* Set authentication result to failure.
*
* @param int $code Any of the constants defined in this class
* @param string $message An optional message to return about the failure
* @return AuthPluginBase
*/
public function setAuthFailure($code = self::ERROR_UNKNOWN_IDENTITY, $message = '')
{
$event = $this->getEvent();
$identity = $this->getEvent()->get('identity');
$identity->id = null;
$event->set('result', new LSAuthResult($code, $message));

return $this;
}

/**
* Set the password to use for authentication
*
* @param string $password
* @return AuthPluginBase
*/
protected function setPassword($password)
{
$this->_password = $password;
$event = $this->getEvent();
$identity = $this->getEvent()->get('identity');
$identity->password = $password;

$event->set('identity', $identity);

return $this;
}

/**
* Set the username to use for authentication
*
* @param string $username The username
* @return AuthPluginBase
*/
protected function setUsername($username)
{
$this->_username = $username;
$event = $this->getEvent();
$identity = $this->getEvent()->get('identity');
$identity->username = $username;

$event->set('identity', $identity);

return $this;
}
}
29 changes: 29 additions & 0 deletions application/libraries/PluginManager/PluginBase.php
Expand Up @@ -10,6 +10,12 @@ abstract class PluginBase implements iPlugin {
*/
protected $api = null;

/**
*
* @var PluginEvent
*/
protected $event = null;

protected $id = null;
protected $storage = 'DummyStorage';

Expand Down Expand Up @@ -61,6 +67,16 @@ public static function getDescription()
return static::$description;
}

/**
* Get the current event this plugin is responding to
*
* @return PluginEvent
*/
public function getEvent()
{
return $this->event;
}

/**
* Returns the id of the plugin
*
Expand Down Expand Up @@ -192,6 +208,19 @@ protected function set($key, $data, $model = null, $id = null)
{
return $this->getStore()->set($this, $key, $data, $model, $id);
}

/**
* Set the event to the plugin, this method is executed by the PluginManager
* just before dispatching the event.
*
* @param PluginEvent $event
* @return PluginBase
*/
public function setEvent(PluginEvent $event)
{
$this->event = $event;
return $this;
}

/**
* Here you should handle subscribing to the events your plugin will handle
Expand Down
2 changes: 2 additions & 0 deletions application/libraries/PluginManager/PluginManager.php
Expand Up @@ -142,7 +142,9 @@ public function dispatchEvent(PluginEvent $event, $target = array())
if (!$event->isStopped()
&& (empty($target) || in_array(get_class($subscription[0]), $target)))
{
$subscription[0]->setEvent($event);
call_user_func($subscription, $event);
$event = $subscription[0]->getEvent();
}
}
}
Expand Down

0 comments on commit 33e4632

Please sign in to comment.