Ejemplo n.º 1
0
        public async Task RevokeAsync(string refreshToken)
        {
            var token = await _refreshTokensRepository.GetAsync(refreshToken);

            if (token is null)
            {
                throw new InvalidRefreshTokenException();
            }

            token.Revoke(DateTime.UtcNow, token.Id);
            await _refreshTokensRepository.UpdateAsync(token);
        }
Ejemplo n.º 2
0
        public async Task <JsonWebToken> HandleAsync(RefreshAccessTokenCommand command)
        {
            var refreshToken = await _refreshTokensRepository.GetAsync(command.Token);

            if (refreshToken is null)
            {
                throw new NotFoundException(ErrorCodes.refresh_token_not_found);
            }

            var user = await _usersRepository.GetAsync(refreshToken.UserId);

            user.NullCheck(ErrorCodes.user_not_found, refreshToken.UserId);

            var jwt = _jwtTokenService.CreateToken(user.Id, user.Role);

            jwt.SetRefreshToken(refreshToken.Token);

            return(jwt);
        }
Ejemplo n.º 3
0
        public async Task HandleAsync(RevokeAccessTokenCommand command)
        {
            var refreshToken = await _refreshTokensRepository.GetAsync(command.RefreshToken);

            if (refreshToken is null || refreshToken.UserId != command.UserId)
            {
                throw new MyShopException("refresh_token_not_found",
                                          $"Refresh token: '{command.RefreshToken}' was not hound.");
            }

            await _refreshTokensRepository.DeleteAsync(refreshToken.Id);

            await _cache.SetStringAsync($"tokens:{GetCurrentAsync()}",
                                        "revoked", new DistributedCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = TimeSpan
                                                  .FromMinutes(_jwtOptions.Value.ExpiryMinutes)
            });
        }