summaryrefslogtreecommitdiff
path: root/classes/auth/base.php
blob: f18cc2d2d3efe19a97a6da6e7da634ba2aca1de8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
abstract class Auth_Base extends Plugin implements IAuthModule {
	protected $pdo;

	const AUTH_SERVICE_API = '_api';

	function __construct() {
		$this->pdo = Db::pdo();
	}

	// compatibility wrapper, because of how pluginhost works (hook name == method name)
	function hook_auth_user(...$args) {
		return $this->authenticate(...$args);
	}

	// Auto-creates specified user if allowed by system configuration
	// Can be used instead of find_user_by_login() by external auth modules
	function auto_create_user(string $login, $password = false) {
		if ($login && Config::get(Config::AUTH_AUTO_CREATE)) {
			$user_id = UserHelper::find_user_by_login($login);

			if (!$user_id) {

				if (!$password) $password = make_password();

				$salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
				$pwd_hash = encrypt_password($password, $salt, true);

				$sth = $this->pdo->prepare("INSERT INTO ttrss_users
						(login,access_level,last_login,created,pwd_hash,salt)
						VALUES (LOWER(?), 0, null, NOW(), ?,?)");
				$sth->execute([$login, $pwd_hash, $salt]);

				return UserHelper::find_user_by_login($login);

			} else {
				return $user_id;
			}
		}

		return UserHelper::find_user_by_login($login);
	}

	// @deprecated
	function find_user_by_login(string $login) {
		return UserHelper::find_user_by_login($login);
	}
}