sso_server_test.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. 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
  7. 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
  8. 3. Create a user in chamilo and in your external system with login = "joe" and password = "doe"
  9. 4. Remember this is just a sample! Check the chamilo drupal extension for more information:
  10. http://drupal.org/node/817682
  11. 5. 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
  12. * that will redirect to this field with some parameters.
  13. *
  14. */
  15. exit; //Uncomment this to execute the page
  16. //After you located this file in you new domain and you set the settings in step 2,
  17. //this page will be loaded when entering to the Chamilo site if the SSO option was set in step 1.
  18. //Getting the chamilo server
  19. $my_chamilo_server = filter_xss($_SERVER['HTTP_HOST']);
  20. $account = array();
  21. if (isset($_SESSION['my_server_user_session'])) {
  22. //validate if the user is already logged in my external system in order to redirect to chamilo
  23. }
  24. //Login process
  25. if (isset($_POST['user']) && isset($_POST['password'])) {
  26. //1. Your Server validations
  27. $validate = validate_user($_POST['user'], $_POST['password']);
  28. if ($validate) {
  29. /* 2.Get the chamilo username and password from your system or from webservices */
  30. $account['username'] = 'jbrion525'; //username in Chamilo
  31. $account['password'] = sha1(sha1('jbrion525')); //encrypted password with assuming that the first encrypted method is sha1 in chamilo
  32. $master_auth_uri = $my_chamilo_server.'/?q=user';
  33. // Creating an array cookie that will be sent to Chamilo
  34. $sso = array(
  35. 'username' => $account['username'],
  36. 'secret' => $account['password'],
  37. 'master_domain' => $my_chamilo_server,
  38. 'master_auth_uri' => $master_auth_uri,
  39. 'lifetime' => time() + 3600,
  40. 'target' => filter_xss($_GET['sso_target']),
  41. );
  42. $cookie = base64_encode(serialize($sso));
  43. $url = chamilo_sso_protocol() . $master_auth_uri;
  44. $params = 'sso_referer='. urlencode($url) .'&sso_cookie='. urlencode($cookie);
  45. $final_url = filter_xss($_GET['sso_referer']) .'?'. $params;
  46. //If your user exists redirect to chamilo and set the account in a session to check it later
  47. $_SESSION['my_server_user_session'] = $account;
  48. //3. After validating the user in the server and getting and setting the user data of chamilo in the sso_cookie variable:
  49. // Redirect to this URL
  50. header('Location: '.$final_url);
  51. exit;
  52. } else {
  53. echo '<h2>Wrong parameters</h2>';
  54. }
  55. }
  56. if (isset($_POST['logout'])) {
  57. //echo do something to logout
  58. }
  59. function validate_user($user, $pass) {
  60. return true;
  61. }
  62. function filter_xss($val) {
  63. //do some cleaning
  64. return $val;
  65. }
  66. function chamilo_sso_protocol() {
  67. //get the sso_protocol from chamilo using webservices
  68. return 'http://';
  69. }
  70. ?>
  71. <html>
  72. <form method="post">
  73. User <input name="user"/>
  74. Pass <input name="password" />
  75. <input type="submit" value="Login">
  76. </form>
  77. </html>