public async Task <JsonResult> EnableTwoFactorAuthentication(TwoFaVM twoFa)
        {
            _logger.LogInformation("AccountController.EnableTwoFactorAuthentication - hidrogenianId=" + twoFa.Id);

            var validation = await _reCaptchaService.IsHumanRegistration(twoFa.CaptchaToken);

            if (!validation.Result)
            {
                return(new JsonResult(validation));
            }

            var secretKey = HelperProvider.GenerateRandomString(12);

            var saved = await _userService.SaveTwoFaSecretKeyFor(twoFa.Id, secretKey);

            if (!saved.HasValue || !saved.Value)
            {
                return(new JsonResult(new { Result = RESULTS.FAILED, Message = "Error occurred while attempting to setup Two-Factor Authentication at the moment. Please try again." }));
            }

            var identity = await _accountService.GetAccountIdentity(twoFa.Id);

            var tfa = new TwoFactorAuthenticator();

            var authenticator = tfa.GenerateSetupCode(
                HidroConstants.PROJECT_NAME, identity.Email,
                secretKey, false, 200
                );

            twoFa.QrImageUrl   = authenticator.QrCodeSetupImageUrl;
            twoFa.ManualQrCode = authenticator.ManualEntryKey;

            return(new JsonResult(new { Result = RESULTS.SUCCESS, Message = twoFa }));
        }
Example #2
0
        public async Task <JsonResult> GetApiToken(string task)
        {
            _logger.LogInformation("WaterController.GetApiToken - Service starts.");

            var tokenLength = HelperProvider.RandomNumberInRange(30, 100);

            var token = new TokenVM {
                Token    = HelperProvider.GenerateRandomString(tokenLength),
                Duration = TOKEN_LIFE,
                Target   = HidroConstants.API_TOKEN_TARGETS[task]
            };

            var result = await _tokenService.SetApiToken(token);

            if (!result)
            {
                return(new JsonResult(new { Result = RESULTS.FAILED, Message = "An error occurred while attempting to load your photos. Please reload page to try again." }));
            }

            return(new JsonResult(new { Result = RESULTS.SUCCESS, Message = token.Token }));
        }
Example #3
0
        public async Task <KeyValuePair <string, string> > SetTempPasswordAndRecoveryToken(RecoveryVM recoveree)
        {
            _logger.LogInformation("AuthenticationService.SetTempPasswordAndRecoveryToken - Service starts.");

            var dbHidrogenian = !recoveree.Reattempt ? await _dbContext.Hidrogenian.FirstOrDefaultAsync(
                h => h.Email == recoveree.Email && h.EmailConfirmed && h.DeactivatedOn == null)
                                                     : await _dbContext.Hidrogenian.FirstOrDefaultAsync(
                h => h.Email == recoveree.Email && !h.EmailConfirmed && h.DeactivatedOn == null &&
                h.RecoveryToken != null && h.TokenSetOn != null);

            if (dbHidrogenian == null)
            {
                return(new KeyValuePair <string, string>(null, null));
            }

            var tempPassword = HelperProvider.GenerateRandomString(15);
            var hashedResult = GenerateHashedPasswordAndSalt(tempPassword);

            dbHidrogenian.PasswordHash = hashedResult.Key;
            dbHidrogenian.PasswordSalt = hashedResult.Value;

            var recoveryToken = GenerateRandomToken();

            dbHidrogenian.RecoveryToken = recoveryToken;
            dbHidrogenian.TokenSetOn    = DateTime.UtcNow;

            dbHidrogenian.EmailConfirmed = false;

            _dbContext.Hidrogenian.Update(dbHidrogenian);
            try {
                await _dbContext.SaveChangesAsync();
            } catch (Exception e) {
                _logger.LogError("AuthenticationService.SetTempPasswordAndRecoveryToken - Error: " + e);
                return(new KeyValuePair <string, string>(string.Empty, null));
            }

            return(new KeyValuePair <string, string>(tempPassword, recoveryToken));
        }