Skip to content

Commit

Permalink
[#67]remove: unlink 관련 코드 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
ujkkk committed Nov 5, 2024
1 parent 5f1b5b2 commit 122de06
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 157 deletions.
3 changes: 1 addition & 2 deletions src/main/java/soma/edupiuser/account/AccountController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import soma.edupiuser.account.models.TokenInfo;
import soma.edupiuser.account.service.AccountService;
import soma.edupiuser.account.service.EmailService;
import soma.edupiuser.oauth.models.OAuth2Provider;
import soma.edupiuser.web.utils.CookieUtils;

@Slf4j
Expand Down Expand Up @@ -87,6 +86,6 @@ public ResponseEntity<LogoutResponse> logout(HttpServletRequest request, HttpSer

return ResponseEntity
.status(HttpStatus.OK)
.body((new LogoutResponse(OAuth2Provider.isOauth(provider), provider)));
.body((new LogoutResponse(false, provider)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class HttpCookieOAuth2AuthorizationRequestRepository

private static final String OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME = "oauth2_auth_request";
private static final String REDIRECT_URI_PARAM_COOKIE_NAME = "redirect_uri";
private static final String MODE_PARAM_COOKIE_NAME = "mode";
private static final int COOKIE_EXPIRE_SECONDS = 60 * 60;

@Override
Expand All @@ -32,11 +31,9 @@ public OAuth2AuthorizationRequest loadAuthorizationRequest(HttpServletRequest re
@Override
public void saveAuthorizationRequest(OAuth2AuthorizationRequest authorizationRequest, HttpServletRequest request,
HttpServletResponse response) {
log.info("Saving authorization request");
if (authorizationRequest == null) {
CookieUtils.deleteCookie(request, response, OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME);
CookieUtils.deleteCookie(request, response, REDIRECT_URI_PARAM_COOKIE_NAME);
CookieUtils.deleteCookie(request, response, MODE_PARAM_COOKIE_NAME);
return;
}

Expand All @@ -52,14 +49,6 @@ public void saveAuthorizationRequest(OAuth2AuthorizationRequest authorizationReq
redirectUriAfterLogin,
COOKIE_EXPIRE_SECONDS);
}

String mode = request.getParameter(MODE_PARAM_COOKIE_NAME);
if (StringUtils.hasText(mode)) {
CookieUtils.addCookie(response,
MODE_PARAM_COOKIE_NAME,
mode,
COOKIE_EXPIRE_SECONDS);
}
}

@Override
Expand All @@ -71,6 +60,5 @@ public OAuth2AuthorizationRequest removeAuthorizationRequest(HttpServletRequest
public void removeAuthorizationRequestCookies(HttpServletRequest request, HttpServletResponse response) {
CookieUtils.deleteCookie(request, response, OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME);
CookieUtils.deleteCookie(request, response, REDIRECT_URI_PARAM_COOKIE_NAME);
CookieUtils.deleteCookie(request, response, MODE_PARAM_COOKIE_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
return;
}

handleMode(request, response, principal);
signupAndLogin(request, response, principal);

if (response.isCommitted()) {
log.debug("Response has already been committed. Unable to redirect to " + targetUrl);
Expand All @@ -63,18 +63,9 @@ private void redirectWithError(HttpServletRequest request, HttpServletResponse r
getRedirectStrategy().sendRedirect(request, response, errorUrl);
}

private void handleMode(HttpServletRequest request, HttpServletResponse response, OAuth2UserPrincipal principal) {
String mode = CookieUtils.getCookie(request, MODE_PARAM_COOKIE_NAME)
.map(Cookie::getValue)
.orElse("");

if ("login".equalsIgnoreCase(mode)) {
oAuth2AccountService.handleLogin(principal, response);
} else if ("unlink".equalsIgnoreCase(mode)) {
oAuth2AccountService.handleUnlink(principal);
} else {
oAuth2AccountService.handleLogin(principal, response);
}
private void signupAndLogin(HttpServletRequest request, HttpServletResponse response,
OAuth2UserPrincipal principal) {
oAuth2AccountService.signupAndLogin(principal, response);
}

protected void clearAuthenticationAttributes(HttpServletRequest request, HttpServletResponse response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
public class GoogleOAuth2UserInfo implements OAuth2UserInfo {

private final Map<String, Object> attributes;
private final String accessToken;
private final String email;
private final String name;

public GoogleOAuth2UserInfo(String accessToken, Map<String, Object> attributes) {
this.accessToken = accessToken;
public GoogleOAuth2UserInfo(Map<String, Object> attributes) {
this.attributes = attributes;
this.email = (String) attributes.get("email");
this.name = (String) attributes.get("name");
Expand All @@ -21,11 +19,6 @@ public OAuth2Provider getProvider() {
return OAuth2Provider.GOOGLE;
}

@Override
public String getAccessToken() {
return accessToken;
}

@Override
public Map<String, Object> getAttributes() {
return attributes;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
public class NaverOAuth2UserInfo implements OAuth2UserInfo {

private final Map<String, Object> attributes;
private final String accessToken;
private final String email;
private final String name;

public NaverOAuth2UserInfo(String accessToken, Map<String, Object> attributes) {
this.accessToken = accessToken;
public NaverOAuth2UserInfo(Map<String, Object> attributes) {
// attributes 맵의 response 키의 값에 실제 attributes 맵이 할당되어 있음
this.attributes = (Map<String, Object>) attributes.get("response");
this.email = (String) this.attributes.get("email");
Expand All @@ -22,11 +20,6 @@ public OAuth2Provider getProvider() {
return OAuth2Provider.NAVER;
}

@Override
public String getAccessToken() {
return accessToken;
}

@Override
public Map<String, Object> getAttributes() {
return attributes;
Expand Down

This file was deleted.

2 changes: 0 additions & 2 deletions src/main/java/soma/edupiuser/oauth/models/OAuth2UserInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ public interface OAuth2UserInfo {

OAuth2Provider getProvider();

String getAccessToken();

Map<String, Object> getAttributes();

String getEmail();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
@Slf4j
public class OAuth2UserInfoFactory {

public static OAuth2UserInfo getOAuth2UserInfo(String registrationId,
String accessToken,
Map<String, Object> attributes) {
public static OAuth2UserInfo getOAuth2UserInfo(String registrationId, Map<String, Object> attributes) {
if (OAuth2Provider.GOOGLE.isEqualRegistrationId(registrationId)) {
return new GoogleOAuth2UserInfo(accessToken, attributes);
return new GoogleOAuth2UserInfo(attributes);
} else if (OAuth2Provider.NAVER.isEqualRegistrationId(registrationId)) {
return new NaverOAuth2UserInfo(accessToken, attributes);
return new NaverOAuth2UserInfo(attributes);
} else {
throw new OAuth2AuthenticationProcessingException("Login with " + registrationId + " is not supported");
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ private OAuth2User processOAuth2User(OAuth2UserRequest userRequest, OAuth2User o
String registrationId = userRequest.getClientRegistration()
.getRegistrationId();

String accessToken = userRequest.getAccessToken().getTokenValue();

OAuth2UserInfo oAuth2UserInfo = OAuth2UserInfoFactory.getOAuth2UserInfo(registrationId,
accessToken, oAuth2User.getAttributes());
oAuth2User.getAttributes());

// OAuth2UserInfo field value validation
if (!StringUtils.hasText(oAuth2UserInfo.getEmail())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import soma.edupiuser.account.models.EmailRequest;
import soma.edupiuser.account.service.domain.Account;
import soma.edupiuser.oauth.exception.OAuth2AuthenticationProcessingException;
import soma.edupiuser.oauth.models.OAuth2Provider;
import soma.edupiuser.oauth.models.OAuth2UserUnlinkManager;
import soma.edupiuser.oauth.models.SignupOAuthRequest;
import soma.edupiuser.web.auth.TokenProvider;
import soma.edupiuser.web.client.MetaServerApiClient;
Expand All @@ -23,7 +21,6 @@ public class OAuth2AccountService {

private final MetaServerApiClient metaServerApiClient;
private final TokenProvider tokenProvider;
private final OAuth2UserUnlinkManager oAuth2UserUnlinkManager;

public OAuth2UserPrincipal getOAuth2UserPrincipal(Authentication authentication) {
Object principal = authentication.getPrincipal();
Expand All @@ -34,7 +31,7 @@ public OAuth2UserPrincipal getOAuth2UserPrincipal(Authentication authentication)
return null;
}

public void handleLogin(OAuth2UserPrincipal principal, HttpServletResponse response) {
public void signupAndLogin(OAuth2UserPrincipal principal, HttpServletResponse response) {
String email = principal.getUserInfo().getEmail();
String name = principal.getUserInfo().getName();
String provider = principal.getUserInfo().getProvider().name().toLowerCase();
Expand All @@ -43,30 +40,26 @@ public void handleLogin(OAuth2UserPrincipal principal, HttpServletResponse respo
if (metaServerApiClient.isExistsEmail(email)) {
throw new OAuth2AuthenticationProcessingException(ErrorEnum.OAUTH2_EXCEPTION.getDetail());
}
log.info("handleLogin - signup, email={}", email);
// DB에 회원 저장
metaServerApiClient.saveAccountWithOauth(SignupOAuthRequest.builder()
.email(email)
.name(name)
.provider(provider)
.build());
signup(email, name, provider);
}
login(response, email);
}

private void signup(String email, String name, String provider) {
log.info("handleLogin - signup, email={}", email);
// DB에 회원 저장
metaServerApiClient.saveAccountWithOauth(SignupOAuthRequest.builder()
.email(email)
.name(name)
.provider(provider)
.build());
}

private void login(HttpServletResponse response, String email) {
log.info("handleLogin - login, email={}", email);
Account account = metaServerApiClient.oauthLogin(new EmailRequest(email));
String token = tokenProvider.generateToken(account);

CookieUtils.addCookie(response, "token", token, 60 * 60);
}

public void handleUnlink(OAuth2UserPrincipal principal) {
if (principal == null) {
return;
}
log.info("handleUnlink - userInfo={}", principal.getUserInfo());
String accessToken = principal.getUserInfo().getAccessToken();
OAuth2Provider provider = principal.getUserInfo().getProvider();
oAuth2UserUnlinkManager.unlink(provider, accessToken);
}


}

0 comments on commit 122de06

Please sign in to comment.