Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.
 
 
 
 
 
 

85 lines
2.8 KiB

  1. <?php
  2. namespace Friendica\Module\Settings\TwoFactor;
  3. use Friendica\Core\L10n;
  4. use Friendica\Core\Renderer;
  5. use Friendica\DI;
  6. use Friendica\Model\TwoFactor\RecoveryCode;
  7. use Friendica\Module\BaseSettingsModule;
  8. use Friendica\Module\Security\Login;
  9. /**
  10. * // Page 3: 2FA enabled but not verified, show recovery codes
  11. *
  12. * @package Friendica\Module\TwoFactor
  13. */
  14. class Recovery extends BaseSettingsModule
  15. {
  16. public static function init(array $parameters = [])
  17. {
  18. if (!local_user()) {
  19. return;
  20. }
  21. $secret = DI::pConfig()->get(local_user(), '2fa', 'secret');
  22. if (!$secret) {
  23. DI::baseUrl()->redirect('settings/2fa');
  24. }
  25. if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
  26. notice(L10n::t('Please enter your password to access this page.'));
  27. DI::baseUrl()->redirect('settings/2fa');
  28. }
  29. }
  30. public static function post(array $parameters = [])
  31. {
  32. if (!local_user()) {
  33. return;
  34. }
  35. if (!empty($_POST['action'])) {
  36. self::checkFormSecurityTokenRedirectOnError('settings/2fa/recovery', 'settings_2fa_recovery');
  37. if ($_POST['action'] == 'regenerate') {
  38. RecoveryCode::regenerateForUser(local_user());
  39. notice(L10n::t('New recovery codes successfully generated.'));
  40. DI::baseUrl()->redirect('settings/2fa/recovery?t=' . self::getFormSecurityToken('settings_2fa_password'));
  41. }
  42. }
  43. }
  44. public static function content(array $parameters = [])
  45. {
  46. if (!local_user()) {
  47. return Login::form('settings/2fa/recovery');
  48. }
  49. parent::content($parameters);
  50. if (!RecoveryCode::countValidForUser(local_user())) {
  51. RecoveryCode::generateForUser(local_user());
  52. }
  53. $recoveryCodes = RecoveryCode::getListForUser(local_user());
  54. $verified = DI::pConfig()->get(local_user(), '2fa', 'verified');
  55. return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/recovery.tpl'), [
  56. '$form_security_token' => self::getFormSecurityToken('settings_2fa_recovery'),
  57. '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
  58. '$title' => L10n::t('Two-factor recovery codes'),
  59. '$help_label' => L10n::t('Help'),
  60. '$message' => L10n::t('<p>Recovery codes can be used to access your account in the event you lose access to your device and cannot receive two-factor authentication codes.</p><p><strong>Put these in a safe spot!</strong> If you lose your device and don’t have the recovery codes you will lose access to your account.</p>'),
  61. '$recovery_codes' => $recoveryCodes,
  62. '$regenerate_message' => L10n::t('When you generate new recovery codes, you must copy the new codes. Your old codes won’t work anymore.'),
  63. '$regenerate_label' => L10n::t('Generate new recovery codes'),
  64. '$verified' => $verified,
  65. '$verify_label' => L10n::t('Next: Verification'),
  66. ]);
  67. }
  68. }