0d9fc1899e298575d0df52bdd6ab45b2b7881af2
[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 user 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 <input type="submit" name="Add" value="Add">
93 </form>
94
95
96 <hr>
97
98
99
100 <h2>Create Token</h2>
101 This form allows you to provision a token for the user<br>
102 <form method="post" action="?action=provision">
103 User:<select name="user">
104 <?php
105 // here we list the users again for a select clause
106 $db = getDatabase();
107 $result = $db->query("select * from users");
108 foreach($result as $row) {
109         if($myga->hasToken($row["users_username"])) $hastoken = "- Has a token";
110         else $hastoken = "- No token";
111         
112         $username = $row["users_username"];
113         
114         echo "<option value=\"$username\">$username $hastoken</option>";
115 }
116 closedatabase($db);
117 ?>
118 </select>
119 <br>
120 Token Type
121 <select name="tokentype">
122 <option value="HOTP">Counter Based</option>
123 <option value="TOTP">Time Based</option>
124 </select>
125 <input type="submit" name="Provision" value="Provision">
126 </form>
127
128 <hr>
129 <h2>Test Authentication</h2>
130 <form method="post" action="?action=auth">
131 User:<select name="user">
132 <?php
133 // here we list the users again for a select clause
134 $db = getDatabase();
135 $result = $db->query("select * from users");
136 foreach($result as $row) {
137         if($myga->hasToken($row["users_username"])) $hastoken = "- Has a token";
138         else $hastoken = "- No token";
139         
140         $username = $row["users_username"];
141         
142         echo "<option value=\"$username\">$username $hastoken</option>";
143 }
144 closedatabase($db);
145 ?>
146 <input type="text" name="tokencode">
147 <input type="submit" name="Auth" value="Auth">
148 </select>
149
150
151 <pre>
152 <?php 
153
154 print_r($myga->internalGetData("asdf"));
155 ?>
156 </pre>
157
158 </html>