Exemple #1
0
        public async Task <ActionResult <Wallet> > GetWallets(string id, string email)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(id))
                {
                    return(NotFound(RequestResponse.NotFound()));
                }

                if (User.Identity.Name != email)
                {
                    return(BadRequest(RequestResponse.BadRequest()));
                }

                var result = await walletManagementService.GetWallets(id, email);

                if (!result.Any())
                {
                    return(NotFound(RequestResponse.NotFound($"Need to initialise wallets for user")));
                }

                return(Ok(result.Select(x => new Wallet(x.WalletId, x.UserId, null, null, true, x.Label, x.Email, x.Addresses)).FirstOrDefault()));
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, RequestResponse.InternalServerError()));
            }
        }
Exemple #2
0
        public async Task <ActionResult <IList <Address> > > CreateAddress([FromBody] CreateWalletAddress request)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(request.Email))
                {
                    return(BadRequest(new ArgumentNullException(nameof(request.Email)).Message));
                }

                if (string.IsNullOrWhiteSpace(request.Label))
                {
                    return(BadRequest(new ArgumentNullException(nameof(request.Label)).Message));
                }

                if (User.Identity.Name != request.Email)
                {
                    return(BadRequest(RequestResponse.BadRequest()));
                }

                var result = await walletManagementService.CreateAddress(request);

                return(Ok(result));
            }
            catch (Exception)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, RequestResponse.InternalServerError()));
            }
        }
Exemple #3
0
        public async Task <ActionResult <Wallet> > GetWallet(Guid id, string email)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(email))
                {
                    return(NotFound(RequestResponse.NotFound()));
                }

                if (User.Identity.Name != email)
                {
                    return(BadRequest(RequestResponse.BadRequest()));
                }

                var result = await walletManagementService.GetWallet(id, email);

                if (result is null)
                {
                    return(NotFound(RequestResponse.NotFound("Need to initialise wallet for user")));
                }

                return(Ok(new Wallet(result.WalletId, result.UserId, null, null, true, result.Label, result.Email, result.Addresses)));
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, RequestResponse.InternalServerError()));
            }
        }
Exemple #4
0
        public async Task <ActionResult <ApiResponse <UserDetail> > > GetUserDetail()
        {
            try
            {
                AuthenticationHeaderValue authHeader;
                AuthenticationHeaderValue.TryParse(Request.Headers["Authorization"], out authHeader);

                if (authHeader is null)
                {
                    return(NotFound(RequestResponse.NotFound("Invalid request.")));
                }

                var result = await firebaseAuthService.GetUser(authHeader.Parameter);

                var userPropertiesResult = await firebaseDbService.GetUserProperties(result.UserId);

                if (userPropertiesResult != null)
                {
                    result.IsMfaEnabled = userPropertiesResult.IsMfaEnabled;
                }

                return(Ok(new ApiResponse <UserDetail>(result)));
            }
            catch (Exception)
            {
                return(BadRequest(RequestResponse.BadRequest("An error occured while trying to get your details.")));
            }
        }
Exemple #5
0
        public async Task <ActionResult <ApiResponse <UserProperties> > > VerifyMfaDisable()
        {
            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.")));
                }

                var currentPropertiesKey = await firebaseDbService.GetUserPropertiesKey(userId);

                if (string.IsNullOrWhiteSpace(currentPropertiesKey))
                {
                    return(BadRequest(RequestResponse.BadRequest("Mfa record not found. Please enable Mfa.")));
                }

                var cacheResult = new UserProperties(userId, string.Empty, false);
                await firebaseDbService.UpdateUserProperties(currentPropertiesKey, cacheResult);

                return(Ok(new ApiResponse <UserProperties>(cacheResult)));
            }
            catch (Exception)
            {
                return(BadRequest(RequestResponse.BadRequest("Something went wrong trying to enable Mfa.")));
            }
        }
Exemple #6
0
        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.")));
            }
        }
Exemple #7
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.")));
            }
        }
Exemple #8
0
        public async Task <ActionResult <ApiResponse <LoginStatus> > > Login([FromBody] Login request)
        {
            try
            {
                var result = await firebaseAuthService.Login(request);

                return(Ok(new ApiResponse <LoginStatus>(result)));
            }
            catch (Exception)
            {
                return(StatusCode((int)HttpStatusCode.Unauthorized, RequestResponse.BadRequest(string.Empty)));
            }
        }
Exemple #9
0
        public async Task <ActionResult <RequestResponse> > ResetPassword([FromBody] PasswordReset passwordReset)
        {
            try
            {
                if (passwordReset is null || string.IsNullOrWhiteSpace(passwordReset.Email))
                {
                    return(BadRequest(RequestResponse.BadRequest("Please check your input.")));
                }

                await firebaseAuthService.SendPasswordResetEmail(passwordReset.Email);

                return(Ok(RequestResponse.Success()));
            }
            catch (Exception)
            {
                return(BadRequest(RequestResponse.BadRequest("Something went wrong trying to send reset email.")));
            }
        }
Exemple #10
0
        public async Task <ActionResult <ApiResponse <LoginStatus> > > RegisterUser([FromBody] Register register)
        {
            try
            {
                if (register is null || string.IsNullOrWhiteSpace(register.Email) || string.IsNullOrWhiteSpace(register.Password))
                {
                    return(BadRequest(RequestResponse.BadRequest("Please check your input.")));
                }

                var result = await firebaseAuthService.RegisterUser(register);

                return(Ok(new ApiResponse <LoginStatus>(result)));
            }
            catch (Exception)
            {
                return(BadRequest(RequestResponse.BadRequest("Something went wrong during the registration.")));
            }
        }
Exemple #11
0
        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.")));
            }
        }
Exemple #12
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.")));
            }
        }
Exemple #13
0
        public async Task <ActionResult <RequestResponse> > VerifyEmail()
        {
            try
            {
                AuthenticationHeaderValue authHeader;
                AuthenticationHeaderValue.TryParse(Request.Headers["Authorization"], out authHeader);

                if (authHeader is null)
                {
                    return(NotFound(RequestResponse.NotFound("Invalid request.")));
                }

                await firebaseAuthService.VerifyEmail(authHeader.Parameter);

                return(Ok(RequestResponse.Success()));
            }
            catch (Exception)
            {
                return(BadRequest(RequestResponse.BadRequest("Something went wrong trying to send verification email.")));
            }
        }
Exemple #14
0
        public async Task <ActionResult <CoinTransfer> > SpendCoins([FromBody] SpendCoins request)
        {
            try
            {
                if (request is null || string.IsNullOrEmpty(request.ToAddress))
                {
                    return(BadRequest("Invalid request."));
                }

                if (User.Identity.Name != request.Email)
                {
                    return(BadRequest(RequestResponse.BadRequest("Invalid request. Please try again.")));
                }

                var result = await walletManagementService.SpendCoins(request);

                return(Ok(result));
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, RequestResponse.InternalServerError(ex.Message)));
            }
        }
Exemple #15
0
        public async Task <ActionResult <TransactionWrapper> > GetTransactions(Guid id, string email, int from = 0, int count = 20)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(email))
                {
                    return(NotFound());
                }

                if (User.Identity.Name != email)
                {
                    return(BadRequest(RequestResponse.BadRequest()));
                }

                var result = await walletManagementService.GetTransactions(id, email, from, count);

                return(Ok(result));
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, RequestResponse.InternalServerError(ex.Message)));
            }
        }
Exemple #16
0
        public async Task <ActionResult <Balance> > GetBalance(Guid id, string email)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(email))
                {
                    return(NotFound());
                }

                if (User.Identity.Name != email)
                {
                    return(BadRequest(RequestResponse.BadRequest()));
                }

                var result = await walletManagementService.GetBalance(id, email);

                return(Ok(result));
            }
            catch (Exception)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, RequestResponse.InternalServerError()));
            }
        }