Skip to content

Commit

Permalink
NABI-101-refactor : 컨벤션 맞게 네이밍, 개행, 인덴트 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
BeommoKoo-dev committed Nov 1, 2023
1 parent dd849f3 commit ed10d85
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package org.prgrms.nabimarketbe.domain.user.dto;

import org.prgrms.nabimarketbe.domain.user.entity.User;
import org.prgrms.nabimarketbe.global.security.jwt.dto.TokenResponseDTO;

public record UserLoginResponseDTO(
UserResponseDto user,
TokenResponseDTO token
) {

public static UserLoginResponseDTO from(User user, TokenResponseDTO tokenResponseDTO) {
UserResponseDto userResponseDto = UserResponseDto.from(user);

UserLoginResponseDTO response = new UserLoginResponseDTO(userResponseDto, tokenResponseDTO);

return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

import org.prgrms.nabimarketbe.global.security.entity.RefreshToken;
import org.prgrms.nabimarketbe.global.security.jwt.dto.TokenResponseDTO;
import org.prgrms.nabimarketbe.oauth2.kakao.repository.RefreshTokenJpaRepo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
Expand All @@ -32,12 +34,17 @@
public class JwtProvider {
@Value("spring.jwt.secret")
private String secretKey;

private String ROLES = "roles";

private final Long accessTokenValidMillisecond = 60 * 60 * 1000L; // 1 hour

private final Long refreshTokenValidMillisecond = 14 * 24 * 60 * 60 * 1000L; // 14 day

private final UserDetailsService userDetailsService;

private final RefreshTokenJpaRepo refreshTokenJpaRepo;

@PostConstruct
protected void init() {
// 암호화
Expand Down Expand Up @@ -67,6 +74,10 @@ public TokenResponseDTO createTokenDto(Long userPk, List<String> roles) {
.signWith(SignatureAlgorithm.HS256, secretKey)
.compact();

RefreshToken refreshTokenEntity = new RefreshToken(userPk, refreshToken);

refreshTokenJpaRepo.save(refreshTokenEntity);

return TokenResponseDTO.builder()
.grantType("Bearer")
.accessToken(accessToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
@Slf4j
@RequestMapping("api/v1/users/oauth2/authorize/google")
public class GoogleOAuth2Controller {

private final GoogleOAuth2Service oauthService;

@GetMapping("/login")
public void socialLogin(
HttpServletResponse response
) throws IOException {
String requestURL = oauthService.requestRedirectUrl();
String requestURL = oauthService.requestRedirectUrl();

response.sendRedirect(requestURL);
}

Expand All @@ -38,8 +38,7 @@ public ResponseEntity<UserLoginResponseDTO> callback(
@RequestParam(name = "code") String code
) throws JsonProcessingException {
UserLoginResponseDTO loginResponseDTO = oauthService.OAuth2Login(code);
log.info("loginResponseDTO : {}", loginResponseDTO);

return ResponseEntity.ok(loginResponseDTO);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.Map;
import java.util.stream.Collectors;

import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleOAuth2Token;
import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleUser;
import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleOAuth2TokenDTO;
import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleUserInfoDTO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
Expand All @@ -19,13 +19,10 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Component
@RequiredArgsConstructor
@Slf4j
public class GoogleOAuth2 implements OAuth2 {

@Value("${spring.security.oauth2.client.registration.google.url}")
private String GOOGLE_SNS_LOGIN_URL;

Expand Down Expand Up @@ -65,7 +62,6 @@ public String getOAuth2RedirectUrl() {
.collect(Collectors.joining("&"));

String redirectURL = GOOGLE_SNS_LOGIN_URL + "?" + parameterString;
log.info("redirect-URL={}", redirectURL);
return redirectURL;
}

Expand All @@ -89,34 +85,33 @@ public ResponseEntity<String> requestAccessToken(String code) {
}

@Override
public GoogleOAuth2Token getAccessToken(ResponseEntity<String> accessToken) throws JsonProcessingException {
log.info("accessTokenBody: {}",accessToken.getBody());

return objectMapper.readValue(accessToken.getBody(), GoogleOAuth2Token.class);
public GoogleOAuth2TokenDTO getAccessToken(ResponseEntity<String> accessToken) throws JsonProcessingException {
return objectMapper.readValue(accessToken.getBody(), GoogleOAuth2TokenDTO.class);
}

@Override
public ResponseEntity<String> requestUserInfo(GoogleOAuth2Token googleOAuthToken) {
public ResponseEntity<String> requestUserInfo(GoogleOAuth2TokenDTO googleOAuthToken) {
HttpHeaders headers = new HttpHeaders();

HttpEntity<MultiValueMap<String,String>> request = new HttpEntity<>(headers);
headers.add("Authorization","Bearer " + googleOAuthToken.accessToken());
headers.add("Authorization", "Bearer " + googleOAuthToken.accessToken());
ResponseEntity<String> exchange = restTemplate.exchange(
GOOGLE_USER_INFO_REQUEST_URL,
HttpMethod.GET,
request,
String.class
);

log.info(exchange.getBody());

return exchange;
}

@Override
public GoogleUser parseUserInfo(ResponseEntity<String> userInfoResponse) throws JsonProcessingException {
GoogleUser googleUser = objectMapper.readValue(userInfoResponse.getBody(), GoogleUser.class);
public GoogleUserInfoDTO parseUserInfo(ResponseEntity<String> userInfoResponse) throws JsonProcessingException {
GoogleUserInfoDTO googleUserInfoDTO = objectMapper.readValue(
userInfoResponse.getBody(),
GoogleUserInfoDTO.class
);

return googleUser;
return googleUserInfoDTO;
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package org.prgrms.nabimarketbe.oauth2.google.domain;

import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleOAuth2Token;
import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleUser;
import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleOAuth2TokenDTO;
import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleUserInfoDTO;
import org.springframework.http.ResponseEntity;

import com.fasterxml.jackson.core.JsonProcessingException;

public interface OAuth2 {

String getOAuth2RedirectUrl();

ResponseEntity<String> requestAccessToken(String code);

GoogleOAuth2Token getAccessToken(ResponseEntity<String> accessToken) throws JsonProcessingException;

ResponseEntity<String> requestUserInfo(GoogleOAuth2Token googleOAuthToken);
GoogleOAuth2TokenDTO getAccessToken(ResponseEntity<String> accessToken) throws JsonProcessingException;

GoogleUser parseUserInfo(ResponseEntity<String> userInfo) throws JsonProcessingException;
ResponseEntity<String> requestUserInfo(GoogleOAuth2TokenDTO googleOAuthToken);

GoogleUserInfoDTO parseUserInfo(ResponseEntity<String> userInfo) throws JsonProcessingException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
@Getter
@RequiredArgsConstructor
public enum Role {

GUEST("ROLE_GUEST"),

USER("ROLE_USER");

private final String key;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;

public record GoogleOAuth2Token(

public record GoogleOAuth2TokenDTO(
@JsonProperty("access_token")
String accessToken,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@

import org.prgrms.nabimarketbe.domain.user.entity.User;

public record GoogleUser(
public record GoogleUserInfoDTO(
String id,
String email,
Boolean verifiedEmail,
String name,
String givenName,
String familyName,
String picture,
String locale
String picture
) {

private static final String PROVIDER = "GOOGLE";

public User toEntity(String nickName) {
Expand All @@ -23,6 +17,7 @@ public User toEntity(String nickName) {
.email(email)
.imageUrl(picture)
.provider(PROVIDER)
.nameAttributeKey(id)
.roles(List.of("ROLE_USER"))
.build();
}
Expand Down

This file was deleted.

0 comments on commit ed10d85

Please sign in to comment.