only look for valid second factor in session if we have a session
[tine20] / tine20 / Tinebase / Auth / SecondFactor / Abstract.php
1 <?php
2 /**
3  * Tine 2.0
4  *
5  * @package     Tinebase
6  * @subpackage  Auth
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @copyright   Copyright (c) 2016-2017 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Philipp Schüle <p.schuele@metaways.de>
10  */
11 abstract class Tinebase_Auth_SecondFactor_Abstract
12 {
13     protected $_options;
14     
15     public function __construct($options)
16     {
17         $this->_options = $options;
18     }
19
20     /**
21      * validate second factor
22      *
23      * @param $username
24      * @param $password
25      * @return mixed
26      */
27     abstract public function validate($username, $password);
28
29     /**
30      * @param int $lifetimeMinutes
31      * @throws Exception
32      * @throws Zend_Session_Exception
33      */
34     public static function saveValidSecondFactor($lifetimeMinutes = 15)
35     {
36         Tinebase_Session::getSessionNamespace()->secondFactorValidUntil =
37             Tinebase_DateTime::now()->addMinute($lifetimeMinutes)->toString();
38     }
39
40     /**
41      * @return bool
42      * @throws Exception
43      * @throws Zend_Session_Exception
44      */
45     public static function hasValidSecondFactor()
46     {
47         if (! Tinebase_Session::isStarted()) {
48             if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__
49                 . ' No session started to check second factor in session');
50             return true;
51         }
52         $currentValidUntil = Tinebase_Session::getSessionNamespace()->secondFactorValidUntil;
53         if ($currentValidUntil) {
54             $validUntil = new Tinebase_DateTime($currentValidUntil);
55             return Tinebase_DateTime::now()->isEarlier($validUntil);
56         } else {
57             return false;
58         }
59     }
60 }