|
| 1 | +<?php defined('SYSPATH') OR die('No direct access allowed.'); |
| 2 | +/** |
| 3 | + * User authorization library. Handles user login and logout, as well as secure |
| 4 | + * password hashing. |
| 5 | + * |
| 6 | + * @package Kohana/Auth |
| 7 | + * @author Kohana Team |
| 8 | + * @copyright (c) 2007-2012 Kohana Team |
| 9 | + * @license http://kohanaframework.org/license |
| 10 | + */ |
| 11 | +abstract class Kohana_Auth { |
| 12 | + |
| 13 | + // Auth instances |
| 14 | + protected static $_instance; |
| 15 | + |
| 16 | + /** |
| 17 | + * Singleton pattern |
| 18 | + * |
| 19 | + * @return Auth |
| 20 | + */ |
| 21 | + public static function instance() |
| 22 | + { |
| 23 | + if ( ! isset(Auth::$_instance)) |
| 24 | + { |
| 25 | + // Load the configuration for this type |
| 26 | + $config = Kohana::$config->load('auth'); |
| 27 | + |
| 28 | + if ( ! $type = $config->get('driver')) |
| 29 | + { |
| 30 | + $type = 'file'; |
| 31 | + } |
| 32 | + |
| 33 | + // Set the session class name |
| 34 | + $class = 'Auth_'.ucfirst($type); |
| 35 | + |
| 36 | + // Create a new session instance |
| 37 | + Auth::$_instance = new $class($config); |
| 38 | + } |
| 39 | + |
| 40 | + return Auth::$_instance; |
| 41 | + } |
| 42 | + |
| 43 | + protected $_session; |
| 44 | + |
| 45 | + protected $_config; |
| 46 | + |
| 47 | + /** |
| 48 | + * Loads Session and configuration options. |
| 49 | + * |
| 50 | + * @param array $config Config Options |
| 51 | + * @return void |
| 52 | + */ |
| 53 | + public function __construct($config = array()) |
| 54 | + { |
| 55 | + // Save the config in the object |
| 56 | + $this->_config = $config; |
| 57 | + |
| 58 | + $this->_session = Session::instance($this->_config['session_type']); |
| 59 | + } |
| 60 | + |
| 61 | + abstract protected function _login($username, $password, $remember); |
| 62 | + |
| 63 | + abstract public function password($username); |
| 64 | + |
| 65 | + abstract public function check_password($password); |
| 66 | + |
| 67 | + /** |
| 68 | + * Gets the currently logged in user from the session. |
| 69 | + * Returns NULL if no user is currently logged in. |
| 70 | + * |
| 71 | + * @param mixed $default Default value to return if the user is currently not logged in. |
| 72 | + * @return mixed |
| 73 | + */ |
| 74 | + public function get_user($default = NULL) |
| 75 | + { |
| 76 | + return $this->_session->get($this->_config['session_key'], $default); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Attempt to log in a user by using an ORM object and plain-text password. |
| 81 | + * |
| 82 | + * @param string $username Username to log in |
| 83 | + * @param string $password Password to check against |
| 84 | + * @param boolean $remember Enable autologin |
| 85 | + * @return boolean |
| 86 | + */ |
| 87 | + public function login($username, $password, $remember = FALSE) |
| 88 | + { |
| 89 | + if (empty($password)) |
| 90 | + return FALSE; |
| 91 | + |
| 92 | + return $this->_login($username, $password, $remember); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Log out a user by removing the related session variables. |
| 97 | + * |
| 98 | + * @param boolean $destroy Completely destroy the session |
| 99 | + * @param boolean $logout_all Remove all tokens for user |
| 100 | + * @return boolean |
| 101 | + */ |
| 102 | + public function logout($destroy = FALSE, $logout_all = FALSE) |
| 103 | + { |
| 104 | + if ($destroy === TRUE) |
| 105 | + { |
| 106 | + // Destroy the session completely |
| 107 | + $this->_session->destroy(); |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + // Remove the user from the session |
| 112 | + $this->_session->delete($this->_config['session_key']); |
| 113 | + |
| 114 | + // Regenerate session_id |
| 115 | + $this->_session->regenerate(); |
| 116 | + } |
| 117 | + |
| 118 | + // Double check |
| 119 | + return ! $this->logged_in(); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Check if there is an active session. Optionally allows checking for a |
| 124 | + * specific role. |
| 125 | + * |
| 126 | + * @param string $role role name |
| 127 | + * @return mixed |
| 128 | + */ |
| 129 | + public function logged_in($role = NULL) |
| 130 | + { |
| 131 | + return ($this->get_user() !== NULL); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Creates a hashed hmac password from a plaintext password. This |
| 136 | + * method is deprecated, [Auth::hash] should be used instead. |
| 137 | + * |
| 138 | + * @deprecated |
| 139 | + * @param string $password Plaintext password |
| 140 | + */ |
| 141 | + public function hash_password($password) |
| 142 | + { |
| 143 | + return $this->hash($password); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Perform a hmac hash, using the configured method. |
| 148 | + * |
| 149 | + * @param string $str string to hash |
| 150 | + * @return string |
| 151 | + */ |
| 152 | + public function hash($str) |
| 153 | + { |
| 154 | + if ( ! $this->_config['hash_key']) |
| 155 | + throw new Kohana_Exception('A valid hash key must be set in your auth config.'); |
| 156 | + |
| 157 | + return hash_hmac($this->_config['hash_method'], $str, $this->_config['hash_key']); |
| 158 | + } |
| 159 | + |
| 160 | + protected function complete_login($user) |
| 161 | + { |
| 162 | + // Regenerate session_id |
| 163 | + $this->_session->regenerate(); |
| 164 | + |
| 165 | + // Store username in session |
| 166 | + $this->_session->set($this->_config['session_key'], $user); |
| 167 | + |
| 168 | + return TRUE; |
| 169 | + } |
| 170 | + |
| 171 | +} // End Auth |
0 commit comments