sso_server_test.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /*
  3. SSO sample
  4. This is the "server" of my institution/university authentification "code"
  5. 1. Active all the SSO option in your Chamilo installation: main/admin/settings.php?category=Security
  6. 2. Copy the main/auth/sso/sso.class.php file to something else representing your remote system, like
  7. sso.Remote.class.php and modify the class name in soo.Remote.class.php to "ssoRemote"
  8. 3. Insert the following setting manually in your database (change the selected_value from 'Remote'
  9. to the name of your system (used in the filename and classname above).
  10. INSERT INTO settings_current (variable, subkey, type, category, selected_value, title, comment, scope, subkeytext)
  11. VALUES ('sso_authentication_subclass',NULL,'textfield','Security','Remote','SSOAuthSubClassTitle','SSOAuthSubClassComment',NULL,NULL);
  12. 4. Make sure this script is located in the index page of the server you fill in the "Domain of the Single Sign On server" Chamilo setting
  13. For example this script must be located in example.com/index.php if you set the "Domain of the Single Sign On server" = example.com
  14. 5. Create a user in chamilo and in your external system with login = "joe" and password = "doe"
  15. 6. Remember this is just a sample! Check the chamilo drupal extension for more information:
  16. http://drupal.org/node/817682
  17. 7. When activating the settings in step 1, the principal Chamilo file main/inc/local.inc.php will load the class main/auth/sso.[class.php library
  18. * that will redirect to this field with some parameters.
  19. *
  20. */
  21. exit; //Uncomment this to execute the page
  22. //After you located this file in you new domain and you set the settings in step 2,
  23. //this page will be loaded when entering to the Chamilo site if the SSO option was set in step 1.
  24. //Getting the chamilo server
  25. $my_chamilo_server = filter_xss($_SERVER['HTTP_HOST']);
  26. $account = [];
  27. if (isset($_SESSION['my_server_user_session'])) {
  28. //validate if the user is already logged in my external system in order to redirect to chamilo
  29. }
  30. //Login process
  31. if (isset($_POST['user']) && isset($_POST['password'])) {
  32. //1. Your Server validations
  33. $validate = validate_user($_POST['user'], $_POST['password']);
  34. if ($validate) {
  35. /* 2.Get the chamilo username and password from your system or from webservices */
  36. $account['username'] = 'jbrion525'; //username in Chamilo
  37. $account['password'] = sha1(sha1('jbrion525')); //encrypted password with assuming that the first encrypted method is sha1 in chamilo
  38. $master_auth_uri = $my_chamilo_server.'/?q=user';
  39. // Creating an array cookie that will be sent to Chamilo
  40. $sso = [
  41. 'username' => $account['username'],
  42. 'secret' => $account['password'],
  43. 'master_domain' => $my_chamilo_server,
  44. 'master_auth_uri' => $master_auth_uri,
  45. 'lifetime' => time() + 3600,
  46. 'target' => filter_xss($_GET['sso_target']),
  47. ];
  48. $cookie = base64_encode(serialize($sso));
  49. $url = chamilo_sso_protocol().$master_auth_uri;
  50. $params = 'sso_referer='.urlencode($url).'&sso_cookie='.urlencode($cookie);
  51. $final_url = filter_xss($_GET['sso_referer']).'?'.$params;
  52. //If your user exists redirect to chamilo and set the account in a session to check it later
  53. $_SESSION['my_server_user_session'] = $account;
  54. //3. After validating the user in the server and getting and setting the user data of chamilo in the sso_cookie variable:
  55. // Redirect to this URL
  56. header('Location: '.$final_url);
  57. exit;
  58. } else {
  59. echo '<h2>Wrong parameters</h2>';
  60. }
  61. }
  62. if (isset($_POST['logout'])) {
  63. //echo do something to logout
  64. }
  65. function validate_user($user, $pass)
  66. {
  67. return true;
  68. }
  69. function filter_xss($val)
  70. {
  71. //do some cleaning
  72. return $val;
  73. }
  74. function chamilo_sso_protocol()
  75. {
  76. //get the sso_protocol from chamilo using webservices
  77. return 'http://';
  78. }
  79. ?>
  80. <html>
  81. <form method="post">
  82. User <input name="user"/>
  83. Pass <input name="password" />
  84. <input type="submit" value="Login">
  85. </form>
  86. </html>