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

[FEATURE] 구글 로그인 구현 완료 #64 #84

Merged
merged 4 commits into from
Oct 24, 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 @@ -4,11 +4,8 @@
import HookKiller.server.auth.dto.request.SingUpRequest;
import HookKiller.server.auth.dto.response.AuthResponse;
import HookKiller.server.auth.dto.response.OAuthResponse;
import HookKiller.server.auth.dto.response.OauthLoginLinkResponse;
import HookKiller.server.auth.dto.response.OauthTokenResponse;
import HookKiller.server.auth.service.AuthService;
import HookKiller.server.common.service.MailHelper;
import HookKiller.server.properties.KakaoOauthProperties;
import HookKiller.server.user.entity.User;
import HookKiller.server.user.service.UserService;
import jakarta.validation.Valid;
Expand All @@ -22,7 +19,6 @@
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {
private final KakaoOauthProperties kakaoOauthProperties;
private final AuthService authService;
private final UserService userService;
private final MailHelper mailHelper;
Expand All @@ -43,65 +39,19 @@ public ResponseEntity<AuthResponse> login(@RequestBody @Valid AuthRequest body)
return authService.loginExecute(body);
}

@GetMapping("/login/test")
public boolean loginTest(@RequestParam String accessToken) {
return authService.loginExecuteTest(accessToken);
}

@PostMapping("/logout")
public boolean logout() {
return true;
}

// kakao Oauth 링크를 프론트에 전달해줌.
// 전달한 링크에서 클라이언트가 카카오로그인 요청
@GetMapping("/oauth/kakao/link")
public OauthLoginLinkResponse getKakaoLoginLink(
@RequestHeader(required = false) String referer, // referer는 http://localhost:8080/ 과 같이 제공됨
@RequestHeader(required = false) String host
) {
log.info("link에서의 referer : {}", referer);
if (referer != null && referer.contains(host)) {
String format = String.format("https://%s/", host);
return authService.getKakaoOauthLink(format); // http://localhost:8080 + /oauth/authorize?client_id=%s&redirect_uri=%s&response_type=code
}
return authService.getKakaoOauthLink(referer);
}

//요청한 카카오 로그인 후 받은 링크의 code로 idToken발급
// @GetMapping("/oauth/kakao")
// public OauthTokenResponse getKakaoCredentialInfo(
// @RequestParam String code
// ) {
// return authService.getCredentialFromKaKao(code, kakaoOauthProperties.getKakaoRedirectUrl());
// log.info("token에서의 referer : {}", referer);
// log.info("token에서의 code : {}", code);
// log.info("token에서의 host : {}", host);
//
// if (!referer.contains(host)) {
// log.info("referer가 host를 포함하나? {}", referer.contains(host));
// String format = String.format("https://%s/", host);
// return authService.getCredentialFromKaKao(code, format); // http://localhost:8080/ + kakao/callback
// }
// return authService.getCredentialFromKaKao(code, referer);
// }

@GetMapping("/oauth/kakao")
public OAuthResponse registerUserForTest(@RequestParam String code) {
log.error("oauth KaKao Code >>> {}", code);
public OAuthResponse registerUserForKakao(@RequestParam String code) {
return authService.registerUserKakaoCode(code);
}

// 받은 idToken으로 카카오 서버의 사용자 정보를 사용할 수 있는지 OIDC로 인증 및 인가 받고 회원가입 처리
@PostMapping("/oauth/kakao/register")
public OAuthResponse registerUser(
@RequestParam("id_token") String token) {
return userService.registerUserByOIDCToken(token);
}

// 받은 idToken을 이용해서 우리가 쓰는 accessToken 발급받기
@PostMapping("/oauth/kakao/login")
public OAuthResponse oauthLogin(@RequestParam String idToken) {
return authService.loginUserByIdToken(idToken);

@GetMapping("/oauth/google")
public OAuthResponse registerUserForGoogle(@RequestParam String code) {
return authService.registerGoogleUser(code);
}

}
38 changes: 38 additions & 0 deletions src/main/java/HookKiller/server/auth/dto/GoogleUserInfoDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package HookKiller.server.auth.dto;

import HookKiller.server.user.entity.OauthInfo;
import HookKiller.server.user.entity.OauthProvider;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class GoogleUserInfoDto {

private String sub;

private String email;

private String name;

private String picture;

private OauthProvider oauthProvider;

@Builder
public GoogleUserInfoDto(String sub, String email, String name, String picture, OauthProvider oauthProvider) {
this.sub = sub;
this.email = email;
this.name = name;
this.picture = picture;
this.oauthProvider = oauthProvider;
}

public OauthInfo toOauthInfo() {
return OauthInfo.builder()
.provider(OauthProvider.GOOGLE)
.oid(sub)
.build();
}
}
24 changes: 0 additions & 24 deletions src/main/java/HookKiller/server/auth/dto/KakaoUserInfoDto.java

This file was deleted.

10 changes: 8 additions & 2 deletions src/main/java/HookKiller/server/auth/dto/OIDCUserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ public class OIDCUserInfo {
private String nickName;

private String thumbnailImg;


private String picture;

private String name;

@Builder
public OIDCUserInfo(OauthInfo oauthInfo, String email, String nickName, String thumbnailImg) {
public OIDCUserInfo(OauthInfo oauthInfo, String email, String nickName, String thumbnailImg, String picture, String name) {
this.oauthInfo = oauthInfo;
this.email = email;
this.nickName = nickName;
this.thumbnailImg = thumbnailImg;
this.picture = picture;
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package HookKiller.server.auth.dto.response;

import HookKiller.server.outer.api.oauth.dto.KakaoTokenResponse;
import HookKiller.server.outer.api.oauth.dto.response.GoogleTokenResponse;
import lombok.Builder;
import lombok.Getter;

Expand All @@ -11,12 +11,16 @@ public class OauthTokenResponse {
private String accessToken;
private String refreshToken;
private String idToken;
private String expiresIn;
private String scope;
private String tokenType;

public static OauthTokenResponse from(KakaoTokenResponse kakaoTokenResponse) {
public static OauthTokenResponse from(GoogleTokenResponse googleTokenResponse) {
return OauthTokenResponse.builder()
.accessToken(kakaoTokenResponse.getAccessToken())
.refreshToken(kakaoTokenResponse.getRefreshToken())
.idToken(kakaoTokenResponse.getIdToken())
.accessToken(googleTokenResponse.getAccessToken())
.expiresIn(googleTokenResponse.getExpiresIn())
.scope(googleTokenResponse.getScope())
.tokenType(googleTokenResponse.getTokenType())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public enum AuthException implements BaseErrorCode {
INVALID_TOKEN_ERROR(UNAUTHORIZED.value(), "Token_403_2", "토큰이 만료되었거나 형식에 맞지않은 토큰입니다."),
TOKEN_NOT_FOUND_ERROR(NOT_FOUND.value(), "Token_404_1", "토큰을 찾을 수 없습니다."),
USER_NOT_FOUND_ERROR(NOT_FOUND.value(), "User_404_1", "유저를 찾을 수 없습니다."),
PASSWORD_INCORRECT_ERROR(NOT_FOUND.value(), "User_404_2", "해당 비밀번호를 가진 유저를 찾을 수 없습니다.")
PASSWORD_INCORRECT_ERROR(NOT_FOUND.value(), "User_404_2", "비밀번호가 다릅니다 다시 입력해 주세요.")
;

private final Integer statusCode;
Expand Down
81 changes: 0 additions & 81 deletions src/main/java/HookKiller/server/auth/helper/KakaoOauthHelper.java

This file was deleted.

4 changes: 1 addition & 3 deletions src/main/java/HookKiller/server/auth/helper/OIDCHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import HookKiller.server.common.dto.OIDCDto;
import HookKiller.server.jwt.JwtOIDCProvider;
import HookKiller.server.outer.api.oauth.dto.OIDCPublicKeyDto;
import HookKiller.server.outer.api.oauth.dto.OIDCResponse;
import HookKiller.server.outer.api.oauth.dto.response.OIDCResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -28,8 +28,6 @@ public OIDCDto getPayloadFromIdToken(
.filter(o -> o.getKid().equals(kid))
.findFirst()
.orElseThrow();

log.info("OIDCPublicKeyDto : {}", oidcPublicKeyDto.toString());

return jwtOIDCProvider.getOIDCTokenBody(
token, oidcPublicKeyDto.getN(), oidcPublicKeyDto.getE());
Expand Down
Loading
Loading