]> git.wh0rd.org Git - tt-rss.git/blob - vendor/OTPHP/TOTP.php
move OTPHP to vendor/; additionally move Base32 class to OTPHP namespace
[tt-rss.git] / vendor / OTPHP / TOTP.php
1 <?php
2 /*
3  * Copyright (c) 2011 Le Lag 
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22
23 namespace OTPHP {
24   /**
25    * TOTP - One time password generator 
26    * 
27    * The TOTP class allow for the generation 
28    * and verification of one-time password using 
29    * the TOTP specified algorithm.
30    *
31    * This class is meant to be compatible with 
32    * Google Authenticator
33    *
34    * This class was originally ported from the rotp
35    * ruby library available at https://github.com/mdp/rotp
36    */
37   class TOTP extends OTP {
38     /**
39      * The interval in seconds for a one-time password timeframe
40      * Defaults to 30
41      * @var integer
42      */
43     public $interval;
44
45     public function __construct($s, $opt = Array()) {
46       $this->interval = isset($opt['interval']) ? $opt['interval'] : 30;
47       parent::__construct($s, $opt);
48     }
49
50     /**
51      *  Get the password for a specific timestamp value 
52      *
53      *  @param integer $timestamp the timestamp which is timecoded and 
54      *  used to seed the hmac hash function.
55      *  @return integer the One Time Password
56      */
57     public function at($timestamp) {
58       return $this->generateOTP($this->timecode($timestamp));
59     }
60
61     /**
62      *  Get the password for the current timestamp value 
63      *
64      *  @return integer the current One Time Password
65      */
66     public function now() {
67       return $this->generateOTP($this->timecode(time()));
68     }
69
70     /**
71      * Verify if a password is valid for a specific counter value
72      *
73      * @param integer $otp the one-time password 
74      * @param integer $timestamp the timestamp for the a given time, defaults to current time.
75      * @return  bool true if the counter is valid, false otherwise
76      */
77     public function verify($otp, $timestamp = null) {
78       if($timestamp === null)
79         $timestamp = time();
80       return ($otp == $this->at($timestamp));
81     }
82
83     /**
84      * Returns the uri for a specific secret for totp method.
85      * Can be encoded as a image for simple configuration in 
86      * Google Authenticator.
87      *
88      * @param string $name the name of the account / profile
89      * @return string the uri for the hmac secret
90      */
91     public function provisioning_uri($name) {
92       return "otpauth://totp/".urlencode($name)."?secret={$this->secret}";
93     }
94
95     /**
96      * Transform a timestamp in a counter based on specified internal
97      *
98      * @param integer $timestamp
99      * @return integer the timecode
100      */
101     protected function timecode($timestamp) {
102       return (int)( (((int)$timestamp * 1000) / ($this->interval * 1000)));
103     }
104   }
105
106 }