Skip to content

Commit

Permalink
Authlite 2.0 for Kohana 3.0 has now begun alpha testing. :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fred Wu committed Oct 31, 2009
1 parent deac55c commit 87e6cda
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 66 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Expand Up @@ -2,4 +2,4 @@

The "k2" branch and 1.x tags are for Kohana 2.3. The "master" branch and 2.x tags are for Kohana 3.0.

Work on Kohana 3.0 has not begun yet, please be patient.
Authlite 2.0 in the master branch has now begun alpha testing.
53 changes: 27 additions & 26 deletions libraries/Authlite.php → classes/authlite.php
@@ -1,18 +1,21 @@
<?php
/**
* Authlite library v1.2.3
* Authlite library v2.0 Alpha 1
*
* Based on Kohana's Auth library.
*
* @package Layerful
* @subpackage Modules
* @author Layerful Team <http://layerful.org/>
* @author Fred Wu <fred@beyondcoding.com>
* @copyright BeyondCoding
* @license http://layerful.org/license MIT
* @since 0.3.0
* @author Fred Wu <fred@wuit.com>
* @copyright Wuit
* @license http://www.opensource.org/licenses/mit-license.php
*/
class Authlite_Core {
class Authlite {

/**
* Authlite instances
*
* @var array
*/
protected static $instances;

/**
* Controller methods that bypass the login
Expand Down Expand Up @@ -88,12 +91,10 @@ public static function factory($config_name = 'authlite')
*/
public static function instance($config_name = 'authlite')
{
static $instance;

// Load the Authlite instance
empty($instance[$config_name]) and $instance[$config_name] = new Authlite($config_name);
empty(Authlite::$instances[$config_name]) and Authlite::$instances[$config_name] = new Authlite($config_name);

return $instance[$config_name];
return Authlite::$instances[$config_name];
}

public function __construct($config_name = 'authlite')
Expand All @@ -107,7 +108,7 @@ public function __construct($config_name = 'authlite')
$this->password_column = $this->config['password'];
$this->session_column = $this->config['session'];

Kohana::log('debug', 'Authlite Library loaded');
Kohana_Log::instance()->add('debug', 'Authlite Library loaded');

$this->ignored_methods = $this->session->get('authlite_ignored_methods');
}
Expand Down Expand Up @@ -168,17 +169,17 @@ public function logged_in()
// Get the user from the cookie
if ($status == false)
{
$token = cookie::get("authlite_{$this->config_name}_autologin");
$token = Cookie::get("authlite_{$this->config_name}_autologin");

if (is_string($token))
{
$user = ORM::factory($this->user_model)->find(array($this->session_column => $token));

if ($user->loaded)
if (is_object($user))
{
$status = true;
$this->session->set($this->config['session_key'], $user);
cookie::set("authlite_{$this->config_name}_autologin", $token, $this->config['lifetime']);
Cookie::set("authlite_{$this->config_name}_autologin", $token, $this->config['lifetime']);
}
}
}
Expand Down Expand Up @@ -217,12 +218,12 @@ public function login($username, $password, $remember = false)
return false;
}

$user = ORM::factory($this->user_model)->where(array(
$this->username_column => $username,
$this->password_column => $this->hash($password)
))->find();
$user = ORM::factory($this->user_model)
->where($this->username_column, '=', $username)
->where($this->password_column, '=', $this->hash($password))
->find();

if ($user->loaded)
if (is_object($user))
{
// Regenerate session_id
$this->session->regenerate();
Expand All @@ -231,10 +232,10 @@ public function login($username, $password, $remember = false)

if ($remember == true)
{
$token = $this->session->id();
$token = session_id();
$user->{$this->session_column} = $token;
$user->save();
cookie::set("authlite_{$this->config_name}_autologin", $token, $this->config['lifetime']);
Cookie::set("authlite_{$this->config_name}_autologin", $token, $this->config['lifetime']);
}

return $user;
Expand Down Expand Up @@ -276,9 +277,9 @@ public function force_login($username)
*/
public function logout($destroy = false)
{
if (cookie::get("authlite_{$this->config_name}_autologin"))
if (Cookie::get("authlite_{$this->config_name}_autologin"))
{
cookie::delete("authlite_{$this->config_name}_autologin");
Cookie::delete("authlite_{$this->config_name}_autologin");
}

if ($destroy === true)
Expand Down
21 changes: 21 additions & 0 deletions classes/controller/user.php
@@ -0,0 +1,21 @@
<?php defined('SYSPATH') or die('No direct script access.');

class Controller_User extends Controller
{
public function action_login()
{
empty($_POST) or $this->authlite->login($_POST['username'], $_POST['password'], TRUE);

if ($this->authlite->logged_in()) {
$this->request->redirect('');
} elseif ( ! empty($_POST)) {
// login error message
}
}

public function action_logout()
{
$this->authlite->logout();
$this->request->redirect('');
}
}
81 changes: 42 additions & 39 deletions config/authlite.php
@@ -1,40 +1,43 @@
<?php
<?php defined('SYSPATH') or die('No direct access allowed.');

/**
* User model
*/
$config['user_model'] = 'user';

/**
* Username column
*/
$config['username'] = 'username';

/**
* Password column
*/
$config['password'] = 'password';

/**
* Session column
*/
$config['session'] = 'session';

/**
* Type of hash to use for passwords. Any algorithm supported by the hash function
* can be used here.
* @see http://php.net/hash
* @see http://php.net/hash_algos
*/
$config['hash_method'] = 'sha1';

/**
* Set the auto-login (remember me) cookie lifetime, in seconds. The default
* lifetime is two weeks.
*/
$config['lifetime'] = 1209600;

/**
* Set the session key that will be used to store the current user.
*/
$config['session_key'] = 'authlite_user';
return array
(
/**
* User model
*/
'user_model' => 'user',

/**
* Username column
*/
'username' => 'username',

/**
* Password column
*/
'password' => 'password',

/**
* Session column
*/
'session' => 'session',

/**
* Type of hash to use for passwords. Any algorithm supported by the hash function
* can be used here.
* @see http://php.net/hash
* @see http://php.net/hash_algos
*/
'hash_method' => 'sha1',

/**
* Set the auto-login (remember me) cookie lifetime, in seconds. The default
* lifetime is two weeks.
*/
'lifetime' => 1209600,

/**
* Set the session key that will be used to store the current user.
*/
'session_key' => 'authlite_user',
);

0 comments on commit 87e6cda

Please sign in to comment.