forked from codesquad-members-2024/issue-tracker
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
appconfig d ..
- Loading branch information
Showing
7 changed files
with
137 additions
and
16 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
be/issue_tracker/src/main/java/team1/issuetracker/config/AppConfig.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,14 @@ | ||
package team1.issuetracker.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class AppConfig { | ||
|
||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
be/issue_tracker/src/main/java/team1/issuetracker/domain/user/auth/GithubLoginUtil.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,52 @@ | ||
package team1.issuetracker.domain.user.auth; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
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 | ||
@RequiredArgsConstructor | ||
public class GithubLoginUtil { | ||
private final RestTemplate restTemplate; | ||
@Value("${github.client_secret}") | ||
private String clientSecret; | ||
@Value("${github.client_id}") | ||
private String clientId; | ||
|
||
public String validateCode(String code) { | ||
OAuthMemberInfoResponse userInfo = getUserInfo(getAccessToken(code)); | ||
return userInfo.id() + "\n" + userInfo.name() + "\n" + userInfo.profileImage(); | ||
} | ||
|
||
private OAuthMemberInfoResponse getUserInfo(String accessToken) { | ||
String url = "https://api.github.com/user"; | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setBearerAuth(accessToken); | ||
HttpEntity<Void> request = new HttpEntity<>(headers); | ||
|
||
return restTemplate.exchange( | ||
url, | ||
HttpMethod.GET, | ||
request, | ||
OAuthMemberInfoResponse.class | ||
).getBody(); | ||
} | ||
|
||
private String getAccessToken(String code) { | ||
String url = "https://github.com/login/oauth/access_token"; | ||
|
||
RestTemplate restTemplate = new RestTemplate(); | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE); | ||
HttpEntity<OAuthAccessTokenRequest> requestEntity = new HttpEntity<>(new OAuthAccessTokenRequest(clientId, clientSecret, code), headers); | ||
|
||
OAuthAccessTokenResponse body = restTemplate.exchange(url, HttpMethod.POST, requestEntity, OAuthAccessTokenResponse.class).getBody(); | ||
return body.accessToken(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ue_tracker/src/main/java/team1/issuetracker/domain/user/auth/OAuthAccessTokenRequest.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,8 @@ | ||
package team1.issuetracker.domain.user.auth; | ||
|
||
import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
|
||
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
public record OAuthAccessTokenRequest(String clientId, String clientSecret, String code) { | ||
} |
15 changes: 15 additions & 0 deletions
15
...e_tracker/src/main/java/team1/issuetracker/domain/user/auth/OAuthAccessTokenResponse.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,15 @@ | ||
package team1.issuetracker.domain.user.auth; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record OAuthAccessTokenResponse( | ||
@JsonProperty("access_token") | ||
String accessToken, | ||
int expiresIn, | ||
String refreshToken, | ||
int refreshTokenExpiresIn, | ||
String scope, | ||
@JsonProperty("token_type") | ||
String tokenType | ||
) | ||
{ } |
12 changes: 12 additions & 0 deletions
12
...ue_tracker/src/main/java/team1/issuetracker/domain/user/auth/OAuthMemberInfoResponse.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,12 @@ | ||
package team1.issuetracker.domain.user.auth; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record OAuthMemberInfoResponse( | ||
@JsonProperty("login") String id, | ||
@JsonProperty("avatar_url") String profileImage, | ||
String name | ||
) | ||
|
||
{ | ||
} |
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