-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuth.php
More file actions
163 lines (141 loc) · 4.5 KB
/
Auth.php
File metadata and controls
163 lines (141 loc) · 4.5 KB
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
<?php
namespace Piwik\Plugins\OneLogin;
use Piwik\Access;
use Piwik\AuthResult;
use Piwik\Config;
use Piwik\Plugins\UsersManager\Model;
use Piwik\Plugins\UsersManager\API as UsersManagerAPI;
class Auth implements \Piwik\Auth
{
/**
* @var Model
*/
private $userModel;
private $usersManagerAPI;
/**
* @var Auth
*/
private $fallbackAuth;
/**
* Constructor.
*
* @param Model|null $userModel
*/
public function __construct(Model $userModel = null)
{
if ($userModel === null) {
$userModel = new Model();
}
$this->userModel = $userModel;
$this->usersManagerAPI = UsersManagerAPI::getInstance();
$this->fallbackAuth = new \Piwik\Plugins\Login\Auth();
}
/**
* Authentication module's name
*
* @return string
*/
public function getName()
{
return 'OneLogin';
}
/**
* Authenticates user
*
* @return \Piwik\AuthResult
*/
public function authenticate()
{
$login = $this->doOneLogin();
if (!empty($login)) {
$user = $this->userModel->getUser($login);
if(empty($user)) {
return new AuthResult(AuthResult::FAILURE, $login, null);
}
$code = !empty($user['superuser_access']) ? AuthResult::SUCCESS_SUPERUSER_AUTH_CODE : AuthResult::SUCCESS;
return new AuthResult($code, $login, $user['token_auth']);
}
return $this->fallbackAuth->authenticate();
}
private function createUser($login, $password, $email, $alias=false) {
$self = $this;
return Access::doAsSuperUser(function () use ($self, $login, $password, $email, $alias) {
$api = $self->usersManagerAPI;
return $api->addUser($login, $password, $email, $alias);
});
}
protected function doOneLogin() {
if (!isset($_GET['email']) || !isset($_GET['timestamp']) || !isset($_GET['signature'])) {
return false;
}
if ($_GET['timestamp'] < time()-600) {
exit('invalid timestamp');
}
$config = Config::getInstance()->OneLogin;
$login = false;
$secret = $config['security_token'];
$firstname = '';
if (isset($_GET['firstname'])) { $firstname = $_GET['firstname']; }
if (isset($_GET['?firstname'])) { $firstname = $_GET['?firstname']; }
$lastname = $_GET['lastname'];
$email = $_GET['email'];
$timestamp = $_GET['timestamp'];
$x = sha1("$firstname$lastname$email$timestamp$secret");
if ($x != $_GET['signature']) {
exit('invalid signature');
}
$login = $this->getLoginByEmail($email);
if (empty($login) && isset($config['add_users']) && $config['add_users'] > 0) {
$newuser = preg_replace("/[^a-zA-Z0-9]+/", "", explode('@', $email)[0]);
$newpass = md5(openssl_random_pseudo_bytes(64));
$r = $this->createUser($newuser, $newpass, $email, "$firstname $lastname");
$login = $this->getLoginByEmail($email);
}
if (empty($login)) {
exit("Hi $firstname $lastname ($email). No account exists for you yet. Please contact: ".$config['admin_contact']);
}
return $login;
}
private function getLoginByEmail($email) {
$login = '';
$user = $this->getUserByEmail($email);
if (is_array($user)) {
$login = $user['login'];
}
return $login;
}
private function getUserByEmail($email) {
$usersManager = $this->usersManagerAPI;
return Access::doAsSuperUser(function () use ($email, $usersManager) {
$user = null;
if ($usersManager->userEmailExists($email)) {
$user = $usersManager->getUserByEmail($email);
}
return $user;
});
}
public function setTokenAuth($token_auth)
{
$this->fallbackAuth->setTokenAuth($token_auth);
}
public function getLogin()
{
$this->fallbackAuth->getLogin();
}
public function getTokenAuthSecret()
{
return $this->fallbackAuth->getTokenAuthSecret();
}
public function setLogin($login)
{
$this->fallbackAuth->setLogin($login);
}
public function setPassword($password)
{
$this->fallbackAuth->setPassword($password);
}
public function setPasswordHash($passwordHash)
{
$this->fallbackAuth->setPasswordHash($passwordHash);
}
}