Example #1
0
        public async Task <ActionResult <ApiResponse <UserProperties> > > EnableMfa()
        {
            try
            {
                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);

                var currentProperties = await firebaseDbService.GetUserProperties(userId);

                if (currentProperties != null && currentProperties.IsMfaEnabled)
                {
                    memoryCache.Remove(cacheKey);
                    memoryCache.Set(cacheKey, currentProperties.Secret, cacheEntryOptions);
                    currentProperties.Account = userName;
                    currentProperties.Issuer  = issuer;
                    return(Ok(new ApiResponse <UserProperties>(currentProperties)));
                }

                if (!doesExists)
                {
                    secret = twoFactorAuth.CreateSecret(160);
                    memoryCache.Set(cacheKey, secret, cacheEntryOptions);
                    var result = new UserProperties(userId, secret, issuer, userName);
                    return(Ok(new ApiResponse <UserProperties>(result)));
                }

                var cacheResult = new UserProperties(userId, secret, issuer, userName);
                return(Ok(new ApiResponse <UserProperties>(cacheResult)));
            }
            catch (Exception)
            {
                return(BadRequest(RequestResponse.BadRequest("Something went wrong trying to enable Mfa.")));
            }
        }
Example #2
0
        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.")));
            }
        }