Esempio n. 1
0
        public async Task <object> UpdateToken(string jwtToken, string refToken)
        {
            var decoded = _identityLogic.DecodeToken(jwtToken);

            if (decoded == default)
            {
                throw new UnauthorizedAccessException();
            }

            var value = decoded.Claims.FirstOrDefault(c => c.Type == JwtRegisteredClaimNames.Sub)?.Value;

            if (value == default || !Guid.TryParse(value, out Guid userId))
            {
                throw new UnauthorizedAccessException();
            }

            var user = await _baseLogic.Of <User>().GetQueryable(x => x.Id == userId)
                       .Include(x => x.UserRoles)
                       .ThenInclude(x => x.Role)
                       .FirstOrDefaultAsync();

            if (user == default)
            {
                throw new ArgumentException("Пользователь не найден");
            }

            if (_identityLogic.TokenExpired(refToken) || user.RefreshToken != refToken)
            {
                throw new UnauthorizedAccessException();
            }
            var accessToken  = _identityLogic.GenerateAccessToken(user);
            var refreshToken = _identityLogic.GenerateRefreshToken(user);

            user.RefreshToken = refreshToken;
            await _baseLogic.Of <User>().Update(user);

            return(new
            {
                accessToken,
                refreshToken
            });
        }