FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Authenticator.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred\User;
4 
5 abstract class Authenticator extends \Rsi\Fred\Component{
6 
7  public $maxSessionTime = null; //!< Maximum duration of a session (minutes; empty = infinite).
8  public $maxInactiveTime = null; //!< Maximum period of inactivity (minutes; empty = infinite).
9 
10  public $controllerName = null; //!< Controller to redirect the user to if more information is required (e.g. a username and
11  // password).
12 
13  /**
14  * Perform the check.
15  * @return bool True if the user is authenticated. False if something is wrong. Null if the user has to provide some more
16  * information (set the $controllerName for this purpose).
17  */
18  abstract protected function check();
19  /**
20  * Process the result of a verification.
21  * @param bool $result
22  */
23  public function verified($result){
24  $this->component('log')->debug(get_called_class() . '::verified(' . \Rsi\Str::bool($result) . ')',__FILE__,__LINE__);
25  $this->component('security')->bruteForceDelay($this->checked = $result ?: null,get_called_class());
26  if($result) $this->regenerateSessionId();
27  }
28 
29  public function regenerateSessionId(){
30  try{
31  $alive = $this->component('alive');
32  $alive->journalFilename; //fixate on current session ID
33  session_regenerate_id(true);
34  $alive->journal('Session ID changed to ' . session_id());
35  }
36  catch(\Exception $e){
37  $this->component('log')->notice('Could not regenerate session id: ' . $e->getMessage(),__FILE__,__LINE__);
38  }
39  }
40 
41  public function invalidate(){
42  $this->checked = null;
43  }
44 
45  protected function getAuthenticated(){
46  if($this->checked === null) $this->checked = $this->check();
47  return $this->checked;
48  }
49 
50  protected function getChecked(){
51  if($this->session->checked){
52  if(
53  ($this->maxSessionTime && (time() - $this->session->start > $this->maxSessionTime * 60)) ||
54  ($this->maxInactiveTime && (time() - $this->session->alive > $this->maxInactiveTime * 60))
55  ) $this->invalidate();
56  }
57  $this->session->alive = time();
58  return $this->session->checked;
59  }
60 
61  protected function setChecked($value){
62  $this->session->start = $this->session->alive = time();
63  $this->session->checked = $value;
64  }
65 
66 }
$maxInactiveTime
Maximum period of inactivity (minutes; empty = infinite).
$maxSessionTime
Maximum duration of a session (minutes; empty = infinite).
check()
Perform the check.
verified($result)
Process the result of a verification.
$controllerName
Controller to redirect the user to if more information is required (e.g. a username and...
component($name)
Get a component (local or default).
Definition: Component.php:80