7 * Error checking, lots of error checking
8 * have a way of encapsulating token data stright into a single field so it could be added
9 * in some way to a preexisting app without modifying the DB as such... or by just adding
10 * a single field to a user table...
11 * Remove all reliance on the SQLite database. Data should come from the encasultating application
12 * which will be expected to provde two function calls where it can get/store data - DONE
17 * For some reason i had it in my head to pass static functions into the construct
18 * of the class in order to hand data going in/out
19 * when in reality i should be making the data in/out func's abstract
20 * and getting implementors to extend the class
22 * For now im going to keep implementing it this way and thus my class will
23 * forever be an example of poor design choices. It'll change it very shortly though
25 class GoogleAuthenticator {
27 // first we init google authenticator by passing it a filename
28 // for its sqlite database.
29 // $getDataFunction expects 1 argument which defines what data it wants
30 // and can be "userlist" or "usertoken:username"
31 // $putDataFunciton expects 2 arguments, $1 is data type, $2 is the data
32 // $1 can be "changetoken:username", "removetoken:username", $2 is the token
33 // data in some encoded form
34 // tokenDATA will be like HOTP;KEY;CID where CID is the current counter value
35 // why encode like this? i cant think of a good reason tbh, i should probably just
36 // use php's arry encoder functions
37 function __construct($getDataFunction, $putDataFunction) {
38 $this->putDataFunction = $putDataFunction;
39 $this->getDataFunction = $getDataFunction;
42 // this could get ugly for large databases.. we'll worry about that if it ever happens.
43 function getUserList() {
44 $func = $this->getDataFunction;
45 return $func("userlist", "");
48 // set the token type the user it going to use.
49 // this defaults to HOTP - we only do 30s token
50 // so lets not be able to set that yet
51 function setupTokenType($username, $tokentype) {
52 if($tokentype!="HOTP" and $tokentype!="TOTP") {
53 $errorText = "Invalid Token Type";
57 $put["username"] = $username;
58 $put["tokentype"] = $tokentype;
59 $func = $this->putDataFunction;
60 $func("settokentype", $put);
66 // create "user" with insert
67 function setUser($username, $key = "", $ttype="HOTP") {
68 if($key == "") $key = $this->createBase32Key();
69 $hkey = $this->helperb322hex($key);
71 $token["username"] = $username;
72 $token["tokenkey"] = $hkey;
73 $token["tokentype"] = $ttype;
75 $func = $this->putDataFunction;
76 $func("setusertoken", $token);
82 // sets the key for a user - this is assuming you dont want
83 // to use one created by the application. returns false
84 // if the key is invalid or the user doesn't exist.
85 function setUserKey($username, $key) {
86 // consider scrapping this
91 function userExists($username) {
92 // need to think about this
97 function deleteUser($username) {
98 $func = $this->putDataFunction;
99 $func("deleteusertoken", $username);
102 // user has input their user name and some code, authenticate
104 function authenticateUser($username, $code) {
106 $func = $this->getDataFunction;
107 $tokendata = $func("gettoken", $username);
109 // TODO: check return value
110 $ttype = $tokendata["tokentype"];
111 $tlid = $tokendata["tokencounter"];
112 $tkey = $tokendata["tokenkey"];
117 for($i=$st; $i<$en; $i++) {
118 $stest = $this->oath_hotp($tkey, $i);
119 //error_log("code: $code, $stest, $tkey, $tid");
120 if($code == $stest) {
121 $tokenset["username"] = $username;
122 $tokenset["tokencounter"] = $i;
124 $func = $this->putDataFunction;
125 $func("settokencounter", $tokenset);
133 $t_ear = $t_now - 45;
134 $t_lat = $t_now + 60;
135 $t_st = ((int)($t_ear/30));
136 $t_en = ((int)($t_lat/30));
137 //error_log("kmac: $t_now, $t_ear, $t_lat, $t_st, $t_en");
138 for($i=$t_st; $i<=$t_en; $i++) {
139 $stest = $this->oath_hotp($tkey, $i);
140 //error_log("code: $code, $stest, $tkey\n");
141 if($code == $stest) {
147 echo "how the frig did i end up here?";
154 // this function allows a user to resync their key. If too
155 // many codes are called, we only check up to 20 codes in the future
156 // so if the user is at 21, they'll always fail.
157 function resyncCode($username, $code1, $code2) {
158 // here we'll go from 0 all the way thru to 200k.. if we cant find the code, so be it, they'll need a new one
159 // for HOTP tokens we start at x and go to x+20
161 // for TOTP we go +/-1min TODO = remember that +/- 1min should
162 // be changed based on stepping if we change the expiration time
165 // $this->dbConnector->query('CREATE TABLE "tokens" ("token_id" INTEGER PRIMARY KEY AUTOINCREMENT,"token_key" TEXT NOT NULL, "token_type" TEXT NOT NULL, "token_lastid" INTEGER NOT NULL)');
167 $func = $this->getDataFunction;
168 $tokendata = $func("gettoken", $username);
170 // TODO: check return value
171 $ttype = $tokendata["tokentype"];
172 $tlid = $tokendata["tokencounter"];
173 $tkey = $tokendata["tokenkey"];
179 for($i=$st; $i<$en; $i++) {
180 $stest = $this->oath_hotp($tkey, $i);
181 //echo "code: $code, $stest, $tkey\n";
182 if($code1 == $stest) {
183 $stest2 = $this->oath_hotp($tkey, $i+1);
184 if($code2 == $stest2) {
185 $tokenset["username"] = $username;
186 $tokenset["tokencounter"] = $i+1;
188 $func = $this->putDataFunction;
189 $func("settokencounter", $tokenset);
199 echo "how the frig did i end up here?";
206 // gets the error text associated with the last error
207 function getErrorText() {
208 return $this->errorText;
211 // create a url compatibile with google authenticator.
212 function createURL($user, $key,$toktype = "HOTP") {
213 // oddity in the google authenticator... hotp needs to be lowercase.
214 $toktype = strtolower($toktype);
215 if($toktype == "hotp") {
216 $url = "otpauth://$toktype/$user?secret=$key&counter=1";
218 $url = "otpauth://$toktype/$user?secret=$key";
220 //echo "url: $url\n";
224 // creeates a base 32 key (random)
225 function createBase32Key() {
226 $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
228 for($i=0; $i<16; $i++) {
229 $offset = rand(0,strlen($alphabet)-1);
230 //echo "$i off is $offset\n";
231 $key .= $alphabet[$offset];
238 function helperb322hex($b32) {
239 $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
244 for($i = 0; $i < strlen($b32); $i++) {
245 $in = strrpos($alphabet, $b32[$i]);
246 $b = str_pad(base_convert($in, 10, 2), 5, "0", STR_PAD_LEFT);
251 $ar = str_split($out,20);
253 //echo "$dous, $b\n";
257 foreach($ar as $val) {
258 $rv = str_pad(base_convert($val, 2, 16), 5, "0", STR_PAD_LEFT);
259 //echo "rv: $rv from $val\n";
268 function helperhex2b32($hex) {
269 $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
271 $ar = str_split($hex, 5);
274 foreach($ar as $var) {
275 $bc = base_convert($var, 16, 2);
276 $bin = str_pad($bc, 20, "0", STR_PAD_LEFT);
278 //echo "$bc was, $var is, $bin are\n";
282 $ar2 = str_split($out, 5);
283 foreach($ar2 as $var2) {
284 $bc = base_convert($var2, 2, 10);
285 $out2 .= $alphabet[$bc];
291 function oath_hotp($key, $counter)
293 $key = pack("H*", $key);
294 $cur_counter = array(0,0,0,0,0,0,0,0);
297 $cur_counter[$i] = pack ('C*', $counter);
298 $counter = $counter >> 8;
300 $bin_counter = implode($cur_counter);
302 if (strlen ($bin_counter) < 8)
304 $bin_counter = str_repeat (chr(0), 8 - strlen ($bin_counter)) . $bin_counter;
308 $hash = hash_hmac ('sha1', $bin_counter, $key);
309 return str_pad($this->oath_truncate($hash), 6, "0", STR_PAD_LEFT);
312 function oath_truncate($hash, $length = 6)
315 foreach(str_split($hash,2) as $hex)
317 $hmac_result[]=hexdec($hex);
321 $offset = $hmac_result[19] & 0xf;
323 // Algorithm from RFC
326 (($hmac_result[$offset+0] & 0x7f) << 24 ) |
327 (($hmac_result[$offset+1] & 0xff) << 16 ) |
328 (($hmac_result[$offset+2] & 0xff) << 8 ) |
329 ($hmac_result[$offset+3] & 0xff)
334 // some private data bits.
335 private $getDatafunction;
336 private $putDatafunction;