private Result <UserPrivateDataDto> GetToken(UserPrivateDataModel model)
        {
            var token = _tokens.CreateToken(model.id, model.Email, model.Scopes, model.Tenants);

            var dto = new UserPrivateDataDto();

            dto.Id         = model.id;
            dto.Email      = model.Email;
            dto.ValidEmail = model.ValidEmail;
            dto.Token      = token;
            dto.Scopes     = model.Scopes;
            dto.Tenants    = model.Tenants;

            return(_result.Create(true, "", dto));
        }
 public LoginDto EndToken(HttpRequest request, string cookie, string path, UserPrivateDataDto model)
 {
     SetCookie(request.HttpContext.Response.Cookies, cookie, model.Token, path);
     if (model.Block && DateTime.Now >= model.ExpirationLock)
     {
         model.Block = false;
     }
     return(new LoginDto()
     {
         Id = model.Id,
         Email = model.Email,
         ValidEmail = model.ValidEmail,
         Scopes = model.Scopes,
         Tenants = model.Tenants,
         Block = model.Block
     });
 }
        public async Task <Result <LoginDto> > RefreshToken(string id, string email, HttpRequest request)
        {
            UserPrivateDataDto model = null;

            if (!_settings.RedisCacheSecurity)
            {
                var result = await _services.GetSecrets(id);

                if (!result.Success)
                {
                    return(_result.Create <LoginDto>(false, result.Message, null));
                }

                if (result.Value.Block)
                {
                    return(_result.Create <LoginDto>(false, _errors.NotAuthorized, null));
                }

                var dataResult = await UpdateSecrets(result.Value);

                if (!dataResult.Success)
                {
                    return(_result.Create <LoginDto>(false, dataResult.Message, null));
                }

                model = dataResult.Value;
            }
            else
            {
                var redishResult = await UpdateRedisCache(id, email);

                if (!redishResult.Success)
                {
                    return(_result.Create <LoginDto>(false, redishResult.Message, null));
                }

                model = redishResult.Value;
            }

            var login = EndToken(request, _settingsSecrets.CookieRefreshToken, _settingsSecrets.CookieRefreshTokenPath, model);

            return(_result.Create(true, "", login));
        }
        public async Task <Result <UserPrivateDataDto> > UpdateRedisCache(string id, string email)
        {
            List <RefreshTokensModel> queryTokens = new List <RefreshTokensModel>();
            RedisSecurityModel        model       = null;
            var tokens = await _redisCache.GetSringValue(id);

            if (!String.IsNullOrEmpty(tokens))
            {
                model = JsonConvert.DeserializeObject <RedisSecurityModel>(tokens);
                if (model.Block)
                {
                    return(_result.Create <UserPrivateDataDto>(true, _errors.NotAuthorized, null));
                }

                queryTokens = model.Tokens;
            }
            else
            {
                model = new RedisSecurityModel();
            }

            var refreshToken       = _crypto.GetRandomNumber();
            var RefreshTokenHashed = _crypto.GetStringSha256Hash(refreshToken + _settings.SecretKey);

            createRefreshToken(id, queryTokens, RefreshTokenHashed);

            model.Tokens = queryTokens;
            var jsonString = JsonConvert.SerializeObject(model);

            await _redisCache.SetStringValue(id, jsonString);

            var dto = new UserPrivateDataDto();

            dto.Id         = id;
            dto.Email      = email;
            dto.Token      = refreshToken;
            dto.Scopes     = model.Scopes;
            dto.Tenants    = model.Tenants;
            dto.ValidEmail = model.ValidEmail;

            return(_result.Create(true, "", dto));
        }
        public async Task <Result <UserPrivateDataDto> > UpdateSecrets(UserPrivateDataModel model)
        {
            var refreshToken       = _crypto.GetRandomNumber();
            var RefreshTokenHashed = _crypto.GetStringSha256Hash(refreshToken + _settings.SecretKey);

            createRefreshToken(model.id, model.RefreshTokens, RefreshTokenHashed);

            var result = await _services.Update(model);

            if (!result.Success)
            {
                return(_result.Create <UserPrivateDataDto>(false, result.Message, null));
            }

            var dto = new UserPrivateDataDto();

            dto.Id    = model.id;
            dto.Email = model.Email;
            dto.Token = refreshToken;

            return(_result.Create(true, "", dto));
        }