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

PET-74 feat : 네이버 소셜로그인 추가 #27

Merged
merged 3 commits into from
Aug 23, 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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
import com.pawith.authmodule.application.port.out.command.handler.AuthHandler;
import com.pawith.jwt.JWTProvider;
import com.pawith.usermodule.application.handler.event.UserSignUpEvent;
import org.springframework.context.ApplicationContext;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

@Component
@RequiredArgsConstructor
public class OAuthInvoker {
private List<AuthHandler> authHandlerList;
private JWTProvider jwtProvider;
private ApplicationEventPublisher publisher;
private final List<AuthHandler> authHandlerList;
private final JWTProvider jwtProvider;
private final ApplicationEventPublisher publisher;

public OAuthResponse execute(OAuthRequest request){
OAuthUserInfo oAuthUserInfo = attemptLogin(request);
Expand Down Expand Up @@ -47,23 +48,4 @@ private OAuthResponse generateServerAuthenticationTokens(OAuthUserInfo oAuthUser
private <T> String attachAuthenticationType(Function<T, String> generateTokenMethod, T includeClaimData) {
return AuthConsts.AUTHENTICATION_TYPE + generateTokenMethod.apply(includeClaimData);
}

public void initStrategy(ApplicationContext applicationContext){
initAuthHandler(applicationContext);
initJWTProvider(applicationContext);
initEventPublisher(applicationContext);
}

private void initAuthHandler(ApplicationContext applicationContext) {
Map<String, AuthHandler> matchingBeans = applicationContext.getBeansOfType(AuthHandler.class);
authHandlerList = new ArrayList<>(matchingBeans.values());
}
private void initJWTProvider(ApplicationContext applicationContext){
jwtProvider = applicationContext.getBean(JWTProvider.class);
}

private void initEventPublisher(ApplicationContext applicationContext) {
publisher = applicationContext;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public boolean isAccessible(OAuthRequest authInfo) {
}

private GoogleUserInfo getGoogleUserInfo(final String accessToken){
return WebClient.create().get()
.uri(GOOGLE_OAUTH_USER_INFO_URL)
return WebClient.create(GOOGLE_OAUTH_USER_INFO_URL)
.get()
.header(GOOGLE_AUTHORIZATION, GOOGLE_AUTHORIZATION_BEARER+accessToken)
.retrieve()
.bodyToMono(GoogleUserInfo.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,55 @@
import com.pawith.authmodule.application.dto.OAuthUserInfo;
import com.pawith.authmodule.application.dto.Provider;
import com.pawith.authmodule.application.port.out.command.handler.AuthHandler;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.Map;

@Component
public class NaverOAuthHandler implements AuthHandler {
private static final Provider OAUTH_TYPE = Provider.NAVER;
private static final String NAVER_OAUTH_USER_INFO_URL = "https://openapi.naver.com/v1/nid/me";
private static final String NAVER_AUTHORIZATION = "Authorization";
private static final String NAVER_AUTHORIZATION_BEARER = "Bearer ";

@Override
public OAuthUserInfo handle(OAuthRequest authenticationInfo) {
return new OAuthUserInfo("naver", "naver");
final NaverUserInfo naverUserInfo = getNaverUserInfo(authenticationInfo.getAccessToken());
return new OAuthUserInfo(naverUserInfo.getNickname(), naverUserInfo.getEmail());
}

@Override
public boolean isAccessible(OAuthRequest authInfo) {
return OAUTH_TYPE.equals(authInfo.getProvider());
}

private NaverUserInfo getNaverUserInfo(String accessToken){
return WebClient.create(NAVER_OAUTH_USER_INFO_URL)
.get()
.accept(MediaType.APPLICATION_JSON)
.header(NAVER_AUTHORIZATION, NAVER_AUTHORIZATION_BEARER+accessToken)
.retrieve()
.bodyToMono(NaverUserInfo.class)
.block();
}

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
private static class NaverUserInfo{
private static final String EMAIL = "email";
private static final String NICKNAME = "nickname";
private Map<String, String> response;
public String getNickname(){
return response.get(NICKNAME);
}

public String getEmail(){
return response.get(EMAIL);
}
}
}