summaryrefslogtreecommitdiffstats
path: root/web/Users.php
blob: f9347596b5871d0d75edf722e5ebd9690300d940 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php

class Users {
    
    
    function __construct($file = "../users.dat") {
        $this->userFile = $file;
        
        $this->users = json_decode(file_get_contents($file),true);   
    }
    function hasSession() {
        session_start();
        if (isset($_SESSION['username'])) {
            return $_SESSION['username'];
        }
        return false;
    }
    
    
    function storeData(User $user) {
        $this->users[$user->getUsername()] = $user->getData();
        file_put_contents($this->userFile,json_encode($this->users));
    }
    
    function loadUser($name) {
        if (isset($this->users[$name])) {
            
            return new User($name,$this->users[$name]);
        } else {
            return false;
        }
    }
    
    
    
}

class User {
    
    function __construct($user,$data) {
        $this->data = $data;
        $this->user = $user;
    }
    
    function auth($pass) {
        if ($this->data['password'] === $pass) {
            return true;
        }
        
        return false;
        
    }
    
    function startSession() {
        
        $_SESSION['username'] = $this->user;
    }
    
    function doLogin() {
        session_regenerate_id();
        $_SESSION['loggedin'] = true;
        $_SESSION['ua'] = $_SERVER['HTTP_USER_AGENT'];
    }
    
    function doOTP() {
        $_SESSION['OTP'] = true;
    }
    
    function isOTP() {
        if (isset($_SESSION['OTP']) && $_SESSION['OTP'] == true) {
            
            return true;
        }
        return false;
        
    }
    function isLoggedIn() {
        if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true &&
            isset($_SESSION['ua']) && $_SESSION['ua'] == $_SERVER['HTTP_USER_AGENT']
        ) {
            
            return $_SESSION['username'];
        }
        return false;
        
    }
    
    
    function getUsername() {
        return $this->user;   
    }
    
    function getSecret() {
        if (isset($this->data['secret'])) {
            return $this->data['secret'];
        }
        return false;
    }
    
    function generateSecret() {
        $g = new GoogleAuthenticator();
        $secret = $g->generateSecret();
        $this->data['secret'] = $secret;
        return $secret;
        
    }
    
    function getData() {
        return $this->data;
    }
    
    function setOTPCookie() {
        $time = floor(time() / (3600 * 24) ); // get day number
        //about using the user agent: It's easy to fake it, but it increases the barrier for stealing and reusing cookies nevertheless
        // and it doesn't do any harm (except that it's invalid after a browser upgrade, but that may be even intented)
        $cookie =  $time.":".hash_hmac("sha1",$this->getUsername().":".$time.":". $_SERVER['HTTP_USER_AGENT'],$this->getSecret());
        setcookie ( "otp", $cookie, time() + (30 * 24 * 3600), null,null,null,true );
    }
    
    function hasValidOTPCookie() {
        // 0 = tomorrow it is invalid
        $daysUntilInvalid = 0;
        $time = (string) floor((time() / (3600 * 24))) ; // get day number
        if (isset($_COOKIE['otp'])) {
            list( $otpday,$hash) = explode(":",$_COOKIE['otp']);
               
            if ( $otpday >= $time - $daysUntilInvalid && $hash == hash_hmac('sha1',$this->getUsername().":".$otpday .":". $_SERVER['HTTP_USER_AGENT'] , $this->getSecret())
                ) {
                  return true;
            }
                
             
        }
        return false;
    
    }
    
}
?>