Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
AuthManager | |
0.00% |
0 / 26 |
|
0.00% |
0 / 9 |
240 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getUserData | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getUserId | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
setUser | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isAuthenticated | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isAnonymous | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
authenticate | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
login | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
logout | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | /** |
3 | * @section LICENSE |
4 | * This file is part of Wikimedia Slim application library |
5 | * |
6 | * Wikimedia Slim application library is free software: you can |
7 | * redistribute it and/or modify it under the terms of the GNU General Public |
8 | * License as published by the Free Software Foundation, either version 3 of |
9 | * the License, or (at your option) any later version. |
10 | * |
11 | * Wikimedia Slim application library is distributed in the hope that it |
12 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty |
13 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with Wikimedia Grants Review application. If not, see |
18 | * <http://www.gnu.org/licenses/>. |
19 | * |
20 | * @file |
21 | * @copyright © 2015 Bryan Davis, Wikimedia Foundation and contributors. |
22 | */ |
23 | |
24 | namespace Wikimedia\Slimapp\Auth; |
25 | |
26 | /** |
27 | * Manage authentication and authorization. |
28 | * |
29 | * @author Bryan Davis <bd808@wikimedia.org> |
30 | * @copyright © 2015 Bryan Davis, Wikimedia Foundation and contributors. |
31 | */ |
32 | class AuthManager { |
33 | |
34 | private const USER_SESSION_KEY = 'AUTH_USER'; |
35 | public const NEXTPAGE_SESSION_KEY = 'AUTH_NEXTPAGE'; |
36 | |
37 | /** |
38 | * @var UserManager |
39 | */ |
40 | protected $manager; |
41 | |
42 | /** |
43 | * @param UserManager $manager |
44 | */ |
45 | public function __construct( UserManager $manager ) { |
46 | $this->manager = $manager; |
47 | } |
48 | |
49 | /** |
50 | * Get the current user's information |
51 | * @return UserData User information or null if not available |
52 | */ |
53 | public function getUserData() { |
54 | return $_SESSION[self::USER_SESSION_KEY] ?? null; |
55 | } |
56 | |
57 | /** |
58 | * Get the current user's Id. |
59 | * @return int|bool Numeric user id or false if not available |
60 | */ |
61 | public function getUserId() { |
62 | $user = $this->getUserData(); |
63 | return $user ? $user->getId() : false; |
64 | } |
65 | |
66 | /** |
67 | * Store the user's information. |
68 | * @param UserData $user User information |
69 | */ |
70 | public function setUser( UserData $user ) { |
71 | $_SESSION[self::USER_SESSION_KEY] = $user; |
72 | } |
73 | |
74 | /** |
75 | * Is the user authenticated? |
76 | * @return bool True if authenticated, false otherwise |
77 | */ |
78 | public function isAuthenticated() { |
79 | return $this->getUserData() !== null; |
80 | } |
81 | |
82 | /** |
83 | * Is the user anonymous? |
84 | * @return bool True if the user is not authenticated, false otherwise |
85 | */ |
86 | public function isAnonymous() { |
87 | return $this->getUserData() === null; |
88 | } |
89 | |
90 | /** |
91 | * Attempt to authenticate a user. |
92 | * @param string $uname Username |
93 | * @param string $password Password |
94 | * @return bool True if authentication is successful, false otherwise |
95 | */ |
96 | public function authenticate( $uname, $password ) { |
97 | $user = $this->manager->getUserData( $uname ); |
98 | $check = Password::comparePasswordToHash( $password, $user->getPassword() ); |
99 | if ( $check && !$user->isBlocked() ) { |
100 | $this->login( $user ); |
101 | return true; |
102 | |
103 | } |
104 | |
105 | return false; |
106 | } |
107 | |
108 | /** |
109 | * Add authentication. |
110 | * |
111 | * @param UserData $user |
112 | */ |
113 | public function login( UserData $user ) { |
114 | // clear session |
115 | foreach ( $_SESSION as $key => $value ) { |
116 | unset( $_SESSION[$key] ); |
117 | } |
118 | |
119 | // generate new session id |
120 | session_regenerate_id( true ); |
121 | |
122 | // store user info in session |
123 | $this->setUser( $user ); |
124 | } |
125 | |
126 | /** |
127 | * Remove authentication. |
128 | */ |
129 | public function logout() { |
130 | // clear session |
131 | foreach ( $_SESSION as $key => $value ) { |
132 | unset( $_SESSION[$key] ); |
133 | } |
134 | |
135 | // delete the session cookie on the client |
136 | if ( ini_get( 'session.use_cookies' ) ) { |
137 | $params = session_get_cookie_params(); |
138 | setcookie( session_name(), '', time() - 42000, |
139 | $params['path'], $params['domain'], |
140 | $params['secure'], $params['httponly'] |
141 | ); |
142 | } |
143 | |
144 | // generate new session id |
145 | session_regenerate_id( true ); |
146 | } |
147 | |
148 | } |