public static bool CheckCodeGoogle(string secret, string token) { var google = new GoogleAuthen.TwoFactorAuthenticator(); var valid = google.ValidateTwoFactorPIN(secret, token); return(valid); }
public string VerifyCodeEnable([FromBody] JObject value = null) { try { var userModel = (User)RouteData.Values[ParseDataKeyApi.KEY_PASS_DATA_USER_MODEL]; if (userModel.IsTwoFactor == 2) { if (!value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_VERIFY_CODE_ENABLE_CODE)) { return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID)); } var code = value[ParseDataKeyApi.KEY_TWO_FA_VERIFY_CODE_ENABLE_CODE].ToString(); var authenticator = new TwoStepsAuthenticator.TimeAuthenticator(); var secretAuthToken = ActionCode.FromJson(userModel.SecretAuthToken); if (string.IsNullOrEmpty(secretAuthToken.CustomTwofa)) { return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR)); } var isOk = authenticator.CheckCode(secretAuthToken.CustomTwofa, code, userModel); if (!isOk) { return(HelpersApi.CreateDataError(MessageApiError.SMS_VERIFY_ERROR)); } } else if (userModel.IsTwoFactor == 0) { if (value.ContainsKey(ParseDataKeyApi.KEY_TWO_FA_VERIFY_CODE_ENABLE_CODE)) { return(HelpersApi.CreateDataError(MessageApiError.PARAM_INVALID)); } } var google = new GoogleAuthen.TwoFactorAuthenticator(); var secretKey = CommonHelper.RandomString(32); var startSetup = google.GenerateSetupCode(userModel.Email, secretKey, 300, 300); userModel.TwoFactorSecret = secretKey; Console.WriteLine(secretKey); var resultUpdate = _userBusiness.UpdateProfile(userModel); if (resultUpdate.Status == Status.STATUS_ERROR) { return(resultUpdate.ToJson()); } return(new ReturnObject { Status = Status.STATUS_SUCCESS, Data = startSetup.ManualEntryKey }.ToJson()); } catch (Exception e) { _logger.Error(KeyLogger.TWOFA_ENABLE_VERIFY + e); return(HelpersApi.CreateDataError(e.Message)); } }