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.

206 lines
7.4 KiB

1 year ago
  1. using Common;
  2. using Common.Helper;
  3. using Common.Model;
  4. using log4net;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using Repository.Authentication;
  8. using System;
  9. namespace Business.Authentication
  10. {
  11. public class AuthenticationBusiness : IAuthenticationBusiness
  12. {
  13. private readonly IAuthenticationRepo _requestAuth;
  14. private static readonly ILog Log = LogManager.GetLogger(typeof(AuthenticationBusiness));
  15. public AuthenticationBusiness(IAuthenticationRepo requestAuth)
  16. {
  17. this._requestAuth = requestAuth;
  18. }
  19. public JsonRxResponse IsMapAPIWithOtherApp(ClientRegistrationKeys credentials)
  20. {
  21. Log.DebugFormat("BusinessLogic.AuthenticationBL.IsMapAPIWithOtherApp | Requested parameters : {0}", JsonConvert.SerializeObject(credentials));
  22. var encryptSecret = Utilities.EncryptString(credentials.secret);
  23. var jsonRxResp = new JsonRxResponse();
  24. credentials.secret = encryptSecret;
  25. try
  26. {
  27. var dbResult = _requestAuth.IsMapAPIWithOtherApp(credentials);
  28. if (dbResult.ResponseCode.Equals("0"))
  29. {
  30. jsonRxResp.SetResponse("0", dbResult.Msg, dbResult.Id, dbResult.Extra);
  31. }
  32. else
  33. {
  34. jsonRxResp.SetResponse("1", dbResult.Msg, null);
  35. }
  36. }
  37. catch (Exception ex)
  38. {
  39. Log.Error("IsMapAPIWithOtherApp", ex);
  40. jsonRxResp.SetResponse("1", "AuthenticationBL Failed", null);
  41. }
  42. return jsonRxResp;
  43. }
  44. /// <summary>
  45. /// DbResult must return the id=@accessCode
  46. /// </summary>
  47. /// <param name="credentials"></param>
  48. /// <returns></returns>
  49. public LoginResponse LoginSystem(LoginCredential credentials)
  50. {
  51. try
  52. {
  53. var ls = _requestAuth.LoginSystem(credentials);
  54. Log.Debug("LoginSystem | DB RESPONSE : " + JsonConvert.SerializeObject(ls));
  55. return ls;
  56. }
  57. catch (Exception ex)
  58. {
  59. Log.Error("Login Fail due to execption", ex);
  60. return null;
  61. }
  62. }
  63. /// <summary>
  64. /// check if the provided access code is valid or not
  65. /// </summary>
  66. /// <param name="credentials"></param>
  67. /// <param name="user"></param>
  68. /// <returns>returns the jsonResponse</returns>
  69. /// <remarks>the out parameter must be set before exiting from the method</remarks>
  70. public string IsAccessCodeValid(string accessCode, string uuid)
  71. {
  72. string scope = null;
  73. try
  74. {
  75. scope = _requestAuth.IsAccessCodeValid(accessCode, uuid);
  76. return scope;
  77. }
  78. catch (Exception ex)
  79. {
  80. return null;
  81. }
  82. }
  83. public JsonRxResponse ReSendVerificationCode(VerificationKeys verification)
  84. {
  85. JsonRxResponse response = new JsonRxResponse { ErrorCode = "1", Msg = "Error", Data = "" };
  86. try
  87. {
  88. var plainOTP = Utilities.GenerateRandomPin();
  89. verification.verificationCode = Utilities.EncryptString(plainOTP);
  90. DbResult dbResult = _requestAuth.ReSendVerificationCode(verification);
  91. if (dbResult.ResponseCode.Equals("0"))
  92. {
  93. string msg = "";
  94. switch (verification.codeType)
  95. {
  96. case "prc":
  97. msg = "Password Recovery Code";
  98. break;
  99. case "dvc":
  100. msg = "Device Verification Code";
  101. break;
  102. }
  103. if (verification.userId.IsValidEmail())
  104. {
  105. if (!string.IsNullOrEmpty(msg))
  106. {
  107. SendEmailParameters _emailParams = new SendEmailParameters()
  108. {
  109. ToEmails = verification.userId,
  110. MsgSubject = msg,
  111. MsgBody = "Verification Code : " + plainOTP
  112. };
  113. //Task.Run(() => _requestComponent.SendEmail(_emailParams));
  114. }
  115. LogicalThreadContext.Properties["email"] = verification.userId;
  116. }
  117. else if (verification.userId.IsValidMsisdn())
  118. {
  119. //SmsParameters _smsParams = new SmsParameters()
  120. //{
  121. // userId = "",
  122. // scheduleType = "0",
  123. // subject = msg,
  124. // message = "Verification Code : " + plainOTP,
  125. // callBackUrl = "",
  126. // todayDate = "",
  127. // sendDate = "",
  128. // mobileNumber = "",
  129. // receiverId = verification.userId + "^" + verification.userId
  130. //};
  131. //ApiCall.SendSmsAsync(_smsParams);
  132. LogicalThreadContext.Properties["MSISDN"] = verification.userId;
  133. }
  134. Log.DebugFormat("BusinessLogic.RegisterBL.ResendVerificationCode | Requested parameters : {0}", JsonConvert.SerializeObject(verification));
  135. JObject res = new JObject();
  136. if (verification.codeType == "dvc")
  137. {
  138. res["verificationCode"] = plainOTP;
  139. }
  140. else if (verification.codeType == "prc")
  141. {
  142. res["forgetCode"] = plainOTP;
  143. }
  144. response = new JsonRxResponse { ErrorCode = "0", Msg = "Successfully sent verification code", Data = res };
  145. }
  146. }
  147. catch (Exception ex)
  148. {
  149. }
  150. return response;
  151. }
  152. public JsonRxResponse GetUser(string userId)
  153. {
  154. try
  155. {
  156. JsonRxResponse res = _requestAuth.GetUser(userId);
  157. return res;
  158. }
  159. catch (Exception ex)
  160. {
  161. Log.Error("Error occured while retriving user information.", ex);
  162. return null;
  163. }
  164. }
  165. public bool ValidateAuthenticity(string user, string guid)
  166. {
  167. try
  168. {
  169. bool res = _requestAuth.ValidateAuthenticity(user, guid);
  170. return res;
  171. }
  172. catch (Exception ex)
  173. {
  174. Log.Error("Error occured while retriving user information.", ex);
  175. return false;
  176. }
  177. }
  178. public string GetDevice(string customerId)
  179. {
  180. try
  181. {
  182. string res = _requestAuth.GetDevice(customerId);
  183. return res;
  184. }
  185. catch (Exception ex)
  186. {
  187. Log.Error("Error occured while retriving user information.", ex);
  188. return "";
  189. }
  190. }
  191. }
  192. }