Created a provisioning example
[ga4php.git] / lib / lib.php
index da5eace..2513ebc 100644 (file)
@@ -1,22 +1,5 @@
 <?php
 
-
-/*
- * TODO's:
- * Implement TOTP fully
- * Error checking, lots of error checking
- * have a way of encapsulating token data stright into a single field so it could be added
- *    in some way to a preexisting app without modifying the DB as such... or by just adding
- *    a single field to a user table...
- * Remove all reliance on the SQLite database. Data should come from the encasultating application
- *    which will be expected to provde two function calls where it can get/store data - DONE
- */
-
-/*
- * The way we should really be doing things is to have an array that encapsulates "normal" data (or a class?)
- * and then just manipulate it, then use a checkin function to push the data base into the db...
- */
-
 abstract class GoogleAuthenticator {
        
        function __construct() {
@@ -75,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);
                
@@ -88,6 +71,15 @@ abstract class GoogleAuthenticator {
        }
        
        
+       function hasToken($username) {
+               $token = $this->internalGetData($username);
+               // TODO: change this to a pattern match for an actual key
+               if(!isset($token["tokenkey"])) return false;
+               if($token["tokenkey"] == "") return false;
+               return true;
+       }
+       
+       
        // sets the key for a user - this is assuming you dont want
        // to use one created by the application. returns false
        // if the key is invalid or the user doesn't exist.
@@ -99,12 +91,6 @@ abstract class GoogleAuthenticator {
        }
        
        
-       // have user?
-       function userExists($username) {
-               // need to think about this
-       }
-       
-       
        // self explanitory?
        function deleteUser($username) {
                // oh, we need to figure out how to do thi?
@@ -117,6 +103,7 @@ abstract class GoogleAuthenticator {
        // it
        function authenticateUser($username, $code) {
 
+               if(preg_match("/[0-9][0-9][0-9][0-9][0-9][0-9]/",$code)<1) return false;
                error_log("begin auth user");
                $tokendata = $this->internalGetData($username);
                $asdf = print_r($tokendata, true);
@@ -229,8 +216,11 @@ abstract class GoogleAuthenticator {
        }
        
        // create a url compatibile with google authenticator.
-       function createURL($user, $key,$toktype = "HOTP") {
+       function createURL($user) {
                // oddity in the google authenticator... hotp needs to be lowercase.
+               $data = $this->internalGetData($user);
+               $toktype = $data["tokentype"];
+               $key = $this->helperhex2b32($data["tokenkey"]);
                $toktype = strtolower($toktype);
                if($toktype == "hotp") {
                        $url = "otpauth://$toktype/$user?secret=$key&counter=1";
@@ -253,7 +243,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";
@@ -355,5 +361,15 @@ abstract class GoogleAuthenticator {
        private $getDatafunction;
        private $putDatafunction;
        private $errorText;
+       private $errorCode;
+       
+       /*
+        * error codes
+        * 1: Auth Failed
+        * 2: No Key
+        * 3: input code was invalid (user input an invalid code - must be 6 numerical digits)
+        * 4: user doesnt exist?
+        * 5: key invalid
+        */
 }
 ?>
\ No newline at end of file