Auth.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * User authorization library. Handles user login and logout, as well as secure
  4. * password hashing.
  5. *
  6. * @package KO7/Auth
  7. *
  8. * @copyright (c) 2007-2016 Kohana Team
  9. * @copyright (c) since 2016 Koseven Team
  10. * @license https://koseven.dev/LICENSE
  11. */
  12. abstract class KO7_Auth {
  13. // Auth instances
  14. protected static $_instance;
  15. /**
  16. * Singleton pattern
  17. *
  18. * @return Auth
  19. */
  20. public static function instance()
  21. {
  22. if ( ! isset(Auth::$_instance))
  23. {
  24. // Load the configuration for this type
  25. $config = KO7::$config->load('auth');
  26. if ( ! $type = $config->get('driver'))
  27. {
  28. $type = 'file';
  29. }
  30. // Set the session class name
  31. $class = 'Auth_'.ucfirst($type);
  32. // Create a new session instance
  33. Auth::$_instance = new $class($config);
  34. }
  35. return Auth::$_instance;
  36. }
  37. protected $_session;
  38. protected $_config;
  39. /**
  40. * Loads Session and configuration options.
  41. *
  42. * @param array $config Config Options
  43. * @return void
  44. */
  45. public function __construct($config = [])
  46. {
  47. // Save the config in the object
  48. $this->_config = $config;
  49. $this->_session = Session::instance($this->_config['session_type']);
  50. }
  51. abstract protected function _login($username, $password, $remember);
  52. abstract public function password($username);
  53. abstract public function check_password($password);
  54. /**
  55. * Gets the currently logged in user from the session.
  56. * Returns NULL if no user is currently logged in.
  57. *
  58. * @param mixed $default Default value to return if the user is currently not logged in.
  59. * @return mixed
  60. */
  61. public function get_user($default = NULL)
  62. {
  63. return $this->_session->get($this->_config['session_key'], $default);
  64. }
  65. /**
  66. * Attempt to log in a user by using an ORM object and plain-text password.
  67. *
  68. * @param string $username Username to log in
  69. * @param string $password Password to check against
  70. * @param boolean $remember Enable autologin
  71. * @return boolean
  72. */
  73. public function login($username, $password, $remember = FALSE)
  74. {
  75. if (empty($password))
  76. return FALSE;
  77. return $this->_login($username, $password, $remember);
  78. }
  79. /**
  80. * Log out a user by removing the related session variables.
  81. *
  82. * @param boolean $destroy Completely destroy the session
  83. * @param boolean $logout_all Remove all tokens for user
  84. * @return boolean
  85. */
  86. public function logout($destroy = FALSE, $logout_all = FALSE)
  87. {
  88. if ($destroy === TRUE)
  89. {
  90. // Destroy the session completely
  91. $this->_session->destroy();
  92. }
  93. else
  94. {
  95. // Remove the user from the session
  96. $this->_session->delete($this->_config['session_key']);
  97. // Regenerate session_id
  98. $this->_session->regenerate();
  99. }
  100. // Double check
  101. return ! $this->logged_in();
  102. }
  103. /**
  104. * Check if there is an active session. Optionally allows checking for a
  105. * specific role.
  106. *
  107. * @param string $role role name
  108. * @return mixed
  109. */
  110. public function logged_in($role = NULL)
  111. {
  112. return ($this->get_user() !== NULL);
  113. }
  114. /**
  115. * Perform a hmac hash, using the configured method.
  116. *
  117. * @param string $str string to hash
  118. * @return string
  119. */
  120. public function hash($str)
  121. {
  122. if ( ! $this->_config['hash_key'])
  123. throw new KO7_Exception('A valid hash key must be set in your auth config.');
  124. return hash_hmac($this->_config['hash_method'], $str, $this->_config['hash_key']);
  125. }
  126. protected function complete_login($user)
  127. {
  128. // Regenerate session_id
  129. $this->_session->regenerate();
  130. // Store username in session
  131. $this->_session->set($this->_config['session_key'], $user);
  132. return TRUE;
  133. }
  134. }