Added a login page example
[ga4php.git] / lib / lib.php
index 218c98f..558e215 100644 (file)
@@ -58,7 +58,7 @@ abstract class GoogleAuthenticator {
        
        
        // create "user" with insert
-       function setUser($username, $key = "", $ttype="HOTP") {
+       function setUser($username, $ttype="HOTP", $key = "") {
                if($key == "") $key = $this->createBase32Key();
                $hkey = $this->helperb322hex($key);
                
@@ -76,6 +76,7 @@ abstract class GoogleAuthenticator {
                // TODO: change this to a pattern match for an actual key
                if(!isset($token["tokenkey"])) return false;
                if($token["tokenkey"] == "") return false;
+               return true;
        }
        
        
@@ -122,11 +123,12 @@ abstract class GoogleAuthenticator {
                error_log("dat is $asdf");
                switch($ttype) {
                        case "HOTP":
+                               error_log("in hotp");
                                $st = $tlid;
                                $en = $tlid+20;
                                for($i=$st; $i<$en; $i++) {
                                        $stest = $this->oath_hotp($tkey, $i);
-                                       //error_log("code: $code, $stest, $tkey, $tid");
+                                       error_log("testing code: $code, $stest, $tkey, $tid");
                                        if($code == $stest) {
                                                $tokendata["tokencounter"] = $i;
                                                $this->internalPutData($username, $tokendata);
@@ -136,6 +138,7 @@ abstract class GoogleAuthenticator {
                                return false;
                                break;
                        case "TOTP":
+                               error_log("in totp");
                                $t_now = time();
                                $t_ear = $t_now - 45;
                                $t_lat = $t_now + 60;
@@ -144,7 +147,7 @@ abstract class GoogleAuthenticator {
                                //error_log("kmac: $t_now, $t_ear, $t_lat, $t_st, $t_en");
                                for($i=$t_st; $i<=$t_en; $i++) {
                                        $stest = $this->oath_hotp($tkey, $i);
-                                       //error_log("code: $code, $stest, $tkey\n");
+                                       error_log("testing code: $code, $stest, $tkey\n");
                                        if($code == $stest) {
                                                return true;
                                        }
@@ -219,7 +222,7 @@ abstract class GoogleAuthenticator {
                // oddity in the google authenticator... hotp needs to be lowercase.
                $data = $this->internalGetData($user);
                $toktype = $data["tokentype"];
-               $key = $data["tokenkey"];
+               $key = $this->helperhex2b32($data["tokenkey"]);
                $toktype = strtolower($toktype);
                if($toktype == "hotp") {
                        $url = "otpauth://$toktype/$user?secret=$key&counter=1";
@@ -242,7 +245,23 @@ abstract class GoogleAuthenticator {
                
                return $key;
        }
+       
+       // returns a hex key
+       function getKey($username) {
+               $data = $this->internalGetData($username);
+               $key = $data["tokenkey"];
+               
+               return $key;
+       }
+               
+       // get key type
+       function getTokenType($username) {
+               $data = $this->internalGetData($username);
+               $toktype = $data["tokentype"];
                
+               return $toktype;
+       }
+       
        
        function helperb322hex($b32) {
         $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";