summaryrefslogtreecommitdiff
path: root/init.php
blob: e5590ec30f594020d167fc90146502036b78b763 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
require_once __DIR__ . "/vendor/autoload.php";

use Jumbojett\OpenIDConnectClient;

class Auth_OIDC extends Auth_Base {

	/** redirect user to this URL after logout; .env:
	 * TTRSS_AUTH_OIDC_POST_LOGOUT_URL=http://127.0.0.1/logout-redirect
	 */
	const AUTH_OIDC_POST_LOGOUT_URL = "AUTH_OIDC_POST_LOGOUT_URL";
	const AUTH_OIDC_NAME = "AUTH_OIDC_NAME";
	const AUTH_OIDC_URL = "AUTH_OIDC_URL";
	const AUTH_OIDC_CLIENT_ID = "AUTH_OIDC_CLIENT_ID";
	const AUTH_OIDC_CLIENT_SECRET = "AUTH_OIDC_CLIENT_SECRET";

	// in seconds
	const AUTH_OIDC_VALIDATE_INTERVAL = "AUTH_OIDC_VALIDATE_INTERVAL";

	/** @var PluginHost $host */
	private $host;

	function about() {
		return array(null,
			"Authenticates against configured OIDC provider",
			"fox",
			true);
	}

	function init($host) {
		Config::add(self::AUTH_OIDC_POST_LOGOUT_URL, "", Config::T_STRING);
		Config::add(self::AUTH_OIDC_NAME, "", Config::T_STRING);
		Config::add(self::AUTH_OIDC_URL, "", Config::T_STRING);
		Config::add(self::AUTH_OIDC_CLIENT_ID, "", Config::T_STRING);
		Config::add(self::AUTH_OIDC_CLIENT_SECRET, "", Config::T_STRING);
		Config::add(self::AUTH_OIDC_VALIDATE_INTERVAL, "3600", Config::T_INT);

		if (Config::get(self::AUTH_OIDC_URL)) {
			$host->add_hook($host::HOOK_AUTH_USER, $this);
			$host->add_hook($host::HOOK_LOGINFORM_ADDITIONAL_BUTTONS, $this);
			$host->add_hook($host::HOOK_VALIDATE_SESSION, $this);

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

		$this->host = $host;
	}

	function is_public_method($method) {
		return $method == "oidc_login";
	}

	public function oidc_login() : void {
		$oidc = new OpenIDConnectClient(Config::get(self::AUTH_OIDC_URL),
			Config::get(self::AUTH_OIDC_CLIENT_ID),
			Config::get(self::AUTH_OIDC_CLIENT_SECRET));

		$oidc->setRedirectURL(Config::get_self_url());
		$oidc->addScope(['openid', 'profile', 'email']);
		$oidc->authenticate();
	}

	function authenticate($login, $password, $service = '') {
		if (!($_SESSION['uid'] ?? false) && ($_REQUEST['code'] ?? false)) {

			$oidc = new OpenIDConnectClient(Config::get(self::AUTH_OIDC_URL),
				Config::get(self::AUTH_OIDC_CLIENT_ID),
				Config::get(self::AUTH_OIDC_CLIENT_SECRET));

			try {
				$oidc->setRedirectURL(Config::get_self_url());
				$oidc->addScope(['openid', 'profile', 'email']);
				$oidc->authenticate();

				$login = $oidc->requestUserInfo("preferred_username");

				$user_id = $this->auto_create_user($login, $password);

				if ($user_id) {
					$name = $oidc->requestUserInfo("name");

					if ($name) {
						$sth = $this->pdo->prepare("UPDATE ttrss_users SET full_name = ? WHERE id = ?");
						$sth->execute([$name, $user_id]);
					}

					$email = $oidc->requestUserInfo("email");

					if ($email) {
						$sth = $this->pdo->prepare("UPDATE ttrss_users SET email = ? WHERE id = ?");
						$sth->execute([$email, $user_id]);
					}
				}

				$_SESSION["auth_oidc:refresh_token"] = $oidc->getRefreshToken();
				$_SESSION["auth_oidc:refresh_token_last_check"] = time();

				return $user_id;

			} catch (Exception $e) {
				$_SESSION["login_error_msg"] = 'OIDC: ' . $e->getMessage();
			}
		}

		return false;
	}

	function hook_validate_session(): bool {
		$refresh_token = $_SESSION["auth_oidc:refresh_token"] ?? false;

		if ($refresh_token && $_SESSION["auth_oidc:refresh_token_last_check"] < time() - Config::get(self::AUTH_OIDC_VALIDATE_INTERVAL)) {
			$oidc = new OpenIDConnectClient(Config::get(self::AUTH_OIDC_URL),
				Config::get(self::AUTH_OIDC_CLIENT_ID),
				Config::get(self::AUTH_OIDC_CLIENT_SECRET));

			try {
				$result = $oidc->introspectToken($refresh_token);

				if ($result->active)
					$_SESSION["auth_oidc:refresh_token_last_check"] = time();

				return $result->active;
			} catch (Exception $e) {
				$_SESSION["login_error_msg"] = 'OIDC: ' . $e->getMessage();
				return false;
			}
		}

		return true;
	}

	function get_login_js() {
		return file_get_contents(__DIR__ . "/init.js");
	}

	function hook_loginform_additional_buttons() {
		print \Controls\button_tag(T_sprintf('Log in with %s', Config::get(self::AUTH_OIDC_NAME)), '',
			['class' => '', 'onclick' => 'Plugins.Auth_OIDC.login("'.htmlspecialchars($this->host->get_public_method_url($this, "oidc_login")).'")']);
	}

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

	function api_version() {
		return 2;
	}

}