99import org .springframework .util .MultiValueMap ;
1010import org .springframework .web .client .RestClient ;
1111import org .springframework .web .util .UriComponentsBuilder ;
12+ import timeeat .exception .BusinessErrorCode ;
13+ import timeeat .exception .BusinessException ;
1214
1315@ Component
1416@ EnableConfigurationProperties (OauthProperties .class )
@@ -17,27 +19,32 @@ public class OauthClient {
1719 private final RestClient restClient ;
1820 private final OauthProperties properties ;
1921
20- public OauthClient (RestClient .Builder restClientBuilder , OauthProperties oauthProperties ) {
22+ public OauthClient (RestClient .Builder restClientBuilder ,
23+ OauthProperties oauthProperties ) {
2124 this .restClient = restClientBuilder
2225 .defaultStatusHandler (HttpStatusCode ::is5xxServerError , new OauthServerErrorHandler ())
2326 .build ();
2427 this .properties = oauthProperties ;
2528 }
2629
27- public URI getOauthLoginUrl () {
30+ public URI getOauthLoginUrl (String origin ) {
31+ validateOrigin (origin );
32+
2833 return UriComponentsBuilder .fromUriString ("https://kauth.kakao.com/oauth/authorize" )
2934 .queryParam ("client_id" , properties .getClientId ())
30- .queryParam ("redirect_uri" , properties .getRedirectUri ())
35+ .queryParam ("redirect_uri" , origin + properties .getRedirectPath ())
3136 .queryParam ("response_type" , "code" )
3237 .build ()
3338 .toUri ();
3439 }
3540
36- public OauthToken requestOauthToken (String code ) {
41+ public OauthToken requestOauthToken (String code , String origin ) {
42+ validateOrigin (origin );
43+
3744 MultiValueMap <String , String > body = new LinkedMultiValueMap <>();
3845 body .add ("grant_type" , "authorization_code" );
3946 body .add ("client_id" , properties .getClientId ());
40- body .add ("redirect_uri" , properties .getRedirectUri ());
47+ body .add ("redirect_uri" , origin + properties .getRedirectPath ());
4148 body .add ("code" , code );
4249
4350 return restClient .post ()
@@ -48,6 +55,12 @@ public OauthToken requestOauthToken(String code) {
4855 .body (OauthToken .class );
4956 }
5057
58+ private void validateOrigin (String origin ) {
59+ if (!properties .isAllowedOrigin (origin )) {
60+ throw new BusinessException (BusinessErrorCode .UNAUTHORIZED_ORIGIN );
61+ }
62+ }
63+
5164 public OauthMemberInformation requestMemberInformation (OauthToken token ) {
5265 return restClient .get ()
5366 .uri ("https://kapi.kakao.com/v2/user/me" )
0 commit comments