SsoServer.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file contains the necessary elements to implement a Single Sign On
  5. * using chamilo as a SSO server
  6. * @package chamilo.auth.sso
  7. */
  8. class SsoServer
  9. {
  10. /**
  11. * This is used to get the url with the SSO params
  12. * @param string $refererSso
  13. * @param array $additionalParams
  14. * @return string
  15. */
  16. public function getUrl($refererSso, $additionalParams = array())
  17. {
  18. if (empty($refererSso)) {
  19. return null;
  20. }
  21. $getParams = parse_url($refererSso, PHP_URL_QUERY);
  22. $userInfo = api_get_user_info(api_get_user_id(), false, true);
  23. $chamiloUrl = api_get_path(WEB_PATH);
  24. $sso = [
  25. 'username' => $userInfo['username'],
  26. 'secret' => sha1($userInfo['password']),
  27. 'master_domain' => $chamiloUrl,
  28. 'master_auth_uri' => $chamiloUrl.'?submitAuth=true',
  29. 'lifetime' => time() + 3600,
  30. 'target' => $refererSso,
  31. ];
  32. if (!empty($additionalParams)) {
  33. foreach ($additionalParams as $key => $value) {
  34. if (!empty($key)) {
  35. $sso[$key] = $value;
  36. continue;
  37. }
  38. $sso[] = $value;
  39. }
  40. }
  41. $cookie = base64_encode(serialize($sso));
  42. return $refererSso
  43. .($getParams ? '&' : '?')
  44. .http_build_query([
  45. 'loginFailed' => 0,
  46. 'sso_referer' => $refererSso,
  47. 'sso_cookie' => $cookie
  48. ]);
  49. }
  50. }