Added a login page example
authorpaulr <me@pjr.cc>
Tue, 16 Nov 2010 23:22:17 +0000 (10:22 +1100)
committerpaulr <me@pjr.cc>
Tue, 16 Nov 2010 23:22:17 +0000 (10:22 +1100)
doco/TODO.txt
example/login/index.php [new file with mode: 0644]
example/provisioning/dbfunctions.php
example/provisioning/index.php
example/provisioning/input.php
lib/lib.php

index d0ff031..813d34d 100644 (file)
@@ -1,6 +1,7 @@
 The Almighty TODO list:
 
 1) Error checking, lots of error checking and sanity checking Then i need to setup error codes and stuff.
+1.1) put code comments in all the examples
 2) a "hasToken" method for determining if a user has a token or not
 3) implement googles key integrity algorithm thing
 4) make a better example
diff --git a/example/login/index.php b/example/login/index.php
new file mode 100644 (file)
index 0000000..fc9ef2b
--- /dev/null
@@ -0,0 +1,140 @@
+<?php 
+/* 
+ * This example rely's on the provisioning example, you must first create accounts in the provisioning 
+ * example then use them here. 
+ * 
+ * This example is solely an example of how a login page might look and/or work
+ * 
+ * If a user doesnt have a token assigned, they wont require it on the login page. This is an example
+ * of when your allowing the user to increase security of their OWN account, not the security of the
+ * site as such.
+ * 
+ */ 
+
+require_once("../provisioning/dbfunctions.php");
+require_once("../provisioning/token.php");
+
+
+session_start();
+
+$myga = new myGA();
+
+// check if we're logged in
+if(isset($_SESSION["loginname"])) {
+       if($_SESSION["loginname"]!="") {
+               
+               // handle logout
+               if(isset($_REQUEST["logout"])) {
+                       error_log("session killer");
+                       unset($_SESSION["loginname"]);
+                       header("Location: index.php");
+                       return;
+               }
+               
+               // display the logged in page
+               displayLogedInPage();
+               return;
+       }
+}
+
+
+// here is where we process the login
+if(isset($_REQUEST["login"])) {
+       $db = getDatabase();
+       
+       // get the data from the post request
+       error_log("begin login");
+       $username = $_REQUEST["username"];
+       $password = $_REQUEST["password"];
+       $tokencode = $_REQUEST["tokencode"];
+       
+       // pull the password hash from the database
+       $sql = "select users_password from users where users_username='$username'";
+       error_log("running sql: $sql");
+       $res = $db->query($sql);
+       
+       foreach($res as $row) {
+               $passhash = $row["users_password"];
+       }
+       
+       // user entered a tokencode, fail the login and tell the user
+       // if they dont have a token code assigned to them
+       if($tokencode != "") {
+               if(!$myga->hasToken($username)) {
+                       $msg = urlencode("Attempted to login with a token when username isnt assigned one");
+                       header("Location: index.php?failure=$msg");
+               }
+       }
+       
+       // check the password hash versus the login password
+       error_log("checking $passhash against $password (".sha1($password).")");
+       if($passhash == sha1($password)) $passright = true;
+       else {
+               header("Location: index.php?failure=LoginIncorrect");
+               return;
+       }
+       
+       // now get myGA to check the token code
+       error_log("passed password auth");
+       if($myga->hasToken($username)) if(!$myga->authenticateUser($username, $tokencode)) {
+               header("Location: index.php?failure=LoginIncorrect");
+               return;
+       }
+
+       // and we're loged in
+       $_SESSION["loginname"] = "$username";
+       
+       header("Location: index.php");
+       return;
+}
+
+
+
+
+// and our "your logged in" page
+function displayLogedInPage()
+{
+?>
+<html>
+<h1>Welcome</h1>
+Welcome <?php echo $_SESSION["loginname"]?>, you are logged in.
+Click <a href="index.php?logout">here</a> to log out.
+</html>
+<?php
+       echo "<pre>";
+       print_r($_REQUEST);
+       print_r($_SESSION);
+       echo "</pre>";
+       
+       return; 
+}
+
+
+
+
+?>
+<html>
+<h2>Welcome to Generic Site</h2>
+<i><b>Note:</b> if the user you've provisioned has not got a token code, its not required for login</i><br>
+Please login:
+<?php
+if(isset($_REQUEST["failure"])) {
+       echo "<hr><font color=\"red\">Login Failure: ".$_REQUEST["failure"]."</font><hr>";      
+} 
+?>
+<form method="post" action="index.php?login">
+<table>
+<tr><td>Username</td><td><input type="text" name="username"></td></tr>
+<tr><td>Password</td><td><input type="password" name="password"></td></tr>
+<tr><td>Pin Code</td><td><input type="text" name="tokencode"></td></tr>
+<tr><td><input type="submit" name="login" value="Login"></td></tr>
+</table>
+</form>
+<hr>
+<pre>
+<?php 
+       print_r($_REQUEST);
+       print_r($_SESSION);
+?>
+</pre>
+</html>
\ No newline at end of file
index 2aa2c05..ca90aa0 100644 (file)
@@ -14,7 +14,7 @@ function getDatabase() {
                } catch(PDOException $exep) {
                        error_log("execpt on db open");
                }
-               $sql = 'CREATE TABLE "users" ("users_id" INTEGER PRIMARY KEY AUTOINCREMENT,"users_username" TEXT,"users_fullname" TEXT,"users_tokendata" TEXT);';
+               $sql = 'CREATE TABLE "users" ("users_id" INTEGER PRIMARY KEY AUTOINCREMENT,"users_username" TEXT,"users_fullname" TEXT,"users_password" TEXT, "users_tokendata" TEXT);';
                $dbobject->query($sql);
        }
        
index 0d9fc18..d2cf4f4 100644 (file)
@@ -33,7 +33,7 @@ if(isset($_REQUEST["failure"])) {
 ?>
 
 <hr>
-<b>How to user this page</b> - Create a user with the "Users" form. Once a user is created, then in the "Create Token" form,
+<b>How to use this page</b> - Create a user with the "Users" form. Once a user is created, then in the "Create Token" form,
 select the user from the drop down box and then select a token type, then click "provision". In the main user list section
 your user should now have a qrcode representing the key for that user. Pull our your mobile phone (with the google
 authenticator app from the market) and scan in the code. Next, select the user who's authentication you wish to test from
@@ -89,6 +89,7 @@ Create a User:
 <form method="post" action="?action=createuser">
 Username/login: <input type="text" name="username">
 Full Name: <input type="text" name="fullname">
+Password: <input type="password" name="password">
 <input type="submit" name="Add" value="Add">
 </form>
 
index 11ab6b7..23ff909 100644 (file)
@@ -10,7 +10,8 @@ function processInput() {
                                // "users_id" INTEGER PRIMARY KEY AUTOINCREMENT,"users_username" TEXT,"users_fullname" TEXT,"users_tokendata" TEXT
                                $username = $_REQUEST["username"];
                                $fullname = $_REQUEST["fullname"];
-                               $sql = "insert into users values (NULL, '$username', '$fullname', '0')";
+                               $password = sha1($_REQUEST["password"]);
+                               $sql = "insert into users values (NULL, '$username', '$fullname', '$password','0')";
                                $db = getDatabase();
                                $db->query($sql);
                                closeDatabase($db);
index 2513ebc..558e215 100644 (file)
@@ -123,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);
@@ -137,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;
@@ -145,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;
                                        }