api_login.class.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  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 <http://www.gnu.org/licenses/>.
  17. */
  18. use Luracast\Restler\RestException;
  19. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  20. /**
  21. * API that allows to log in with an user account.
  22. */
  23. class Login
  24. {
  25. function __construct() {
  26. global $db;
  27. $this->db = $db;
  28. }
  29. /**
  30. * Login
  31. *
  32. * Log user with username and password. Using method POST is recommanded for security reasons (method GET is often logged by default by web servers with parameters so with login and pass)
  33. *
  34. * @param string $login Username
  35. * @param string $password User password
  36. * @param int $entity Entity (when multicompany module is used). Empty means 1=first company.
  37. * @param int $reset Reset token (0=get current token, 1=ask a new token, meaning that all future access using current token will failed)
  38. * @return array Response status and user token
  39. *
  40. * @throws RestException
  41. *
  42. * @url GET /
  43. * @url POST /
  44. */
  45. public function index($login, $password, $entity=0, $reset=0) {
  46. global $conf, $dolibarr_main_authentication, $dolibarr_auto_user;
  47. // Authentication mode
  48. if (empty($dolibarr_main_authentication))
  49. $dolibarr_main_authentication = 'http,dolibarr';
  50. // Authentication mode: forceuser
  51. if ($dolibarr_main_authentication == 'forceuser' && empty($dolibarr_auto_user))
  52. $dolibarr_auto_user = 'auto';
  53. // Set authmode
  54. $authmode = explode(',', $dolibarr_main_authentication);
  55. include_once DOL_DOCUMENT_ROOT . '/core/lib/security2.lib.php';
  56. $login = checkLoginPassEntity($login, $password, $entity, $authmode);
  57. if (empty($login))
  58. {
  59. throw new RestException(403, 'Access denied');
  60. }
  61. $token = 'failedtogenerateorgettoken';
  62. $tmpuser=new User($this->db);
  63. $tmpuser->fetch(0, $login);
  64. // Renew the hash
  65. if (empty($tmpuser->api_key) || $reset)
  66. {
  67. // Generate token for user
  68. $token = dol_hash($login.uniqid().$conf->global->MAIN_API_KEY,1);
  69. // We store API token into database
  70. $sql = "UPDATE ".MAIN_DB_PREFIX."user";
  71. $sql.= " SET api_key = '".$this->db->escape($token)."'";
  72. $sql.= " WHERE login = '".$this->db->escape($login)."'";
  73. dol_syslog(get_class($this)."::login", LOG_DEBUG); // No log
  74. $result = $this->db->query($sql);
  75. if (!$result)
  76. {
  77. throw new RestException(500, 'Error when updating api_key for user :'.$this->db->lasterror());
  78. }
  79. }
  80. else
  81. {
  82. $token = $tmpuser->api_key;
  83. }
  84. //return token
  85. return array(
  86. 'success' => array(
  87. 'code' => 200,
  88. 'token' => $token,
  89. 'message' => 'Welcome ' . $login.($reset?' - Token is new':' - This is your token (generated by a previous call)')
  90. )
  91. );
  92. }
  93. }