-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.IdentityAccessManager.inc.php
148 lines (114 loc) · 4.02 KB
/
class.IdentityAccessManager.inc.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
<?php
class IdentityAccessManager
{
private $credentialsStorage;
private $rememberMe;
private $auth;
private $kdf;
function __construct(
CredentialsStorage $credentialsStorage,
RememberMe $rememberMe
) {
$this->credentialsStorage = $credentialsStorage;
$this->auth = new Session();
$this->kdf = new Sha256();
$this->rememberMe = $rememberMe;
return $this;
}
function setThisKDF(KDF $kdf)
{
$this->kdf = $kdf;
return $this;
}
function setThisAuthentication(Authentication $auth)
{
$this->auth = $auth;
return $this;
}
function setThisRememberMe(RememberMe $rememberMe)
{
$this->rememberMe = $rememberMe;
return $this;
}
// ---------------------- Password reset ----------------------
private $pwdReset;
function setPwdReset(PwdResetStorage $pwdResetStorage)
{
$this->pwdReset = new PwdReset($pwdResetStorage);
}
function createPwdResetRequest($email)
{
if (!Helper::isCorrectEmail($email))
throw new Exception("Email invalid.");
$userArray = $this->credentialsStorage->findUserbyEmail($email);
if ($userArray == null)
throw new Exception("Email invalid.");
$this->pwdReset->createRequest($userArray["email"]);
return true;
}
function resetPwd($selector, $validator, $newPassword)
{
if (!Helper::isCorrectPassword($newPassword))
throw new Exception("Password invalid.");
$this->credentialsStorage->updateIdentityDataByEmail(
$this->pwdReset->verifyRequest($selector, $validator, $newPassword),
"password",
$this->kdf->hash($newPassword)
);
}
// -----------------------------------------------------------
function register($username, $email, $password)
{
if (!Helper::isCorrectLogin($username))
throw new Exception("Username invalid.");
if (!Helper::isCorrectEmail($email))
throw new Exception("Email invalid.");
if (!Helper::isCorrectPassword($password))
throw new Exception("Password invalid.");
// If login or email are taken, mysqli will throw an error
$this->credentialsStorage->saveUser(
$username,
$email,
$this->kdf->hash($password),
Helper::ip_addr()
);
header("Location: /", true, 301);
exit;
}
function login($username, $password, $rememberMe)
{
if (!Helper::isCorrectLogin($username))
throw new Exception("Username invalid.");
if (!Helper::isCorrectPassword($password))
throw new Exception("Password invalid.");
$userArray = $this->credentialsStorage->findUserByUsername($username);
if ($userArray == null || !$this->kdf->compare($password, $userArray["password"]))
throw new Exception("Invalid credentials.");
$this->auth->set($userArray["id"]);
if ($rememberMe)
$this->rememberMe->create($userArray["username"]);
header("Location: /", true, 301);
exit;
}
function isUserLoggedIn()
{
$user_id = $this->auth->get();
if ($user_id) {
$userArray = $this->credentialsStorage->findUserbyId($user_id);
return new User($userArray["id"], $userArray["username"]);
}
$username = $this->rememberMe->hasValid();
if ($username) {
$userArray = $this->credentialsStorage->findUserByUsername($username["username"]);
return new User($userArray["id"], $userArray["username"]);
}
return null;
}
function logout(User $user)
{
$this->auth->clear();
$this->rememberMe->remove($user->username);
header("Location: /", true, 301);
exit;
}
}