Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
tidavid1 committed Mar 20, 2024
2 parents 52701ce + b9936d9 commit b97e91d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.oeid.mogakgo.domain.achievement.infrastructure.AchievementJpaRepository;
import io.oeid.mogakgo.domain.achievement.infrastructure.UserAchievementJpaRepository;
import io.oeid.mogakgo.domain.achievement.infrastructure.UserActivityJpaRepository;
import io.oeid.mogakgo.domain.achievement.infrastructure.session.AchievementSessionRepository;
import io.oeid.mogakgo.domain.user.application.UserCommonService;
import io.oeid.mogakgo.domain.user.domain.User;
import java.util.List;
Expand All @@ -45,6 +46,7 @@ public class AchievementEventHandler {
private final UserActivityJpaRepository userActivityRepository;
private final UserCommonService userCommonService;
private final AchievementSocketService achievementSocketService;
private final AchievementSessionRepository achievementSessionRepository;

@Retryable(retryFor = EventListenerProcessingException.class, maxAttempts = 3, backoff = @Backoff(1000))
@Transactional
Expand All @@ -66,8 +68,9 @@ public void executeActivity(final UserActivityEvent event) {

log.info("call socket for event {} in progress", event.getAchievementId());

achievementSocketService.sendMessageAboutAchievmentCompletion(
event.getUserId(), AchievementMessage.builder()
achievementSocketService.sendMessageForAchievementCompletion(
achievementSessionRepository.getSession(event.getUserId()),
AchievementMessage.builder()
.userId(event.getUserId())
.achievementId(event.getAchievementId())
.progressCount(event.getProgressCount())
Expand Down Expand Up @@ -127,8 +130,9 @@ public void executeEvent(final AccumulateAchievementEvent event) {

log.info("call socket for event {} completion", event.getAchievementId());

achievementSocketService.sendMessageAboutAchievmentCompletion(
event.getUserId(), AchievementMessage.builder()
achievementSocketService.sendMessageForAchievementCompletion(
achievementSessionRepository.getSession(event.getUserId()),
AchievementMessage.builder()
.userId(event.getUserId())
.achievementId(event.getAchievementId())
.progressCount(event.getProgressCount())
Expand Down Expand Up @@ -156,8 +160,9 @@ public void executeEvent(final AccumulateAchievementUpdateEvent event) {
log.info("call socket for event {} completion", event.getAchievementId());

// 업적 달성 후, 클라이언트에게 socket 통신
achievementSocketService.sendMessageAboutAchievmentCompletion(
event.getUserId(), AchievementMessage.builder()
achievementSocketService.sendMessageForAchievementCompletion(
achievementSessionRepository.getSession(event.getUserId()),
AchievementMessage.builder()
.userId(event.getUserId())
.achievementId(event.getAchievementId())
.completed(Boolean.TRUE)
Expand Down Expand Up @@ -190,8 +195,9 @@ public void executeEvent(final SequenceAchievementUpdateEvent event) {
log.info("call socket for event {} completion", event.getAchievementId());

// 업적 달성 후, 클라이언트에게 socket 통신
achievementSocketService.sendMessageAboutAchievmentCompletion(
event.getUserId(), AchievementMessage.builder()
achievementSocketService.sendMessageForAchievementCompletion(
achievementSessionRepository.getSession(event.getUserId()),
AchievementMessage.builder()
.userId(event.getUserId())
.achievementId(event.getAchievementId())
.completed(Boolean.TRUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ public void sendMessageAboutAchievmentCompletion(Long userId, AchievementMessage
}
}

@Async
public void sendMessageForAchievementCompletion(WebSocketSession session, AchievementMessage message) {

if (session != null && session.isOpen()) {
try {
String jsonMessage = objectMapper.writeValueAsString(message);
TextMessage textMessage = new TextMessage(jsonMessage);
session.sendMessage(textMessage);
} catch (JsonProcessingException e) {
log.error("failed to convert Json message from object: {}", e.getMessage());
} catch (Exception e) {
log.error("sendMessageToSocket: {}", e.getMessage());
throw new AchievementException(ACHIEVEMENT_WEB_SOCKET_ERROR);
}
} else if (session != null && !session.isOpen()){
log.info("session closed: {}", session.getId());
}
}

public User validateUser(Long userId) {
return userCommonService.getUserById(userId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ public class AuthService {
private final GithubOAuth2Manager githubOAuth2Manager;

public AuthReissueResponse reissue(String expiredAccessToken, String refreshToken) {
log.debug("reissue expiredAccessToken: {}", expiredAccessToken);
log.debug("reissue refreshToken: {}", refreshToken);
expiredAccessToken = expiredAccessToken.substring(7); // remove "Bearer " (7 characters)
String verifyRefreshToken = jwtRedisDao.getRefreshTokenByAccessToken(expiredAccessToken);
log.debug("verifyRefreshToken: {}", verifyRefreshToken);
if (!refreshToken.equals(verifyRefreshToken)) {
log.debug("refreshToken not matched");
throw new AuthException(ErrorCode401.AUTH_MISSING_CREDENTIALS);
}
String accessToken = generateAccessToken(expiredAccessToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import io.oeid.mogakgo.exception.code.ErrorCode401;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class JwtRedisDao {

Expand All @@ -36,6 +38,9 @@ public void saveTokens(String accessToken, String refreshToken, int expireHour)
@Transactional(readOnly = true)
public String getRefreshTokenByAccessToken(String accessToken) {
var result = Optional.ofNullable(redisTemplate.opsForValue().get(accessToken));
return result.orElseThrow(() -> new AuthException(ErrorCode401.AUTH_MISSING_CREDENTIALS));
return result.orElseThrow(() -> {
log.debug("refreshToken not found");
return new AuthException(ErrorCode401.AUTH_MISSING_CREDENTIALS);
});
}
}

0 comments on commit b97e91d

Please sign in to comment.