Your Ad Here

Posted By

mladoux on 02/06/12


Tagged

codeigniter


Versions (?)

Auth Library


 / Published in: PHP
 

URL: http://markladoux.com

Simple Authentication Library

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. /**
  4.  * Auth Class
  5.  *
  6.  * Simple authentication library for my blog
  7.  *
  8.  * @author Mark A. LaDoux
  9.  * @copyright Copyright © 2012 Mark LaDoux
  10.  * @link http://markladoux.com/
  11.  * @version 1.0.0
  12.  */
  13.  
  14. class Auth
  15. {
  16. /**
  17. * Internal Variables
  18. *
  19. * @param object $ci CodeIgniter instance
  20. * @param integer $rounds number of iterations to process the hash
  21. * @param string $user_table table to use for user data
  22. * @param array $errors array of errors
  23. */
  24.  
  25. protected $ci = null;
  26. protected static $rounds = null;
  27. protected static $user_table = null;
  28. protected static $errors = array();
  29.  
  30. /**
  31. * __construct
  32. *
  33. * prepares the library for first use
  34. *
  35. * @access public
  36. * @since 1.0.0
  37. * @return void
  38. */
  39.  
  40. public function __construct()
  41. {
  42. // get CodeIgniter Instance
  43. $this->ci =& get_instance();
  44.  
  45. // load required libraries
  46. $this->ci->load->library('database');
  47. $this->ci->load->library('session');
  48.  
  49. // get config settings
  50. self::$rounds = $this->ci->config->item('auth_rounds');
  51. self::$user_table = $this->ci->config->item('auth_user_table');
  52.  
  53. // run a couple tests
  54. if(self::$rounds < 4 || self::$rounds > 32) self::$rounds = 8;
  55. if(self::$user_table === false) self::$user_table = 'users';
  56. }
  57.  
  58. /**
  59. * generate_hash
  60. *
  61. * generates a random password hash
  62. *
  63. * @access protected
  64. * @since 1.0.0
  65. * @param string $password password to hash
  66. * @return string hash of $password
  67. */
  68.  
  69. protected function _generate_hash($password)
  70. {
  71. // generate a salt
  72. $lowercase = str_shuffle('abcdefghijklmnopqrstuvwxyz');
  73. $uppercase = str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
  74. $other = str_shuffle('./');
  75. $legal_chars = str_shuffle($lowercase.$uppercase.$other);
  76. $salt = '';
  77.  
  78. for($i = 0; $i < 22; $i++)
  79. {
  80. $salt .= $legal_chars[mt_rand(0,63)];
  81. }
  82.  
  83. // format salt
  84. $salt = sprintf('$2a$%02d$', $this->rounds).str_shuffle($salt);
  85.  
  86. // return hash
  87. return crypt($password, $salt);
  88. }
  89.  
  90. /**
  91. * _verify_hash
  92. *
  93. * tests to see if password matches hash evaluation
  94. *
  95. * @access protected
  96. * @since 1.0.0
  97. * @param string $password password to check
  98. * @param string $stored_hash hash to check password against
  99. * @return bool true if passes, else false
  100. */
  101.  
  102. protected function _verify_password($password, $stored_hash)
  103. {
  104. $test = (crypt($password, $stored_hash) == $stored_hash) ? true : false;
  105. return $test;
  106. }
  107.  
  108. /**
  109. * _check_email
  110. *
  111. * tests if email is valid and available for registration
  112. *
  113. * @access protected
  114. * @since 1.0.0
  115. * @param string $email email address to check
  116. * @return void
  117. */
  118.  
  119. protected function _check_email($email)
  120. {
  121. $valid_email = filter_var($email, FILTER_VALIDATE_EMAIL);
  122. if($valid_email)
  123. {
  124. $this->ci->db->where('email', $email);
  125. $this->ci->db->limit(1);
  126. $query = $this->db->get(self::$user_table);
  127. $in_use = ($query->num_rows() > 0) ? true : false;
  128. }
  129.  
  130. if(! $valid_email)
  131. {
  132. self::$errors['email'] = 'invalid';
  133. }
  134. elseif($in_use)
  135. {
  136. self::$errors['email'] = 'unavailable';
  137. }
  138. }
  139.  
  140. /**
  141. * _check_username
  142. *
  143. * tests if username is available for registration
  144. *
  145. * @access protected
  146. * @since 1.0.0
  147. * @param string $username username to check
  148. * @return void
  149. */
  150.  
  151. protected function _check_username($username)
  152. {
  153. $this->ci->db->where('username', $username);
  154. $this->ci->db->limit(1);
  155. $query = $this->ci->db->get(self::$user_table);
  156. if($query->num_rows() > 0)
  157. {
  158. self::$errors['username'] = 'unavailable';
  159. }
  160. }
  161.  
  162. /**
  163. * login
  164. *
  165. * checks a users credentials and logs him in to the system.
  166. *
  167. * @access public
  168. * @since 1.0.0
  169. * @param string $username user to log in
  170. * @param string $password user's password
  171. * @return bool true if logged in, else false
  172. */
  173.  
  174. public function login($username, $password)
  175. {
  176. $this->db->where('username', $username);
  177. $this->db->limit(1);
  178. $query = $this->db->get(self::$user_table);
  179. if($query->num_rows() < 1)
  180. {
  181. $this->ci->session->set_flashdata('login_error', 'invalid_username');
  182. return false;
  183. }
  184. $data = $query->fetch_assoc();
  185. $valid_password = $this->_verify_password($password, $data['password']);
  186. if($valid_password)
  187. {
  188. // remove password from data array
  189. unset($data['password']);
  190.  
  191. // set the session
  192. $data['logged_in'] = true;
  193. $this->ci->session->set_userdata($row);
  194. return true;
  195. }
  196. else
  197. {
  198. $this->ci->session->set_flashdata('login_error', 'invalid_password');
  199. return false;
  200. }
  201. }
  202.  
  203. /**
  204. * logout
  205. *
  206. * logs user off site.
  207. *
  208. * @access public
  209. * @since 1.0.0
  210. * @return void
  211. */
  212.  
  213. public function logout()
  214. {
  215. $this->ci->session->sess_destroy();
  216. }
  217.  
  218. /**
  219. * create
  220. *
  221. * creates a new user in the database
  222. *
  223. * @access public
  224. * @since 1.0.0
  225. * @param string $username username to create
  226. * @param string $email email address of $username
  227. * @param string $password password for $username
  228. */
  229.  
  230. public function create($username, $email, $password)
  231. {
  232. $this->_check_username($username);
  233. $this->_check_email($email);
  234. if(isset(self::$errrors['username']) || isset(self::$errors['email']))
  235. {
  236. $this->ci->session->set_flashdata(self::$errors);
  237. return false;
  238. }
  239.  
  240. $password_hash = $this->_generate_hash($password);
  241.  
  242. $data['username'] = $username;
  243. $data['password'] = $password_hash;
  244. $data['email'] = $email;
  245.  
  246. $this->ci->db->insert(self::$user_table, $data);
  247. return true;
  248. }
  249.  
  250. /**
  251. * delete
  252. *
  253. * deletes a user from the database
  254. *
  255. * @access public
  256. * @since 1.0.0
  257. * @param string $username username to remove
  258. * @return void
  259. */
  260.  
  261. public function delete($username)
  262. {
  263. $this->ci->db->delete(self::$user_table, array('username' => $username));
  264. }
  265.  
  266. /**
  267. * set_password
  268. *
  269. * changes the password of an existing user
  270. *
  271. * @access public
  272. * @since 1.0.0
  273. * @param string $username username to edit
  274. * @param string $password new password to set
  275. * @return void
  276. */
  277.  
  278. public function set_password($username, $password)
  279. {
  280. $password = $this->_generate_hash($password);
  281. $this->ci->db->where('username', $username);
  282. $this->ci->db->update(self::$user_table, array('password' => $password));
  283. }
  284.  
  285. /**
  286. * set_email
  287. *
  288. * changes the email of an existing user
  289. *
  290. * @access public
  291. * @since 1.0.0
  292. * @param string $username username to edit
  293. * @param string $email new email address to set
  294. * @return void
  295. */
  296.  
  297. public function set_email($username, $email)
  298. {
  299. $this->_check_email($email);
  300. if(isset(self::$errors['email']))
  301. {
  302. $this->ci->session->set_flashdata(self::$errors);
  303. }
  304. else
  305. {
  306. $this->ci->db->where('username', $username);
  307. $this->ci->db->update(self::$user_table, array('email' => $email));
  308. }
  309. }
  310. }

Report this snippet  

You need to login to post a comment.