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.
68 lines
2.5 KiB
68 lines
2.5 KiB
using API.Model;
|
|
using Common.Utility;
|
|
using Google.Authenticator;
|
|
using System.Configuration;
|
|
using System.Web;
|
|
|
|
namespace API
|
|
{
|
|
public class GoogleAuthenticatorAPI
|
|
{
|
|
protected TwoFactorAuthenticator _tfa = new TwoFactorAuthenticator();
|
|
protected string _key = ReadWebConfig("2FAGoogle", "");
|
|
protected string _keyForEncDec = ReadWebConfig("keyForEncryptionDecryption", "");
|
|
|
|
public GoogleAuthenticatorModel GenerateCodeAndImageURL(string userName)
|
|
{
|
|
GoogleAuthenticatorModel _model = new GoogleAuthenticatorModel();
|
|
string userUniqueKeyEncrypted = EncryptDecryptUtility.Encrypt(userName + _key, _keyForEncDec);
|
|
WriteSession("UserUniqueKey", userUniqueKeyEncrypted);
|
|
|
|
var _googleSetupInfo = _tfa.GenerateSetupCode("JME REMIT", userName, userUniqueKeyEncrypted, 200, 200, true);
|
|
_model.SetupCode = _googleSetupInfo.ManualEntryKey;
|
|
_model.BarCodeImageUrl = _googleSetupInfo.QrCodeSetupImageUrl;
|
|
|
|
return _model;
|
|
}
|
|
|
|
public GoogleAuthenticatorModel GenerateCodeAndImageURL(string userName, string userUniqueKeyEncrypted)
|
|
{
|
|
GoogleAuthenticatorModel _model = new GoogleAuthenticatorModel();
|
|
|
|
var _googleSetupInfo = _tfa.GenerateSetupCode("JME REMIT", userName, userUniqueKeyEncrypted, 200, 200, true);
|
|
_model.SetupCode = _googleSetupInfo.ManualEntryKey;
|
|
_model.BarCodeImageUrl = _googleSetupInfo.QrCodeSetupImageUrl;
|
|
_model.ManualEntryKey = _googleSetupInfo.ManualEntryKey;
|
|
|
|
return _model;
|
|
}
|
|
|
|
public DbResult Verify2FA(string otp, string userUniqueKey)
|
|
{
|
|
DbResult _dbRes = new DbResult();
|
|
if (string.IsNullOrEmpty(otp))
|
|
{
|
|
_dbRes.SetError("1", "OTP Code can not be blank!", null);
|
|
return _dbRes;
|
|
}
|
|
|
|
bool isValid = _tfa.ValidateTwoFactorPIN(userUniqueKey, otp);
|
|
if (isValid)
|
|
_dbRes.SetError("0", "Two factor authentication verified successfully!", null);
|
|
else
|
|
_dbRes.SetError("1", "Please enter valid OTP!", null);
|
|
|
|
return _dbRes;
|
|
}
|
|
|
|
public static string ReadWebConfig(string key, string defValue)
|
|
{
|
|
return (ConfigurationSettings.AppSettings[key] ?? defValue).ToString();
|
|
}
|
|
|
|
public static void WriteSession(string key, string value)
|
|
{
|
|
HttpContext.Current.Session[key] = value;
|
|
}
|
|
}
|
|
}
|