callback.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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", PluginSinglesignonToolbox::getBaseURL() . '/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 ($_SESSION["glpiactiveprofile"]["interface"] == "helpdesk") {
  72. if ($_SESSION['glpiactiveprofile']['create_ticket_on_login'] && empty($REDIRECT)) {
  73. $url_redirect = PluginSinglesignonToolbox::getBaseURL() . "/front/helpdesk.public.php?create_ticket=1";
  74. } else {
  75. $url_redirect = PluginSinglesignonToolbox::getBaseURL() . "/front/helpdesk.public.php$REDIRECT";
  76. }
  77. } else {
  78. if ($_SESSION['glpiactiveprofile']['create_ticket_on_login'] && empty($REDIRECT)) {
  79. $url_redirect = PluginSinglesignonToolbox::getBaseURL() . "/front/ticket.form.php";
  80. } else {
  81. $url_redirect = PluginSinglesignonToolbox::getBaseURL() . "/front/central.php$REDIRECT";
  82. }
  83. }
  84. Html::nullHeader("Login", PluginSinglesignonToolbox::getBaseURL() . '/index.php');
  85. echo '<div class="center spaced"><a href="' . $url_redirect . '">' .
  86. __sso('Automatic redirection, else click') . '</a>';
  87. echo '<script type="text/javascript">
  88. if (window.opener) {
  89. window.opener.location="' . $url_redirect . '";
  90. window.close();
  91. } else {
  92. window.location="' . $url_redirect . '";
  93. }
  94. </script></div>';
  95. Html::nullFooter();
  96. exit();
  97. // Auth::redirectIfAuthenticated();
  98. }
  99. // we have done at least a good login? No, we exit.
  100. Html::nullHeader("Login", PluginSinglesignonToolbox::getBaseURL() . '/index.php');
  101. echo '<div class="center b">' . __('User not authorized to connect in GLPI') . '<br><br>';
  102. // Logout whit noAUto to manage auto_login with errors
  103. echo '<a href="' . PluginSinglesignonToolbox::getBaseURL() . '/front/logout.php?noAUTO=1' .
  104. str_replace("?", "&", $REDIRECT) . '" class="singlesignon">' . __('Log in again') . '</a></div>';
  105. echo '<script type="text/javascript">
  106. if (window.opener) {
  107. $(".singlesignon").on("click", function (e) {
  108. e.preventDefault();
  109. window.opener.location = $(this).attr("href");
  110. window.focus();
  111. window.close();
  112. });
  113. }
  114. </script>';
  115. Html::nullFooter();
  116. exit();