Google2FA.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Module\User\Validator;
  3. use App\Module\Sys\Config;
  4. use App\Module\User\Services\UserService;
  5. use UCore\Validator;
  6. class Google2FA extends Validator
  7. {
  8. public function validate(mixed $value, array $data): bool
  9. {
  10. $userField = $this->args[0];
  11. $scene = $this->validation->getName();
  12. $user_id = $data[$userField];
  13. return self::check($user_id, $value, $scene);
  14. }
  15. /**
  16. * 检查
  17. *
  18. * @param $user_id
  19. * @param $value
  20. * @return bool|int
  21. * @throws \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException
  22. * @throws \PragmaRX\Google2FA\Exceptions\InvalidCharactersException
  23. * @throws \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException
  24. */
  25. static public function check($user_id, $value, $scene = '')
  26. {
  27. $info = UserService::infoinfo($user_id);
  28. $google2fa = new \PragmaRX\Google2FA\Google2FA();
  29. if (APP_DEBUG) {
  30. if ($info->google2fa_secret === '8888888888') {
  31. if ($value == '123456') {
  32. return true;
  33. }else{
  34. return false;
  35. }
  36. }
  37. }
  38. $check = \App\Module\User\Services\Google2Fa::must_check($info, $scene);
  39. if($check){
  40. $f = true;
  41. }
  42. if (empty($info->google2fa_secret)) {
  43. if ($f) {
  44. return false;
  45. }
  46. return true;
  47. }
  48. $valid = $google2fa->verifyKey($info->google2fa_secret, $value);
  49. return $valid;
  50. }
  51. }