Added a login page example
[ga4php.git] / example / provisioning / index.php
1 <?php 
2 /*
3  * This example is simply an example of how a provisioning page may look
4  * which includes such funcationality as createing users, initialising their
5  * data, create a token for them, testing the token and resyncing it as needed
6  * 
7  */
8
9 // Require our php libraries
10 require_once("token.php");
11 require_once("dbfunctions.php");
12 require_once("input.php");
13
14 // now lets get an instance of our class
15 $myga = new myGA();
16 global $myga;
17
18 // this part of the page resonds to user input
19 processInput();
20 ?>
21
22 <html>
23 <h1>Welcome to GA Provisioning!</h1>
24
25 <?php 
26 // in this part of the code we look for "success" or "fail" things
27 if(isset($_REQUEST["success"])) {
28         echo "<br><font color=\"green\">".$_REQUEST["success"]."</font><br>";
29 }
30 if(isset($_REQUEST["failure"])) {
31         echo "<br><font color=\"red\">".$_REQUEST["failure"]."</font><br>";
32 }
33 ?>
34
35 <hr>
36 <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,
37 select the user from the drop down box and then select a token type, then click "provision". In the main user list section
38 your user should now have a qrcode representing the key for that user. Pull our your mobile phone (with the google
39 authenticator app from the market) and scan in the code. Next, select the user who's authentication you wish to test from
40 the drop down list under "test authentication" section, generate a code for that user on your phone and click "Auth".
41 this should fail/succeed depending on whether you have typed in the right code.
42 <hr>
43
44
45
46 <h2>Users</h2>
47 <table border="1">
48 <tr><th>Username/Login</th><th>Fullname</th><th>Has Token?</th><th>Key</th><th>Base 32 Key</th><th>Hex Key</th></tr>
49 <?php
50 // now we get our list of users - this part of the page just has a list of users
51 // and the ability to create new ones. This isnt really in the scope of the
52 // GA4PHP, but for this example, we need to be able to create users, so heres where
53 // you do it.
54 $db = getDatabase();
55 $result = $db->query("select * from users");
56 foreach($result as $row) {
57         if($myga->hasToken($row["users_username"])) {
58                 $hastoken = "Yes";
59                 $type = $myga->getTokenType($row["users_username"]);
60                 if($type == "HOTP") {
61                         $type = "- Counter Based";
62                 } else {
63                         $type = "- Time Based";
64                 }
65                 $hexkey = $myga->getKey($row["users_username"]);
66                 $b32key = $myga->helperhex2b32($hexkey);
67                 
68                 $url = urlencode($myga->createURL($row["users_username"]));
69                 $keyurl = "<img src=\"http://chart.apis.google.com/chart?cht=qr&chl=$url&chs=100x100\">";
70                 
71         }
72         else {
73                 $b32key = "";
74                 $hexkey = "";
75                 $type = "";
76                 $hastoken = "no";
77                 $keyurl = "";
78         }
79         
80         
81         // now we generate the qrcode for the user
82         
83         echo "<tr><td>".$row["users_username"]."</td><td>".$row["users_fullname"]."</td><td>$hastoken $type</td><td>$keyurl</td><td>$b32key</td><td>$hexkey</td></tr>";
84 }
85 closeDatabase($db);
86 ?>
87 </table>
88 Create a User:
89 <form method="post" action="?action=createuser">
90 Username/login: <input type="text" name="username">
91 Full Name: <input type="text" name="fullname">
92 Password: <input type="password" name="password">
93 <input type="submit" name="Add" value="Add">
94 </form>
95
96
97 <hr>
98
99
100
101 <h2>Create Token</h2>
102 This form allows you to provision a token for the user<br>
103 <form method="post" action="?action=provision">
104 User:<select name="user">
105 <?php
106 // here we list the users again for a select clause
107 $db = getDatabase();
108 $result = $db->query("select * from users");
109 foreach($result as $row) {
110         if($myga->hasToken($row["users_username"])) $hastoken = "- Has a token";
111         else $hastoken = "- No token";
112         
113         $username = $row["users_username"];
114         
115         echo "<option value=\"$username\">$username $hastoken</option>";
116 }
117 closedatabase($db);
118 ?>
119 </select>
120 <br>
121 Token Type
122 <select name="tokentype">
123 <option value="HOTP">Counter Based</option>
124 <option value="TOTP">Time Based</option>
125 </select>
126 <input type="submit" name="Provision" value="Provision">
127 </form>
128
129 <hr>
130 <h2>Test Authentication</h2>
131 <form method="post" action="?action=auth">
132 User:<select name="user">
133 <?php
134 // here we list the users again for a select clause
135 $db = getDatabase();
136 $result = $db->query("select * from users");
137 foreach($result as $row) {
138         if($myga->hasToken($row["users_username"])) $hastoken = "- Has a token";
139         else $hastoken = "- No token";
140         
141         $username = $row["users_username"];
142         
143         echo "<option value=\"$username\">$username $hastoken</option>";
144 }
145 closedatabase($db);
146 ?>
147 <input type="text" name="tokencode">
148 <input type="submit" name="Auth" value="Auth">
149 </select>
150
151
152 <pre>
153 <?php 
154
155 print_r($myga->internalGetData("asdf"));
156 ?>
157 </pre>
158
159 </html>