-
Notifications
You must be signed in to change notification settings - Fork 2
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
Feature/#41 깃허브 로그인 기능 구현 #50
The head ref may contain hidden characters: "Feature/#41-\uAE43\uD5C8\uBE0C_\uB85C\uADF8\uC778_\uAE30\uB2A5_\uAD6C\uD604"
Changes from 1 commit
ba1c7c3
2f33d70
8c528a6
52eb783
1f9b17c
9d99baf
d1cd05c
85e3454
eff599f
1a101cb
bb1b348
5422e89
38a1afb
4701ddb
6179009
7a7b853
5abbf9b
28f71c0
feeab0e
e7f913d
da1decf
0cd66d0
c79cb57
39c97a4
c003c2a
4d99f76
1cc219d
b52fc15
fac42f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.emmsale.login.application; | ||
|
||
import com.emmsale.login.application.dto.GithubAccessTokenRequest; | ||
import com.emmsale.login.application.dto.GithubAccessTokenResponse; | ||
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.MediaType; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Component | ||
public class GithubClient { | ||
|
||
@Value("${github.client.id}") | ||
private String clientId; | ||
@Value("${github.client.secret}") | ||
private String clientSecret; | ||
@Value("${github.url.access-token}") | ||
private String accessTokenUrl; | ||
@Value("${github.url.profile}") | ||
private String profileUrl; | ||
|
||
public String getAccessTokenFromGithub(final String code) { | ||
final GithubAccessTokenRequest githubAccessTokenRequest = new GithubAccessTokenRequest( | ||
code, | ||
clientId, | ||
clientSecret | ||
); | ||
|
||
final HttpHeaders headers = new HttpHeaders(); | ||
headers.add("Accept", MediaType.APPLICATION_JSON_VALUE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 상수는 따로 빼주거나, 아니면 spring 이 제공해주는 HttpHeaders.ACCEPT 를 사용해보시는건 어떠신가요? |
||
|
||
final HttpEntity httpEntity = new HttpEntity(githubAccessTokenRequest, headers); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. HttpEntity를 row 타입으로 사용하고 있는데 이유가 있으실까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다른 코드를 참고하며 작성하다 보니 놓친 것 같습니다. HttpEntity<GithubAccessTokenRequest> httpEntity;
httpEntity.getBody().getClientId(); // O
HttpEntity httpEntity;
httpEntity.getBody().getClientId(); // Cannot resolve method 'getClientId' in 'Object' |
||
final RestTemplate restTemplate = new RestTemplate(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RestTemplate 객체를 싱글톤으로 관리해보시는건 어떠신가요? 반복적으로 사용되는 것 같아서요~ |
||
|
||
final String accessToken = restTemplate | ||
.exchange(accessTokenUrl, HttpMethod.POST, httpEntity, GithubAccessTokenResponse.class) | ||
.getBody() | ||
.getAccessToken(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제가 잘 몰라서 그러는데 get함과 동시에 accessToken이 존재하지 않는다면 바로 NPE가 발생하나요??
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. final String accessToken = REST_TEMPLATE
.exchange(accessTokenUrl, HttpMethod.POST, httpEntity, GithubAccessTokenResponse.class)
.getBody()
.getAccessToken(); 위 코드에서 RestTemplate과 동일한 라이브러리는 아니지만 유사항 사례가 있어 참고로 남깁니다. |
||
|
||
if (accessToken == null) { | ||
//TODO: 커스텀 예외 정의 | ||
throw new IllegalArgumentException("code가 유효하지 않습니다."); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 유효성 검사 부분은 메서드로 추출해주는 편이 가독성에 더 좋을 것 같습니다! |
||
return accessToken; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.emmsale.login.application.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public class GithubAccessTokenRequest { | ||
|
||
private final String code; | ||
@JsonProperty("client_id") | ||
private final String clientId; | ||
@JsonProperty("client_secret") | ||
private final String clientSecret; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.emmsale.login.application.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 NoArgsCounstructor를 PROTECTED로 추가하신 이유가 있을까요? |
||
@Getter | ||
public class GithubAccessTokenResponse { | ||
|
||
@JsonProperty("access_token") | ||
private String accessToken; | ||
@JsonProperty("token_type") | ||
private String tokenType; | ||
private String scope; | ||
private String bearer; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
패키지 상으로도 그렇고 기능적으로도 Service에 해당하는 것 같은데 클래스명을 GithubClientService가 아니라 GithubClient로 지어주신 이유가 있을까요??
제가 OAuth쪽은 익숙하지 않아서 잘 모르는 것일 수도 있습니다...!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
컨트롤러의 요청을 받아 도메인 로직을 실행하고 이것을 반환하는 것이 아니므로 서비스라고 짓지 않았습니다.