init.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. class Auth_Internal extends Plugin implements IAuthModule {
  3. private $host;
  4. function about() {
  5. return array(1.0,
  6. "Authenticates against internal tt-rss database",
  7. "fox",
  8. true);
  9. }
  10. /* @var PluginHost $host */
  11. function init($host) {
  12. $this->host = $host;
  13. $this->pdo = Db::pdo();
  14. $host->add_hook($host::HOOK_AUTH_USER, $this);
  15. }
  16. function authenticate($login, $password, $service = '') {
  17. $pwd_hash1 = encrypt_password($password);
  18. $pwd_hash2 = encrypt_password($password, $login);
  19. $otp = $_REQUEST["otp"];
  20. if (get_schema_version() > 96) {
  21. $sth = $this->pdo->prepare("SELECT otp_enabled,salt FROM ttrss_users WHERE
  22. login = ?");
  23. $sth->execute([$login]);
  24. if ($row = $sth->fetch()) {
  25. $base32 = new \OTPHP\Base32();
  26. $otp_enabled = $row['otp_enabled'];
  27. $secret = $base32->encode(mb_substr(sha1($row["salt"]), 0, 12), false);
  28. $topt = new \OTPHP\TOTP($secret);
  29. $otp_check = $topt->now();
  30. if ($otp_enabled) {
  31. // only allow app password checking if OTP is enabled
  32. if ($service && get_schema_version() > 138) {
  33. return $this->check_app_password($login, $password, $service);
  34. }
  35. if ($otp) {
  36. if ($otp != $otp_check) {
  37. return false;
  38. }
  39. } else {
  40. $return = urlencode($_REQUEST["return"]);
  41. ?>
  42. <!DOCTYPE html>
  43. <html>
  44. <head>
  45. <title>Tiny Tiny RSS</title>
  46. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  47. </head>
  48. <?php echo stylesheet_tag("css/default.css") ?>
  49. <body class="ttrss_utility otp">
  50. <h1><?php echo __("Authentication") ?></h1>
  51. <div class="content">
  52. <form action="public.php?return=<?php echo $return ?>"
  53. method="POST" class="otpform">
  54. <input type="hidden" name="op" value="login">
  55. <input type="hidden" name="login" value="<?php echo htmlspecialchars($login) ?>">
  56. <input type="hidden" name="password" value="<?php echo htmlspecialchars($password) ?>">
  57. <input type="hidden" name="bw_limit" value="<?php echo htmlspecialchars($_POST["bw_limit"]) ?>">
  58. <input type="hidden" name="remember_me" value="<?php echo htmlspecialchars($_POST["remember_me"]) ?>">
  59. <input type="hidden" name="profile" value="<?php echo htmlspecialchars($_POST["profile"]) ?>">
  60. <fieldset>
  61. <label><?php echo __("Please enter your one time password:") ?></label>
  62. <input autocomplete="off" size="6" name="otp" value=""/>
  63. <input type="submit" value="Continue"/>
  64. </fieldset>
  65. </form></div>
  66. <script type="text/javascript">
  67. document.forms[0].otp.focus();
  68. </script>
  69. <?php
  70. exit;
  71. }
  72. }
  73. }
  74. }
  75. // check app passwords first but allow regular password as a fallback for the time being
  76. // if OTP is not enabled
  77. if ($service && get_schema_version() > 138) {
  78. $user_id = $this->check_app_password($login, $password, $service);
  79. if ($user_id)
  80. return $user_id;
  81. }
  82. if (get_schema_version() > 87) {
  83. $sth = $this->pdo->prepare("SELECT salt FROM ttrss_users WHERE login = ?");
  84. $sth->execute([$login]);
  85. if ($row = $sth->fetch()) {
  86. $salt = $row['salt'];
  87. if ($salt == "") {
  88. $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
  89. login = ? AND (pwd_hash = ? OR pwd_hash = ?)");
  90. $sth->execute([$login, $pwd_hash1, $pwd_hash2]);
  91. // verify and upgrade password to new salt base
  92. if ($row = $sth->fetch()) {
  93. // upgrade password to MODE2
  94. $user_id = $row['id'];
  95. $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
  96. $pwd_hash = encrypt_password($password, $salt, true);
  97. $sth = $this->pdo->prepare("UPDATE ttrss_users SET
  98. pwd_hash = ?, salt = ? WHERE login = ?");
  99. $sth->execute([$pwd_hash, $salt, $login]);
  100. return $user_id;
  101. } else {
  102. return false;
  103. }
  104. } else {
  105. $pwd_hash = encrypt_password($password, $salt, true);
  106. $sth = $this->pdo->prepare("SELECT id
  107. FROM ttrss_users WHERE
  108. login = ? AND pwd_hash = ?");
  109. $sth->execute([$login, $pwd_hash]);
  110. if ($row = $sth->fetch()) {
  111. return $row['id'];
  112. }
  113. }
  114. } else {
  115. $sth = $this->pdo->prepare("SELECT id
  116. FROM ttrss_users WHERE
  117. login = ? AND (pwd_hash = ? OR pwd_hash = ?)");
  118. $sth->execute([$login, $pwd_hash1, $pwd_hash2]);
  119. if ($row = $sth->fetch()) {
  120. return $row['id'];
  121. }
  122. }
  123. } else {
  124. $sth = $this->pdo->prepare("SELECT id
  125. FROM ttrss_users WHERE
  126. login = ? AND (pwd_hash = ? OR pwd_hash = ?)");
  127. $sth->execute([$login, $pwd_hash1, $pwd_hash2]);
  128. if ($row = $sth->fetch()) {
  129. return $row['id'];
  130. }
  131. }
  132. return false;
  133. }
  134. function check_password($owner_uid, $password) {
  135. $sth = $this->pdo->prepare("SELECT salt,login,otp_enabled FROM ttrss_users WHERE
  136. id = ?");
  137. $sth->execute([$owner_uid]);
  138. if ($row = $sth->fetch()) {
  139. $salt = $row['salt'];
  140. $login = $row['login'];
  141. if (!$salt) {
  142. $password_hash1 = encrypt_password($password);
  143. $password_hash2 = encrypt_password($password, $login);
  144. $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
  145. id = ? AND (pwd_hash = ? OR pwd_hash = ?)");
  146. $sth->execute([$owner_uid, $password_hash1, $password_hash2]);
  147. return $sth->fetch();
  148. } else {
  149. $password_hash = encrypt_password($password, $salt, true);
  150. $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
  151. id = ? AND pwd_hash = ?");
  152. $sth->execute([$owner_uid, $password_hash]);
  153. return $sth->fetch();
  154. }
  155. }
  156. return false;
  157. }
  158. function change_password($owner_uid, $old_password, $new_password) {
  159. if ($this->check_password($owner_uid, $old_password)) {
  160. $new_salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
  161. $new_password_hash = encrypt_password($new_password, $new_salt, true);
  162. $sth = $this->pdo->prepare("UPDATE ttrss_users SET
  163. pwd_hash = ?, salt = ?, otp_enabled = false
  164. WHERE id = ?");
  165. $sth->execute([$new_password_hash, $new_salt, $owner_uid]);
  166. $_SESSION["pwd_hash"] = $new_password_hash;
  167. $sth = $this->pdo->prepare("SELECT email, login FROM ttrss_users WHERE id = ?");
  168. $sth->execute([$owner_uid]);
  169. if ($row = $sth->fetch()) {
  170. $mailer = new Mailer();
  171. require_once "lib/MiniTemplator.class.php";
  172. $tpl = new MiniTemplator;
  173. $tpl->readTemplateFromFile("templates/password_change_template.txt");
  174. $tpl->setVariable('LOGIN', $row["login"]);
  175. $tpl->setVariable('TTRSS_HOST', SELF_URL_PATH);
  176. $tpl->addBlock('message');
  177. $tpl->generateOutputToString($message);
  178. $mailer->mail(["to_name" => $row["login"],
  179. "to_address" => $row["email"],
  180. "subject" => "[tt-rss] Password change notification",
  181. "message" => $message]);
  182. }
  183. return __("Password has been changed.");
  184. } else {
  185. return "ERROR: ".__('Old password is incorrect.');
  186. }
  187. }
  188. private function check_app_password($login, $password, $service) {
  189. return false;
  190. }
  191. function api_version() {
  192. return 2;
  193. }
  194. }