3 class GoogleAuthenticator {
5 // first we init google authenticator by passing it a filename
6 // for its sqlite database.
7 function __construct($file) {
8 if(file_exists($file)) {
10 $this->dbConnector = new PDO("sqlite:$file");
11 } catch(PDOException $exep) {
12 $this->errorText = $exep->getMessage();
13 $this->dbConnector = false;
19 $this->dbFile = $file;
22 // creates the database (tables);
25 $this->$dbConnector = new PDO("sqlite:$file");
26 } catch(PDOException $exep) {
27 $this->errorText = $exep->getMessage();
28 $this->dbConnector = false;
31 // here we create some tables and stuff
34 // creates "user" in the database and returns a url for
35 // the phone. If user already exists, this returns false
36 // if any error occurs, this returns false
37 function setupUser($username) {
38 $key = _createBase32Key();
41 // Replcate "user" in the database... All this really
42 // does is to replace the key for the user. Returns false
43 // if the user doesnt exist of the key is poop
44 function replaceUser($username) {
48 // sets the key for a user - this is assuming you dont want
49 // to use one created by the application. returns false
50 // if the key is invalid or the user doesn't exist.
51 function setUserKey($username, $key) {
56 function deleteUser($username) {
60 // user has input their user name and some code, authenticate
62 function authenticateUser($username, $code) {
66 // this function allows a user to resync their key. If too
67 // many codes are called, we only check up to 20 codes in the future
68 // so if the user is at 21, they'll always fail.
69 function resyncCode($username, $code1, $code2) {
73 // gets the error text associated with the last error
74 function getErrorText() {
75 return $this->errorText;
78 // create a url compatibile with google authenticator.
79 function createURL($user, $key) {
80 $url = "otpauth://hotp/$user?secret=$key";
84 // creeates a base 32 key (random)
85 function createBase32Key() {
86 $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
88 for($i=0; $i<16; $i++) {
89 $offset = rand(0,strlen($alphabet)-1);
90 //echo "$i off is $offset\n";
91 $key .= $alphabet[$offset];
98 function helperb322hex($b32) {
99 $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
104 for($i = 0; $i < strlen($b32); $i++) {
105 $in = strrpos($alphabet, $b32[$i]);
106 $b = str_pad(base_convert($in, 10, 2), 5, "0", STR_PAD_LEFT);
111 $ar = str_split($out,20);
113 //echo "$dous, $b\n";
117 foreach($ar as $val) {
118 $rv = str_pad(base_convert($val, 2, 16), 5, "0", STR_PAD_LEFT);
119 //echo "rv: $rv from $val\n";
128 function helperhex2b32($hex) {
129 $alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
131 $ar = str_split($hex, 5);
134 foreach($ar as $var) {
135 $bc = base_convert($var, 16, 2);
136 $bin = str_pad($bc, 20, "0", STR_PAD_LEFT);
138 //echo "$bc was, $var is, $bin are\n";
142 $ar2 = str_split($out, 5);
143 foreach($ar2 as $var2) {
144 $bc = base_convert($var2, 2, 10);
145 $out2 .= $alphabet[$bc];
152 // some private data bits.
155 private $dbConnector;