2fa-functions.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. function create2FA($type)
  3. {
  4. $result['type'] = $type;
  5. switch ($type) {
  6. case 'google':
  7. try {
  8. $google2fa = new PragmaRX\Google2FA\Google2FA();
  9. $google2fa->setAllowInsecureCallToGoogleApis(true);
  10. $result['secret'] = $google2fa->generateSecretKey();
  11. $result['url'] = $google2fa->getQRCodeGoogleUrl(
  12. $GLOBALS['title'],
  13. $GLOBALS['organizrUser']['username'],
  14. $result['secret']
  15. );
  16. } catch (PragmaRX\Google2FA\Exceptions\InsecureCallException $e) {
  17. return false;
  18. }
  19. break;
  20. default:
  21. return false;
  22. }
  23. return $result;
  24. }
  25. function save2FA($secret, $type)
  26. {
  27. try {
  28. $connect = new Dibi\Connection([
  29. 'driver' => 'sqlite3',
  30. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  31. ]);
  32. $connect->query('
  33. UPDATE users SET', [
  34. 'auth_service' => $type . '::' . $secret
  35. ], '
  36. WHERE id=?', $GLOBALS['organizrUser']['userID']);
  37. writeLog('success', 'User Management Function - User added 2FA', $GLOBALS['organizrUser']['username']);
  38. return true;
  39. } catch (Dibi\Exception $e) {
  40. writeLog('error', 'User Management Function - Error Adding User 2FA', $GLOBALS['organizrUser']['username']);
  41. return false;
  42. }
  43. }
  44. function verify2FA($secret, $code, $type)
  45. {
  46. switch ($type) {
  47. case 'google':
  48. $google2fa = new PragmaRX\Google2FA\Google2FA();
  49. $google2fa->setWindow(5);
  50. $valid = $google2fa->verifyKey($secret, $code);
  51. break;
  52. default:
  53. return false;
  54. }
  55. return ($valid) ? true : false;
  56. }
  57. function remove2FA()
  58. {
  59. try {
  60. $connect = new Dibi\Connection([
  61. 'driver' => 'sqlite3',
  62. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  63. ]);
  64. $connect->query('
  65. UPDATE users SET', [
  66. 'auth_service' => 'internal'
  67. ], '
  68. WHERE id=?', $GLOBALS['organizrUser']['userID']);
  69. writeLog('success', 'User Management Function - User removed 2FA', $GLOBALS['organizrUser']['username']);
  70. return true;
  71. } catch (Dibi\Exception $e) {
  72. writeLog('error', 'User Management Function - Error Removing User 2FA', $GLOBALS['organizrUser']['username']);
  73. return false;
  74. }
  75. }