diff options
author | Christian Riesen <chris.riesen@gmail.com> | 2017-01-08 17:58:00 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-08 17:58:00 +0100 |
commit | 83f941e1ad6f7a2ff318e30cbf5b3219e63a9a62 (patch) | |
tree | 1a79932cdc6d9ca38367087728fa98f9337b6478 | |
parent | e85a008d3deb156200eb9149ceb7144b18a3a400 (diff) | |
parent | ab68b3ece538c15b60c6c769d1ae3abec96249d4 (diff) | |
download | otp-2.3.0.zip otp-2.3.0.tar.gz otp-2.3.0.tar.bz2 |
Merge pull request #15 from mithodin/master2.3.0
Add support for a time offset
-rw-r--r-- | src/Otp.php | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/src/Otp.php b/src/Otp.php index 1df9782..6fd49e8 100644 --- a/src/Otp.php +++ b/src/Otp.php @@ -56,6 +56,13 @@ class Otp implements OtpInterface * @var string */ protected $algorithm = 'sha1'; + + /** + * Time offset between system time and GMT in seconds + * + * @var integer + */ + protected $totpOffset = 0; /* (non-PHPdoc) * @see Otp.OtpInterface::hotp() @@ -252,6 +259,34 @@ class Otp implements OtpInterface { return $this->digits; } + + /** + * Set offset between system time and GMT + * + * @param integer $offset GMT - time() + * @throws \InvalidArgumentException + * @return \Otp\Otp + */ + public function setTotpOffset($offset) + { + if (!is_int($offset)) { + throw new \InvalidArgumentException('Offset must be an integer'); + } + + $this->totpOffset = $offset; + + return $this; + } + + /** + * Returns offset between system time and GMT in seconds + * + * @return integer + */ + public function getTotpOffset() + { + return $this->totpOffset; + } /** * Generates a binary counter for hashing @@ -275,7 +310,7 @@ class Otp implements OtpInterface */ private function getTimecounter() { - return floor(time() / $this->period); + return floor((time() + $this->totpOffset) / $this->period); } /** |