summaryrefslogtreecommitdiff
path: root/app/Auth/TotpAuth.php
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2015-12-05 20:31:27 -0500
committerFrederic Guillot <fred@kanboard.net>2015-12-05 20:31:27 -0500
commite9fedf3e5cd63aea4da7a71f6647ee427c62fa49 (patch)
treeabc2de5aebace4a2d7c94805552264dab6b10bc7 /app/Auth/TotpAuth.php
parent346b8312e5ac877ce3192c2db3a26b500018bbb5 (diff)
Rewrite of the authentication and authorization system
Diffstat (limited to 'app/Auth/TotpAuth.php')
-rw-r--r--app/Auth/TotpAuth.php126
1 files changed, 126 insertions, 0 deletions
diff --git a/app/Auth/TotpAuth.php b/app/Auth/TotpAuth.php
new file mode 100644
index 00000000..f41fabd8
--- /dev/null
+++ b/app/Auth/TotpAuth.php
@@ -0,0 +1,126 @@
+<?php
+
+namespace Kanboard\Auth;
+
+use Otp\Otp;
+use Otp\GoogleAuthenticator;
+use Base32\Base32;
+use Kanboard\Core\Base;
+use Kanboard\Core\Security\PostAuthenticationProviderInterface;
+
+/**
+ * TOTP Authentication Provider
+ *
+ * @package auth
+ * @author Frederic Guillot
+ */
+class TotpAuth extends Base implements PostAuthenticationProviderInterface
+{
+ /**
+ * User pin code
+ *
+ * @access private
+ * @var string
+ */
+ private $code = '';
+
+ /**
+ * Private key
+ *
+ * @access private
+ * @var string
+ */
+ private $secret = '';
+
+ /**
+ * Get authentication provider name
+ *
+ * @access public
+ * @return string
+ */
+ public function getName()
+ {
+ return 'Time-based One-time Password Algorithm';
+ }
+
+ /**
+ * Authenticate the user
+ *
+ * @access public
+ * @return boolean
+ */
+ public function authenticate()
+ {
+ $otp = new Otp;
+ return $otp->checkTotp(Base32::decode($this->secret), $this->code);
+ }
+
+ /**
+ * Set validation code
+ *
+ * @access public
+ * @param string $code
+ */
+ public function setCode($code)
+ {
+ $this->code = $code;
+ }
+
+ /**
+ * Set secret token
+ *
+ * @access public
+ * @param string $secret
+ */
+ public function setSecret($secret)
+ {
+ $this->secret = $secret;
+ }
+
+ /**
+ * Get secret token
+ *
+ * @access public
+ * @return string
+ */
+ public function getSecret()
+ {
+ if (empty($this->secret)) {
+ $this->secret = GoogleAuthenticator::generateRandom();
+ }
+
+ return $this->secret;
+ }
+
+ /**
+ * Get QR code url
+ *
+ * @access public
+ * @param string $label
+ * @return string
+ */
+ public function getQrCodeUrl($label)
+ {
+ if (empty($this->secret)) {
+ return '';
+ }
+
+ return GoogleAuthenticator::getQrCodeUrl('totp', $label, $this->secret);
+ }
+
+ /**
+ * Get key url (empty if no url can be provided)
+ *
+ * @access public
+ * @param string $label
+ * @return string
+ */
+ public function getKeyUrl($label)
+ {
+ if (empty($this->secret)) {
+ return '';
+ }
+
+ return GoogleAuthenticator::getKeyUri('totp', $label, $this->secret);
+ }
+}