functions_openid.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_openid.php
  20. * \ingroup core
  21. * \brief Authentication functions for OpenId mode
  22. */
  23. include_once DOL_DOCUMENT_ROOT.'/core/class/openid.class.php';
  24. /**
  25. * Check validity of user/password/entity
  26. * If test is ko, reason must be filled into $_SESSION["dol_loginmesg"]
  27. *
  28. * @param string $usertotest Login
  29. * @param string $passwordtotest Password
  30. * @param int $entitytotest Number of instance (always 1 if module multicompany not enabled)
  31. * @return string Login if OK, '' if KO
  32. */
  33. function check_user_password_openid($usertotest, $passwordtotest, $entitytotest)
  34. {
  35. global $db, $conf, $langs;
  36. dol_syslog("functions_openid::check_user_password_openid usertotest=".$usertotest);
  37. $login = '';
  38. // Get identity from user and redirect browser to OpenID Server
  39. if (GETPOSTISSET('username')) {
  40. $openid = new SimpleOpenID();
  41. $openid->SetIdentity(GETPOST('username'));
  42. $protocol = ($conf->file->main_force_https ? 'https://' : 'http://');
  43. $openid->SetTrustRoot($protocol.$_SERVER["HTTP_HOST"]);
  44. $openid->SetRequiredFields(array('email', 'fullname'));
  45. $_SESSION['dol_entity'] = GETPOST("entity", 'int');
  46. //$openid->SetOptionalFields(array('dob','gender','postcode','country','language','timezone'));
  47. if ($openid->sendDiscoveryRequestToGetXRDS()) {
  48. $openid->SetApprovedURL($protocol.$_SERVER["HTTP_HOST"].$_SERVER["SCRIPT_NAME"]); // Send Response from OpenID server to this script
  49. $openid->Redirect(); // This will redirect user to OpenID Server
  50. } else {
  51. $_SESSION["dol_loginmesg"] = $openid->GetError();
  52. return false;
  53. }
  54. return false;
  55. } elseif ($_GET['openid_mode'] == 'id_res') {
  56. // Perform HTTP Request to OpenID server to validate key
  57. $openid = new SimpleOpenID();
  58. $openid->SetIdentity(GETPOST('openid_identity'));
  59. $openid_validation_result = $openid->ValidateWithServer();
  60. if ($openid_validation_result === true) {
  61. // OK HERE KEY IS VALID
  62. $sql = "SELECT login, entity, datestartvalidity, dateendvalidity";
  63. $sql .= " FROM ".MAIN_DB_PREFIX."user";
  64. $sql .= " WHERE openid = '".$db->escape(GETPOST('openid_identity'))."'";
  65. $sql .= " AND entity IN (0,".($_SESSION["dol_entity"] ? ((int) $_SESSION["dol_entity"]) : 1).")";
  66. dol_syslog("functions_openid::check_user_password_openid", LOG_DEBUG);
  67. $resql = $db->query($sql);
  68. if ($resql) {
  69. $obj = $db->fetch_object($resql);
  70. if ($obj) {
  71. $now = dol_now();
  72. if ($obj->datestartvalidity && $db->jdate($obj->datestartvalidity) > $now) {
  73. // Load translation files required by the page
  74. $langs->loadLangs(array('main', 'errors'));
  75. $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorLoginDateValidity");
  76. return '--bad-login-validity--';
  77. }
  78. if ($obj->dateendvalidity && $db->jdate($obj->dateendvalidity) < dol_get_first_hour($now)) {
  79. // Load translation files required by the page
  80. $langs->loadLangs(array('main', 'errors'));
  81. $_SESSION["dol_loginmesg"] = $langs->transnoentitiesnoconv("ErrorLoginDateValidity");
  82. return '--bad-login-validity--';
  83. }
  84. $login = $obj->login;
  85. }
  86. }
  87. } elseif ($openid->IsError() === true) {
  88. // ON THE WAY, WE GOT SOME ERROR
  89. $_SESSION["dol_loginmesg"] = $openid->GetError();
  90. return false;
  91. } else {
  92. // Signature Verification Failed
  93. //echo "INVALID AUTHORIZATION";
  94. return false;
  95. }
  96. } elseif ($_GET['openid_mode'] == 'cancel') {
  97. // User Canceled your Request
  98. //echo "USER CANCELED REQUEST";
  99. return false;
  100. }
  101. return $login;
  102. }