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