Skip to content

Commit

Permalink
Merge pull request #25 from KusitmsHDmedi/feature/24-auth
Browse files Browse the repository at this point in the history
[fix] redis token 확인 로직 수정
  • Loading branch information
RyuKwanKon authored Sep 16, 2023
2 parents 034bb91 + e9661e3 commit d60389f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
@RedisHash(value = "authCode", timeToLive = 604800000)
public class AuthCode {
@Id
private String authCode;
private Long id;
private String id;
private Long value;

public static AuthCode createAuthCode(String authCode, Long id){
return AuthCode.builder()
.authCode(authCode)
.id(id)
.id(authCode)
.value(id)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import java.util.Optional;

public interface AuthCodeRepository extends CrudRepository<AuthCode, Long> {
Optional<AuthCode> findByAuthCode(String AuthCode);
public interface AuthCodeRepository extends CrudRepository<AuthCode, String> {
Optional<AuthCode> findById(String id);

boolean existsByAuthCode(String authCode);
boolean existsById(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public AuthCodeResponseDto createAuthCode(HDmediUser hDmediUser) {
String authCode = createAuthCodeAtSecureRandom(new SecureRandom());
AuthCode createdAuthCode = AuthCode.createAuthCode(authCode, hDmediUser.getId());
saveAuthCode(createdAuthCode);
return AuthCodeResponseDto.of(createdAuthCode.getAuthCode());
return AuthCodeResponseDto.of(createdAuthCode.getId());
}

public GuestSignInResponseDto geustSignIn(String authCode) {
AuthCode findAuthCode = getUserFromAuthCode(authCode);
User findUser = getUserFromId(findAuthCode.getId());
User findUser = getUserFromId(findAuthCode.getValue());
Token issuedToken = issueAccessTokenAndRefreshToken(findUser, Boolean.TRUE);
return GuestSignInResponseDto.of(findUser, findUser.getChildren(), issuedToken.getAccessToken());
}
Expand All @@ -80,7 +80,8 @@ private User getUserFromId(Long userId) {
}

private AuthCode getUserFromAuthCode(String authCode) {
return authCodeRepository.findByAuthCode(authCode)
String AuthCodeId = jwtProvider.deletePrefixOfToken(authCode);
return authCodeRepository.findById(AuthCodeId)
.orElseThrow(() -> new UnauthorizedException(INVALID_AUTH_CODE));
}

Expand All @@ -89,7 +90,7 @@ private void saveAuthCode(AuthCode createdAuthCode) {
}

private boolean duplicateAuthCode(String authCode) {
return authCodeRepository.existsByAuthCode(authCode);
return authCodeRepository.existsById(authCode);
}

private String createAuthCodeAtSecureRandom(SecureRandom random) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.security.Key;
import java.util.Base64;
Expand All @@ -35,6 +36,10 @@ public Token issueToken(HDmediUser hDmediUser) {
return responseToken;
}

public String deletePrefixOfToken(String token){
return StringUtils.delete(token, "Bearer ");
}

public void validateAccessToken(String accessToken) {
try {
getJwtParser().parseClaimsJws(accessToken);
Expand Down

0 comments on commit d60389f

Please sign in to comment.