/
LoginPKI.php
164 lines (138 loc) · 6.1 KB
/
LoginPKI.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
158
159
160
161
162
163
164
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\LoginPKI;
use Piwik\Piwik;
use Piwik\Config;
use Piwik\Cookie;
use Piwik\ProxyHttp;
use Piwik\Session;
use Piwik\FrontController;
use Piwik\Plugins\ClientCertificates\API as ClientCertificatesAPI;
use Piwik\Plugins\Login\API as LoginAPI;
/**
*/
class LoginPKI extends \Piwik\Plugin {
public function getListHooksRegistered() {
return array(
'Request.initAuthenticationObject' => 'initAuthenticationObject',
'API.Request.authenticate' => 'ApiRequestAuthenticate'
);
}
public function allowPasswordChange(&$allowPasswordChange) {
$allowPasswordChange = false;
}
public function allowSignout(&$allowSignout) {
$allowSignout = false;
}
public function initAuthenticationObject($activateCookieAuth = false) {
$clientCertificateAPI = ClientCertificatesAPI::getInstance();
$loginAPI = LoginAPI::getInstance();
$dn = $clientCertificateAPI->getUserDN();
$issuer_dn = $clientCertificateAPI->getIssuerDN();
if($dn != null) {
$auth = new CertAuth();
$previousAuth = \Piwik\Registry::get('auth');
\Piwik\Registry::set('auth', $auth);
if(!$this->initAuthenticationFromCookie($auth, $activateCookieAuth)) {
$result = $clientCertificateAPI->queryGovport($dn, $issuer_dn);
if($result) {
$username = $this->getProperty($result, 'uid');
$fullname = $this->getProperty($result, 'fullName');
$email = $this->getProperty($result, 'email');
$firstname = $this->getProperty($result, 'firstName');
$lastname = $this->getProperty($result, 'lastName');
$agency = null;
if(property_exists($result, 'grantBy')) {
$agency = $result->{'grantBy'}[0];
}
if($agency == null)
{
if(property_exists($result, 'organizations')) {
$agency = $result->{'organizations'}[0];
}
if($agency == null) {
$agency = 'N/A';
}
}
\Piwik\Log::debug("Login PKI Response: $username, $fullname, $email, $firstname, $lastname, $agency");
$auth->setLogin($username);
$auth->setUserDN($dn);
$auth->setPassword($username . $dn);
$auth->setTokenAuth(md5($username . $auth->getTokenAuthSecret()));
$auth->setEmail($email);
$auth->setAlias($this->getAlias($firstname, $lastname, $fullname));
$authResult = $auth->authenticate();
if($authResult->wasAuthenticationSuccessful()) {
Session::regenerateId();
//Create Cookie
$authCookieExpiry = 0;
$authCookieName = Config::getInstance()->General['login_cookie_name'];
$authCookiePath = Config::getInstance()->General['login_cookie_path'];
$cookie = new Cookie($authCookieName, $authCookieExpiry, $authCookiePath);
$cookie->set('login', $authResult->getIdentity());
$cookie->set('token_auth', md5($username . $auth->getTokenAuthSecret()));
$cookie->setSecure(ProxyHttp::isHttps());
$cookie->setHttpOnly(true);
$cookie->save();
} else {
// Error message set by auth result
\Piwik\Registry::set('auth', $previousAuth);
}
} else {
\Piwik\Registry::set('auth', $previousAuth);
$loginAPI->setErrorMessage("Could not verify user against authorization service");
\Piwik\Log::debug("Could not verify user against authorization service. Falling back on standard auth.");
}
}
} else {
$loginAPI->setErrorMessage("No certificate provided");
\Piwik\Log::debug("No certificate provided. Falling back on standard login mechanism.");
}
}
public function ApiRequestAuthenticate($tokenAuth) {
\Piwik\Registry::get('auth')->setLogin($login = null);
\Piwik\Registry::get('auth')->setTokenAuth($tokenAuth);
}
private static function initAuthenticationFromCookie(\Piwik\Auth $auth, $activateCookieAuth)
{
$authCookieName = Config::getInstance()->General['login_cookie_name'];
$authCookieExpiry = 0;
$authCookiePath = Config::getInstance()->General['login_cookie_path'];
$authCookie = new Cookie($authCookieName, $authCookieExpiry, $authCookiePath);
if ($authCookie->isCookieFound()) {
$login = $authCookie->get('login');
$tokenAuth = $authCookie->get('token_auth');
\Piwik\Log::debug("Login [".$login."] from browser token");
$auth->setLogin($login);
$auth->setTokenAuth($tokenAuth);
return true;
} else {
return false;
}
}
private static function isModuleIsAPI()
{
return Piwik::getModule() === 'API'
&& (Piwik::getAction() == '' || Piwik::getAction() == 'index');
}
private function getProperty($data, $property) {
if(property_exists($data, $property)) {
return $data->{$property};
} else {
return null;
}
}
private function getAlias($firstname, $lastname, $fullname) {
if($firstname == null || $lastname == null) {
return $fullname;
} else {
return ucwords(strtolower($firstname)) . " " . ucwords(strtolower($lastname));
}
}
}