using Common; using Common.Helper; using Common.Model; using log4net; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Repository.Authentication; using System; namespace Business.Authentication { public class AuthenticationBusiness : IAuthenticationBusiness { private readonly IAuthenticationRepo _requestAuth; private static readonly ILog Log = LogManager.GetLogger(typeof(AuthenticationBusiness)); public AuthenticationBusiness(IAuthenticationRepo requestAuth) { this._requestAuth = requestAuth; } public JsonRxResponse IsMapAPIWithOtherApp(ClientRegistrationKeys credentials) { Log.DebugFormat("BusinessLogic.AuthenticationBL.IsMapAPIWithOtherApp | Requested parameters : {0}", JsonConvert.SerializeObject(credentials)); var encryptSecret = Utilities.EncryptString(credentials.secret); var jsonRxResp = new JsonRxResponse(); credentials.secret = encryptSecret; try { var dbResult = _requestAuth.IsMapAPIWithOtherApp(credentials); if (dbResult.ResponseCode.Equals("0")) { jsonRxResp.SetResponse("0", dbResult.Msg, dbResult.Id, dbResult.Extra); } else { jsonRxResp.SetResponse("1", dbResult.Msg, null); } } catch (Exception ex) { Log.Error("IsMapAPIWithOtherApp", ex); jsonRxResp.SetResponse("1", "AuthenticationBL Failed", null); } return jsonRxResp; } /// /// DbResult must return the id=@accessCode /// /// /// public LoginResponse LoginSystem(LoginCredential credentials) { try { var ls = _requestAuth.LoginSystem(credentials); Log.Debug("LoginSystem | DB RESPONSE : " + JsonConvert.SerializeObject(ls)); return ls; } catch (Exception ex) { Log.Error("Login Fail due to execption", ex); return null; } } /// /// check if the provided access code is valid or not /// /// /// /// returns the jsonResponse /// the out parameter must be set before exiting from the method public string IsAccessCodeValid(string accessCode, string uuid) { string scope = null; try { scope = _requestAuth.IsAccessCodeValid(accessCode, uuid); return scope; } catch (Exception ex) { return null; } } public JsonRxResponse ReSendVerificationCode(VerificationKeys verification) { JsonRxResponse response = new JsonRxResponse { ErrorCode = "1", Msg = "Error", Data = "" }; try { var plainOTP = Utilities.GenerateRandomPin(); verification.verificationCode = Utilities.EncryptString(plainOTP); DbResult dbResult = _requestAuth.ReSendVerificationCode(verification); if (dbResult.ResponseCode.Equals("0")) { string msg = ""; switch (verification.codeType) { case "prc": msg = "Password Recovery Code"; break; case "dvc": msg = "Device Verification Code"; break; } if (verification.userId.IsValidEmail()) { if (!string.IsNullOrEmpty(msg)) { SendEmailParameters _emailParams = new SendEmailParameters() { ToEmails = verification.userId, MsgSubject = msg, MsgBody = "Verification Code : " + plainOTP }; //Task.Run(() => _requestComponent.SendEmail(_emailParams)); } LogicalThreadContext.Properties["email"] = verification.userId; } else if (verification.userId.IsValidMsisdn()) { //SmsParameters _smsParams = new SmsParameters() //{ // userId = "", // scheduleType = "0", // subject = msg, // message = "Verification Code : " + plainOTP, // callBackUrl = "", // todayDate = "", // sendDate = "", // mobileNumber = "", // receiverId = verification.userId + "^" + verification.userId //}; //ApiCall.SendSmsAsync(_smsParams); LogicalThreadContext.Properties["MSISDN"] = verification.userId; } Log.DebugFormat("BusinessLogic.RegisterBL.ResendVerificationCode | Requested parameters : {0}", JsonConvert.SerializeObject(verification)); JObject res = new JObject(); if (verification.codeType == "dvc") { res["verificationCode"] = plainOTP; } else if (verification.codeType == "prc") { res["forgetCode"] = plainOTP; } response = new JsonRxResponse { ErrorCode = "0", Msg = "Successfully sent verification code", Data = res }; } } catch (Exception ex) { } return response; } public JsonRxResponse GetUser(string userId) { try { JsonRxResponse res = _requestAuth.GetUser(userId); return res; } catch (Exception ex) { Log.Error("Error occured while retriving user information.", ex); return null; } } public bool ValidateAuthenticity(string user, string guid) { try { bool res = _requestAuth.ValidateAuthenticity(user, guid); return res; } catch (Exception ex) { Log.Error("Error occured while retriving user information.", ex); return false; } } public string GetDevice(string customerId) { try { string res = _requestAuth.GetDevice(customerId); return res; } catch (Exception ex) { Log.Error("Error occured while retriving user information.", ex); return ""; } } } }