callback.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. //Disable CSRF token
  3. //define('GLPI_USE_CSRF_CHECK', 0);
  4. include ('../../../inc/includes.php');
  5. $params = [];
  6. if (isset($_SERVER['PATH_INFO'])) {
  7. $path_info = trim($_SERVER['PATH_INFO'], '/');
  8. $parts = explode('/', $path_info);
  9. $key = null;
  10. foreach ($parts as $part) {
  11. $part = str_replace('~', '/', $part);
  12. if ($key === null) {
  13. $key = $part;
  14. } else {
  15. $params[$key] = $part;
  16. $key = null;
  17. }
  18. }
  19. }
  20. if (!isset($params['provider'])) {
  21. Html::displayErrorAndDie(__sso("Provider not defined."), false);
  22. }
  23. $provider_id = (int) $params['provider'];
  24. $signon_provider = new PluginSinglesignonProvider();
  25. if (!$signon_provider->getFromDB($provider_id)) {
  26. Html::displayErrorAndDie(__sso("Provider not found."), true);
  27. }
  28. if (!$signon_provider->fields['is_active']) {
  29. Html::displayErrorAndDie(__sso("Provider not active."), true);
  30. }
  31. $httpClient = new GuzzleHttp\Client([
  32. 'verify' => false,
  33. ]);
  34. $collaborators = [
  35. 'httpClient' => $httpClient,
  36. ];
  37. $provider = $signon_provider->prepareProviderInstance([], $collaborators);
  38. $signon_provider->checkAuthorization();
  39. if ($signon_provider->login()) {
  40. $url_redirect = '';
  41. $REDIRECT = "";
  42. if (isset($params['redirect'])) {
  43. $REDIRECT = '?redirect=' . $params['redirect'];
  44. }
  45. if ($_SESSION["glpiactiveprofile"]["interface"] == "helpdesk") {
  46. if ($_SESSION['glpiactiveprofile']['create_ticket_on_login'] && empty($REDIRECT)) {
  47. $url_redirect = $CFG_GLPI['root_doc'] . "/front/helpdesk.public.php?create_ticket=1";
  48. } else {
  49. $url_redirect = $CFG_GLPI['root_doc'] . "/front/helpdesk.public.php$REDIRECT";
  50. }
  51. } else {
  52. if ($_SESSION['glpiactiveprofile']['create_ticket_on_login'] && empty($REDIRECT)) {
  53. $url_redirect = $CFG_GLPI['root_doc'] . "/front/ticket.form.php";
  54. } else {
  55. $url_redirect = $CFG_GLPI['root_doc'] . "/front/central.php$REDIRECT";
  56. }
  57. }
  58. echo "<script type=\"text/javascript\">
  59. if (window.opener) {
  60. window.opener.location='" . $url_redirect . "';
  61. window.close();
  62. } else {
  63. window.location='" . $url_redirect . "';
  64. }
  65. </script>";
  66. exit();
  67. }
  68. try {
  69. // Try to get an access token (using the authorization code grant)
  70. $token = $provider->getAccessToken('authorization_code', [
  71. 'code' => $_GET['code']
  72. ]);
  73. var_dump($token);
  74. // Optional: Now you have a token you can look up a users profile data
  75. // We got an access token, let's now get the user's details
  76. $user = $provider->getResourceOwner($token);
  77. // Use these details to create a new profile
  78. printf('Hello %s!', $user->getNickname());
  79. var_dump($user->toArray());
  80. } catch (Exception $e) {
  81. var_dump($e);
  82. // Failed to get user details
  83. exit('Oh dear...' . $e->getMessage());
  84. }
  85. // Use this to interact with an API on the users behalf
  86. echo $token->getToken();