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