-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
340 additions
and
26 deletions.
There are no files selected for viewing
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
31 changes: 31 additions & 0 deletions
31
src/main/java/com/gamzabat/algohub/auth/controller/OAuth2Controller.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,31 @@ | ||
package com.gamzabat.algohub.auth.controller; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.gamzabat.algohub.auth.service.OAuth2Service; | ||
import com.gamzabat.algohub.feature.user.dto.TokenResponse; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api") | ||
@Tag(name = "oauth 소셜 로그인 관련 API", description = "code를 사용해 Github 소셜 로그인하는 API") | ||
public class OAuth2Controller { | ||
private final OAuth2Service oAuth2Service; | ||
|
||
@PostMapping("/oauth/github/sign-in") | ||
@Operation(summary = "Github 소셜 로그인 API", description = "Github 소셜 로그인 후 발급된 code를 전달해 로그인하는 API") | ||
public ResponseEntity<TokenResponse> signIn(@RequestParam String code) { | ||
TokenResponse response = oAuth2Service.githubSignIn(code); | ||
return ResponseEntity.ok(response); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/gamzabat/algohub/auth/dto/GithubEmailDto.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 com.gamzabat.algohub.auth.dto; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class GithubEmailDto { | ||
private String email; | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/gamzabat/algohub/auth/dto/GithubUserDto.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,11 @@ | ||
package com.gamzabat.algohub.auth.dto; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class GithubUserDto { | ||
private String login; | ||
private String email; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/gamzabat/algohub/auth/dto/OAuthAccessTokenDto.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,13 @@ | ||
package com.gamzabat.algohub.auth.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class OAuthAccessTokenDto { | ||
@JsonProperty("access_token") | ||
private String accessToken; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/gamzabat/algohub/auth/exception/GithubApiException.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,10 @@ | ||
package com.gamzabat.algohub.auth.exception; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class GithubApiException extends RuntimeException { | ||
public GithubApiException(String message) { | ||
super(message); | ||
} | ||
} |
164 changes: 164 additions & 0 deletions
164
src/main/java/com/gamzabat/algohub/auth/service/OAuth2Service.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,164 @@ | ||
package com.gamzabat.algohub.auth.service; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | ||
import org.springframework.security.oauth2.core.user.DefaultOAuth2User; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import com.gamzabat.algohub.auth.dto.GithubEmailDto; | ||
import com.gamzabat.algohub.auth.dto.GithubUserDto; | ||
import com.gamzabat.algohub.auth.dto.OAuthAccessTokenDto; | ||
import com.gamzabat.algohub.auth.exception.GithubApiException; | ||
import com.gamzabat.algohub.common.jwt.TokenProvider; | ||
import com.gamzabat.algohub.common.jwt.dto.JwtDTO; | ||
import com.gamzabat.algohub.constants.ApiConstants; | ||
import com.gamzabat.algohub.enums.Role; | ||
import com.gamzabat.algohub.feature.user.domain.User; | ||
import com.gamzabat.algohub.feature.user.dto.TokenResponse; | ||
import com.gamzabat.algohub.feature.user.repository.UserRepository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class OAuth2Service { | ||
private final TokenProvider tokenProvider; | ||
@Value("${github.client_id}") | ||
private String clientId; | ||
@Value("${github.client_secret}") | ||
private String secretKey; | ||
private final RestTemplate restTemplate; | ||
private final UserRepository userRepository; | ||
|
||
@Transactional | ||
public TokenResponse githubSignIn(String code) { | ||
String accessToken = getGithubAccessToken(code); | ||
GithubUserDto githubUser = getGithubUserInfo(accessToken); | ||
syncWithUser(githubUser); | ||
Authentication authentication = createGithubAuthentication(githubUser); | ||
JwtDTO tokens = tokenProvider.generateTokens(authentication); | ||
return new TokenResponse(tokens.getAccessToken(), tokens.getRefreshToken()); | ||
} | ||
|
||
private void syncWithUser(GithubUserDto githubUser) { | ||
Optional<User> optionalUser = userRepository.findByEmail(githubUser.getEmail()); | ||
if (optionalUser.isEmpty()) { | ||
register(githubUser); | ||
return; | ||
} | ||
User user = optionalUser.get(); | ||
user.editGithubName(githubUser.getLogin()); | ||
} | ||
|
||
private void register(GithubUserDto githubUser) { | ||
User newUser = userRepository.save(User.builder() | ||
.email(githubUser.getEmail()) | ||
.password(null) | ||
.nickname(null) | ||
.bjNickname(null) | ||
.role(Role.USER) | ||
.build()); | ||
newUser.editNickname(createTemporaryNickname(githubUser.getLogin(), newUser.getId())); | ||
newUser.editGithubName(githubUser.getLogin()); | ||
} | ||
|
||
private String createTemporaryNickname(String login, Long id) { | ||
return login + "@" + id; | ||
} | ||
|
||
private String getGithubAccessToken(String code) { | ||
HttpEntity<OAuthAccessTokenDto> response = restTemplate.exchange( | ||
ApiConstants.GITHUB_ACCESS_TOKEN_URL, | ||
HttpMethod.POST, | ||
createAccessTokenRequest(code), | ||
OAuthAccessTokenDto.class); | ||
|
||
if (response.getBody() == null) { | ||
log.error("failed to request GitHub access token : GitHub API response body is null."); | ||
throw new GithubApiException("Github access token 응답에 실패했습니다."); | ||
} | ||
|
||
return response.getBody().getAccessToken(); | ||
} | ||
|
||
private GithubUserDto getGithubUserInfo(String accessToken) { | ||
ResponseEntity<GithubUserDto> response = restTemplate.exchange( | ||
ApiConstants.GITHUB_USER_URL, | ||
HttpMethod.GET, | ||
createAuthorizationHeader(accessToken), | ||
GithubUserDto.class); | ||
|
||
GithubUserDto user = response.getBody(); | ||
if (user == null) { | ||
log.error("failed to request GitHub user info : GitHub API response body is null."); | ||
throw new GithubApiException("Github 사용자 정보를 가져오는데 실패했습니다."); | ||
} | ||
|
||
if (user.getEmail() == null || user.getEmail().isEmpty()) { | ||
user.setEmail(getEmail(accessToken)); | ||
} | ||
|
||
return user; | ||
} | ||
|
||
private String getEmail(String accessToken) { | ||
ResponseEntity<List<GithubEmailDto>> emails = restTemplate.exchange( | ||
ApiConstants.GITHUB_EMAIL_URL, | ||
HttpMethod.GET, | ||
createAuthorizationHeader(accessToken), | ||
new ParameterizedTypeReference<>() { | ||
} | ||
); | ||
|
||
if (emails.getBody() == null) { | ||
log.error("failed to request GitHub user email : GitHub API response body is null."); | ||
throw new GithubApiException("Github 유저의 이메일을 가져오는데 실패했습니다."); | ||
} | ||
|
||
return emails.getBody().getFirst().getEmail(); | ||
} | ||
|
||
private Authentication createGithubAuthentication(GithubUserDto githubUser) { | ||
OAuth2User oAuth2User = new DefaultOAuth2User( | ||
Collections.singleton(new SimpleGrantedAuthority(Role.USER.toString())), | ||
Map.of("email", githubUser.getEmail()), | ||
"email" | ||
); | ||
|
||
return new OAuth2AuthenticationToken(oAuth2User, oAuth2User.getAuthorities(), "github"); | ||
} | ||
|
||
private HttpEntity<MultiValueMap<String, String>> createAccessTokenRequest(String code) { | ||
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>(); | ||
params.add("client_id", clientId); | ||
params.add("client_secret", secretKey); | ||
params.add("code", code); | ||
|
||
return new HttpEntity<>(params, new HttpHeaders()); | ||
} | ||
|
||
private HttpEntity<Map<String, String>> createAuthorizationHeader(String accessToken) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add("Authorization", "Bearer " + accessToken); | ||
return new HttpEntity<>(headers); | ||
} | ||
} |
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
34 changes: 33 additions & 1 deletion
34
src/main/java/com/gamzabat/algohub/config/SwaggerConfig.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 |
---|---|---|
@@ -1,31 +1,63 @@ | ||
package com.gamzabat.algohub.config; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.gamzabat.algohub.constants.ApiConstants; | ||
|
||
import io.swagger.v3.oas.models.Components; | ||
import io.swagger.v3.oas.models.OpenAPI; | ||
import io.swagger.v3.oas.models.info.Info; | ||
import io.swagger.v3.oas.models.security.SecurityRequirement; | ||
import io.swagger.v3.oas.models.security.SecurityScheme; | ||
import io.swagger.v3.oas.models.servers.Server; | ||
|
||
@Configuration | ||
public class SwaggerConfig { | ||
|
||
@Value("${server.port}") | ||
private String serverPort; | ||
|
||
@Value("${spring.profiles.active:dev}") // 기본값을 local로 설정 | ||
private String activeProfile; | ||
|
||
@Bean | ||
public OpenAPI openAPI() { | ||
SecurityScheme scheme = new SecurityScheme() | ||
.type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT") | ||
.in(SecurityScheme.In.HEADER).name("Authorization"); | ||
SecurityRequirement requirement = new SecurityRequirement().addList("bearerAuth"); | ||
|
||
List<Server> servers = createServers(); | ||
|
||
return new OpenAPI() | ||
.components(new Components().addSecuritySchemes("bearerAuth", scheme)) | ||
.security(Collections.singletonList(requirement)) | ||
.info(new Info() | ||
.title("AlgoHub API 명세서") | ||
.description("AlgoHub API 명세서 입니다.") | ||
.version("1.0.0")); | ||
.version("1.0.0")) | ||
.servers(servers); | ||
|
||
} | ||
|
||
private List<Server> createServers() { | ||
Server prodServer = new Server() | ||
.description("Algohub API") | ||
.url(ApiConstants.SERVER_HTTPS_ENDPOINT); | ||
|
||
if ("prod".equalsIgnoreCase(activeProfile)) { | ||
return Collections.singletonList(prodServer); | ||
} | ||
|
||
Server localServer = new Server() | ||
.description("Algohub API for LOCAL") | ||
.url("http://localhost:" + serverPort); | ||
|
||
return List.of(localServer, prodServer); | ||
} | ||
} |
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.