]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | class Auth_Internal extends Auth_Base { | |
3 | ||
4 | function authenticate($login, $password) { | |
5 | ||
6 | $pwd_hash1 = encrypt_password($password); | |
7 | $pwd_hash2 = encrypt_password($password, $login); | |
8 | $login = db_escape_string($login); | |
9 | $otp = db_escape_string($_REQUEST["otp"]); | |
10 | ||
11 | if (get_schema_version($this->link) > 96) { | |
12 | if (!defined('AUTH_DISABLE_OTP') || !AUTH_DISABLE_OTP) { | |
13 | $result = db_query($this->link, "SELECT otp_enabled,salt FROM ttrss_users WHERE | |
14 | login = '$login'"); | |
15 | ||
16 | if (db_num_rows($result) > 0) { | |
17 | require_once "lib/otphp/vendor/base32.php"; | |
18 | require_once "lib/otphp/lib/otp.php"; | |
19 | require_once "lib/otphp/lib/totp.php"; | |
20 | ||
21 | $base32 = new Base32(); | |
22 | ||
23 | $otp_enabled = sql_bool_to_bool(db_fetch_result($result, 0, "otp_enabled")); | |
24 | $secret = $base32->encode(sha1(db_fetch_result($result, 0, "salt"))); | |
25 | ||
26 | $topt = new \OTPHP\TOTP($secret); | |
27 | $otp_check = $topt->now(); | |
28 | ||
29 | if ($otp_enabled) { | |
30 | if ($otp) { | |
31 | if ($otp != $otp_check) { | |
32 | return false; | |
33 | } | |
34 | } else { | |
35 | $return = urlencode($_REQUEST["return"]); | |
36 | ?><html> | |
37 | <head><title>Tiny Tiny RSS</title></head> | |
38 | <body> | |
39 | <form action="public.php?return=<?php echo $return ?>" | |
40 | method="POST"> | |
41 | <input type="hidden" name="op" value="login"> | |
42 | <input type="hidden" name="login" value="<?php echo htmlspecialchars($login) ?>"> | |
43 | <input type="hidden" name="password" value="<?php echo htmlspecialchars($password) ?>"> | |
44 | ||
45 | <label><?php echo __("Please enter your one time password:") ?></label> | |
46 | <input type="password" size="6" name="otp"/> | |
47 | <input type="submit" value="Continue"/> | |
48 | </form> | |
49 | <script type="text/javascript"> | |
50 | document.forms[0].otp.focus(); | |
51 | </script> | |
52 | <?php | |
53 | exit; | |
54 | } | |
55 | } | |
56 | } | |
57 | } | |
58 | } | |
59 | ||
60 | if (get_schema_version($this->link) > 87) { | |
61 | ||
62 | $result = db_query($this->link, "SELECT salt FROM ttrss_users WHERE | |
63 | login = '$login'"); | |
64 | ||
65 | if (db_num_rows($result) != 1) { | |
66 | return false; | |
67 | } | |
68 | ||
69 | $salt = db_fetch_result($result, 0, "salt"); | |
70 | ||
71 | if ($salt == "") { | |
72 | ||
73 | $query = "SELECT id | |
74 | FROM ttrss_users WHERE | |
75 | login = '$login' AND (pwd_hash = '$pwd_hash1' OR | |
76 | pwd_hash = '$pwd_hash2')"; | |
77 | ||
78 | // verify and upgrade password to new salt base | |
79 | ||
80 | $result = db_query($this->link, $query); | |
81 | ||
82 | if (db_num_rows($result) == 1) { | |
83 | // upgrade password to MODE2 | |
84 | ||
85 | $salt = substr(bin2hex(get_random_bytes(125)), 0, 250); | |
86 | $pwd_hash = encrypt_password($password, $salt, true); | |
87 | ||
88 | db_query($this->link, "UPDATE ttrss_users SET | |
89 | pwd_hash = '$pwd_hash', salt = '$salt' WHERE login = '$login'"); | |
90 | ||
91 | $query = "SELECT id | |
92 | FROM ttrss_users WHERE | |
93 | login = '$login' AND pwd_hash = '$pwd_hash'"; | |
94 | ||
95 | } else { | |
96 | return false; | |
97 | } | |
98 | ||
99 | } else { | |
100 | ||
101 | $pwd_hash = encrypt_password($password, $salt, true); | |
102 | ||
103 | $query = "SELECT id | |
104 | FROM ttrss_users WHERE | |
105 | login = '$login' AND pwd_hash = '$pwd_hash'"; | |
106 | ||
107 | } | |
108 | ||
109 | } else { | |
110 | $query = "SELECT id | |
111 | FROM ttrss_users WHERE | |
112 | login = '$login' AND (pwd_hash = '$pwd_hash1' OR | |
113 | pwd_hash = '$pwd_hash2')"; | |
114 | } | |
115 | ||
116 | $result = db_query($this->link, $query); | |
117 | ||
118 | if (db_num_rows($result) == 1) { | |
119 | return db_fetch_result($result, 0, "id"); | |
120 | } | |
121 | ||
122 | return false; | |
123 | } | |
124 | ||
125 | function check_password($owner_uid, $password) { | |
126 | $owner_uid = db_escape_string($owner_uid); | |
127 | ||
128 | $result = db_query($this->link, "SELECT salt,login FROM ttrss_users WHERE | |
129 | id = '$owner_uid'"); | |
130 | ||
131 | $salt = db_fetch_result($result, 0, "salt"); | |
132 | $login = db_fetch_result($result, 0, "login"); | |
133 | ||
134 | if (!$salt) { | |
135 | $password_hash1 = encrypt_password($password); | |
136 | $password_hash2 = encrypt_password($password, $login); | |
137 | ||
138 | $query = "SELECT id FROM ttrss_users WHERE | |
139 | id = '$owner_uid' AND (pwd_hash = '$password_hash1' OR | |
140 | pwd_hash = '$password_hash2')"; | |
141 | ||
142 | } else { | |
143 | $password_hash = encrypt_password($password, $salt, true); | |
144 | ||
145 | $query = "SELECT id FROM ttrss_users WHERE | |
146 | id = '$owner_uid' AND pwd_hash = '$password_hash'"; | |
147 | } | |
148 | ||
149 | $result = db_query($this->link, $query); | |
150 | ||
151 | return db_num_rows($result) != 0; | |
152 | } | |
153 | ||
154 | function change_password($owner_uid, $old_password, $new_password) { | |
155 | $owner_uid = db_escape_string($owner_uid); | |
156 | ||
157 | if ($this->check_password($owner_uid, $old_password)) { | |
158 | ||
159 | $new_salt = substr(bin2hex(get_random_bytes(125)), 0, 250); | |
160 | $new_password_hash = encrypt_password($new_password, $new_salt, true); | |
161 | ||
162 | db_query($this->link, "UPDATE ttrss_users SET | |
163 | pwd_hash = '$new_password_hash', salt = '$new_salt', otp_enabled = false | |
164 | WHERE id = '$owner_uid'"); | |
165 | ||
166 | $_SESSION["pwd_hash"] = $new_password_hash; | |
167 | ||
168 | return __("Password has been changed."); | |
169 | } else { | |
170 | return "ERROR: ".__('Old password is incorrect.'); | |
171 | } | |
172 | } | |
173 | } | |
174 | ?> |