-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NO-JIRA--chore : oAuth2 패키지 domain으로 변경
- Loading branch information
1 parent
b054809
commit e448cb3
Showing
16 changed files
with
203 additions
and
213 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
...h2/google/api/GoogleOAuth2Controller.java → ...h2/google/api/GoogleOAuth2Controller.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
117 changes: 117 additions & 0 deletions
117
src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/domain/GoogleOAuth2.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,117 @@ | ||
package org.prgrms.nabimarketbe.domain.oauth2.google.domain; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import org.prgrms.nabimarketbe.domain.oauth2.google.dto.GoogleOAuth2TokenDTO; | ||
import org.prgrms.nabimarketbe.domain.oauth2.google.dto.GoogleUserInfoDTO; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class GoogleOAuth2 implements OAuth2 { | ||
@Value("${spring.security.oauth2.client.registration.google.url}") | ||
private String GOOGLE_SNS_LOGIN_URL; | ||
|
||
@Value("${spring.security.oauth2.client.registration.google.client-id}") | ||
private String GOOGLE_SNS_CLIENT_ID; | ||
|
||
@Value("${spring.security.oauth2.client.registration.google.redirect-uri}") | ||
private String GOOGLE_SNS_CALLBACK_URL; | ||
|
||
@Value("${spring.security.oauth2.client.registration.google.client-secret}") | ||
private String GOOGLE_SNS_CLIENT_SECRET; | ||
|
||
@Value("${spring.security.oauth2.client.registration.google.scope}") | ||
private String GOOGLE_DATA_ACCESS_SCOPE; | ||
|
||
@Value("${spring.security.oauth2.client.registration.google.token-url}") | ||
private String GOOGLE_TOKEN_REQUEST_URL; | ||
|
||
@Value("${spring.security.oauth2.client.registration.google.user-info-url}") | ||
private String GOOGLE_USER_INFO_REQUEST_URL; | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
private final RestTemplate restTemplate; | ||
|
||
@Override | ||
public String getOAuth2RedirectUrl() { | ||
Map<String, Object> params = new HashMap<>(); | ||
|
||
params.put("scope", GOOGLE_DATA_ACCESS_SCOPE); | ||
params.put("response_type", "code"); | ||
params.put("client_id", GOOGLE_SNS_CLIENT_ID); | ||
params.put("redirect_uri", GOOGLE_SNS_CALLBACK_URL); | ||
|
||
String parameterString = params.entrySet().stream() | ||
.map(x -> x.getKey() + "=" + x.getValue()) | ||
.collect(Collectors.joining("&")); | ||
|
||
String redirectURL = GOOGLE_SNS_LOGIN_URL + "?" + parameterString; | ||
return redirectURL; | ||
} | ||
|
||
@Override | ||
public ResponseEntity<String> requestAccessToken(String code) { | ||
Map<String, Object> params = new HashMap<>(); | ||
|
||
params.put("code", code); | ||
params.put("client_id", GOOGLE_SNS_CLIENT_ID); | ||
params.put("client_secret", GOOGLE_SNS_CLIENT_SECRET); | ||
params.put("redirect_uri", GOOGLE_SNS_CALLBACK_URL); | ||
params.put("grant_type", "authorization_code"); | ||
|
||
ResponseEntity<String> accessToken = restTemplate.postForEntity( | ||
GOOGLE_TOKEN_REQUEST_URL, | ||
params, | ||
String.class | ||
); | ||
|
||
return accessToken; | ||
} | ||
|
||
@Override | ||
public GoogleOAuth2TokenDTO getAccessToken(ResponseEntity<String> accessToken) throws JsonProcessingException { | ||
return objectMapper.readValue(accessToken.getBody(), GoogleOAuth2TokenDTO.class); | ||
} | ||
|
||
@Override | ||
public ResponseEntity<String> requestUserInfo(GoogleOAuth2TokenDTO googleOAuthToken) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
|
||
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(headers); | ||
headers.add("Authorization", "Bearer " + googleOAuthToken.accessToken()); | ||
ResponseEntity<String> exchange = restTemplate.exchange( | ||
GOOGLE_USER_INFO_REQUEST_URL, | ||
HttpMethod.GET, | ||
request, | ||
String.class | ||
); | ||
|
||
return exchange; | ||
} | ||
|
||
@Override | ||
public GoogleUserInfoDTO parseUserInfo(ResponseEntity<String> userInfoResponse) throws JsonProcessingException { | ||
GoogleUserInfoDTO googleUserInfoDTO = objectMapper.readValue( | ||
userInfoResponse.getBody(), | ||
GoogleUserInfoDTO.class | ||
); | ||
|
||
return googleUserInfoDTO; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/domain/OAuth2.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,19 @@ | ||
package org.prgrms.nabimarketbe.domain.oauth2.google.domain; | ||
|
||
import org.prgrms.nabimarketbe.domain.oauth2.google.dto.GoogleOAuth2TokenDTO; | ||
import org.prgrms.nabimarketbe.domain.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); | ||
|
||
GoogleOAuth2TokenDTO getAccessToken(ResponseEntity<String> accessToken) throws JsonProcessingException; | ||
|
||
ResponseEntity<String> requestUserInfo(GoogleOAuth2TokenDTO googleOAuthToken); | ||
|
||
GoogleUserInfoDTO parseUserInfo(ResponseEntity<String> userInfo) throws JsonProcessingException; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/dto/GoogleOAuth2TokenDTO.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,20 @@ | ||
package org.prgrms.nabimarketbe.domain.oauth2.google.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record GoogleOAuth2TokenDTO( | ||
@JsonProperty("access_token") | ||
String accessToken, | ||
|
||
@JsonProperty("expires_in") | ||
int expiresIn, | ||
|
||
String scope, | ||
|
||
@JsonProperty("token_type") | ||
String tokenType, | ||
|
||
@JsonProperty("id_token") | ||
String idToken | ||
) { | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/dto/GoogleUserInfoDTO.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,19 @@ | ||
package org.prgrms.nabimarketbe.domain.oauth2.google.dto; | ||
|
||
import org.prgrms.nabimarketbe.domain.user.entity.Role; | ||
import org.prgrms.nabimarketbe.domain.user.entity.User; | ||
|
||
public record GoogleUserInfoDTO( | ||
String id | ||
) { | ||
private static final String PROVIDER = "GOOGLE"; | ||
|
||
public User toEntity(String nickName) { | ||
return User.builder() | ||
.nickname(nickName) | ||
.provider(PROVIDER) | ||
.accountId(id) | ||
.role(Role.USER.getKey()) | ||
.build(); | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
...2/google/service/GoogleOAuth2Service.java → ...2/google/service/GoogleOAuth2Service.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
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
2 changes: 1 addition & 1 deletion
2
...rketbe/oauth2/kakao/dto/KakaoProfile.java → ...domain/oauth2/kakao/dto/KakaoProfile.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
2 changes: 1 addition & 1 deletion
2
...ketbe/oauth2/kakao/dto/RetKakaoOAuth.java → ...omain/oauth2/kakao/dto/RetKakaoOAuth.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
3 changes: 2 additions & 1 deletion
3
...kao/service/CustomUserDetailsService.java → ...kao/service/CustomUserDetailsService.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
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
Oops, something went wrong.