]> git.wh0rd.org - tt-rss.git/blob - plugins/auth_internal/init.php
auth_internal: load Base32 using proper namespace
[tt-rss.git] / plugins / auth_internal / init.php
1 <?php
2 class Auth_Internal extends Plugin implements IAuthModule {
3
4 private $host;
5
6 function about() {
7 return array(1.0,
8 "Authenticates against internal tt-rss database",
9 "fox",
10 true);
11 }
12
13 /* @var PluginHost $host */
14 function init($host) {
15 $this->host = $host;
16 $this->pdo = Db::pdo();
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 $otp = $_REQUEST["otp"];
26
27 if (get_schema_version() > 96) {
28 if (!defined('AUTH_DISABLE_OTP') || !AUTH_DISABLE_OTP) {
29
30 $sth = $this->pdo->prepare("SELECT otp_enabled,salt FROM ttrss_users WHERE
31 login = ?");
32 $sth->execute([$login]);
33
34 if ($row = $sth->fetch()) {
35
36 $base32 = new \OTPHP\Base32();
37
38 $otp_enabled = $row['otp_enabled'];
39 $secret = $base32->encode(sha1($row['salt']));
40
41 $topt = new \OTPHP\TOTP($secret);
42 $otp_check = $topt->now();
43
44 if ($otp_enabled) {
45 if ($otp) {
46 if ($otp != $otp_check) {
47 return false;
48 }
49 } else {
50 $return = urlencode($_REQUEST["return"]);
51 ?><html>
52 <head>
53 <title>Tiny Tiny RSS</title>
54 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
55 </head>
56 <?php echo stylesheet_tag("css/default.css") ?>
57 <body class="ttrss_utility otp"><div class="content">
58 <form action="public.php?return=<?php echo $return ?>"
59 method="POST" class="otpform">
60 <input type="hidden" name="op" value="login">
61 <input type="hidden" name="login" value="<?php echo htmlspecialchars($login) ?>">
62 <input type="hidden" name="password" value="<?php echo htmlspecialchars($password) ?>">
63 <input type="hidden" name="bw_limit" value="<?php echo htmlspecialchars($_POST["bw_limit"]) ?>">
64 <input type="hidden" name="remember_me" value="<?php echo htmlspecialchars($_POST["remember_me"]) ?>">
65 <input type="hidden" name="profile" value="<?php echo htmlspecialchars($_POST["profile"]) ?>">
66
67 <label><?php echo __("Please enter your one time password:") ?></label>
68 <input autocomplete="off" size="6" name="otp" value=""/>
69 <input type="submit" value="Continue"/>
70 </form></div>
71 <script type="text/javascript">
72 document.forms[0].otp.focus();
73 </script>
74 <?php
75 exit;
76 }
77 }
78 }
79 }
80 }
81
82 if (get_schema_version() > 87) {
83
84 $sth = $this->pdo->prepare("SELECT salt FROM ttrss_users WHERE login = ?");
85 $sth->execute([$login]);
86
87 if ($row = $sth->fetch()) {
88 $salt = $row['salt'];
89
90 if ($salt == "") {
91
92 $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
93 login = ? AND (pwd_hash = ? OR pwd_hash = ?)");
94
95 $sth->execute([$login, $pwd_hash1, $pwd_hash2]);
96
97 // verify and upgrade password to new salt base
98
99 if ($row = $sth->fetch()) {
100 // upgrade password to MODE2
101
102 $user_id = $row['id'];
103
104 $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
105 $pwd_hash = encrypt_password($password, $salt, true);
106
107 $sth = $this->pdo->prepare("UPDATE ttrss_users SET
108 pwd_hash = ?, salt = ? WHERE login = ?");
109
110 $sth->execute([$pwd_hash, $salt, $login]);
111
112 return $user_id;
113
114 } else {
115 return false;
116 }
117
118 } else {
119 $pwd_hash = encrypt_password($password, $salt, true);
120
121 $sth = $this->pdo->prepare("SELECT id
122 FROM ttrss_users WHERE
123 login = ? AND pwd_hash = ?");
124 $sth->execute([$login, $pwd_hash]);
125
126 if ($row = $sth->fetch()) {
127 return $row['id'];
128 }
129 }
130
131 } else {
132 $sth = $this->pdo->prepare("SELECT id
133 FROM ttrss_users WHERE
134 login = ? AND (pwd_hash = ? OR pwd_hash = ?)");
135
136 $sth->execute([$login, $pwd_hash1, $pwd_hash2]);
137
138 if ($row = $sth->fetch()) {
139 return $row['id'];
140 }
141 }
142 } else {
143 $sth = $this->pdo->prepare("SELECT id
144 FROM ttrss_users WHERE
145 login = ? AND (pwd_hash = ? OR pwd_hash = ?)");
146
147 $sth->execute([$login, $pwd_hash1, $pwd_hash2]);
148
149 if ($row = $sth->fetch()) {
150 return $row['id'];
151 }
152 }
153
154 return false;
155 }
156
157 function check_password($owner_uid, $password) {
158
159 $sth = $this->pdo->prepare("SELECT salt,login FROM ttrss_users WHERE
160 id = ?");
161 $sth->execute([$owner_uid]);
162
163 if ($row = $sth->fetch()) {
164
165 $salt = $row['salt'];
166 $login = $row['login'];
167
168 if (!$salt) {
169 $password_hash1 = encrypt_password($password);
170 $password_hash2 = encrypt_password($password, $login);
171
172 $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
173 id = ? AND (pwd_hash = ? OR pwd_hash = ?)");
174
175 $sth->execute([$owner_uid, $password_hash1, $password_hash2]);
176
177 return $sth->fetch();
178
179 } else {
180 $password_hash = encrypt_password($password, $salt, true);
181
182 $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
183 id = ? AND pwd_hash = ?");
184
185 $sth->execute([$owner_uid, $password_hash]);
186
187 return $sth->fetch();
188 }
189 }
190
191 return false;
192 }
193
194 function change_password($owner_uid, $old_password, $new_password) {
195
196 if ($this->check_password($owner_uid, $old_password)) {
197
198 $new_salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
199 $new_password_hash = encrypt_password($new_password, $new_salt, true);
200
201 $sth = $this->pdo->prepare("UPDATE ttrss_users SET
202 pwd_hash = ?, salt = ?, otp_enabled = false
203 WHERE id = ?");
204 $sth->execute([$new_password_hash, $new_salt, $owner_uid]);
205
206 $_SESSION["pwd_hash"] = $new_password_hash;
207
208 return __("Password has been changed.");
209 } else {
210 return "ERROR: ".__('Old password is incorrect.');
211 }
212 }
213
214 function api_version() {
215 return 2;
216 }
217
218 }
219 ?>