-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.php
158 lines (133 loc) · 4.88 KB
/
init.php
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
153
154
155
156
157
<?php
/**
* Tiny Tiny RSS plugin for SAML authentication (uses Onelogin PHP-Saml)
* @author tsmgeek (tsmgeek@gmail.com)
* @copyright GPL2
* @version 0.1
*/
/**
Following code needs to be added into /include/login_form.php under the Login button
<?php if (strpos(PLUGINS, "auth_saml") !== FALSE) {
echo PluginHost::getInstance()->get_plugin('auth_saml')->hook_login_button();
}?>
*/
class Auth_Saml extends Plugin implements IHandler {
private $link;
private $host;
private $base;
private $logClass;
private $auth;
function about() {
return array(0.1,
"Authenticates against a SAML server (configured in config.php)",
"tsmgeek",
true,
"https://github.com/tsmgeek/ttrss-auth-saml");
}
function csrf_ignore($method) {
return true;
}
function before($method) {
if(!$this->auth) return false;
$auth=$this->auth;
if ($method==='sso') {
$auth->login();
return true;
} else if ($method==='sso2') {
$returnTo = $spBaseUrl.'/demo1/attrs.php';
$auth->login($returnTo);
} else if ($method==='slo') {
$returnTo = null;
$paramters = array();
$nameId = null;
$sessionIndex = null;
if (isset($_SESSION['samlNameId'])) {
$nameId = $_SESSION['samlNameId'];
}
if (isset($_SESSION['samlSessionIndex'])) {
$sessionIndex = $_SESSION['samlSessionIndex'];
}
$auth->logout($returnTo, $paramters, $nameId, $sessionIndex);
} else if ($method==='acs') {
$auth->processResponse();
$errors = $auth->getErrors();
if (!empty($errors)) {
print_r('<p>'.implode(', ', $errors).'</p>');
}
if (!$auth->isAuthenticated()) {
$this->_log('SAML authentication failed');
return false;
}
$user_id = $this->base->auto_create_user($auth->getNameId());
if($user_id){
@session_start();
$_SESSION['samlUserdata'] = $auth->getAttributes();
$_SESSION['samlNameId'] = $auth->getNameId();
$_SESSION['samlSessionIndex'] = $auth->getSessionIndex();
$_SESSION["uid"] = $user_id;
$_SESSION["version"] = VERSION_STATIC;
$result = db_query("SELECT login,access_level,pwd_hash FROM ttrss_users
WHERE id = '$user_id'");
$_SESSION["name"] = db_fetch_result($result, 0, "login");
$_SESSION["access_level"] = db_fetch_result($result, 0, "access_level");
$_SESSION["csrf_token"] = uniqid_short();
db_query("UPDATE ttrss_users SET last_login = NOW() WHERE id = " .
$_SESSION["uid"]);
$_SESSION["ip_address"] = $_SERVER["REMOTE_ADDR"];
$_SESSION["user_agent"] = sha1($_SERVER['HTTP_USER_AGENT']);
$_SESSION["pwd_hash"] = db_fetch_result($result, 0, "pwd_hash");
$_SESSION["last_version_check"] = time();
$_SESSION["hide_logout"] = true;
initialize_user_prefs($_SESSION["uid"]);
$auth->redirectTo('/');
return true;
}
return false;
} else if ($method==='sls') {
$auth->processSLO();
$errors = $auth->getErrors();
if (empty($errors)) {
logout_user();
$auth->redirectTo('/');
} else {
print_r('<p>'.implode(', ', $errors).'</p>');
}
}
return false;
}
function after() {
return true;
}
function hook_action_item(){
if(isset($_SESSION['samlUserdata'])){
return '<div dojoType="dijit.MenuItem" onclick="gotoLogoutSSO()">Logout SSO</div><div dojoType="dijit.MenuItem" onclick="quickMenuGo(\'qmcLogout\')">'.__('Logout (only TTRSS)').'</div>';
}
}
function hook_login_button(){
return '<button onclick="document.location.href=\'backend.php?op=saml&subop=sso\';" dojoType="dijit.form.Button">'. __("SSO").'</button>';
}
function get_js() {
return file_get_contents(dirname(__FILE__) . "/init.js");
}
function init($host) {
$this->link = $host->get_link();
$this->host = $host;
$this->base = new Auth_Base($this->link);
$host->add_handler('saml','*',$this);
$host->add_hook($host::HOOK_ACTION_ITEM, $this);
require_once(dirname(dirname(__FILE__)).'/auth_saml/saml/_toolkit_loader.php');
if(file_exists(dirname(dirname(__FILE__)).'/auth_saml/settings.php')){
require_once(dirname(dirname(__FILE__)).'/auth_saml/settings.php');
$this->auth = new OneLogin_Saml2_Auth($samlConfig);
}
return true;
}
/**
* Returns plugin API version
* Required for plugin interface
* @return number
*/
function api_version() {
return 2;
}
}