コード例 #1
0
        public async Task <ExchangeRefreshTokenResponse> Handle(ExchangeRefreshTokenRequest request,
                                                                CancellationToken cancellationToken)
        {
            var result = _validator.Validate(request);

            if (!result.IsValid)
            {
                return(new ExchangeRefreshTokenResponse(result.ToErrors(), "Could not verify token"));
            }

            var claims        = _tokenService.GetUserClaims(request.AccessToken);
            var identityClaim = claims.FirstOrDefault(x => x.Type == _tokenService.IdentityIdClaimType);

            if (identityClaim == null)
            {
                return(new ExchangeRefreshTokenResponse(
                           new[] { new Error("InvalidToken", "Token not in correct format.") }, "Could not verify token."));
            }

            var user = (await _unitOfWork.Repository
                        .GetAsync(u => u.IdentityId == identityClaim.Value, null, $"{nameof(User.RefreshTokens)}")).SingleOrDefault();

            if (user == null)
            {
                return(new ExchangeRefreshTokenResponse(
                           new[] { new Error("InvalidUser", "Could not verify user.") }, "Could not verify user."));
            }

            var claimsResponse = await _unitOfWork.Repository.GetClaimsAsync(user);

            if (user.CheckRefreshToken(request.RefreshToken) && claimsResponse.Succeeded)
            {
                var(token, expiresIn) = _tokenService.GenerateAccessToken(user, claimsResponse.Entity);
                var newRefreshToken = _tokenService.GenerateRefreshToken();
                user.RemoveRefreshToken(request.RefreshToken);
                user.AddRefreshToken(newRefreshToken, user.IdentityId, IPAddress.Parse(request.RemoteAddress));
                _unitOfWork.Repository.Update(user);
                await _unitOfWork.SaveChangesAsync();

                if (user is Caretaker)
                {
                    _scheduler.ScheduleTask(new CacheCustomersForCaretakerRequest(user.Guid));
                }
                if (user is Customer)
                {
                    _scheduler.ScheduleTask(new CacheCustomerApplianceHistoryRequest(user.Guid));
                }

                return(new ExchangeRefreshTokenResponse(token, expiresIn, newRefreshToken, "Successfully exchanged RefreshToken."));
            }

            return(new ExchangeRefreshTokenResponse(
                       new [] { new Error("InvalidRefreshToken", "Could not verify supplied RefreshToken.") }, "Could not verify supplied RefreshToken."));
        }
コード例 #2
0
        public async Task <LoginUserResponse> Handle(LoginUserRequest request, CancellationToken cancellationToken)
        {
            var result = _validator.Validate(request);

            if (!result.IsValid)
            {
                return(new LoginUserResponse(result.ToErrors(), "Invalid login credentials."));
            }

            var response = await _userUnitOfWork.Repository.GetUserByName(request.Username);

            if (!response.Succeeded)
            {
                return(new LoginUserResponse(response.Errors, ""));
            }

            var user         = response.Entity;
            var dataResponse = await _userUnitOfWork.Repository.CheckCredentials(user, request.Password);

            if (dataResponse.Succeeded)
            {
                var refreshToken = _tokenService.GenerateRefreshToken();
                user.AddRefreshToken(refreshToken, user.IdentityId, IPAddress.Parse(request.RemoteAddress));
                _userUnitOfWork.Repository.Update(user);
                await _userUnitOfWork.SaveChangesAsync();

                var(token, expiresIn) = _tokenService.GenerateAccessToken(user, user.Claims);

                if (user is Caretaker)
                {
                    _scheduler.ScheduleTask(new CacheCustomersForCaretakerRequest(user.Guid));
                }
                if (user is Customer)
                {
                    _scheduler.ScheduleTask(new CacheCustomerApplianceHistoryRequest(user.Guid));
                }
                return(new LoginUserResponse(token, expiresIn, refreshToken, "Successfully logged in."));
            }

            return(new LoginUserResponse(dataResponse.Errors, "Could not authenticate user."));
        }