예제 #1
0
        public async Task <Results <RefreshTokenContract> > RefreshToken(RefreshTokenContract token)
        {
            var principal = _jwtTokenService.GetPrincipalFromExpiredToken(token.Token);

            if (principal == null)
            {
                return(PandaResponse.CreateErrorResponse <RefreshTokenContract>("Invalid refresh token"));
            }

            var email = _jwtTokenService.GetValueFromClaimType(principal, JwtRegisteredClaimNames.Email);

            if (string.IsNullOrEmpty(email))
            {
                return(PandaResponse.CreateErrorResponse <RefreshTokenContract>("Invalid claim email"));
            }

            var userId = _jwtTokenService.GetValueFromClaimType(principal, JwtRegisteredClaimNames.NameId);

            if (string.IsNullOrEmpty(userId))
            {
                return(PandaResponse.CreateErrorResponse <RefreshTokenContract>("Invalid claim userId"));
            }

            var tokenValid = await _userLogic.ValidateRefreshToken(email, token.RefreshToken);

            if (tokenValid.IsError())
            {
                return(PandaResponse.CreateErrorResponse <RefreshTokenContract>(tokenValid.Errors.ToArray()));
            }

            var newJwtToken              = _jwtTokenService.GenerateToken(email, userId);
            var newRefreshToken          = _jwtTokenService.GenerateRefreshToken();
            var updateRefreshTokenResult = await _userLogic.UpdateRefreshTokenToUser(email, newRefreshToken);

            if (updateRefreshTokenResult.IsError())
            {
                return(PandaResponse.CreateErrorResponse <RefreshTokenContract>(updateRefreshTokenResult.Errors.ToArray()));
            }

            var result = PandaResponse.CreateSuccessResponse <RefreshTokenContract>(
                new RefreshTokenContract
            {
                Token        = newJwtToken,
                RefreshToken = newRefreshToken
            });

            return(result);
        }