Created a provisioning example
[ga4php.git] / example / provisioning / token.php
1 <?php
2
3 require_once("../../lib/lib.php");
4
5 // define our token class
6 class myGA extends GoogleAuthenticator {
7         function getData($username) {
8                 
9                 // get our database connection
10                 $dbObject = getDatabase();
11                 
12                 // set the sql for retreiving the data
13                 $sql = "select users_tokendata from users where users_username='$username'";
14                 
15                 // run the query
16                 $result = $dbObject->query($sql);
17                 
18                 // check the result
19                 if(!$result) return false;
20                 
21                 // now just retreieve all the data (there should only be one, but whatever)
22                 $tokendata = false;
23                 foreach($result as $row) {
24                         $tokendata = $row["users_tokendata"];
25                 }
26                 
27                 // now we have our data, we just return it. If we got no data
28                 // we'll just return false by default
29                 return $tokendata;
30                 
31                 // and there you have it, simple eh?
32         }
33         
34         
35         // now we need a function for putting the data back into our user table.
36         // in this example, we wont check anything, we'll just overwrite it.
37         function putData($username, $data) {
38                 // get our database connection
39                 $dbObject = getDatabase();
40                 
41                 // set the sql for updating the data
42                 // token data is stored as a base64 encoded string, it should
43                 // not need to be escaped in any way prior to storing in a database
44                 // but feel free to call your databases "addslashes" (or whatever)
45                 // function on $data prior to doing the SQL.
46                 $sql = "update users set users_tokendata='$data' where users_username='$username'";
47                 
48                 // now execute the sql and return straight away - you should probably
49                 // clean up after yourselves, but im going to assume pdo does this
50                 // for us anyway in this exmaple
51                 if($dbObject->query($sql)) {
52                         return true;
53                 } else {
54                         return false;
55                 }
56                 
57                 // even simpler!
58         }
59         
60         function getUsers() {
61                 // get our database connection
62                 $dbObject = getDatabase();
63                 
64                 // now the sql again
65                 $sql = "select users_username from users";
66                 
67                 // run the query
68                 $result = $dbObject->query($sql);
69                 
70                 // iterate over the results - we expect a simple array containing
71                 // a list of usernames
72                 $i = 0;
73                 $users = array();
74                 foreach($result as $row) {
75                         $users[$i] = $row["username"];
76                         $i++;
77                 }
78                 
79                 // now return the list
80                 return $users;
81         }
82 }
83
84 ?>