callback.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // === ROBUSTER SSO-LOGIN BEGIN ===
  58. try {
  59. $provider = $signon_provider->getProvider();
  60. $token = $provider->getAccessToken('authorization_code', [
  61. 'code' => $_GET['code']
  62. ]);
  63. $userinfo = $provider->getResourceOwner($token)->toArray();
  64. // Debug-Log schreiben
  65. file_put_contents('/tmp/glpi_sso_debug.log', print_r($userinfo, true));
  66. // Felder mit Fallbacks auslesen
  67. $email = $userinfo['email'] ?? $userinfo['preferred_username'] ?? null;
  68. $name = $userinfo['name'] ?? ($email ? explode('@', $email)[0] : null);
  69. if (!$email && !$name) {
  70. Toolbox::logInFile('ssodebug', 'Kein gültiger Benutzername/email in Azure-Antwort', true);
  71. Html::displayErrorAndDie(__('SSO-Login: Benutzerinformationen fehlen oder unvollständig.', 'singlesignon'));
  72. }
  73. // Eindeutigen Benutzer in GLPI suchen
  74. $username = $signon_provider->fields['use_email_for_login'] ? $email : $name;
  75. $users = getAllDataFromTable('glpi_users', ['name' => $username]);
  76. if (count($users) !== 1) {
  77. Toolbox::logInFile('ssodebug', "GLPI-Benutzer '$username' mehrfach oder nicht gefunden", true);
  78. Html::displayErrorAndDie(__('SSO-Login: Benutzer nicht eindeutig zuordenbar. Kontaktieren Sie den Admin.', 'singlesignon'));
  79. }
  80. // Setze Session manuell (falls benötigt)
  81. $user = new User();
  82. $user->getFromDB(current($users)['id']);
  83. Session::init($user->getID());
  84. } catch (Exception $e) {
  85. Toolbox::logInFile('ssodebug', 'SSO-Ausnahme: ' . $e->getMessage(), true);
  86. Html::displayErrorAndDie(__('SSO-Fehler: ' . $e->getMessage(), 'singlesignon'));
  87. }
  88. // === ROBUSTER SSO-LOGIN ENDE ===
  89. $user_id = Session::getLoginUserID();
  90. $REDIRECT = "";
  91. if ($user_id || $signon_provider->login()) {
  92. $user_id = $user_id ?: Session::getLoginUserID();
  93. if ($user_id) {
  94. $signon_provider->linkUser($user_id);
  95. }
  96. $params = PluginSinglesignonToolbox::getCallbackParameters('q');
  97. if (isset($params['redirect'])) {
  98. $REDIRECT = '?redirect=' . $params['redirect'];
  99. } else if (isset($_GET['state']) && is_integer(strpos($_GET['state'], ";redirect="))) {
  100. $REDIRECT = '?' . substr($_GET['state'], strpos($_GET['state'], ";redirect=") + 1);
  101. }
  102. $url_redirect = '';
  103. if ($_SESSION["glpiactiveprofile"]["interface"] == "helpdesk") {
  104. if ($_SESSION['glpiactiveprofile']['create_ticket_on_login'] && empty($REDIRECT)) {
  105. $url_redirect = PluginSinglesignonToolbox::getBaseURL() . "/front/helpdesk.public.php?create_ticket=1";
  106. } else {
  107. $url_redirect = PluginSinglesignonToolbox::getBaseURL() . "/front/helpdesk.public.php$REDIRECT";
  108. }
  109. } else {
  110. if ($_SESSION['glpiactiveprofile']['create_ticket_on_login'] && empty($REDIRECT)) {
  111. $url_redirect = PluginSinglesignonToolbox::getBaseURL() . "/front/ticket.form.php";
  112. } else {
  113. $url_redirect = PluginSinglesignonToolbox::getBaseURL() . "/front/central.php$REDIRECT";
  114. }
  115. }
  116. Html::nullHeader("Login", PluginSinglesignonToolbox::getBaseURL() . '/index.php');
  117. echo '<div class="center spaced"><a href="' . $url_redirect . '">' .
  118. __sso('Automatic redirection, else click') . '</a>';
  119. echo '<script type="text/javascript">
  120. if (window.opener) {
  121. window.opener.location="' . $url_redirect . '";
  122. window.close();
  123. } else {
  124. window.location="' . $url_redirect . '";
  125. }
  126. </script></div>';
  127. Html::nullFooter();
  128. exit();
  129. // Auth::redirectIfAuthenticated();
  130. }
  131. // we have done at least a good login? No, we exit.
  132. Html::nullHeader("Login", PluginSinglesignonToolbox::getBaseURL() . '/index.php');
  133. echo '<div class="center b">' . __('User not authorized to connect in GLPI') . '<br><br>';
  134. // Logout whit noAUto to manage auto_login with errors
  135. echo '<a href="' . PluginSinglesignonToolbox::getBaseURL() . '/front/logout.php?noAUTO=1' .
  136. str_replace("?", "&", $REDIRECT) . '" class="singlesignon">' . __('Log in again') . '</a></div>';
  137. echo '<script type="text/javascript">
  138. if (window.opener) {
  139. $(".singlesignon").on("click", function (e) {
  140. e.preventDefault();
  141. window.opener.location = $(this).attr("href");
  142. window.focus();
  143. window.close();
  144. });
  145. }
  146. </script>';
  147. Html::nullFooter();
  148. exit();