Elgg  Version 7.1
PersistentLoginService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
6 use Elgg\Traits\TimeUsing;
7 
18 
19  use TimeUsing;
20 
21  protected array $cookie_config;
22 
23  protected string $cookie_token;
24 
29  public $_callable_elgg_set_cookie = 'elgg_set_cookie';
30 
40  public function __construct(
41  protected UsersRememberMeCookiesTable $cookie_table,
42  protected \ElggSession $session,
43  protected \Elgg\Security\Crypto $crypto,
45  \Elgg\Http\Request $request
46  ) {
47  $global_cookies_config = $config->getCookieConfig();
48 
49  $this->cookie_config = $global_cookies_config['remember_me'];
50  $this->cookie_token = $request->cookies->get($this->cookie_config['name'], '');
51  }
52 
60  public function makeLoginPersistent(\ElggUser $user): void {
61  $token = $this->generateToken();
62  $hash = $this->hashToken($token);
63 
64  $this->cookie_table->insertHash($user, $hash);
65  $this->setCookie($token);
66  $this->setSessionToken($token);
67  }
68 
74  public function removePersistentLogin(): void {
75  if ($this->cookie_token) {
76  $client_hash = $this->hashToken($this->cookie_token);
77  $this->cookie_table->deleteHash($client_hash);
78  }
79 
80  $this->setCookie('');
81  $this->setSessionToken('');
82  }
83 
92  public function handlePasswordChange(\ElggUser $subject, ?\ElggUser $modifier = null): void {
93  $this->cookie_table->deleteAllHashes($subject);
94 
95  $modifier = $modifier ?? _elgg_services()->session_manager->getLoggedInUser();
96  if (!$modifier || ($modifier->guid !== $subject->guid) || !$this->cookie_token) {
97  return;
98  }
99 
100  $this->makeLoginPersistent($subject);
101  }
102 
109  public function bootSession(): ?\ElggUser {
110  if (!$this->cookie_token) {
111  return null;
112  }
113 
114  // is this token good?
115  $user = $this->getUserFromToken($this->cookie_token);
116  if ($user) {
117  $this->setSessionToken($this->cookie_token);
118 
119  return $user;
120  }
121 
122  $this->setCookie('');
123  return null;
124  }
125 
133  public function getUserFromToken(string $token): ?\ElggUser {
134  if (empty($token)) {
135  return null;
136  }
137 
138  $hash = $this->hashToken($token);
139  if (empty($hash)) {
140  return null;
141  }
142 
143  $user_row = $this->cookie_table->getRowFromHash($hash);
144  if (empty($user_row)) {
145  return null;
146  }
147 
148  $user = get_user($user_row->guid);
149  return ($user instanceof \ElggUser) ? $user : null;
150  }
151 
159  public function updateTokenUsage(\ElggUser $user): ?bool {
160  if (!$this->cookie_token) {
161  return null;
162  }
163 
164  // update the database record
165  // not interested in number of updated rows, as an update in the same second won't update the row
166  $this->cookie_table->updateHash($user, $this->hashToken($this->cookie_token));
167 
168  // also update the cookie lifetime client-side
169  $this->setCookie($this->cookie_token);
170 
171  return true;
172  }
173 
181  public function removeExpiredTokens($time): bool {
182  $time = Values::normalizeTime($time);
183 
184  $expires = Values::normalizeTime($this->cookie_config['expire']);
185  $diff = $time->diff($expires);
186 
187  $time->sub($diff);
188  if ($time->getTimestamp() > time()) {
189  return false;
190  }
191 
192  return (bool) $this->cookie_table->deleteExpiredHashes($time->getTimestamp());
193  }
194 
202  protected function hashToken(string $token): string {
203  // note: with user passwords, you'd want legit password hashing, but since these are randomly
204  // generated and long tokens, rainbow tables aren't any help.
205  return md5($token);
206  }
207 
215  protected function setCookie(string $token): void {
216  $cookie = new \ElggCookie($this->cookie_config['name']);
217  foreach (['expire', 'path', 'domain', 'secure', 'httpOnly'] as $key) {
218  $cookie->$key = $this->cookie_config[strtolower($key)];
219  }
220 
221  $cookie->value = $token;
222  if (!$token) {
223  $cookie->expire = $this->getCurrentTime('-30 days')->getTimestamp();
224  }
225 
226  call_user_func($this->_callable_elgg_set_cookie, $cookie);
227  }
228 
236  protected function setSessionToken(string $token): void {
237  if ($token) {
238  $this->session->set('code', $token);
239  } else {
240  $this->session->remove('code');
241  }
242  }
243 
252  protected function generateToken(): string {
253  return 'z' . $this->crypto->getRandomString(31);
254  }
255 }
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/clear'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin', 'controller'=> \Elgg\Diagnostics\DownloadController::class,], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:76
if(! $annotation instanceof ElggAnnotation) $time
Definition: time.php:20
$user
Definition: ban.php:7
Elgg Session Management.
Definition: ElggSession.php:20
Manage the users_remember_me_cookies table.
\Elgg\PersistentLoginService
setCookie(string $token)
Store the token in the client cookie (or remove the cookie)
generateToken()
Generate a random token (base 64 URL)
getUserFromToken(string $token)
Get a user from a persistent cookie token.
bootSession()
Boot the persistent login session, possibly returning the user who should be silently logged in.
setSessionToken(string $token)
Store the token in the session (or remove it from the session)
removePersistentLogin()
Remove the persisted login token from client and server.
makeLoginPersistent(\ElggUser $user)
Make the user's login persistent.
updateTokenUsage(\ElggUser $user)
Update the timestamp linked to a persistent cookie code, this indicates that the code was used recent...
__construct(protected UsersRememberMeCookiesTable $cookie_table, protected \ElggSession $session, protected \Elgg\Security\Crypto $crypto, \Elgg\Config $config, \Elgg\Http\Request $request)
Constructor.
hashToken(string $token)
Create a hash from the token.
removeExpiredTokens($time)
Remove all persistent codes from the database which have expired based on the cookie config.
handlePasswordChange(\ElggUser $subject, ?\ElggUser $modifier=null)
Handle a password change.
Request container.
Definition: Request.php:12
Functions for use as event handlers or other situations where you need a globally accessible callable...
Definition: Values.php:12
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
$subject
HTML body of an email.
Definition: body.php:11
_elgg_services()
Get the global service provider.
Definition: elgglib.php:347
get_user(int $guid)
Elgg users Functions to manage multiple or single users in an Elgg install.
Definition: users.php:16
$request
Definition: livesearch.php:12
if(isset($_COOKIE['elggperm'])) $session
Definition: login_as.php:28
$hash
Definition: logout_as.php:12
if($container instanceof ElggGroup && $container->guid !=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
$token
$expires