Modules
Auth_File
extends Kohana_Auth_File
extends Auth
extends Kohana_Auth
File Auth driver. [!!] this Auth driver does not support roles nor autologin.
Class declared in MODPATH/auth/classes/auth/file.php on line 3.
Constants
- None
Properties
Properties
protected
$_configprotected static
$_instanceNULL
protected
$_sessionprotected
$_users
Methods
public __construct( ) (defined in Kohana_Auth_File)
Constructor loads the user list into the class.
Source Code
public function __construct($config = array())
{
parent::__construct($config);
// Load user list
$this->_users = Arr::get($config, 'users', array());
}
public check_password( string $password ) (defined in Kohana_Auth_File)
Compare password with original (plain text). Works for current (logged in) user
Parameters
-
string$password required - $password
Return Values
boolean
Source Code
public function check_password($password)
{
$username = $this->get_user();
if ($username === FALSE)
{
return FALSE;
}
return ($password === $this->password($username));
}
public force_login( mixed $username ) (defined in Kohana_Auth_File)
Forces a user to be logged in, without specifying a password.
Parameters
-
mixed$username required - Username
Return Values
boolean
Source Code
public function force_login($username)
{
// Complete the login
return $this->complete_login($username);
}
public password( mixed $username ) (defined in Kohana_Auth_File)
Get the stored password for a username.
Parameters
-
mixed$username required - Username
Return Values
string
Source Code
public function password($username)
{
return Arr::get($this->_users, $username, FALSE);
}
public static factory( ) (defined in Kohana_Auth)
Create an instance of Auth.
Return Values
Auth
Source Code
public static function factory($config = array())
{
return new Auth($config);
}
public find_salt( string $password ) (defined in Kohana_Auth)
Finds the salt from a password, based on the configured salt pattern.
Parameters
-
string$password required - Hashed password
Return Values
string
Source Code
public function find_salt($password)
{
$salt = '';
foreach ($this->_config['salt_pattern'] as $i => $offset)
{
// Find salt characters, take a good long look...
$salt .= substr($password, $offset + $i, 1);
}
return $salt;
}
public get_user( ) (defined in Kohana_Auth)
Gets the currently logged in user from the session. Returns FALSE if no user is currently logged in.
Return Values
mixed
Source Code
public function get_user()
{
return $this->_session->get($this->_config['session_key'], FALSE);
}
public hash( string $str ) (defined in Kohana_Auth)
Perform a hash, using the configured method.
Parameters
-
string$str required - String to hash
Return Values
string
Source Code
public function hash($str)
{
return hash($this->_config['hash_method'], $str);
}
public hash_password( string $password [, $salt = bool FALSE ] ) (defined in Kohana_Auth)
Creates a hashed password from a plaintext password, inserting salt based on the configured salt pattern.
Parameters
-
string$password required - Plaintext password -
unknown$salt = bool FALSE
Return Values
string- Hashed password string
Source Code
public function hash_password($password, $salt = FALSE)
{
if ($salt === FALSE)
{
// Create a salt seed, same length as the number of offsets in the pattern
$salt = substr($this->hash(uniqid(NULL, TRUE)), 0, count($this->_config['salt_pattern']));
}
// Password hash that the salt will be inserted into
$hash = $this->hash($salt.$password);
// Change salt to an array
$salt = str_split($salt, 1);
// Returned password
$password = '';
// Used to calculate the length of splits
$last_offset = 0;
foreach ($this->_config['salt_pattern'] as $offset)
{
// Split a new part of the hash off
$part = substr($hash, 0, $offset - $last_offset);
// Cut the current part out of the hash
$hash = substr($hash, $offset - $last_offset);
// Add the part to the password, appending the salt character
$password .= $part.array_shift($salt);
// Set the last offset to the current offset
$last_offset = $offset;
}
// Return the password, with the remaining hash appended
return $password.$hash;
}
public static instance( ) (defined in Kohana_Auth)
Singleton pattern
Return Values
Auth
Source Code
public static function instance()
{
if ( ! isset(Auth::$_instance))
{
// Load the configuration for this type
$config = Kohana::config('auth');
if ( ! $type = $config->get('driver'))
{
$type = 'ORM';
}
// Set the session class name
$class = 'Auth_'.ucfirst($type);
// Create a new session instance
Auth::$_instance = new $class($config);
}
return Auth::$_instance;
}
public logged_in( [ string $role = NULL ] ) (defined in Kohana_Auth)
Check if there is an active session. Optionally allows checking for a specific role.
Parameters
-
string$role = NULL - Role name
Return Values
mixed
Source Code
public function logged_in($role = NULL)
{
return FALSE !== $this->get_user();
}
public login( string $username , string $password [, boolean $remember = bool FALSE ] ) (defined in Kohana_Auth)
Attempt to log in a user by using an ORM object and plain-text password.
Parameters
-
string$username required - Username to log in -
string$password required - Password to check against -
boolean$remember = bool FALSE - Enable autologin
Return Values
boolean
Source Code
public function login($username, $password, $remember = FALSE)
{
if (empty($password))
return FALSE;
if (is_string($password))
{
// Get the salt from the stored password
$salt = $this->find_salt($this->password($username));
// Create a hashed password using the salt from the stored password
$password = $this->hash_password($password, $salt);
}
return $this->_login($username, $password, $remember);
}
public logout( [ boolean $destroy = bool FALSE , boolean $logout_all = bool FALSE ] ) (defined in Kohana_Auth)
Log out a user by removing the related session variables.
Parameters
-
boolean$destroy = bool FALSE - Completely destroy the session -
boolean$logout_all = bool FALSE - Remove all tokens for user
Return Values
boolean
Source Code
public function logout($destroy = FALSE, $logout_all = FALSE)
{
if ($destroy === TRUE)
{
// Destroy the session completely
$this->_session->destroy();
}
else
{
// Remove the user from the session
$this->_session->delete($this->_config['session_key']);
// Regenerate session_id
$this->_session->regenerate();
}
// Double check
return ! $this->logged_in();
}
protected _login( string $username , string $password , boolean $remember ) (defined in Kohana_Auth_File)
Logs a user in.
Parameters
-
string$username required - Username -
string$password required - Password -
boolean$remember required - Enable autologin (not supported)
Return Values
boolean
Source Code
protected function _login($username, $password, $remember)
{
if (isset($this->_users[$username]) AND $this->_users[$username] === $password)
{
// Complete the login
return $this->complete_login($username);
}
// Login failed
return FALSE;
}
protected complete_login( ) (defined in Kohana_Auth)
Source Code
protected function complete_login($user)
{
// Regenerate session_id
$this->_session->regenerate();
// Store username in session
$this->_session->set($this->_config['session_key'], $user);
return TRUE;
}