summaryrefslogtreecommitdiff
path: root/init.php
blob: 4ed6c78add9ef40ffe8f0db3bcc8e8fc522e8beb (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
<?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";

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

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

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

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

		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 == "callback";
	}

	function callback() {
		print "IN_CALLBACK";
		die;
	}

	function authenticate($login, $password, $service = '') {
		$oidc = new OpenIDConnectClient('https://auth.fakecake.org',
                                'dev-debian-ttrss',
                                'Bu3vuCi0wBeQteJ7di4H6SKgqvYnpSludEP68SHu9wLekxXl');

		if (!($_SESSION['uid'] ?? false)) {
			$oidc->setRedirectURL(Config::get_self_url());

			try {
				$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]);
					}
				}

				return $user_id;

			} catch (Exception $e) {
				var_dump($e);
				die;
			}
		}

		return false;
	}

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

	function api_version() {
		return 2;
	}

}