7fdb67e1e2b5dc5a2cc49f8265ea096536ad7faf
[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         $currentValidUntil = Tinebase_Session::getSessionNamespace()->secondFactorValidUntil;
48         if ($currentValidUntil) {
49             $validUntil = new Tinebase_DateTime($currentValidUntil);
50             return Tinebase_DateTime::now()->isEarlier($validUntil);
51         } else {
52             return false;
53         }
54     }
55 }