working example page
[ga4php.git] / lib / lib.php
index 262149f..01340d9 100644 (file)
@@ -44,13 +44,15 @@ class GoogleAuthenticator {
                $sql = "select * from users where user_name='$username'";
                $res = $this->dbConnector->query($sql);
 
-               if($res->fetchCount()>0) {
-                       $this->errorText = "User Already Exists, $username";
-                       return false;
-               }
+               //if($res->fetchCount()>0) {
+                       //$this->errorText = "User Already Exists, $username";
+                       //return false;
+               //}
                
                // and finally create 'em
-               $this->dbConnector->query("insert into tokens values (NULL, '$key', 'HOTP','0')");
+               $hkey = $this->helperb322hex($key);
+               error_log("key for user $username is $hkey, $key");
+               $this->dbConnector->query("insert into tokens values (NULL, '$hkey', 'HOTP','0')");
                $id = $this->dbConnector->lastInsertID();
                $this->dbConnector->query("insert into users values (NULL, '$username', '$id')");
 
@@ -95,7 +97,8 @@ class GoogleAuthenticator {
                //}
                
                // and finally create 'em
-               $this->dbConnector->query("insert into tokens values (NULL, '$key', 'HOTP', '0')");
+               $hkey = $this->helperb322hex($key);
+               $this->dbConnector->query("insert into tokens values (NULL, '$hkey', 'HOTP', '0')");
                $id = $this->dbConnector->lastInsertID();
                $this->dbConnector->query("insert into users values (NULL, '$username', '$id')");
 
@@ -239,8 +242,8 @@ class GoogleAuthenticator {
        
        // create a url compatibile with google authenticator.
        function createURL($user, $key) {
-               $url = "otpauth://totp/$user?secret=$key";
-               echo "url: $url\n";
+               $url = "otpauth://hotp/$user?secret=$key";
+               //echo "url: $url\n";
                return $url;
        }
        
@@ -329,7 +332,7 @@ class GoogleAuthenticator {
        
            // HMAC
            $hash = hash_hmac ('sha1', $bin_counter, $key);
-           return $this->oath_truncate($hash);
+           return str_pad($this->oath_truncate($hash), 6, "0", STR_PAD_LEFT);
        }
        
        function oath_truncate($hash, $length = 6)