]> git.wh0rd.org - tt-rss.git/blob - plugins/auth_internal/init.php
add some styling to otp form
[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($this->link, $login);
26 $otp = db_escape_string($this->link, $_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 <?php echo stylesheet_tag("utility.css") ?>
56 <body class="otp"><div class="content">
57 <form action="public.php?return=<?php echo $return ?>"
58 method="POST" class="otpform">
59 <input type="hidden" name="op" value="login">
60 <input type="hidden" name="login" value="<?php echo htmlspecialchars($login) ?>">
61 <input type="hidden" name="password" value="<?php echo htmlspecialchars($password) ?>">
62
63 <label><?php echo __("Please enter your one time password:") ?></label>
64 <input autocomplete="off" size="6" name="otp" value=""/>
65 <input type="submit" value="Continue"/>
66 </form></div>
67 <script type="text/javascript">
68 document.forms[0].otp.focus();
69 </script>
70 <?php
71 exit;
72 }
73 }
74 }
75 }
76 }
77
78 if (get_schema_version($this->link) > 87) {
79
80 $result = db_query($this->link, "SELECT salt FROM ttrss_users WHERE
81 login = '$login'");
82
83 if (db_num_rows($result) != 1) {
84 return false;
85 }
86
87 $salt = db_fetch_result($result, 0, "salt");
88
89 if ($salt == "") {
90
91 $query = "SELECT id
92 FROM ttrss_users WHERE
93 login = '$login' AND (pwd_hash = '$pwd_hash1' OR
94 pwd_hash = '$pwd_hash2')";
95
96 // verify and upgrade password to new salt base
97
98 $result = db_query($this->link, $query);
99
100 if (db_num_rows($result) == 1) {
101 // upgrade password to MODE2
102
103 $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
104 $pwd_hash = encrypt_password($password, $salt, true);
105
106 db_query($this->link, "UPDATE ttrss_users SET
107 pwd_hash = '$pwd_hash', salt = '$salt' WHERE login = '$login'");
108
109 $query = "SELECT id
110 FROM ttrss_users WHERE
111 login = '$login' AND pwd_hash = '$pwd_hash'";
112
113 } else {
114 return false;
115 }
116
117 } else {
118
119 $pwd_hash = encrypt_password($password, $salt, true);
120
121 $query = "SELECT id
122 FROM ttrss_users WHERE
123 login = '$login' AND pwd_hash = '$pwd_hash'";
124
125 }
126
127 } else {
128 $query = "SELECT id
129 FROM ttrss_users WHERE
130 login = '$login' AND (pwd_hash = '$pwd_hash1' OR
131 pwd_hash = '$pwd_hash2')";
132 }
133
134 $result = db_query($this->link, $query);
135
136 if (db_num_rows($result) == 1) {
137 return db_fetch_result($result, 0, "id");
138 }
139
140 return false;
141 }
142
143 function check_password($owner_uid, $password) {
144 $owner_uid = db_escape_string($this->link, $owner_uid);
145
146 $result = db_query($this->link, "SELECT salt,login FROM ttrss_users WHERE
147 id = '$owner_uid'");
148
149 $salt = db_fetch_result($result, 0, "salt");
150 $login = db_fetch_result($result, 0, "login");
151
152 if (!$salt) {
153 $password_hash1 = encrypt_password($password);
154 $password_hash2 = encrypt_password($password, $login);
155
156 $query = "SELECT id FROM ttrss_users WHERE
157 id = '$owner_uid' AND (pwd_hash = '$password_hash1' OR
158 pwd_hash = '$password_hash2')";
159
160 } else {
161 $password_hash = encrypt_password($password, $salt, true);
162
163 $query = "SELECT id FROM ttrss_users WHERE
164 id = '$owner_uid' AND pwd_hash = '$password_hash'";
165 }
166
167 $result = db_query($this->link, $query);
168
169 return db_num_rows($result) != 0;
170 }
171
172 function change_password($owner_uid, $old_password, $new_password) {
173 $owner_uid = db_escape_string($this->link, $owner_uid);
174
175 if ($this->check_password($owner_uid, $old_password)) {
176
177 $new_salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
178 $new_password_hash = encrypt_password($new_password, $new_salt, true);
179
180 db_query($this->link, "UPDATE ttrss_users SET
181 pwd_hash = '$new_password_hash', salt = '$new_salt', otp_enabled = false
182 WHERE id = '$owner_uid'");
183
184 $_SESSION["pwd_hash"] = $new_password_hash;
185
186 return __("Password has been changed.");
187 } else {
188 return "ERROR: ".__('Old password is incorrect.');
189 }
190 }
191 }
192 ?>