changed default token type to totp
[ga4php.git] / authserver / lib / lib.php
1 <?php
2
3 if(!isset($MSG_QUEUE_KEY_ID_SERVER)) $MSG_QUEUE_KEY_ID_SERVER = "189751072"; // i would use ftok, but its crap
4 if(!isset($MSG_QUEUE_KEY_ID_CLIENT)) $MSG_QUEUE_KEY_ID_CLIENT = "189751073"; // ftok is not ok!
5 global $MSG_QUEUE_KEY_ID_SERVER, $MSG_QUEUE_KEY_ID_CLIENT;
6
7 define("MSG_AUTH_USER_TOKEN", 1);
8 define("MSG_ADD_USER_TOKEN", 2);
9 define("MSG_DELETE_USER", 3);
10 define("MSG_AUTH_USER_PASSWORD", 4);
11 define("MSG_SET_USER_PASSWORD", 5);
12 define("MSG_SET_USER_REALNAME", 6);
13 define("MSG_SET_USER_TOKEN", 7);
14 define("MSG_SET_USER_TOKEN_TYPE", 8);
15 define("MSG_GET_USERS", 9);
16 define("MSG_GET_OTK_PNG", 10);
17 define("MSG_GET_OTK_ID", 11);
18 define("MSG_DELETE_USER_TOKEN", 12);
19
20 // BASE_DIR = 
21 $BASE_DIR = realpath(dirname(__FILE__)."/../../");
22 global $BASE_DIR;
23
24 // messy
25 require_once(dirname(__FILE__)."/../../lib/ga4php.php");
26
27 function generateRandomString()
28 {
29         $str = "";
30         $strpos = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
31         
32         for($i=0; $i<128; $i++) {
33                 $str .= $strpos[rand(0, strlen($strpos)-1)];
34         }
35         
36         return $str;
37 }
38
39
40 function getDatabase() {
41         $dbobject = false;
42         global $BASE_DIR;
43         if(file_exists("$BASE_DIR/authserver/authd/gaasdata.sqlite")) {
44                 try {
45                         $dbobject = new PDO("sqlite:$BASE_DIR/authserver/authd/gaasdata.sqlite");
46                 } catch(PDOException $exep) {
47                         error_log("execpt on db open");
48                 }
49         } else {
50                 try {
51                         $dbobject = new PDO("sqlite:$BASE_DIR/authserver/authd/gaasdata.sqlite");
52                 } catch(PDOException $exep) {
53                         error_log("execpt on db open");
54                 }
55                 $sql = 'CREATE TABLE "users" ("users_id" INTEGER PRIMARY KEY AUTOINCREMENT,"users_username" TEXT, "users_realname" TEXT, "users_password" TEXT, "users_tokendata" TEXT, "users_otk" TEXT);';
56                 $dbobject->query($sql);
57         }
58         
59         return $dbobject;
60 }
61
62 function closeDatabase($db) {
63         // doesnt do anything yet
64 }
65
66 class gaasGA extends GoogleAuthenticator {
67         function getData($username) {
68                 echo "called into getdata\n";
69                 
70                 // get our database connection
71                 $dbObject = getDatabase();
72                 
73                 // set the sql for retreiving the data
74                 $sql = "select users_tokendata from users where users_username='$username'";
75                 
76                 // run the query
77                 $result = $dbObject->query($sql);
78                 
79                 // check the result
80                 echo "next1\n";
81                 if(!$result) return false;
82                 
83                 // now just retreieve all the data (there should only be one, but whatever)
84                 echo "next2\n";
85                 $tokendata = false;
86                 foreach($result as $row) {
87                         $tokendata = $row["users_tokendata"];
88                 }
89
90                 echo "next3, $username, $tokendata\n";
91                 // now we have our data, we just return it. If we got no data
92                 // we'll just return false by default
93                 return $tokendata;
94                 
95                 // and there you have it, simple eh?
96         }
97         
98         
99         function putData($username, $data) {
100                 // get our database connection
101                 $dbObject = getDatabase();
102                 
103                 // we need to check if the user exists, and if so put the data, if not create the data
104                 $sql = "select * from users where users_username='$username'";
105                 $res = $dbObject->query($sql);
106                 if($res->fetchColumn() > 0) {
107                         // do update
108                         error_log("doing userdata update");
109                         $sql = "update users set users_tokendata='$data' where users_username='$username'";
110                 } else {
111                         // do insert
112                         error_log("doing user data create");
113                         $sql = "insert into users values (NULL, '$username', '', '', '$data', '')";
114                 }
115                 
116                 if($dbObject->query($sql)) {
117                         return true;
118                 } else {
119                         return false;
120                 }
121
122         }
123         
124         function getUsers() {
125                 // get our database connection
126                 $dbObject = getDatabase();
127                 
128                 // now the sql again
129                 $sql = "select users_username from users";
130                 
131                 // run the query
132                 $result = $dbObject->query($sql);
133                 
134                 // iterate over the results - we expect a simple array containing
135                 // a list of usernames
136                 $i = 0;
137                 $users = array();
138                 foreach($result as $row) {
139                         $users[$i] = $row["username"];
140                         $i++;
141                 }
142                 
143                 // now return the list
144                 return $users;
145         }       
146 }
147
148 ?>