init.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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) {
  17. $pwd_hash1 = encrypt_password($password);
  18. $pwd_hash2 = encrypt_password($password, $login);
  19. $otp = $_REQUEST["otp"];
  20. if (get_schema_version() > 96) {
  21. if (!defined('AUTH_DISABLE_OTP') || !AUTH_DISABLE_OTP) {
  22. $sth = $this->pdo->prepare("SELECT otp_enabled,salt FROM ttrss_users WHERE
  23. login = ?");
  24. $sth->execute([$login]);
  25. if ($row = $sth->fetch()) {
  26. $base32 = new \OTPHP\Base32();
  27. $otp_enabled = $row['otp_enabled'];
  28. $secret = $base32->encode(sha1($row['salt']));
  29. $topt = new \OTPHP\TOTP($secret);
  30. $otp_check = $topt->now();
  31. if ($otp_enabled) {
  32. if ($otp) {
  33. if ($otp != $otp_check) {
  34. return false;
  35. }
  36. } else {
  37. $return = urlencode($_REQUEST["return"]);
  38. ?>
  39. <!DOCTYPE html>
  40. <html>
  41. <head>
  42. <title>Tiny Tiny RSS</title>
  43. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  44. </head>
  45. <?php echo stylesheet_tag("css/default.css") ?>
  46. <body class="ttrss_utility otp">
  47. <h1><?php echo __("Authentication") ?></h1>
  48. <div class="content">
  49. <form action="public.php?return=<?php echo $return ?>"
  50. method="POST" class="otpform">
  51. <input type="hidden" name="op" value="login">
  52. <input type="hidden" name="login" value="<?php echo htmlspecialchars($login) ?>">
  53. <input type="hidden" name="password" value="<?php echo htmlspecialchars($password) ?>">
  54. <input type="hidden" name="bw_limit" value="<?php echo htmlspecialchars($_POST["bw_limit"]) ?>">
  55. <input type="hidden" name="remember_me" value="<?php echo htmlspecialchars($_POST["remember_me"]) ?>">
  56. <input type="hidden" name="profile" value="<?php echo htmlspecialchars($_POST["profile"]) ?>">
  57. <fieldset>
  58. <label><?php echo __("Please enter your one time password:") ?></label>
  59. <input autocomplete="off" size="6" name="otp" value=""/>
  60. <input type="submit" value="Continue"/>
  61. </fieldset>
  62. </form></div>
  63. <script type="text/javascript">
  64. document.forms[0].otp.focus();
  65. </script>
  66. <?php
  67. exit;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. if (get_schema_version() > 87) {
  74. $sth = $this->pdo->prepare("SELECT salt FROM ttrss_users WHERE login = ?");
  75. $sth->execute([$login]);
  76. if ($row = $sth->fetch()) {
  77. $salt = $row['salt'];
  78. if ($salt == "") {
  79. $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
  80. login = ? AND (pwd_hash = ? OR pwd_hash = ?)");
  81. $sth->execute([$login, $pwd_hash1, $pwd_hash2]);
  82. // verify and upgrade password to new salt base
  83. if ($row = $sth->fetch()) {
  84. // upgrade password to MODE2
  85. $user_id = $row['id'];
  86. $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
  87. $pwd_hash = encrypt_password($password, $salt, true);
  88. $sth = $this->pdo->prepare("UPDATE ttrss_users SET
  89. pwd_hash = ?, salt = ? WHERE login = ?");
  90. $sth->execute([$pwd_hash, $salt, $login]);
  91. return $user_id;
  92. } else {
  93. return false;
  94. }
  95. } else {
  96. $pwd_hash = encrypt_password($password, $salt, true);
  97. $sth = $this->pdo->prepare("SELECT id
  98. FROM ttrss_users WHERE
  99. login = ? AND pwd_hash = ?");
  100. $sth->execute([$login, $pwd_hash]);
  101. if ($row = $sth->fetch()) {
  102. return $row['id'];
  103. }
  104. }
  105. } else {
  106. $sth = $this->pdo->prepare("SELECT id
  107. FROM ttrss_users WHERE
  108. login = ? AND (pwd_hash = ? OR pwd_hash = ?)");
  109. $sth->execute([$login, $pwd_hash1, $pwd_hash2]);
  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. return false;
  124. }
  125. function check_password($owner_uid, $password) {
  126. $sth = $this->pdo->prepare("SELECT salt,login FROM ttrss_users WHERE
  127. id = ?");
  128. $sth->execute([$owner_uid]);
  129. if ($row = $sth->fetch()) {
  130. $salt = $row['salt'];
  131. $login = $row['login'];
  132. if (!$salt) {
  133. $password_hash1 = encrypt_password($password);
  134. $password_hash2 = encrypt_password($password, $login);
  135. $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
  136. id = ? AND (pwd_hash = ? OR pwd_hash = ?)");
  137. $sth->execute([$owner_uid, $password_hash1, $password_hash2]);
  138. return $sth->fetch();
  139. } else {
  140. $password_hash = encrypt_password($password, $salt, true);
  141. $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
  142. id = ? AND pwd_hash = ?");
  143. $sth->execute([$owner_uid, $password_hash]);
  144. return $sth->fetch();
  145. }
  146. }
  147. return false;
  148. }
  149. function change_password($owner_uid, $old_password, $new_password) {
  150. if ($this->check_password($owner_uid, $old_password)) {
  151. $new_salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
  152. $new_password_hash = encrypt_password($new_password, $new_salt, true);
  153. $sth = $this->pdo->prepare("UPDATE ttrss_users SET
  154. pwd_hash = ?, salt = ?, otp_enabled = false
  155. WHERE id = ?");
  156. $sth->execute([$new_password_hash, $new_salt, $owner_uid]);
  157. $_SESSION["pwd_hash"] = $new_password_hash;
  158. $sth = $this->pdo->prepare("SELECT email, login FROM ttrss_users WHERE id = ?");
  159. $sth->execute([$owner_uid]);
  160. if ($row = $sth->fetch()) {
  161. $mailer = new Mailer();
  162. require_once "lib/MiniTemplator.class.php";
  163. $tpl = new MiniTemplator;
  164. $tpl->readTemplateFromFile("templates/password_change_template.txt");
  165. $tpl->setVariable('LOGIN', $row["login"]);
  166. $tpl->setVariable('TTRSS_HOST', SELF_URL_PATH);
  167. $tpl->addBlock('message');
  168. $tpl->generateOutputToString($message);
  169. $mailer->mail(["to_name" => $row["login"],
  170. "to_address" => $row["email"],
  171. "subject" => "[tt-rss] Password change notification",
  172. "message" => $message]);
  173. }
  174. return __("Password has been changed.");
  175. } else {
  176. return "ERROR: ".__('Old password is incorrect.');
  177. }
  178. }
  179. function api_version() {
  180. return 2;
  181. }
  182. }
  183. ?>