-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
회원가입, 로그인, 로그아웃, 회원탈퇴
- Loading branch information
Showing
11 changed files
with
369 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/solitour_backend/solitour/auth/exception/RevokeFailException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package solitour_backend.solitour.auth.exception; | ||
|
||
public class RevokeFailException extends RuntimeException { | ||
public RevokeFailException(String message) { | ||
super(message); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/solitour_backend/solitour/auth/exception/UnsupportedLoginTypeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package solitour_backend.solitour.auth.exception; | ||
|
||
public class UnsupportedLoginTypeException extends RuntimeException { | ||
public UnsupportedLoginTypeException(String message) { | ||
super(message); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/solitour_backend/solitour/auth/exception/UserRevokeErrorException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package solitour_backend.solitour.auth.exception; | ||
|
||
public class UserRevokeErrorException extends RuntimeException { | ||
public UserRevokeErrorException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/main/java/solitour_backend/solitour/auth/support/naver/NaverConnector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package solitour_backend.solitour.auth.support.naver; | ||
|
||
|
||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.UUID; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
import solitour_backend.solitour.auth.support.kakao.dto.KakaoUserResponse; | ||
import solitour_backend.solitour.auth.support.naver.dto.NaverTokenAndUserResponse; | ||
import solitour_backend.solitour.auth.support.naver.dto.NaverTokenResponse; | ||
import solitour_backend.solitour.auth.support.naver.dto.NaverUserResponse; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
@Component | ||
public class NaverConnector { | ||
|
||
private static final String BEARER_TYPE = "Bearer"; | ||
private static final RestTemplate REST_TEMPLATE = new RestTemplate(); | ||
|
||
private final NaverProvider provider; | ||
|
||
public NaverTokenAndUserResponse requestNaverUserInfo(String code) { | ||
NaverTokenResponse naverTokenResponse = requestAccessToken(code); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set("Authorization", String.join(" ", BEARER_TYPE, naverTokenResponse.getAccessToken())); | ||
HttpEntity<Void> entity = new HttpEntity<>(headers); | ||
|
||
ResponseEntity<NaverUserResponse> responseEntity = REST_TEMPLATE.exchange(provider.getUserInfoUrl(), | ||
HttpMethod.GET, entity, | ||
NaverUserResponse.class); | ||
|
||
return new NaverTokenAndUserResponse(naverTokenResponse, responseEntity.getBody()); | ||
|
||
} | ||
|
||
public NaverTokenResponse requestAccessToken(String code) { | ||
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>( | ||
createBody(code), createHeaders()); | ||
|
||
return REST_TEMPLATE.postForEntity( | ||
provider.getAccessTokenUrl(), | ||
entity, | ||
NaverTokenResponse.class).getBody(); | ||
} | ||
|
||
public String refreshToken(String refreshToken) { | ||
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>( | ||
createRefreshBody(refreshToken), createHeaders()); | ||
|
||
return REST_TEMPLATE.postForEntity( | ||
provider.getAccessTokenUrl(), | ||
entity, NaverTokenResponse.class).getBody().getAccessToken(); | ||
} | ||
|
||
private HttpHeaders createHeaders() { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); | ||
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); | ||
return headers; | ||
} | ||
|
||
private MultiValueMap<String, String> createBody(String code) { | ||
MultiValueMap<String, String> body = new LinkedMultiValueMap<>(); | ||
body.add("code", code); | ||
body.add("grant_type", provider.getGrantType()); | ||
body.add("client_id", provider.getClientId()); | ||
body.add("client_secret", provider.getClientSecret()); | ||
body.add("state", UUID.randomUUID().toString()); | ||
return body; | ||
} | ||
|
||
private MultiValueMap<String, String> createRefreshBody(String refreshToken) { | ||
MultiValueMap<String, String> body = new LinkedMultiValueMap<>(); | ||
body.add("grant_type", provider.getRefreshGrantType()); | ||
body.add("client_id", provider.getClientId()); | ||
body.add("client_secret", provider.getClientSecret()); | ||
body.add("refresh_token", refreshToken); | ||
return body; | ||
} | ||
|
||
public HttpStatusCode requestRevoke(String token) throws IOException { | ||
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(createRevokeBody(token),createRevokeHeaders(token)); | ||
|
||
ResponseEntity<String> response = REST_TEMPLATE.postForEntity(provider.getAccessTokenUrl(), entity, String.class); | ||
|
||
return response.getStatusCode(); | ||
} | ||
|
||
private MultiValueMap<String, String> createRevokeBody(String accessToken) { | ||
MultiValueMap<String, String> body = new LinkedMultiValueMap<>(); | ||
body.add("client_id", provider.getClientId()); | ||
body.add("client_secret", provider.getClientSecret()); | ||
body.add("grant_type", provider.getRevokeGrantType()); | ||
body.add("access_token", accessToken); | ||
body.add("service_provider", provider.getServiceProvider()); | ||
return body; | ||
} | ||
|
||
private HttpHeaders createRevokeHeaders(String token) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); | ||
headers.set("Authorization", String.join(" ", BEARER_TYPE, token)); | ||
return headers; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/solitour_backend/solitour/auth/support/naver/NaverProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package solitour_backend.solitour.auth.support.naver; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
import lombok.Getter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.MultiValueMap; | ||
import solitour_backend.solitour.auth.support.naver.dto.NaverTokenResponse; | ||
|
||
@Getter | ||
@Component | ||
public class NaverProvider { | ||
|
||
private final String clientId; | ||
private final String clientSecret; | ||
private final String authUrl; | ||
private final String accessTokenUrl; | ||
private final String userInfoUrl; | ||
private final String grantType; | ||
private final String refreshGrantType; | ||
private final String revokeGrantType; | ||
private final String serviceProvider; | ||
private final String state = UUID.randomUUID().toString(); | ||
|
||
|
||
public NaverProvider(@Value("${oauth2.naver.client.id}") String clientId, | ||
@Value("${oauth2.naver.client.secret}") String clientSecret, | ||
@Value("${oauth2.naver.url.auth}") String authUrl, | ||
@Value("${oauth2.naver.url.token}") String accessTokenUrl, | ||
@Value("${oauth2.naver.url.userinfo}") String userInfoUrl, | ||
@Value("${oauth2.naver.service-provider}") String serviceProvider, | ||
@Value("${oauth2.naver.grant-type}") String grantType, | ||
@Value("${oauth2.naver.refresh-grant-type}") String refreshGrantType, | ||
@Value("${oauth2.naver.revoke-grant-type}") String revokeGrantType) { | ||
this.clientId = clientId; | ||
this.clientSecret = clientSecret; | ||
this.authUrl = authUrl; | ||
this.accessTokenUrl = accessTokenUrl; | ||
this.userInfoUrl = userInfoUrl; | ||
this.serviceProvider = serviceProvider; | ||
this.grantType = grantType; | ||
this.refreshGrantType = refreshGrantType; | ||
this.revokeGrantType = revokeGrantType; | ||
} | ||
|
||
public String generateTokenUrl(String grantType, String code) { | ||
Map<String, String> params = new HashMap<>(); | ||
params.put("grant_type", grantType); | ||
params.put("client_id", clientId); | ||
params.put("client_secret", clientSecret); | ||
params.put("code", code); | ||
params.put("state", state); | ||
return authUrl + "?" + concatParams(params); | ||
} | ||
|
||
public String generateAuthUrl(String redirectUrl) { | ||
Map<String, String> params = new HashMap<>(); | ||
params.put("response_type", "code"); | ||
params.put("client_id", clientId); | ||
params.put("redirect_uri", redirectUrl); | ||
params.put("state", state); | ||
return authUrl + "?" + concatParams(params); | ||
} | ||
|
||
private String concatParams(Map<String, String> params) { | ||
return params.entrySet() | ||
.stream() | ||
.map(entry -> entry.getKey() + "=" + entry.getValue()) | ||
.collect(Collectors.joining("&")); | ||
} | ||
|
||
public String generateAccessTokenUrl(String code) { | ||
return generateTokenUrl("authorization_code", code); | ||
} | ||
} |
Oops, something went wrong.