sso.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * This file contains the necessary elements to implement a Single Sign On
  4. * mechanism with an arbitrary external web application (given some light
  5. * development there) and is based on the Drupal-Chamilo module implementation.
  6. * To develop a new authentication mechanism, please extend this class and
  7. * overwrite its method, then modify the corresponding calling code in
  8. * main/inc/local.inc.php
  9. */
  10. /**
  11. * The SSO class allows for management or remote Single Sign On resources
  12. */
  13. class sso {
  14. public $protocol; // 'http://',
  15. public $domain; // 'localhost/project/drupal5',
  16. public $auth_uri; // '/?q=user',
  17. public $deauth_uri; // '/?q=logout',
  18. public $referer; // http://my.chamilo.com/main/auth/profile.php
  19. /**
  20. * Instanciates the object, initializing all relevant URL strings
  21. */
  22. public function __construct() {
  23. $this->protocol = api_get_setting('sso_authentication_protocol');
  24. $this->domain = api_get_setting('sso_authentication_domain');
  25. $this->auth_uri = api_get_setting('sso_authentication_auth_uri');
  26. $this->deauth_uri = api_get_setting('sso_authentication_unauth_uri');
  27. $this->referer = $this->protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  28. $this->deauth_url = $this->protocol.$this->domain.$this->deauth_uri;
  29. $this->master_url = $this->protocol.$this->domain.$this->auth_uri;
  30. $this->target = api_get_path(WEB_PATH);
  31. }
  32. /**
  33. * Unlogs the user from the remote server
  34. */
  35. public function logout() {
  36. header('Location: '.$this->deauth_url);
  37. exit;
  38. }
  39. /**
  40. * Sends the user to the master URL for a check of active connection
  41. */
  42. public function ask_master() {
  43. header('Location: '.$this->master_url.'&sso_referer='.urlencode($this->referer).'&sso_target='.urlencode($this->target));
  44. exit;
  45. }
  46. /**
  47. * Validates the received active connection data with the database
  48. * @return bool Return the loginFailed variable value to local.inc.php
  49. */
  50. public function check_user() {
  51. global $_user, $userPasswordCrypted, $_configuration;
  52. $loginFailed = false;
  53. //change the way we recover the cookie depending on how it is formed
  54. $sso = $this->decode_cookie($_GET['sso_cookie']);
  55. //lookup the user in the main database
  56. $user_table = Database::get_main_table(TABLE_MAIN_USER);
  57. $sql = "SELECT user_id, username, password,
  58. auth_source, active, expiration_date
  59. FROM $user_table
  60. WHERE username = '".trim(addslashes($sso['username']))."'";
  61. $result = Database::query($sql);
  62. if (Database::num_rows($result) > 0) {
  63. $uData = Database::fetch_array($result);
  64. //Check the user's password
  65. if ($uData['auth_source'] == PLATFORM_AUTH_SOURCE) {
  66. // Make sure password is encrypted with md5
  67. if (!$userPasswordCrypted) {
  68. $uData['password'] = md5($uData['password']);
  69. }
  70. //the authentification of this user is managed by Chamilo itself
  71. // check the user's password
  72. // password hash comes into a sha1
  73. if ($sso['secret'] === sha1($uData['password'])
  74. && ($sso['username'] == $uData['username'])) {
  75. //Check if the account is active (not locked)
  76. if ($uData['active']=='1') {
  77. // check if the expiration date has not been reached
  78. if ($uData['expiration_date'] > date('Y-m-d H:i:s')
  79. OR $uData['expiration_date']=='0000-00-00 00:00:00') {
  80. //If Multiple URL is enabled
  81. if ($_configuration['multiple_access_urls']) {
  82. $admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
  83. //Check if user is an admin
  84. $sql = "SELECT user_id FROM $admin_table
  85. WHERE user_id = '".trim(addslashes($uData['user_id']))."' LIMIT 1";
  86. $result = Database::query($sql);
  87. $my_user_is_admin = false;
  88. if (Database::num_rows($result) > 0) {
  89. $my_user_is_admin = true;
  90. }
  91. //Check the access_url configuration setting if
  92. // the user is registered in the
  93. // access_url_rel_user table
  94. //Getting the current access_url_id
  95. // of the platform
  96. $current_access_url_id = api_get_current_access_url_id();
  97. // my user is subscribed in these
  98. //sites: $my_url_list
  99. $my_url_list = api_get_access_url_from_user($uData['user_id']);
  100. if ($my_user_is_admin === false) {
  101. if (is_array($my_url_list) && count($my_url_list)>0 ) {
  102. if (in_array($current_access_url_id, $my_url_list)) {
  103. // the user has permission to enter at this site
  104. $_user['user_id'] = $uData['user_id'];
  105. api_session_register('_user');
  106. event_login();
  107. // Redirect to homepage
  108. $sso_target = isset($sso['target']) ? $sso['target'] : api_get_path(WEB_PATH) .'.index.php';
  109. header('Location: '. $sso_target);
  110. exit;
  111. } else {
  112. // user does not have permission for this site
  113. $loginFailed = true;
  114. api_session_unregister('_uid');
  115. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=access_url_inactive');
  116. exit;
  117. }
  118. } else {
  119. // there is no URL in the multiple
  120. // urls list for this user
  121. $loginFailed = true;
  122. api_session_unregister('_uid');
  123. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=access_url_inactive');
  124. exit;
  125. }
  126. } else {
  127. //Only admins of the "main" (first) Chamilo
  128. // portal can login wherever they want
  129. if (in_array(1, $my_url_list)) {
  130. //Check if this admin is admin on the
  131. // principal portal
  132. $_user['user_id'] = $uData['user_id'];
  133. api_session_register('_user');
  134. event_login();
  135. } else {
  136. //Secondary URL admin wants to login
  137. // so we check as a normal user
  138. if (in_array($current_access_url_id, $my_url_list)) {
  139. $_user['user_id'] = $uData['user_id'];
  140. api_session_register('_user');
  141. event_login();
  142. } else {
  143. $loginFailed = true;
  144. api_session_unregister('_uid');
  145. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=access_url_inactive');
  146. exit;
  147. }
  148. }
  149. }
  150. } else {
  151. //Single URL access (Only 1 portal)
  152. $_user['user_id'] = $uData['user_id'];
  153. api_session_register('_user');
  154. event_login();
  155. // Redirect to homepage
  156. $protocol = api_get_setting('sso_authentication_protocol');
  157. $master_url = api_get_setting('sso_authentication_domain');
  158. $target = $protocol.$master_url;
  159. $sso_target = isset($target) ? $target : api_get_path(WEB_PATH) .'.index.php';
  160. header('Location: '. $sso_target);
  161. exit;
  162. }
  163. } else {
  164. // user account expired
  165. $loginFailed = true;
  166. api_session_unregister('_uid');
  167. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=account_expired');
  168. exit;
  169. }
  170. } else {
  171. //User not active
  172. $loginFailed = true;
  173. api_session_unregister('_uid');
  174. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=account_inactive');
  175. exit;
  176. }
  177. } else {
  178. //SHA1 of password is wrong
  179. $loginFailed = true;
  180. api_session_unregister('_uid');
  181. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=wrong_password');
  182. exit;
  183. }
  184. } else {
  185. //Auth_source is wrong
  186. $loginFailed = true;
  187. api_session_unregister('_uid');
  188. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=wrong_authentication_source');
  189. exit;
  190. }
  191. } else {
  192. //No user by that login
  193. $loginFailed = true;
  194. api_session_unregister('_uid');
  195. header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=user_not_found');
  196. exit;
  197. }
  198. return $loginFailed;
  199. }
  200. /**
  201. * Decode the cookie (this function may vary depending on the
  202. * Single Sign On implementation
  203. * @param string Encoded cookie
  204. * @result array Parsed and unencoded cookie
  205. */
  206. private function decode_cookie($cookie) {
  207. return unserialize(base64_decode($cookie));
  208. }
  209. }