コード例 #1
0
        public async Task <JsonWebToken> Handle(RefreshAccessToken request,
                                                CancellationToken cancellationToken)
        {
            var token = request.Token;

            var refreshToken = await RefreshTokenRepository.GetAsync(token);

            if (refreshToken == null)
            {
                throw new IdentityException(Codes.RefreshTokenNotFound,
                                            "Refresh accessToken was not found.");
            }

            if (refreshToken.Revoked)
            {
                throw new IdentityException(Codes.RefreshTokenAlreadyRevoked,
                                            $"Refresh accessToken: '{refreshToken.Id}' was revoked.");
            }

            var user = await GetUserOrThrowAsync(refreshToken.UserId);

            var claims = await ClaimsProvider.GetAsync(user.Id);

            var jwt = JwtService.CreateToken(user.Id.ToString("N"), user.Role, claims);

            jwt.RefreshToken = refreshToken.Token;

            var @event = new AccessTokenRefreshedIntegrationEvent(user.Id);

            BusPublisher.Publish(@event);

            return(jwt);
        }
コード例 #2
0
        public async Task <Unit> Handle(RevokeRefreshToken request, CancellationToken cancellationToken)
        {
            var token  = request.Token;
            var userId = request.UserId;

            var refreshToken = await RefreshTokenRepository.GetAsync(token);

            if (refreshToken == null || refreshToken.UserId != userId)
            {
                throw new IdentityException(Codes.RefreshTokenNotFound,
                                            "Refresh accessToken was not found.");
            }

            refreshToken.Revoke();
            await RefreshTokenRepository.UpdateAndSaveAsync(refreshToken);

            return(Unit.Value);
        }