21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/core/lib/security.lib.php';
24require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
45 throw new RestException(403,
"Error: The login APIs are disabled. It is recommended to call the APIs using the DOLAPIKEY token of an allowed user instead of the login/password. API key can be set from the user card. If you really need to get the API token from the login API, you can set the constant API_ENABLE_LOGIN_API.");
72 return $this->
index($login, $password, $entity, $reset);
96 public function index($login, $password, $entity =
'', $reset = 0)
98 global $dolibarr_main_authentication, $dolibarr_auto_user;
102 dol_syslog(
"Warning: A try to use the login API has been done while the login API is disabled (Option API_ENABLE_LOGIN_API is off). You must generate or get the DOLAPIKEY token from the backoffice instead of using the /login API.", LOG_WARNING);
103 throw new RestException(403,
"Error, the login API has been disabled for security purpose (Option API_ENABLE_LOGIN_API is off). You must generate or get the DOLAPIKEY token from the backoffice instead of using the /login API.");
107 if (empty($dolibarr_main_authentication) || $dolibarr_main_authentication ==
'openid_connect') {
108 $dolibarr_main_authentication =
'dolibarr';
112 if ($dolibarr_main_authentication ==
'forceuser') {
113 if (empty($dolibarr_auto_user)) {
114 $dolibarr_auto_user =
'auto';
116 if ($dolibarr_auto_user != $login) {
117 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.");
118 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.");
123 $authmode = explode(
',', $dolibarr_main_authentication);
125 if ($entity !=
'' && !is_numeric($entity)) {
126 throw new RestException(403,
"Bad value for entity, must be the numeric ID of company.");
132 include_once DOL_DOCUMENT_ROOT.
'/core/lib/security2.lib.php';
134 if ($login ===
'--bad-login-validity--') {
138 throw new RestException(403,
'Access denied');
141 $token =
'failedtogenerateorgettoken';
143 $tmpuser =
new User($this->db);
144 $tmpuser->fetch(0, $login,
'0', 0, $entity);
145 if (empty($tmpuser->id)) {
146 throw new RestException(500,
'Failed to load user');
150 if (empty($tmpuser->api_key) || $reset) {
151 $tmpuser->loadRights();
152 if (!$tmpuser->hasRight(
'user',
'self',
'creer')) {
153 if (empty($tmpuser->api_key)) {
154 throw new RestException(403,
'No API token set for this user and user need write permission on itself to reset its API token');
156 throw new RestException(403,
'User need write permission on itself to reset its API token');
164 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"user";
165 $sql .=
" SET api_key = '".$this->db->escape(
dolEncrypt($token,
'',
'',
'dolibarr')).
"'";
166 $sql .=
" WHERE login = '".$this->db->escape($login).
"'";
168 dol_syslog(get_class($this).
"::login", LOG_DEBUG);
169 $result = $this->db->query($sql);
171 throw new RestException(500,
'Error when updating api_key for user :'.$this->db->lasterror());
174 $token = $tmpuser->api_key;
176 throw new RestException(500,
'Error, the API token of this user has a non valid value. Try to update it with a valid value.');
181 throw new RestException(500,
'Error the token for this user has not an hexa format. Try first to reset it.');
189 'entity' => $tmpuser->entity ?? 1,
190 '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.')
API that allows to log in with an user account.
__construct()
Constructor of the class.
index($login, $password, $entity='', $reset=0)
Login.
loginUnsecured($login, $password, $entity='', $reset=0)
Login.
Class to manage Dolibarr users.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
ascii_check($str)
Check if a string is in ASCII.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
utf8_check($str)
Check if a string is in UTF8.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
checkLoginPassEntity($usertotest, $passwordtotest, $entitytotest, $authmode, $context='')
Return a login if login/pass was successful.
dol_hash($chain, $type='0', $nosalt=0, $mode=0)
Returns a hash (non reversible encryption) of a string.
dolEncrypt($chain, $key='', $ciphering='', $forceseed='', $obfuscationmode='dolcrypt')
Encode a string with a symmetric encryption.