public async Task UpdateFailedVerificationsInfoAsync(RequestorInfo requestor, uint?pinType, uint failedAttempts, DateTime lastFailed)
        {
            var prefix = _cacheKeyGenerator.GenerateKeyForVerificationFailures(requestor, pinType);
            await _redisClient.Db0.AddAsync(prefix + FailedCountSuffix, failedAttempts);

            await _redisClient.Db0.AddAsync(prefix + FailedLastSuffix, lastFailed);
        }
        public async Task SetPinAsync(RequestorInfo requestor, uint?pinType, string pin)
        {
            var sha    = SHA256.Create();
            var loaded = await _dbContext.Pins.FirstOrDefaultAsync(x => x.HouseholdId == requestor.HouseholdId && x.ProfileId == requestor.ProfileId && x.PinType == pinType);

            loaded.PinHash = sha.ComputeHash(Encoding.ASCII.GetBytes(loaded.PinSalt + pin)).ToHexString();
            await _dbContext.SaveChangesAsync();

            var prefix = _cacheKeyGenerator.GenerateKeyForHash(requestor, pinType);
            await _redisClient.Db0.AddAsync(prefix, loaded.PinHash);
        }
        public async Task <string?> GetPinHashAsync(RequestorInfo requestor, uint?pinType)
        {
            var prefix = _cacheKeyGenerator.GenerateKeyForHash(requestor, pinType);
            var hash   = await _redisClient.Db0.GetAsync <string>(prefix);

            if (hash == null)
            {
                var loaded = await _dbContext.Pins.FirstOrDefaultAsync(x => x.HouseholdId == requestor.HouseholdId && x.ProfileId == requestor.ProfileId && x.PinType == pinType);

                if (loaded != null)
                {
                    await _redisClient.Db0.AddAsync(prefix, loaded.PinHash);

                    return(loaded.PinHash);
                }
            }
            return(hash);
        }
 public string GenerateKeyForVerificationFailures(RequestorInfo requestor, uint?pinType)
 {
     pinType = pinType ?? 0;
     return($"{requestor.OpCoId}-PIN_VERIFY-{requestor.HouseholdId}-{requestor.ProfileId}");
 }
 public string GenerateKeyForHash(RequestorInfo requestor, uint?pinType)
 {
     pinType = pinType ?? 0;
     return($"{requestor.OpCoId}-PIN_HASH-{requestor.HouseholdId}-{requestor.ProfileId}");
 }
        public async Task <(uint FailedAttemptsCount, System.DateTime LastFailedAttempt)> GetFailedVerificationsInfoAsync(RequestorInfo requestor, uint?pinType)
        {
            var prefix = _cacheKeyGenerator.GenerateKeyForVerificationFailures(requestor, pinType);
            var FailedAttemptsCount = await _redisClient.Db0.GetAsync <uint?>(prefix + FailedCountSuffix) ?? 0;

            var LastFailedAttempt = await _redisClient.Db0.GetAsync <DateTime?>(prefix + FailedLastSuffix) ?? DateTime.MinValue;

            return(FailedAttemptsCount, LastFailedAttempt);
        }
 public async Task DeleteFailedAttemptsInfoAsync(RequestorInfo requestor, uint?pinType)
 {
     var prefix = _cacheKeyGenerator.GenerateKeyForVerificationFailures(requestor, pinType);
     await _redisClient.Db0.RemoveAllAsync(new[] { prefix + FailedCountSuffix, prefix + FailedLastSuffix });
 }