public async Task <ActionResult <RequestResponse> > MfaAuth([FromBody] VerifyMfa mfaEnable) { try { if (mfaEnable is null || string.IsNullOrWhiteSpace(mfaEnable.MfaCode)) { return(BadRequest(RequestResponse.BadRequest("Mfa code is required for verification."))); } var userName = User?.Identity?.Name; var userId = User?.Claims.Where(x => x.Type == CustomClaims.USER_ID).FirstOrDefault()?.Value; if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(userId)) { return(BadRequest(RequestResponse.BadRequest("Something went wrong trying to validate your request."))); } var userProperties = await firebaseDbService.GetUserProperties(userId); if (userProperties is null) { return(NotFound(RequestResponse.NotFound("Error finding the data you are looking for."))); } if (!userProperties.IsMfaEnabled) { return(BadRequest(RequestResponse.BadRequest("Mfa not enabled for this user."))); } var verified = twoFactorAuth.VerifyCode(userProperties.Secret, mfaEnable.MfaCode); if (!verified) { return(BadRequest(RequestResponse.BadRequest("Invalid Mfa code provided. Please try again."))); } return(Ok(RequestResponse.Success())); } catch (Exception) { return(BadRequest(RequestResponse.BadRequest("Something went wrong trying to enable Mfa."))); } }
public async Task <ActionResult <ApiResponse <ExportPrivateKey> > > ExportPrivKey([FromBody] VerifyMfa mfaEnable) { try { if (mfaEnable is null || string.IsNullOrWhiteSpace(mfaEnable.MfaCode)) { return(BadRequest(RequestResponse.BadRequest("Mfa code is required in order to export your private key."))); } var userName = User?.Identity?.Name; var userId = User?.Claims.Where(x => x.Type == CustomClaims.USER_ID).FirstOrDefault()?.Value; if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(userId)) { return(BadRequest(RequestResponse.BadRequest("Something went wrong trying to validate your request."))); } var userProperties = await firebaseDbService.GetUserProperties(userId); if (userProperties is null) { return(NotFound(RequestResponse.NotFound("Error trying to find your Mfa data. Please try again."))); } if (!userProperties.IsMfaEnabled) { return(BadRequest(RequestResponse.BadRequest("You must first enable Mfa in order to use this function."))); } var verified = twoFactorAuth.VerifyCode(userProperties.Secret, mfaEnable.MfaCode); if (!verified) { return(BadRequest(RequestResponse.BadRequest("Invalid Mfa code provided. Please try again."))); } var result = await walletManagementService.GetWallets(userId, userName); if (!result.Any()) { return(NotFound(RequestResponse.NotFound($"No wallets found."))); } var wallet = result.FirstOrDefault(); var exportPrivKey = new ExportPrivateKey(wallet.PrivateKey, wallet.Addresses.FirstOrDefault()?.MyAddress); return(Ok(new ApiResponse <ExportPrivateKey>(exportPrivKey))); } catch (Exception) { return(BadRequest(RequestResponse.BadRequest("Something went wrong trying to export you private key."))); } }
public async Task <ActionResult <ApiResponse <UserProperties> > > VerifyMfaEnable([FromBody] VerifyMfa mfaEnable) { try { if (mfaEnable is null || string.IsNullOrWhiteSpace(mfaEnable.MfaCode)) { return(BadRequest(RequestResponse.BadRequest("Mfa code is required for verification."))); } var userName = User?.Identity?.Name; var userId = User?.Claims.Where(x => x.Type == CustomClaims.USER_ID).FirstOrDefault()?.Value; if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(userId)) { return(BadRequest(RequestResponse.BadRequest("Something went wrong trying to validate your request."))); } string secret; var cacheKey = $"{MFA_CACHE_KEY}{userId}"; bool doesExists = memoryCache.TryGetValue(cacheKey, out secret); if (!doesExists) { return(BadRequest(RequestResponse.BadRequest("Something went wrong. Please try restart the Mfa process."))); } var verified = twoFactorAuth.VerifyCode(secret, mfaEnable.MfaCode); if (!verified) { return(BadRequest(RequestResponse.BadRequest("Invalid Mfa code provided. Please try again."))); } var cacheResult = new UserProperties(userId, secret, true); var currentPropertiesKey = await firebaseDbService.GetUserPropertiesKey(userId); if (!string.IsNullOrWhiteSpace(currentPropertiesKey)) { await firebaseDbService.UpdateUserProperties(currentPropertiesKey, cacheResult); } else { await firebaseDbService.CreateUserProperties(cacheResult); } memoryCache.Remove(cacheKey); return(Ok(new ApiResponse <UserProperties>(cacheResult))); } catch (Exception) { return(BadRequest(RequestResponse.BadRequest("Something went wrong trying to enable Mfa."))); } }