Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

72 wiersze
2.1 KiB

  1. <?php
  2. namespace Friendica\Module\Security\TwoFactor;
  3. use Friendica\BaseModule;
  4. use Friendica\Core\L10n;
  5. use Friendica\Core\Renderer;
  6. use Friendica\Core\Session;
  7. use Friendica\DI;
  8. use PragmaRX\Google2FA\Google2FA;
  9. /**
  10. * Page 1: Authenticator code verification
  11. *
  12. * @package Friendica\Module\TwoFactor
  13. */
  14. class Verify extends BaseModule
  15. {
  16. private static $errors = [];
  17. public static function post(array $parameters = [])
  18. {
  19. if (!local_user()) {
  20. return;
  21. }
  22. if (($_POST['action'] ?? '') == 'verify') {
  23. self::checkFormSecurityTokenRedirectOnError('2fa', 'twofactor_verify');
  24. $a = DI::app();
  25. $code = $_POST['verify_code'] ?? '';
  26. $valid = (new Google2FA())->verifyKey(DI::pConfig()->get(local_user(), '2fa', 'secret'), $code);
  27. // The same code can't be used twice even if it's valid
  28. if ($valid && Session::get('2fa') !== $code) {
  29. Session::set('2fa', $code);
  30. // Resume normal login workflow
  31. DI::auth()->setForUser($a, $a->user, true, true);
  32. } else {
  33. self::$errors[] = L10n::t('Invalid code, please retry.');
  34. }
  35. }
  36. }
  37. public static function content(array $parameters = [])
  38. {
  39. if (!local_user()) {
  40. DI::baseUrl()->redirect();
  41. }
  42. // Already authenticated with 2FA token
  43. if (Session::get('2fa')) {
  44. DI::baseUrl()->redirect();
  45. }
  46. return Renderer::replaceMacros(Renderer::getMarkupTemplate('twofactor/verify.tpl'), [
  47. '$form_security_token' => self::getFormSecurityToken('twofactor_verify'),
  48. '$title' => L10n::t('Two-factor authentication'),
  49. '$message' => L10n::t('<p>Open the two-factor authentication app on your device to get an authentication code and verify your identity.</p>'),
  50. '$errors_label' => L10n::tt('Error', 'Errors', count(self::$errors)),
  51. '$errors' => self::$errors,
  52. '$recovery_message' => L10n::t('Don’t have your phone? <a href="%s">Enter a two-factor recovery code</a>', '2fa/recovery'),
  53. '$verify_code' => ['verify_code', L10n::t('Please enter a code from your authentication app'), '', '', 'required', 'autofocus placeholder="000000"', 'tel'],
  54. '$verify_label' => L10n::t('Verify code and complete login'),
  55. ]);
  56. }
  57. }