Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] redis token 확인 로직 수정 #25

Merged
merged 4 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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