SsoServer.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. *
  7. * @package chamilo.auth.sso
  8. */
  9. class SsoServer
  10. {
  11. /**
  12. * This is used to get the url with the SSO params.
  13. *
  14. * @param string $refererSso
  15. * @param array $additionalParams
  16. *
  17. * @return string
  18. */
  19. public function getUrl($refererSso, $additionalParams = [])
  20. {
  21. if (empty($refererSso)) {
  22. return null;
  23. }
  24. $getParams = parse_url($refererSso, PHP_URL_QUERY);
  25. $userInfo = api_get_user_info(api_get_user_id(), false, true);
  26. $chamiloUrl = api_get_path(WEB_PATH);
  27. $sso = [
  28. 'username' => $userInfo['username'],
  29. 'secret' => sha1($userInfo['password']),
  30. 'master_domain' => $chamiloUrl,
  31. 'master_auth_uri' => $chamiloUrl.'?submitAuth=true',
  32. 'lifetime' => time() + 3600,
  33. 'target' => $refererSso,
  34. ];
  35. if (!empty($additionalParams)) {
  36. foreach ($additionalParams as $key => $value) {
  37. if (!empty($key)) {
  38. $sso[$key] = $value;
  39. continue;
  40. }
  41. $sso[] = $value;
  42. }
  43. }
  44. $cookie = base64_encode(serialize($sso));
  45. return $refererSso
  46. .($getParams ? '&' : '?')
  47. .http_build_query([
  48. 'loginFailed' => 0,
  49. 'sso_referer' => $refererSso,
  50. 'sso_cookie' => $cookie,
  51. ]);
  52. }
  53. }