Beispiel #1
0
        public async Task <IActionResult> RefreshToken([FromBody] Token model)
        {
            var refreshTokenValue = model.RefreshToken;

            if (string.IsNullOrWhiteSpace(refreshTokenValue))
            {
                return(BadRequest("refreshToken is not set."));
            }

            var token = await _tokenStoreService.FindTokenAsync(refreshTokenValue);

            if (token == null)
            {
                return(Unauthorized());
            }

            var result = await _tokenFactoryService.CreateJwtTokensAsync(token.User);

            await _tokenStoreService.AddUserTokenAsync(token.User, result.RefreshTokenSerial, result.AccessToken,
                                                       _tokenFactoryService.GetRefreshTokenSerial(refreshTokenValue));

            await _uow.SaveChangesAsync();

            _antiforgery.RegenerateAntiForgeryCookies(result.Claims);

            return(Ok(new { access_token = result.AccessToken, refresh_token = result.RefreshToken }));
        }
Beispiel #2
0
        public async Task RevokeUserBearerTokensAsync(Guid userIdValue, string refreshTokenValue)
        {
            if (_configuration.Value.AllowSignoutAllUserActiveClients)
            {
                await InvalidateUserTokensAsync(userIdValue);
            }


            if (!string.IsNullOrWhiteSpace(refreshTokenValue))
            {
                var refreshTokenSerial = _tokenFactoryService.GetRefreshTokenSerial(refreshTokenValue);
                if (!string.IsNullOrWhiteSpace(refreshTokenSerial))
                {
                    var refreshTokenIdHashSource = _securityService.GetSha256Hash(refreshTokenSerial);
                    await DeleteTokensWithSameRefreshTokenSourceAsync(refreshTokenIdHashSource);
                }
            }

            await DeleteExpiredTokensAsync();
        }
Beispiel #3
0
        public void RevokeCustomerBearerTokens(string customerIdValue, string refreshTokenValue)
        {
            if (!string.IsNullOrWhiteSpace(customerIdValue) && int.TryParse(customerIdValue, out int customerId))
            {
                if (_jwtConfig.AllowSignoutAllCustomerActiveClients)
                {
                    InvalidateCustomerTokens(customerId);
                }
            }

            if (!string.IsNullOrWhiteSpace(refreshTokenValue))
            {
                var refreshTokenSerial = _tokenFactoryService.GetRefreshTokenSerial(refreshTokenValue);
                if (!string.IsNullOrWhiteSpace(refreshTokenSerial))
                {
                    var refreshTokenIdHashSource = _encryptionService.GetSha256Hash(refreshTokenSerial);
                    DeleteTokensWithSameRefreshTokenSource(refreshTokenIdHashSource);
                }
            }

            DeleteExpiredTokens();
        }
        public Auth_UserToken FindToken(string refreshTokenValue)
        {
            if (string.IsNullOrWhiteSpace(refreshTokenValue))
            {
                return(null);
            }
            var refreshTokenSerial = _tokenFactoryService.GetRefreshTokenSerial(refreshTokenValue);

            if (string.IsNullOrWhiteSpace(refreshTokenSerial))
            {
                return(null);
            }

            var refreshTokenIdHash = _encryptionService.GetSha256Hash(refreshTokenSerial);

            return(_tokenUserRepository.Table.FirstOrDefault(x => x.RefreshTokenIdHash == refreshTokenIdHash));
        }
Beispiel #5
0
        public IActionResult RefreshToken([FromBody] Token model)
        {
            var refreshTokenValue = model.RefreshToken;

            if (string.IsNullOrWhiteSpace(refreshTokenValue))
            {
                return(BadRequest("refreshToken is not set."));
            }

            var token = _tokenStoreService.FindToken(refreshTokenValue);

            if (token == null)
            {
                return(Unauthorized());
            }
            var user   = _userService.GetUserById(token.UserId);
            var result = _tokenFactoryService.CreateJwtTokensAsync(user);

            _tokenStoreService.AddUserToken(user, result.RefreshTokenSerial, result.AccessToken, _tokenFactoryService.GetRefreshTokenSerial(refreshTokenValue));

            return(Ok(new { access_token = result.AccessToken, refresh_token = result.RefreshToken }));
        }
Beispiel #6
0
        public IActionResult RefreshToken([FromBody] JToken jsonBody)
        {
            var response = new LoginResponse {
                Result = ResultType.Error
            };

            var refreshTokenValue = jsonBody.Value <string>("refreshToken");

            if (string.IsNullOrWhiteSpace(refreshTokenValue))
            {
                response.Messages.Add(_localizationService.GetResource("account.token.refreshtoken.nullrefreshtoken"));
                return(BadRequest(response));
            }

            var token = _tokenStoreService.FindToken(refreshTokenValue);

            if (token == null)
            {
                response.Messages.Add(_localizationService.GetResource("account.token.refreshtoken.nulltoken"));
                return(Unauthorized(response));
            }


            var jwtToken = _tokenFactoryService.CreateJwtTokens(token.Customer);

            _tokenStoreService.AddCustomerToken(token.Customer, jwtToken.RefreshTokenSerial, jwtToken.AccessToken, _tokenFactoryService.GetRefreshTokenSerial(refreshTokenValue));

            response.Result       = ResultType.Success;
            response.AccessToken  = jwtToken.AccessToken;
            response.RefreshToken = jwtToken.RefreshToken;

            return(Ok(response));
        }