Skip to content

Commit

Permalink
Merge pull request #71 from teamSynapse6/develop
Browse files Browse the repository at this point in the history
fix: 토큰 갱신 API 로직 수정 (#70)
  • Loading branch information
sejineer authored Apr 20, 2024
2 parents 1bb30dd + 515cc60 commit c2016b9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public AuthRes refresh(final RefreshTokenReq tokenRefreshRequest) {

Token refreshToken = tokenRepository.findByRefreshToken(tokenRefreshRequest.getRefreshToken())
.orElseThrow(() -> new DefaultAuthenticationException(ErrorCode.INVALID_AUTHENTICATION));
Authentication authentication = customTokenProviderService.getAuthenticationByEmail(refreshToken.getProviderId());

Authentication authentication = customTokenProviderService.getAuthenticationByProviderId(refreshToken.getProviderId());

//4. refresh token 정보 값을 업데이트 한다.
//시간 유효성 확인
Expand Down Expand Up @@ -102,8 +103,9 @@ private boolean valid(String refreshToken){
DefaultAssert.isTrue(token.isPresent(), "탈퇴 처리된 회원입니다.");

//3. email 값을 통해 인증값을 불러온다
Authentication authentication = customTokenProviderService.getAuthenticationByEmail(token.get().getProviderId());
DefaultAssert.isTrue(token.get().getProviderId().equals(authentication.getName()), "사용자 인증에 실패하였습니다.");
Authentication authentication = customTokenProviderService.getAuthenticationByProviderId(token.get().getProviderId());
UserPrincipal userPrincipal = (UserPrincipal) authentication.getPrincipal();
DefaultAssert.isTrue(token.get().getProviderId().equals(userPrincipal.getPassword()), "사용자 인증에 실패하였습니다.");

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,25 @@ public Long getUserIdFromToken(String token) {
return Long.parseLong(claims.getSubject());
}

public UsernamePasswordAuthenticationToken getAuthenticationById(String token){
public UsernamePasswordAuthenticationToken getAuthenticationById(final String token){
Long userId = getUserIdFromToken(token);
UserDetails userDetails = customUserDetailsService.loadUserById(userId);
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
return authentication;
}

public UsernamePasswordAuthenticationToken getAuthenticationByEmail(String email){
public UsernamePasswordAuthenticationToken getAuthenticationByEmail(final String email){
UserDetails userDetails = customUserDetailsService.loadUserByUsername(email);
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
return authentication;
}

public UsernamePasswordAuthenticationToken getAuthenticationByProviderId(final String providerId){
UserDetails userDetails = customUserDetailsService.loadUserByProviderId(providerId);
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
return authentication;
}

public Long getExpiration(String token) {
// accessToken 남은 유효시간
Date expiration = Jwts.parserBuilder().setSigningKey(authConfig.getAuth().getTokenSecret()).build().parseClaimsJws(token).getBody().getExpiration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@

@RequiredArgsConstructor
@Service
public class CustomUserDetailsService implements UserDetailsService{
public class CustomUserDetailsService implements UserDetailsService {

private final UserRepository userRepository;

@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {

User user = userRepository.findByEmailAndStatus(email, Status.ACTIVE)
.orElseThrow(() ->
new UsernameNotFoundException("유저 정보를 찾을 수 없습니다.")
);
);

return UserPrincipal.create(user);
}
Expand All @@ -40,5 +40,13 @@ public UserDetails loadUserById(Long id) {

return UserPrincipal.create(user.get());
}


@Transactional
public UserDetails loadUserByProviderId(final String providerId) {
Optional<User> user = userRepository.findByProviderIdAndStatus(providerId, Status.ACTIVE);
DefaultAssert.isOptionalPresent(user);

return UserPrincipal.create(user.get());
}

}

0 comments on commit c2016b9

Please sign in to comment.