api_login.class.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <https://www.gnu.org/licenses/>.
  17. */
  18. use Luracast\Restler\RestException;
  19. require_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
  20. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  21. /**
  22. * API that allows to log in with an user account.
  23. */
  24. class Login
  25. {
  26. /**
  27. * Constructor of the class
  28. */
  29. public function __construct()
  30. {
  31. global $conf, $db;
  32. $this->db = $db;
  33. //$conf->global->MAIN_MODULE_API_LOGIN_DISABLED = 1;
  34. if (!empty($conf->global->MAIN_MODULE_API_LOGIN_DISABLED)) {
  35. throw new RestException(403, "Error login APIs are disabled. You must get the token from backoffice to be able to use APIs");
  36. }
  37. }
  38. /**
  39. * Login
  40. *
  41. * Request the API token for a couple username / password.
  42. * WARNING: You should NEVER use this API, like you should never use the similare API that uses the POST method. This will expose your password.
  43. * To use the APIs, you should instead set an API token to the user you want to allow to use API (This API token called DOLAPIKEY can be found/set on the user page) and use this token as credential for any API call.
  44. * From the API explorer, you can enter directly the "DOLAPIKEY" into the field at the top right of the page to get access to any allowed APIs.
  45. *
  46. * @param string $login User login
  47. * @param string $password User password
  48. * @param string $entity Entity (when multicompany module is used). '' means 1=first company.
  49. * @param int $reset Reset token (0=get current token, 1=ask a new token and canceled old token. This means access using current existing API token of user will fails: new token will be required for new access)
  50. * @return array Response status and user token
  51. *
  52. * @throws RestException 403 Access denied
  53. * @throws RestException 500 System error
  54. *
  55. * @url GET /
  56. */
  57. public function loginUnsecured($login, $password, $entity = '', $reset = 0)
  58. {
  59. return $this->index($login, $password, $entity, $reset);
  60. }
  61. /**
  62. * Login
  63. *
  64. * Request the API token for a couple username / password.
  65. * WARNING: You should NEVER use this API, like you should never use the similare API that uses the POST method. This will expose your password.
  66. * To use the APIs, you should instead set an API token to the user you want to allow to use API (This API token called DOLAPIKEY can be found/set on the user page) and use this token as credential for any API call.
  67. * From the API explorer, you can enter directly the "DOLAPIKEY" into the field at the top right of the page to get access to any allowed APIs.
  68. *
  69. * @param string $login User login
  70. * @param string $password User password
  71. * @param string $entity Entity (when multicompany module is used). '' means 1=first company.
  72. * @param int $reset Reset token (0=get current token, 1=ask a new token and canceled old token. This means access using current existing API token of user will fails: new token will be required for new access)
  73. * @return array Response status and user token
  74. *
  75. * @throws RestException 403 Access denied
  76. * @throws RestException 500 System error
  77. *
  78. * @url POST /
  79. */
  80. public function index($login, $password, $entity = '', $reset = 0)
  81. {
  82. global $conf, $dolibarr_main_authentication, $dolibarr_auto_user;
  83. // Is the login API disabled ? The token must be generated from backoffice only.
  84. if (!empty($conf->global->API_DISABLE_LOGIN_API)) {
  85. dol_syslog("Warning: A try to use the login API has been done while the login API is disabled. You must generate or get the token from the backoffice.", LOG_WARNING);
  86. throw new RestException(403, "Error, the login API has been disabled for security purpose. You must generate or get the token from the backoffice.");
  87. }
  88. // Authentication mode
  89. if (empty($dolibarr_main_authentication)) {
  90. $dolibarr_main_authentication = 'dolibarr';
  91. }
  92. // Authentication mode: forceuser
  93. if ($dolibarr_main_authentication == 'forceuser') {
  94. if (empty($dolibarr_auto_user)) {
  95. $dolibarr_auto_user = 'auto';
  96. }
  97. if ($dolibarr_auto_user != $login) {
  98. dol_syslog("Warning: your instance is set to use the automatic forced login '".$dolibarr_auto_user."' that is not the requested login. API usage is forbidden in this mode.");
  99. throw new RestException(403, "Your instance is set to use the automatic login '".$dolibarr_auto_user."' that is not the requested login. API usage is forbidden in this mode.");
  100. }
  101. }
  102. // Set authmode
  103. $authmode = explode(',', $dolibarr_main_authentication);
  104. if ($entity != '' && !is_numeric($entity)) {
  105. throw new RestException(403, "Bad value for entity, must be the numeric ID of company.");
  106. }
  107. if ($entity == '') {
  108. $entity = 1;
  109. }
  110. include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
  111. $login = checkLoginPassEntity($login, $password, $entity, $authmode, 'api'); // Check credentials.
  112. if (empty($login)) {
  113. throw new RestException(403, 'Access denied');
  114. }
  115. $token = 'failedtogenerateorgettoken';
  116. $tmpuser = new User($this->db);
  117. $tmpuser->fetch(0, $login, 0, 0, $entity);
  118. if (empty($tmpuser->id)) {
  119. throw new RestException(500, 'Failed to load user');
  120. }
  121. // Renew the hash
  122. if (empty($tmpuser->api_key) || $reset) {
  123. $tmpuser->getrights();
  124. if (empty($tmpuser->rights->user->self->creer)) {
  125. if (empty($tmpuser->api_key)) {
  126. throw new RestException(403, 'No API token set for this user and user need write permission on itself to reset its API token');
  127. } else {
  128. throw new RestException(403, 'User need write permission on itself to reset its API token');
  129. }
  130. }
  131. // Generate token for user
  132. $token = dol_hash($login.uniqid().(empty($conf->global->MAIN_API_KEY)?'':$conf->global->MAIN_API_KEY), 1);
  133. // We store API token into database
  134. $sql = "UPDATE ".MAIN_DB_PREFIX."user";
  135. $sql .= " SET api_key = '".$this->db->escape(dolEncrypt($token, '', '', 'dolibarr'))."'";
  136. $sql .= " WHERE login = '".$this->db->escape($login)."'";
  137. dol_syslog(get_class($this)."::login", LOG_DEBUG); // No log
  138. $result = $this->db->query($sql);
  139. if (!$result) {
  140. throw new RestException(500, 'Error when updating api_key for user :'.$this->db->lasterror());
  141. }
  142. } else {
  143. $token = $tmpuser->api_key;
  144. }
  145. //return token
  146. return array(
  147. 'success' => array(
  148. 'code' => 200,
  149. 'token' => $token,
  150. 'entity' => $tmpuser->entity,
  151. 'message' => 'Welcome '.$login.($reset ? ' - Token is new' : ' - This is your token (recorded for your user). You can use it to make any REST API call, or enter it into the DOLAPIKEY field to use the Dolibarr API explorer.')
  152. )
  153. );
  154. }
  155. }