/modules/openid/classes/openid/auth.php

https://github.com/cms3/cms3 · PHP · 191 lines · 79 code · 28 blank · 84 comment · 5 complexity · ff518a690b7d9930661b853aa010286e MD5 · raw file

  1. <?php
  2. /**
  3. * Library Class for handling OpenID authentication.
  4. *
  5. * $Id: auth.php 2008-09-25 17:28:07 BST Atomless $
  6. *
  7. * Instantiation of this class initiates a chain of extension down to the Openid base class:
  8. * Openid_Auth <- Openid_Relyingparty <- Openid_Response <-
  9. * Openid_Association <- Openid_Discovery <- Openid_Request <- Openid
  10. *
  11. * @package Openid
  12. * @author Kohana Team
  13. * @copyright (c) 2007-2008 Kohana Team
  14. * @license http://kohanaphp.com/license.html
  15. */
  16. class Openid_Auth extends Openid_Relyingparty {
  17. /**
  18. * Singleton instance of Openid_Auth.
  19. */
  20. public static function instance()
  21. {
  22. static $instance;
  23. // Create the instance if it does not exist
  24. ($instance === NULL) AND $instance = new Openid_Auth;
  25. return $instance;
  26. }
  27. /**
  28. * Create an instance of Openid_Auth.
  29. *
  30. * @param array - openid fields contained in parent Openid class
  31. * @return object
  32. */
  33. public static function factory($fields = array())
  34. {
  35. return new Openid_Auth($fields);
  36. }
  37. /**
  38. * Constructor.
  39. *
  40. * @param array - openid fields to be set in the base Openid.php class
  41. * @return void
  42. */
  43. public function __construct($fields = array())
  44. {
  45. // See the set_authentication_fields method of the parent Openid library class
  46. // and the Openid_identifier helper for the steps taken when
  47. // the claimed_id is set to a param passed to the Openid constructor
  48. parent::__construct($fields);
  49. }
  50. /**
  51. * Check if there's a user session
  52. *
  53. * @return boolean
  54. */
  55. public function logged_in()
  56. {
  57. $logged_in_user = $this->session->get('user', FALSE);
  58. // Checks if a user is logged in and valid
  59. return ( ! empty($logged_in_user)
  60. AND is_object($logged_in_user)
  61. AND ($logged_in_user instanceof Model_User)
  62. AND $logged_in_user->loaded);
  63. }
  64. /**
  65. * Logs a user in.
  66. *
  67. * @param String username
  68. * @param boolean enable auto-login
  69. * @return boolean
  70. */
  71. public function login($user, $remember)
  72. {
  73. if ($remember)
  74. {
  75. // Create a new autologin token
  76. $token = new Model_User_Token;
  77. // Set token data
  78. $token->user_id = $user->id;
  79. $token->expires = time() + KOHANA::config('openid.login_token_lifetime');
  80. $token->save();
  81. // Set the autologin cookie - links to user_token in the db
  82. cookie::set('openidautologin', $token->token, KOHANA::config('openid.login_token_lifetime'));
  83. }
  84. // Finish the login
  85. $this->complete_login($user);
  86. return TRUE;
  87. }
  88. /**
  89. * Logs a user in, based on stored credentials in authautologin cookie.
  90. *
  91. * @return boolean
  92. */
  93. public function auto_login()
  94. {
  95. if ($token = cookie::get('openidautologin'))
  96. {
  97. // Load the token and user
  98. $token = new Model_User_token($token);
  99. if ($token->id > 0 AND $token->user->id > 0)
  100. {
  101. if ($token->user_agent === sha1(Kohana::$user_agent))
  102. {
  103. // Save the token to create a new unique token
  104. $token->save();
  105. // Set the new token
  106. cookie::set('openidautologin', $token->token, $token->expires - time());
  107. // Complete the login with the found data
  108. $this->complete_login($token->user);
  109. // Automatic login was successful
  110. return TRUE;
  111. }
  112. // Token is invalid
  113. $token->delete();
  114. }
  115. }
  116. return FALSE;
  117. }
  118. /**
  119. * Complete the login for an openid user by incrementing the logins and setting
  120. * session data: user_id, username, roles
  121. *
  122. * @param object user model object
  123. * @return void
  124. */
  125. protected function complete_login(Model_User $user)
  126. {
  127. // Update the number of logins
  128. $user->logins += 1;
  129. // Set the last login date
  130. $user->last_login = time();
  131. // Save the user
  132. $user->save();
  133. // Regenerate session_id
  134. $this->session->regenerate();
  135. // Store session data
  136. $this->session->set('user', $user);
  137. }
  138. /**
  139. * Log a user out.
  140. *
  141. * @param boolean completely destroy the session - also delete authautologin cookie
  142. * @return boolean
  143. */
  144. public function logout($destroy)
  145. {
  146. // Delete the autologin cookie if it exists
  147. cookie::get('openidautologin') and cookie::delete('openidautologin');
  148. if ($destroy === TRUE)
  149. {
  150. // Destroy the session completely
  151. $this->session->destroy();
  152. }
  153. else
  154. {
  155. // Remove the user object from the session
  156. $this->session->delete('user');
  157. // Regenerate session_id
  158. $this->session->regenerate();
  159. }
  160. // Double check
  161. return ! $this->session->get('user', FALSE);
  162. }
  163. }