Skip to content

Commit 3643d99

Browse files
committed
Feat: 깃허브 로그인 연동
1 parent ee047ef commit 3643d99

File tree

6 files changed

+66
-14
lines changed

6 files changed

+66
-14
lines changed

src/main/java/com/back/domain/user/controller/AuthControllerDocs.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,6 @@ ResponseEntity<RsData<LoginResponse>> login(
292292
content = @Content(
293293
mediaType = "application/json",
294294
examples = {
295-
@ExampleObject(name = "이메일 없음", value = """
296-
{
297-
"success": false,
298-
"code": "AUTH_010",
299-
"message": "소셜 계정에서 이메일 정보를 확인할 수 없습니다.",
300-
"data": null
301-
}
302-
"""),
303295
@ExampleObject(name = "필수 정보 누락", value = """
304296
{
305297
"success": false,
@@ -311,7 +303,7 @@ ResponseEntity<RsData<LoginResponse>> login(
311303
@ExampleObject(name = "인증 처리 실패", value = """
312304
{
313305
"success": false,
314-
"code": "AUTH_011",
306+
"code": "AUTH_010",
315307
"message": "소셜 로그인 인증에 실패했습니다.",
316308
"data": null
317309
}

src/main/java/com/back/global/exception/ErrorCode.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ public enum ErrorCode {
6868
ACCESS_DENIED(HttpStatus.FORBIDDEN, "AUTH_007", "권한이 없습니다."),
6969
UNSUPPORTED_OAUTH_PROVIDER(HttpStatus.BAD_REQUEST, "AUTH_008", "지원하지 않는 소셜 로그인 제공자입니다."),
7070
OAUTH2_ATTRIBUTE_MISSING(HttpStatus.UNAUTHORIZED, "AUTH_009", "소셜 계정에서 필요한 사용자 정보를 가져올 수 없습니다."),
71-
OAUTH2_EMAIL_NOT_FOUND(HttpStatus.UNAUTHORIZED, "AUTH_010", "소셜 계정에서 이메일 정보를 확인할 수 없습니다."),
72-
OAUTH2_AUTHENTICATION_FAILED(HttpStatus.UNAUTHORIZED, "AUTH_011", "소셜 로그인 인증에 실패했습니다.");
71+
OAUTH2_AUTHENTICATION_FAILED(HttpStatus.UNAUTHORIZED, "AUTH_010", "소셜 로그인 인증에 실패했습니다.");
7372

7473

7574
private final HttpStatus status;

src/main/java/com/back/global/security/oauth/CustomOAuth2UserService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic
4545
case "kakao" -> new KakaoOAuth2UserInfo(attributes);
4646
case "naver" -> new NaverOAuth2UserInfo(attributes);
4747
case "google" -> new GoogleOAuth2UserInfo(attributes);
48+
case "github" -> new GithubOAuth2UserInfo(attributes);
4849
default -> throw new CustomException(ErrorCode.UNSUPPORTED_OAUTH_PROVIDER);
4950
};
5051

5152
// 필수 정보 검증
52-
if (userInfo.getEmail() == null || userInfo.getEmail().isBlank()) {
53-
throw new CustomException(ErrorCode.OAUTH2_EMAIL_NOT_FOUND);
54-
}
5553
if (userInfo.getProviderId() == null) {
5654
throw new CustomException(ErrorCode.OAUTH2_ATTRIBUTE_MISSING);
5755
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.back.global.security.oauth;
2+
3+
import java.util.Map;
4+
5+
/**
6+
* 깃허브 OAuth2 사용자 정보 구현체
7+
*/
8+
public class GithubOAuth2UserInfo implements OAuth2UserInfo {
9+
private final Map<String, Object> attributes;
10+
11+
public GithubOAuth2UserInfo(Map<String, Object> attributes) {
12+
this.attributes = attributes;
13+
}
14+
15+
@Override
16+
public String getProvider() {
17+
return "github";
18+
}
19+
20+
@Override
21+
public String getProviderId() {
22+
return String.valueOf(attributes.get("id")); // GitHub user id
23+
}
24+
25+
@Override
26+
public String getEmail() {
27+
return (String) attributes.get("email"); // 이메일 공개 설정 안 돼 있으면 null
28+
}
29+
30+
@Override
31+
public String getNickname() {
32+
return (String) attributes.get("login"); // GitHub username
33+
}
34+
35+
@Override
36+
public String getProfileImageUrl() {
37+
return (String) attributes.get("avatar_url");
38+
}
39+
}

src/main/resources/application-dev.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ spring:
5050
scope:
5151
- email
5252
- profile
53+
github:
54+
client-id: ${GITHUB_CLIENT_ID}
55+
client-secret: ${GITHUB_CLIENT_SECRET}
56+
client-name: GitHub
57+
authorization-grant-type: authorization_code
58+
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
59+
scope: user:email
5360
provider:
5461
kakao:
5562
authorization-uri: https://kauth.kakao.com/oauth/authorize
@@ -66,6 +73,11 @@ spring:
6673
token-uri: https://oauth2.googleapis.com/token
6774
user-info-uri: https://www.googleapis.com/oauth2/v3/userinfo
6875
user-name-attribute: sub
76+
github:
77+
authorization-uri: https://github.com/login/oauth/authorize
78+
token-uri: https://github.com/login/oauth/access_token
79+
user-info-uri: https://api.github.com/user
80+
user-name-attribute: id
6981

7082
springdoc:
7183
default-produces-media-type: application/json;charset=UTF-8

src/main/resources/application-test.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ spring:
3131
scope:
3232
- email
3333
- profile
34+
github:
35+
client-id: dummy-github-client-id
36+
client-secret: dummy-github-client-secret
37+
client-name: GitHub
38+
authorization-grant-type: authorization_code
39+
redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
40+
scope: user:email
3441
provider:
3542
kakao:
3643
authorization-uri: https://kauth.kakao.com/oauth/authorize
@@ -47,6 +54,11 @@ spring:
4754
token-uri: https://oauth2.googleapis.com/token
4855
user-info-uri: https://www.googleapis.com/oauth2/v3/userinfo
4956
user-name-attribute: sub
57+
github:
58+
authorization-uri: https://github.com/login/oauth/authorize
59+
token-uri: https://github.com/login/oauth/access_token
60+
user-info-uri: https://api.github.com/user
61+
user-name-attribute: id
5062

5163
jwt:
5264
secret: test-jwt-secret-key-12345678901234567890

0 commit comments

Comments
 (0)