summaryrefslogtreecommitdiff
path: root/classes/api.php
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2021-02-11 10:22:27 +0300
committerAndrew Dolgov <[email protected]>2021-02-11 10:22:27 +0300
commit09e9f34bb495b435e826bce8cf716258039d4642 (patch)
tree60cdaf053a0af182cc66002790548caf09339c25 /classes/api.php
parent7af8744c856545f62a2f24fd1a700f40b90b8e37 (diff)
add UserHelper::find_user_by_login() and rewrite some user checks to invoke it instead of going through PDO
Diffstat (limited to 'classes/api.php')
-rwxr-xr-xclasses/api.php40
1 files changed, 15 insertions, 25 deletions
diff --git a/classes/api.php b/classes/api.php
index 7e4691b32..fd783a63e 100755
--- a/classes/api.php
+++ b/classes/api.php
@@ -59,35 +59,25 @@ class API extends Handler {
if (SINGLE_USER_MODE) $login = "admin";
- $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE LOWER(login) = LOWER(?)");
- $sth->execute([$login]);
-
- if ($row = $sth->fetch()) {
- $uid = $row["id"];
+ if ($uid = UserHelper::find_user_by_login($login)) {
+ if (get_pref("ENABLE_API_ACCESS", $uid)) {
+ if (UserHelper::authenticate($login, $password, false, Auth_Base::AUTH_SERVICE_API)) { // try login with normal password
+ $this->wrap(self::STATUS_OK, array("session_id" => session_id(),
+ "api_level" => self::API_LEVEL));
+ } else if (UserHelper::authenticate($login, $password_base64, false, Auth_Base::AUTH_SERVICE_API)) { // else try with base64_decoded password
+ $this->wrap(self::STATUS_OK, array("session_id" => session_id(),
+ "api_level" => self::API_LEVEL));
+ } else { // else we are not logged in
+ user_error("Failed login attempt for $login from " . UserHelper::get_user_ip(), E_USER_WARNING);
+ $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
+ }
+ } else {
+ $this->wrap(self::STATUS_ERR, array("error" => "API_DISABLED"));
+ }
} else {
- $uid = 0;
- }
-
- if (!$uid) {
$this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
return;
}
-
- if (get_pref("ENABLE_API_ACCESS", $uid)) {
- if (UserHelper::authenticate($login, $password, false, Auth_Base::AUTH_SERVICE_API)) { // try login with normal password
- $this->wrap(self::STATUS_OK, array("session_id" => session_id(),
- "api_level" => self::API_LEVEL));
- } else if (UserHelper::authenticate($login, $password_base64, false, Auth_Base::AUTH_SERVICE_API)) { // else try with base64_decoded password
- $this->wrap(self::STATUS_OK, array("session_id" => session_id(),
- "api_level" => self::API_LEVEL));
- } else { // else we are not logged in
- user_error("Failed login attempt for $login from " . UserHelper::get_user_ip(), E_USER_WARNING);
- $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR"));
- }
- } else {
- $this->wrap(self::STATUS_ERR, array("error" => "API_DISABLED"));
- }
-
}
function logout() {