functions_googleoauth.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /* Copyright (C) 2007-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2007-2009 Regis Houssin <regis.houssin@inodbox.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/login/functions_googleoauth.php
  20. * \ingroup core
  21. * \brief Authentication functions for Google OAuth mode using "Server flow"
  22. * Another method could be to use the "Implicit flow" using Google-Signin library.
  23. */
  24. //include_once DOL_DOCUMENT_ROOT.'/core/class/openid.class.php';
  25. /**
  26. * Check validity of user/password/entity
  27. * If test is ko, reason must be filled into $_SESSION["dol_loginmesg"]
  28. *
  29. * @param string $usertotest Login
  30. * @param string $passwordtotest Password
  31. * @param int $entitytotest Number of instance (always 1 if module multicompany not enabled)
  32. * @return string Login if OK, '' if KO
  33. */
  34. function check_user_password_googleoauth($usertotest, $passwordtotest, $entitytotest)
  35. {
  36. global $_POST, $conf;
  37. dol_syslog("functions_googleoauth::check_user_password_googleoauth usertotest=".$usertotest." GETPOST('actionlogin')=".GETPOST('actionlogin'));
  38. $login = '';
  39. // Get identity from user and redirect browser to Google OAuth Server
  40. if (GETPOST('actionlogin') == 'login') {
  41. if (GETPOST('beforeoauthloginredirect')) {
  42. // We post the form on the login page by clicking on the link to login using Google.
  43. dol_syslog("We post the form on the login page by clicking on the link to login using Google. We save _SESSION['datafromloginform']");
  44. // We save data of form into a variable
  45. $_SESSION['datafromloginform'] = array(
  46. 'entity'=>GETPOST('entity', 'int'),
  47. 'backtopage'=>GETPOST('backtopage'),
  48. 'tz'=>GETPOST('tz'),
  49. 'tz_string'=>GETPOST('tz_string'),
  50. 'dst_observed'=>GETPOST('dst_observed'),
  51. 'dst_first'=>GETPOST('dst_first'),
  52. 'dst_second'=>GETPOST('dst_second'),
  53. 'dol_screenwidth'=>GETPOST('screenwidth'),
  54. 'dol_screenheight'=>GETPOST('screenheight'),
  55. 'dol_hide_topmenu'=>GETPOST('dol_hide_topmenu'),
  56. 'dol_hide_leftmenu'=>GETPOST('dol_hide_leftmenu'),
  57. 'dol_optimize_smallscreen'=>GETPOST('dol_optimize_smallscreen'),
  58. 'dol_no_mouse_hover'=>GETPOST('dol_no_mouse_hover'),
  59. 'dol_use_jmobile'=>GETPOST('dol_use_jmobile')
  60. );
  61. // Make the redirect to the google_authcallback.php page to start the redirect to Google OAUTH.
  62. // Define $urlwithroot
  63. //global $dolibarr_main_url_root;
  64. //$urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
  65. //$urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  66. $urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  67. //$shortscope = 'userinfo_email,userinfo_profile';
  68. $shortscope = 'openid,email,profile'; // For openid connect
  69. $oauthstateanticsrf = bin2hex(random_bytes(128/8));
  70. $_SESSION['oauthstateanticsrf'] = $shortscope.'-'.$oauthstateanticsrf;
  71. $url = $urlwithroot.'/core/modules/oauth/google_oauthcallback.php?shortscope='.urlencode($shortscope).'&state='.urlencode('forlogin-'.$shortscope.'-'.$oauthstateanticsrf).'&username='.urlencode($usertotest);
  72. // we go on oauth provider authorization page
  73. header('Location: '.$url);
  74. exit();
  75. }
  76. if (GETPOST('afteroauthloginreturn')) {
  77. // We reach this code after a call of a redirect to the targeted page from the callback url page of Google OAUTH2
  78. dol_syslog("We reach the code after a call of a redirect to the targeted page from the callback url page of Google OAUTH2");
  79. $tmparray = (empty($_SESSION['datafromloginform']) ? array() : $_SESSION['datafromloginform']);
  80. if (!empty($tmparray)) {
  81. $_POST['entity'] = $tmparray['entity'];
  82. $_POST['backtopage'] = $tmparray['backtopage'];
  83. $_POST['tz'] = $tmparray['tz'];
  84. $_POST['tz_string'] = $tmparray['tz_string'];
  85. $_POST['dst_observed'] = $tmparray['dst_observed'];
  86. $_POST['dst_first'] = $tmparray['dst_first'];
  87. $_POST['dst_second'] = $tmparray['dst_second'];
  88. $_POST['screenwidth'] = $tmparray['dol_screenwidth'];
  89. $_POST['screenheight'] = $tmparray['dol_screenheight'];
  90. $_POST['dol_hide_topmenu'] = $tmparray['dol_hide_topmenu'];
  91. $_POST['dol_hide_leftmenu'] = $tmparray['dol_hide_leftmenu'];
  92. $_POST['dol_optimize_smallscreen'] = $tmparray['dol_optimize_smallscreen'];
  93. $_POST['dol_no_mouse_hover'] = $tmparray['dol_no_mouse_hover'];
  94. $_POST['dol_use_jmobile'] = $tmparray['dol_use_jmobile'];
  95. }
  96. // If googleoauth_login has been set (by google_oauthcallback after a successfull OAUTH2 request on openid scope
  97. if (!empty($_SESSION['googleoauth_receivedlogin']) && dol_verifyHash($conf->file->instance_unique_id.$usertotest, $_SESSION['googleoauth_receivedlogin'], '0')) {
  98. unset($_SESSION['googleoauth_receivedlogin']);
  99. $login = $usertotest;
  100. }
  101. }
  102. }
  103. return $login;
  104. }