This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Rudolf\Component\Auth; |
||
4 | |||
5 | use PDO; |
||
6 | |||
7 | class Auth |
||
8 | { |
||
9 | /** |
||
10 | * @var PDO |
||
11 | */ |
||
12 | private $pdo; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $prefix; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $table; |
||
23 | |||
24 | /** |
||
25 | * @var Session |
||
26 | */ |
||
27 | private $session; |
||
28 | |||
29 | /** |
||
30 | * Auth constructor. |
||
31 | * @param PDO $pdo |
||
32 | * @param string $prefix |
||
33 | */ |
||
34 | public function __construct(PDO $pdo, $prefix = '') |
||
35 | { |
||
36 | $this->pdo = $pdo; |
||
37 | $this->prefix = $prefix; |
||
38 | |||
39 | $this->table = $this->prefix.'users'; |
||
40 | |||
41 | $this->session = new Session($pdo, $prefix, include CONFIG_ROOT.'/'.'auth.php'); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Login user. |
||
46 | * |
||
47 | * @param string $email |
||
48 | * @param string $password |
||
49 | * |
||
50 | * @return int |
||
51 | * 1 - logged in! |
||
52 | * 2 - email not valid |
||
53 | * 3 - password not valid |
||
54 | * 4 - user not exist |
||
55 | * 5 - email or password incorrect |
||
56 | * 6 - account is inactive |
||
57 | * 7 - unnamed error |
||
58 | */ |
||
59 | public function login($email, $password) |
||
60 | { |
||
61 | |||
62 | #validation |
||
63 | if (false === $this->validateEmail($email)) { |
||
64 | return 2; |
||
65 | } |
||
66 | |||
67 | if (false === $this->validatePassword($password)) { |
||
68 | return 3; |
||
69 | } |
||
70 | |||
71 | #get user data by email |
||
72 | $userData = $this->getUserDataByEmail($email); |
||
73 | if (false === $userData) { |
||
74 | return 4; |
||
75 | } |
||
76 | |||
77 | #check password |
||
78 | if (!password_verify($password, $userData['password'])) { |
||
79 | return 5; |
||
80 | } |
||
81 | |||
82 | #check is user active |
||
83 | if (false === $userData['active']) { |
||
84 | return 6; |
||
85 | } |
||
86 | |||
87 | #create session |
||
88 | if (false === $this->session->createSession($userData)) { |
||
0 ignored issues
–
show
|
|||
89 | return 7; |
||
90 | } |
||
91 | |||
92 | return 1; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Logout current user. |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | public function logout() |
||
101 | { |
||
102 | return $this->session->destroySession(); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Check is session exists. |
||
107 | * |
||
108 | * @return bool |
||
109 | */ |
||
110 | public function check() |
||
111 | { |
||
112 | return $this->session->checkSession(); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Get logged user info. |
||
117 | * |
||
118 | * @param int|bool $uid User ID |
||
119 | * not set gives current logged user data |
||
120 | * |
||
121 | * @return array|bool |
||
122 | */ |
||
123 | View Code Duplication | public function getUser($uid = false) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
124 | { |
||
125 | if (false === $uid) { |
||
126 | $uid = $this->session->getSessionUID(); |
||
127 | } |
||
128 | |||
129 | $stmt = $this->pdo->prepare(" |
||
130 | SELECT id, |
||
131 | nick, |
||
132 | first_name, |
||
133 | surname, |
||
134 | email, |
||
135 | active, |
||
136 | dt |
||
137 | FROM {$this->table} |
||
138 | WHERE id = :uid |
||
139 | "); |
||
140 | $stmt->bindValue(':uid', $uid, \PDO::PARAM_INT); |
||
141 | $stmt->execute(); |
||
142 | |||
143 | $data = $stmt->fetch(\PDO::FETCH_ASSOC); |
||
144 | if (empty($data)) { |
||
145 | return false; |
||
146 | } |
||
147 | |||
148 | return $data; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Get password hash. |
||
153 | * |
||
154 | * @param string $password |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getPasswordHash($password) |
||
159 | { |
||
160 | return password_hash($password, PASSWORD_BCRYPT); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Get user data by email. |
||
165 | * |
||
166 | * @param string $email |
||
167 | * |
||
168 | * @return array|bool |
||
169 | */ |
||
170 | View Code Duplication | public function getUserDataByEmail($email) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
171 | { |
||
172 | $stmt = $this->pdo->prepare(" |
||
173 | SELECT * |
||
174 | FROM {$this->table} |
||
175 | WHERE email = :email |
||
176 | "); |
||
177 | $stmt->bindValue(':email', $email, \PDO::PARAM_STR); |
||
178 | $stmt->execute(); |
||
179 | $results = $stmt->fetchAll(\PDO::FETCH_ASSOC); |
||
180 | |||
181 | if (empty($results[0])) { |
||
182 | return false; |
||
183 | } |
||
184 | |||
185 | return $results[0]; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Validate email. |
||
190 | * |
||
191 | * @param string $email |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function validateEmail($email) |
||
0 ignored issues
–
show
|
|||
196 | { |
||
197 | return true; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Validate password. |
||
202 | * |
||
203 | * @param string $password |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | public function validatePassword($password) |
||
0 ignored issues
–
show
|
|||
208 | { |
||
209 | return true; |
||
210 | } |
||
211 | } |
||
212 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.