init.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. require_once "lib/otphp/vendor/base32.php";
  27. require_once "lib/otphp/lib/otp.php";
  28. require_once "lib/otphp/lib/totp.php";
  29. $base32 = new Base32();
  30. $otp_enabled = $row['otp_enabled'];
  31. $secret = $base32->encode(sha1($row['salt']));
  32. $topt = new \OTPHP\TOTP($secret);
  33. $otp_check = $topt->now();
  34. if ($otp_enabled) {
  35. if ($otp) {
  36. if ($otp != $otp_check) {
  37. return false;
  38. }
  39. } else {
  40. $return = urlencode($_REQUEST["return"]);
  41. ?><html>
  42. <head>
  43. <title>Tiny Tiny RSS</title>
  44. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  45. </head>
  46. <?php echo stylesheet_tag("css/default.css") ?>
  47. <body class="ttrss_utility otp"><div class="content">
  48. <form action="public.php?return=<?php echo $return ?>"
  49. method="POST" class="otpform">
  50. <input type="hidden" name="op" value="login">
  51. <input type="hidden" name="login" value="<?php echo htmlspecialchars($login) ?>">
  52. <input type="hidden" name="password" value="<?php echo htmlspecialchars($password) ?>">
  53. <input type="hidden" name="bw_limit" value="<?php echo htmlspecialchars($_POST["bw_limit"]) ?>">
  54. <input type="hidden" name="remember_me" value="<?php echo htmlspecialchars($_POST["remember_me"]) ?>">
  55. <input type="hidden" name="profile" value="<?php echo htmlspecialchars($_POST["profile"]) ?>">
  56. <label><?php echo __("Please enter your one time password:") ?></label>
  57. <input autocomplete="off" size="6" name="otp" value=""/>
  58. <input type="submit" value="Continue"/>
  59. </form></div>
  60. <script type="text/javascript">
  61. document.forms[0].otp.focus();
  62. </script>
  63. <?php
  64. exit;
  65. }
  66. }
  67. }
  68. }
  69. }
  70. if (get_schema_version() > 87) {
  71. $sth = $this->pdo->prepare("SELECT salt FROM ttrss_users WHERE login = ?");
  72. $sth->execute([$login]);
  73. if ($row = $sth->fetch()) {
  74. $salt = $row['salt'];
  75. if ($salt == "") {
  76. $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
  77. login = ? AND (pwd_hash = ? OR pwd_hash = ?)");
  78. $sth->execute([$login, $pwd_hash1, $pwd_hash2]);
  79. // verify and upgrade password to new salt base
  80. if ($row = $sth->fetch()) {
  81. // upgrade password to MODE2
  82. $user_id = $row['id'];
  83. $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
  84. $pwd_hash = encrypt_password($password, $salt, true);
  85. $sth = $this->pdo->prepare("UPDATE ttrss_users SET
  86. pwd_hash = ?, salt = ? WHERE login = ?");
  87. $sth->execute([$pwd_hash, $salt, $login]);
  88. return $user_id;
  89. } else {
  90. return false;
  91. }
  92. } else {
  93. $pwd_hash = encrypt_password($password, $salt, true);
  94. $sth = $this->pdo->prepare("SELECT id
  95. FROM ttrss_users WHERE
  96. login = ? AND pwd_hash = ?");
  97. $sth->execute([$login, $pwd_hash]);
  98. if ($row = $sth->fetch()) {
  99. return $row['id'];
  100. }
  101. }
  102. } else {
  103. $sth = $this->pdo->prepare("SELECT id
  104. FROM ttrss_users WHERE
  105. login = ? AND (pwd_hash = ? OR pwd_hash = ?)");
  106. $sth->execute([$login, $pwd_hash1, $pwd_hash2]);
  107. if ($row = $sth->fetch()) {
  108. return $row['id'];
  109. }
  110. }
  111. } else {
  112. $sth = $this->pdo->prepare("SELECT id
  113. FROM ttrss_users WHERE
  114. login = ? AND (pwd_hash = ? OR pwd_hash = ?)");
  115. $sth->execute([$login, $pwd_hash1, $pwd_hash2]);
  116. if ($row = $sth->fetch()) {
  117. return $row['id'];
  118. }
  119. }
  120. return false;
  121. }
  122. function check_password($owner_uid, $password) {
  123. $sth = $this->pdo->prepare("SELECT salt,login FROM ttrss_users WHERE
  124. id = ?");
  125. $sth->execute([$owner_uid]);
  126. if ($row = $sth->fetch()) {
  127. $salt = $row['salt'];
  128. $login = $row['login'];
  129. if (!$salt) {
  130. $password_hash1 = encrypt_password($password);
  131. $password_hash2 = encrypt_password($password, $login);
  132. $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
  133. id = ? AND (pwd_hash = ? OR pwd_hash = ?)");
  134. $sth->execute([$owner_uid, $password_hash1, $password_hash2]);
  135. return $sth->fetch();
  136. } else {
  137. $password_hash = encrypt_password($password, $salt, true);
  138. $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE
  139. id = ? AND pwd_hash = ?");
  140. $sth->execute([$owner_uid, $password_hash]);
  141. return $sth->fetch();
  142. }
  143. }
  144. return false;
  145. }
  146. function change_password($owner_uid, $old_password, $new_password) {
  147. if ($this->check_password($owner_uid, $old_password)) {
  148. $new_salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
  149. $new_password_hash = encrypt_password($new_password, $new_salt, true);
  150. $sth = $this->pdo->prepare("UPDATE ttrss_users SET
  151. pwd_hash = ?, salt = ?, otp_enabled = false
  152. WHERE id = ?");
  153. $sth->execute([$new_password_hash, $new_salt, $owner_uid]);
  154. $_SESSION["pwd_hash"] = $new_password_hash;
  155. return __("Password has been changed.");
  156. } else {
  157. return "ERROR: ".__('Old password is incorrect.');
  158. }
  159. }
  160. function api_version() {
  161. return 2;
  162. }
  163. }
  164. ?>