4 * This file just shows a simple example of how to extend the GoogleAuthenticator schema
6 require_once("../../lib/ga4php.php");
9 /* we now define the three methods we have to define
10 * the way the class can get and save data. The class calls
11 * these methods frequently
13 * What we need to define is methods for saving the auth data
16 * Lets assume our application already has a user table, so what
17 * we do is add a new column into our table for saving auth data
19 * i.e. "alter table users add tokendata text" would add a column
20 * we can use to our "users" tables... Lets also assume that we
21 * have a column called "username" which defines the username
23 * Lastly, lets assume we can get a connection to the database
24 * by calling a function GetDatabase(); which is a PDO object
27 class MyGoogleAuth extends GoogleAuthenticator {
29 function getData($username) {
31 // get our database connection
32 $dbObject = GetDatabase();
34 // set the sql for retreiving the data
35 $sql = "select tokendata from users where username='$username'";
38 $result = $dbObject->query($sql);
41 if(!$result) return false;
43 // now just retreieve all the data (there should only be one, but whatever)
45 foreach($result as $row) {
46 $tokendata = $row["tokendata"];
49 // now we have our data, we just return it. If we got no data
50 // we'll just return false by default
53 // and there you have it, simple eh?
57 // now we need a function for putting the data back into our user table.
58 // in this example, we wont check anything, we'll just overwrite it.
59 function putData($username, $data) {
60 // get our database connection
61 $dbObject = GetDatabase();
63 // set the sql for updating the data
64 // token data is stored as a base64 encoded string, it should
65 // not need to be escaped in any way prior to storing in a database
66 // but feel free to call your databases "addslashes" (or whatever)
67 // function on $data prior to doing the SQL.
68 $sql = "update users set tokendata='$data' where username='$username'"
70 // now execute the sql and return straight away - you should probably
71 // clean up after yourselves, but im going to assume pdo does this
72 // for us anyway in this exmaple
73 if($dbObject->query($sql)) {
82 // the get users method isnt actually used within the google authenticator
83 // class as yet, but it probably will in the future, so feel free to implement it
85 // get our database connection
86 $dbObject = GetDatabase();
89 $sql = "select username from users";
92 $result = $dbObject->query($sql);
94 // iterate over the results - we expect a simple array containing
95 // a list of usernames
98 foreach($result as $row) {
99 $users[$i] = $row["username"];
103 // now return the list