callback.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * ---------------------------------------------------------------------
  4. * SingleSignOn is a plugin which allows to use SSO for auth
  5. * ---------------------------------------------------------------------
  6. * Copyright (C) 2022 Edgard
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. * ---------------------------------------------------------------------
  21. * @copyright Copyright © 2021 - 2022 Edgard
  22. * @license http://www.gnu.org/licenses/gpl.txt GPLv3+
  23. * @link https://github.com/edgardmessias/glpi-singlesignon/
  24. * ---------------------------------------------------------------------
  25. */
  26. //Disable CSRF token
  27. define('GLPI_USE_CSRF_CHECK', 0);
  28. ini_set('display_errors', 1);
  29. ini_set('display_startup_errors', 1);
  30. error_reporting(E_ALL);
  31. include('../../../inc/includes.php');
  32. $provider_id = PluginSinglesignonToolbox::getCallbackParameters('provider');
  33. if (!$provider_id) {
  34. Html::displayErrorAndDie(__sso("Provider not defined."), false);
  35. }
  36. $signon_provider = new PluginSinglesignonProvider();
  37. if (!$signon_provider->getFromDB($provider_id)) {
  38. Html::displayErrorAndDie(__sso("Provider not found."), true);
  39. }
  40. if (!$signon_provider->fields['is_active']) {
  41. Html::displayErrorAndDie(__sso("Provider not active."), true);
  42. }
  43. $signon_provider->checkAuthorization();
  44. $test = PluginSinglesignonToolbox::getCallbackParameters('test');
  45. if ($test) {
  46. $signon_provider->debug = true;
  47. Html::nullHeader("Login", $CFG_GLPI["root_doc"] . '/index.php');
  48. echo '<div class="left spaced">';
  49. echo '<pre>';
  50. echo "### BEGIN ###\n";
  51. $signon_provider->getResourceOwner();
  52. echo "### END ###";
  53. echo '</pre>';
  54. Html::nullFooter();
  55. exit();
  56. }
  57. $user_id = Session::getLoginUserID();
  58. $REDIRECT = "";
  59. if ($user_id || $signon_provider->login()) {
  60. $user_id = $user_id ?: Session::getLoginUserID();
  61. if ($user_id) {
  62. $signon_provider->linkUser($user_id);
  63. }
  64. $params = PluginSinglesignonToolbox::getCallbackParameters('q');
  65. if (isset($params['redirect'])) {
  66. $REDIRECT = '?redirect=' . $params['redirect'];
  67. } else if (isset($_GET['state']) && is_integer(strpos($_GET['state'], "&redirect="))) {
  68. $REDIRECT = '?' . substr($_GET['state'], strpos($_GET['state'], "&redirect=") + 1);
  69. }
  70. $url_redirect = '';
  71. if (isset($params['redirect'])) {
  72. $REDIRECT = '?redirect=' . $params['redirect'];
  73. } else if (isset($_GET['state']) && is_integer(strpos($_GET['state'], "&redirect="))) {
  74. $REDIRECT = '?' . substr($_GET['state'], strpos($_GET['state'], "&redirect=") + 1);
  75. }
  76. if ($_SESSION["glpiactiveprofile"]["interface"] == "helpdesk") {
  77. if ($_SESSION['glpiactiveprofile']['create_ticket_on_login'] && empty($REDIRECT)) {
  78. $url_redirect = $CFG_GLPI['root_doc'] . "/front/helpdesk.public.php?create_ticket=1";
  79. } else {
  80. $url_redirect = $CFG_GLPI['root_doc'] . "/front/helpdesk.public.php$REDIRECT";
  81. }
  82. } else {
  83. if ($_SESSION['glpiactiveprofile']['create_ticket_on_login'] && empty($REDIRECT)) {
  84. $url_redirect = $CFG_GLPI['root_doc'] . "/front/ticket.form.php";
  85. } else {
  86. $url_redirect = $CFG_GLPI['root_doc'] . "/front/central.php$REDIRECT";
  87. }
  88. }
  89. Html::nullHeader("Login", $CFG_GLPI["root_doc"] . '/index.php');
  90. echo '<div class="center spaced"><a href="' . $url_redirect . '">' .
  91. __sso('Automatic redirection, else click') . '</a>';
  92. echo '<script type="text/javascript">
  93. if (window.opener) {
  94. window.opener.location="' . $url_redirect . '";
  95. window.close();
  96. } else {
  97. window.location="' . $url_redirect . '";
  98. }
  99. </script></div>';
  100. Html::nullFooter();
  101. exit();
  102. // Auth::redirectIfAuthenticated();
  103. }
  104. // we have done at least a good login? No, we exit.
  105. Html::nullHeader("Login", $CFG_GLPI["root_doc"] . '/index.php');
  106. echo '<div class="center b">' . __('User not authorized to connect in GLPI') . '<br><br>';
  107. // Logout whit noAUto to manage auto_login with errors
  108. echo '<a href="' . $CFG_GLPI["root_doc"] . '/front/logout.php?noAUTO=1' .
  109. str_replace("?", "&", $REDIRECT) . '" class="singlesignon">' . __('Log in again') . '</a></div>';
  110. echo '<script type="text/javascript">
  111. if (window.opener) {
  112. $(".singlesignon").on("click", function (e) {
  113. e.preventDefault();
  114. window.opener.location = $(this).attr("href");
  115. window.focus();
  116. window.close();
  117. });
  118. }
  119. </script>';
  120. Html::nullFooter();
  121. exit();