summaryrefslogtreecommitdiff
path: root/plugins/auth_remote/init.php
blob: 3203d41fe1170cd36d385f0cadf5e0d4c521dc52 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
class Auth_Remote extends Auth_Base {

	/** redirect user to this URL after logout; .env:
	 * TTRSS_AUTH_REMOTE_POST_LOGOUT_URL=http://127.0.0.1/logout-redirect
	 */
	const AUTH_REMOTE_POST_LOGOUT_URL = "AUTH_REMOTE_POST_LOGOUT_URL";

	function about() {
		return array(null,
			"Authenticates against external passwords (HTTP Authentication, SSL certificates)",
			"fox",
			true);
	}

	function init($host) {
		$host->add_hook($host::HOOK_AUTH_USER, $this);

		Config::add(self::AUTH_REMOTE_POST_LOGOUT_URL, "", Config::T_STRING);

		if (Config::get(self::AUTH_REMOTE_POST_LOGOUT_URL) != "") {
			$host->add_hook($host::HOOK_POST_LOGOUT, $this);
		}
	}

	function get_login_by_ssl_certificate() : string {
		$cert_serial = Pref_Prefs::_get_ssl_certificate_id();

		if ($cert_serial) {
			$sth = $this->pdo->prepare("SELECT login FROM ttrss_user_prefs2, ttrss_users
				WHERE pref_name = 'SSL_CERT_SERIAL' AND value = ? AND
				owner_uid = ttrss_users.id");
			$sth->execute([$cert_serial]);

			if ($row = $sth->fetch()) {
				return $row['login'];
			}
		}

		return "";
	}

	function authenticate($login, $password, $service = '') {
		$try_login = "";

		foreach (["REMOTE_USER", "HTTP_REMOTE_USER", "REDIRECT_REMOTE_USER", "PHP_AUTH_USER"] as $hdr) {
			if (!empty($_SERVER[$hdr])) {
				$try_login = $_SERVER[$hdr];
				break;
			}
		}

		if (!$try_login) $try_login = $this->get_login_by_ssl_certificate();

		if ($try_login) {
			$user_id = $this->auto_create_user($try_login, $password);

			if ($user_id) {
				$_SESSION["fake_login"] = $try_login;
				$_SESSION["fake_password"] = "******";
				$_SESSION["hide_hello"] = true;
				$_SESSION["hide_logout"] = true;

				// LemonLDAP can send user informations via HTTP HEADER
				if (Config::get(Config::AUTH_AUTO_CREATE)) {
					// update user name
					$fullname = isset($_SERVER['HTTP_USER_NAME']) ? $_SERVER['HTTP_USER_NAME'] : ($_SERVER['AUTHENTICATE_CN'] ?? "");
					if ($fullname){
						$sth = $this->pdo->prepare("UPDATE ttrss_users SET full_name = ? WHERE id = ?");
						$sth->execute([$fullname, $user_id]);
					}
					// update user mail
					$email = isset($_SERVER['HTTP_USER_MAIL']) ? $_SERVER['HTTP_USER_MAIL'] : ($_SERVER['AUTHENTICATE_MAIL'] ?? "");
					if ($email){
						$sth = $this->pdo->prepare("UPDATE ttrss_users SET email = ? WHERE id = ?");
						$sth->execute([$email, $user_id]);
					}
				}

				return $user_id;
			}
		}

		return false;
	}

	function hook_post_logout($login, $user_id) {
		return [
			Config::get(self::AUTH_REMOTE_POST_LOGOUT_URL)
			];
	}

	function api_version() {
		return 2;
	}

}