set token types
[ga4php.git] / lib / ga4php.php
index a052575..125556a 100644 (file)
@@ -112,7 +112,10 @@ abstract class GoogleAuthenticator {
                // consider scrapping this
                $token = $this->internalGetData($username);
                $token["tokenkey"] = $key;
-               $this->internalPutData($username, $token);              
+               $this->internalPutData($username, $token);
+               
+               // TODO error checking
+               return true;
        }
        
        
@@ -147,11 +150,11 @@ abstract class GoogleAuthenticator {
                switch($ttype) {
                        case "HOTP":
                                error_log("in hotp");
-                               $st = $tlid;
+                               $st = $tlid+1;
                                $en = $tlid+$this->hotpSkew;
                                for($i=$st; $i<$en; $i++) {
                                        $stest = $this->oath_hotp($tkey, $i);
-                                       error_log("testing code: $code, $stest, $tkey, $tid");
+                                       //error_log("testing code: $code, $stest, $tkey, $tid");
                                        if($code == $stest) {
                                                $tokendata["tokencounter"] = $i;
                                                $this->internalPutData($username, $tokendata);
@@ -196,7 +199,7 @@ abstract class GoogleAuthenticator {
                // for keys
                
                //              $this->dbConnector->query('CREATE TABLE "tokens" ("token_id" INTEGER PRIMARY KEY AUTOINCREMENT,"token_key" TEXT NOT NULL, "token_type" TEXT NOT NULL, "token_lastid" INTEGER NOT NULL)');
-               $tokendata = internalGetData($username);
+               $tokendata = $this->internalGetData($username);
                
                // TODO: check return value
                $ttype = $tokendata["tokentype"];
@@ -219,7 +222,7 @@ abstract class GoogleAuthenticator {
                                                $stest2 = $this->oath_hotp($tkey, $i+1);
                                                if($code2 == $stest2) {
                                                        $tokendata["tokencounter"] = $i+1;
-                                                       internalPutData($username, $tokendata);                                         
+                                                       $this->internalPutData($username, $tokendata);                                          
                                                        return true;
                                                }
                                        }
@@ -247,7 +250,10 @@ abstract class GoogleAuthenticator {
                $data = $this->internalGetData($user);
                $toktype = $data["tokentype"];
                $key = $this->helperhex2b32($data["tokenkey"]);
-               $counter = $data["tokencounter"];
+
+               // token counter should be one more then current token value, otherwise
+               // it gets confused
+               $counter = $data["tokencounter"]+1;
                $toktype = strtolower($toktype);
                if($toktype == "hotp") {
                        $url = "otpauth://$toktype/$user?secret=$key&counter=$counter";