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.

54 lines
2.2 KiB

  1. using Google.Authenticator;
  2. using Swift.API.Common;
  3. namespace Swift.API.GoogleAuthenticator
  4. {
  5. public class GoogleAuthenticatorAPI
  6. {
  7. protected TwoFactorAuthenticator _tfa = new TwoFactorAuthenticator();
  8. protected string _key = Utility.ReadWebConfig("2FAGoogle", "");
  9. protected string _keyForEncDec = Utility.ReadWebConfig("keyForEncryptionDecryption", "");
  10. public GoogleAuthenticatorModel GenerateCodeAndImageURL(string userName)
  11. {
  12. GoogleAuthenticatorModel _model = new GoogleAuthenticatorModel();
  13. string userUniqueKeyEncrypted = EncryptDecryptUtility.Encrypt(userName + _key, _keyForEncDec);
  14. Utility.WriteSession("UserUniqueKey", userUniqueKeyEncrypted);
  15. var _googleSetupInfo = _tfa.GenerateSetupCode("IME LONDON", userName, userUniqueKeyEncrypted, 200, 200, true);
  16. _model.SetupCode = _googleSetupInfo.ManualEntryKey;
  17. _model.BarCodeImageUrl = _googleSetupInfo.QrCodeSetupImageUrl;
  18. return _model;
  19. }
  20. public GoogleAuthenticatorModel GenerateCodeAndImageURL(string userName, string userUniqueKeyEncrypted)
  21. {
  22. GoogleAuthenticatorModel _model = new GoogleAuthenticatorModel();
  23. var _googleSetupInfo = _tfa.GenerateSetupCode("IME LONDON", userName, userUniqueKeyEncrypted, 200, 200, true);
  24. _model.SetupCode = _googleSetupInfo.ManualEntryKey;
  25. _model.BarCodeImageUrl = _googleSetupInfo.QrCodeSetupImageUrl;
  26. _model.ManualEntryKey = _googleSetupInfo.ManualEntryKey;
  27. return _model;
  28. }
  29. public DbResult Verify2FA(string otp, string userUniqueKey)
  30. {
  31. DbResult _dbRes = new DbResult();
  32. if (string.IsNullOrEmpty(otp))
  33. {
  34. _dbRes.SetError("1", "OTP Code can not be blank!", null);
  35. return _dbRes;
  36. }
  37. bool isValid = _tfa.ValidateTwoFactorPIN(userUniqueKey, otp);
  38. if (isValid)
  39. _dbRes.SetError("0", "Two factor authentication verified successfully!", null);
  40. else
  41. _dbRes.SetError("1", "Please enter valid OTP!", null);
  42. return _dbRes;
  43. }
  44. }
  45. }