Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2fa.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?
  2. require_once(SERVER_ROOT.'/classes/twofa.class.php');
  3. require_once(SERVER_ROOT.'/classes/u2f.class.php');
  4. $TwoFA = new TwoFactorAuth(SITE_NAME);
  5. $U2F = new u2f\U2F('https://'.SITE_DOMAIN);
  6. if ($Type = $_POST['type'] ?? false) {
  7. if ($Type == 'PGP') {
  8. if (!empty($_POST['publickey']) && (strpos($_POST['publickey'], 'BEGIN PGP PUBLIC KEY BLOCK') === false || strpos($_POST['publickey'], 'END PGP PUBLIC KEY BLOCK') === false)) {
  9. $Error = "Invalid PGP public key";
  10. } else {
  11. $DB->query("
  12. UPDATE users_main
  13. SET PublicKey = '".db_string($_POST['publickey'])."'
  14. WHERE ID = $UserID");
  15. $Message = 'Public key '.(empty($_POST['publickey']) ? 'removed' : 'updated') ;
  16. }
  17. }
  18. if ($Type == '2FA-E') {
  19. if ($TwoFA->verifyCode($_POST['twofasecret'], $_POST['twofa'])) {
  20. $DB->query("
  21. UPDATE users_main
  22. SET TwoFactor='".db_string($_POST['twofasecret'])."'
  23. WHERE ID = $UserID");
  24. $Message = "Two Factor Authentication enabled";
  25. } else {
  26. $Error = "Invalid 2FA verification code";
  27. }
  28. }
  29. if ($Type == '2FA-D') {
  30. $DB->query("
  31. UPDATE users_main
  32. SET TwoFactor = NULL
  33. WHERE ID = $UserID");
  34. $Message = "Two Factor Authentication disabled";
  35. }
  36. if ($Type == 'U2F-E') {
  37. try {
  38. $U2FReg = $U2F->doRegister(json_decode($_POST['u2f-request']), json_decode($_POST['u2f-response']));
  39. $DB->query("
  40. INSERT INTO u2f
  41. (UserID, KeyHandle, PublicKey, Certificate, Counter, Valid)
  42. Values ($UserID, '".db_string($U2FReg->keyHandle)."', '".db_string($U2FReg->publicKey)."', '".db_string($U2FReg->certificate)."', '".db_string($U2FReg->counter)."', '1')");
  43. $Message = "U2F token registered";
  44. } catch(Exception $e) {
  45. $Error = "Failed to register U2F token";
  46. }
  47. }
  48. if ($Type == 'U2F-D') {
  49. $DB->query("
  50. DELETE FROM u2f
  51. WHERE UserID = $UserID");
  52. $Message = 'U2F tokens deregistered';
  53. }
  54. }
  55. $U2FRegs = [];
  56. $DB->query("
  57. SELECT KeyHandle, PublicKey, Certificate, Counter
  58. FROM u2f
  59. WHERE UserID = $UserID");
  60. // Needs to be an array of objects, so we can't use to_array()
  61. while (list($KeyHandle, $PublicKey, $Certificate, $Counter) = $DB->next_record()) {
  62. $U2FRegs[] = (object)['keyHandle'=>$KeyHandle, 'publicKey'=>$PublicKey, 'certificate'=>$Certificate, 'counter'=>$Counter];
  63. }
  64. $DB->query("
  65. SELECT PublicKey, TwoFactor
  66. FROM users_main
  67. WHERE ID = $UserID");
  68. list($PublicKey, $TwoFactor) = $DB->next_record();
  69. list($U2FRequest, $U2FSigs) = $U2F->getRegisterData($U2FRegs);
  70. View::show_header("Two-factor Authentication Settings", 'u2f');
  71. ?>
  72. <h2>Additional Account Security Options</h2>
  73. <div class="thin">
  74. <? if (isset($Message)) { ?>
  75. <div class="alertbar"><?=$Message?></div>
  76. <? }
  77. if (isset($Error)) { ?>
  78. <div class="alertbar error"><?=$Error?></div>
  79. <? } ?>
  80. <div class="box">
  81. <div class="head">
  82. <strong>PGP Public Key</strong>
  83. </div>
  84. <div class="pad">
  85. <? if (empty($PublicKey)) {
  86. if (!empty($TwoFactor) || sizeof($U2FRegs) > 0) { ?>
  87. <strong class="important_text">You have a form of 2FA enabled but no PGP key associated with your account. If you lose access to your 2FA device, you will permanently lose access to your account.</strong>
  88. <? } ?>
  89. <p>When setting up any form of second factor authentication, it is strongly recommended that you add your PGP public key as a form of secure recovery in the event that you lose access to your second factor device.</p>
  90. <p>After adding a PGP public key to your account, you will be able to disable your account's second factor protection by solving a challenge that only someone with your private key could solve.</p>
  91. <p>Additionally, being able to solve such a challenge when given manually by staff will suffice to provide proof of ownership of your account, provided no revocation certificate has been published for your key.</p>
  92. <p>Before adding your PGP public key, please make sure that you have taken the necessary precautions to protect it from loss (backup) or theft (revocation certificate).</p>
  93. <? } else { ?>
  94. <p>The PGP public key associated with your account is shown below.</p>
  95. <p>This key can be used to create challenges that are only solvable by the holder of the related private key. Successfully solving these challenges is necessary for disabling any form of second factor authentication or proving ownership of this account to staff when you are unable to login.</p>
  96. <? } ?>
  97. <form method="post">
  98. <input type="hidden" name="type" value="PGP">
  99. Public Key:
  100. <br>
  101. <textarea name="publickey" id="publickey" spellcheck="false" cols="64" rows="8"><?=display_str($PublicKey)?></textarea>
  102. <br>
  103. <button type="submit" name="type" value="PGP">Update Public Key</button>
  104. </form>
  105. </div>
  106. </div>
  107. <div class="box">
  108. <div class="head">
  109. <strong>Two-Factor Authentication (2FA-TOTP)</strong>
  110. </div>
  111. <div class="pad">
  112. <? $TwoFASecret = empty($TwoFactor) ? $TwoFA->createSecret() : $TwoFactor;
  113. if (empty($TwoFactor)) {
  114. if (sizeof($U2FRegs) == 0) { ?>
  115. <p>Two Factor Authentication is not currently enabled for this account.</p>
  116. <p>To enable it, add the secret key below to your 2FA client either manually or by scanning the QR code, then enter a verification code generated by your 2FA client and click the "Enable 2FA" button.</p>
  117. <form method="post">
  118. <input type="text" size="60" name="twofasecret" id="twofasecret" value="<?=$TwoFASecret?>" readonly><br>
  119. <img src="<?=$TwoFA->getQRCodeImageAsDataUri(SITE_NAME.':'.$LoggedUser['Username'], $TwoFASecret)?>"><br>
  120. <input type="text" size="20" maxlength="6" pattern="[0-9]{0,6}" name="twofa" id="twofa" placeholder="Verification Code" autocomplete="off"><br><br>
  121. <button type="submit" name="type" value="2FA-E">Enable 2FA</button>
  122. </form>
  123. <? } else { ?>
  124. <p>Two Factor Authentication is not currently enabled for this account.</p>
  125. <p>To enable 2FA, you must first disable U2F below.</p>
  126. <? }
  127. } else {?>
  128. <form method="post">
  129. <input type="hidden" name="type" value="2FA-D">
  130. <p>2FA is enabled for this account with the following secret:</p>
  131. <input type="text" size="20" name="twofasecret" id="twofasecret" value="<?=$TwoFASecret?>" readonly><br>
  132. <img src="<?=$TwoFA->getQRCodeImageAsDataUri(SITE_NAME.':'.$LoggedUser['Username'], $TwoFASecret)?>"><br><br>
  133. <p>To disable 2FA, click the button below.</p>
  134. <button type="submit" name="type" value="2FA-D">Disable 2FA</button>
  135. </form>
  136. <? } ?>
  137. </div>
  138. </div>
  139. <div class="box">
  140. <div class="head">
  141. <strong>Universal Two Factor (FIDO U2F)</strong>
  142. </div>
  143. <div class="pad">
  144. <? if (sizeof($U2FRegs) == 0) { ?>
  145. <? if (empty($TwoFactor)) { ?>
  146. <form method="post" id="u2f_register_form">
  147. <input type="hidden" name="u2f-request" value='<?=json_encode($U2FRequest)?>'>
  148. <input type="hidden" name="u2f-sigs" value='<?=json_encode($U2FSigs)?>'>
  149. <input type="hidden" name="u2f-response">
  150. <input type="hidden" value="U2F-E">
  151. </form>
  152. <p>Universal Two Factor is not currently enabled for this account.</p>
  153. <p>To enable Universal Two Factor, plug in your U2F token and press the button on it.</p>
  154. <? } else { ?>
  155. <p>Universal Two Factor is not currently enabled for this account.</p>
  156. <p>To enable Universal Two Factor, you must first disable normal 2FA above.</p>
  157. <? } ?>
  158. <? } else { ?>
  159. <form method="post" id="u2f_register_form">
  160. <input type="hidden" name="u2f-request" value='<?=json_encode($U2FRequest)?>'>
  161. <input type="hidden" name="u2f-sigs" value='<?=json_encode($U2FSigs)?>'>
  162. <input type="hidden" name="u2f-response">
  163. <input type="hidden" value="U2F-E">
  164. <p>Universal Two Factor is enabled.</p>
  165. <p>To add an additional U2F token, plug it in and press the button on it</p>
  166. <p>To disable U2F completely and deregister all tokens, press the button below</p>
  167. <button type="submit" name="type" value="U2F-D">Disable U2F</button>
  168. </form>
  169. <? } ?>
  170. </div>
  171. </div>
  172. </div>
  173. <?
  174. View::show_footer();
  175. ?>