init.php 5.8 KB

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