Skip to content

Commit

Permalink
github 로그인 구현
Browse files Browse the repository at this point in the history
appconfig

d

..
  • Loading branch information
Miensoap committed May 30, 2024
1 parent ffce2f4 commit 64c23c7
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 16 deletions.
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();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package team1.issuetracker.domain.user;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import team1.issuetracker.domain.user.auth.GithubLoginUtil;
import team1.issuetracker.domain.user.dto.CheckDuplicateRequest;
import team1.issuetracker.domain.user.dto.LoginInfo;
import team1.issuetracker.domain.user.dto.RegisterInfo;
Expand All @@ -11,15 +13,12 @@

@RequestMapping("/user")
@RestController
@RequiredArgsConstructor
public class UserController {

private final UserService userService;
private final JwtUtil jwtUtil;

public UserController(UserService userService, JwtUtil jwtUtil) {
this.userService = userService;
this.jwtUtil = jwtUtil;
}
private final GithubLoginUtil githubLoginUtil;

@PostMapping
public void register(@RequestBody RegisterInfo registerInfo) throws IllegalArgumentException {
Expand All @@ -40,6 +39,12 @@ public String login(@RequestBody LoginInfo loginInfo) {
return jwtUtil.generateToken(loginInfo.id());
}

@GetMapping("/login/github")
public String githubLogin(@RequestParam String code){
String id = githubLoginUtil.validateCode(code);
return id;
}

@GetMapping("/{id}")
public UserInfoResponse userInfo(@PathVariable String id) throws NoSuchElementException {
return userService.getUserInfo(id);
Expand Down
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();
}
}
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) {
}
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
)
{ }
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
)

{
}
37 changes: 26 additions & 11 deletions be/issue_tracker/src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@
margin: 0;
background-color: #f0f0f0;
}

.image-container {
display: flex;
flex-direction: column;
align-items: center;
gap: 20px;
}

.image-container a {
display: block;
width: 200px;
height: 200px;
}

.image-container img {
width: 100%;
height: 100%;
Expand All @@ -33,16 +36,28 @@
</style>
</head>
<body>
<div class="image-container">
<a href="https://github.com/codesquad-masters2024-team01/issue-tracker/tree/Deploy" target="_blank">
<img src="https://soapbucket51.s3.ap-northeast-2.amazonaws.com/issue_tracker/github.png" alt="Image 1">
</a>
<a href="https://orange-evergreen-0a3.notion.site/Issue-Tracker-ee324589c29d45f49f03e2b373ee068f?pvs=74" target="_blank">
<img src="https://soapbucket51.s3.ap-northeast-2.amazonaws.com/issue_tracker/notion.png" alt="Image 2">
</a>
<a href="https://issuetracker01.site/issue/1" target="_blank">
<img src="https://soapbucket51.s3.ap-northeast-2.amazonaws.com/issue_tracker/poo.jpg" alt="Image 3">
</a>
</div>
<div class="image-container">
<a href="https://github.com/login/oauth/authorize?client_id=Ov23liHjM2Bq7fvWH4iZ"
style="
text-align: center;
color: orange;
font-size: 3.0em;
font-weight: bold;
background: #efefef;
"
>
로그인
</a>
<a href="https://github.com/codesquad-masters2024-team01/issue-tracker/tree/Deploy" target="_blank">
<img src="https://soapbucket51.s3.ap-northeast-2.amazonaws.com/issue_tracker/github.png" alt="Image 1">
</a>
<a href="https://orange-evergreen-0a3.notion.site/Issue-Tracker-ee324589c29d45f49f03e2b373ee068f?pvs=74"
target="_blank">
<img src="https://soapbucket51.s3.ap-northeast-2.amazonaws.com/issue_tracker/notion.png" alt="Image 2">
</a>
<a href="https://issuetracker01.site/issue/1" target="_blank">
<img src="https://soapbucket51.s3.ap-northeast-2.amazonaws.com/issue_tracker/poo.jpg" alt="Image 3">
</a>
</div>
</body>
</html>

0 comments on commit 64c23c7

Please sign in to comment.