logo

𝝅번째 알파카의 개발 낙서장

[OAuth2.0] ScribeJAVA로 OAuth2.0 인증서버 구축하기 - 7. GitHub OAuth 서비스 신청 및 모듈 구현하기

게시글
⏰ 2021-10-22 20:08:46

D O W N

https://user-images.githubusercontent.com/50317129/137171016-99af1db1-a346-4def-9329-6072b927bdc0.png
OAuth2.0 인증서버 구축기
이 게시글은 OAuth2.0 인증서버 구축기 시리즈의 12개 중 7번 째 게시글입니다.
https://user-images.githubusercontent.com/50317129/260317030-e4b8575b-f09e-47f4-ab70-168a817268c6.png

Table of Contents

https://user-images.githubusercontent.com/50317129/260317030-e4b8575b-f09e-47f4-ab70-168a817268c6.png

개요

마지막 플랫폼으로, GitHub에 OAuth 서비스를 신청하고 인증 모듈을 구현한다.

GitHub OAuth 서비스 신청하기

API 정보를 얻기 위해 GitHub OAuth 서비스를 신청하자.

1. OAuth Apps 생성하기

로그인 후 🔗 GitHub Developer Settings에 접속하자.

상단 프로필 메뉴에서 [Setting - Developer Settings - OAuth Apps]를 클릭하여 접속할 수도 있다.

[New OAuth App]을 클릭하여 새로운 애플리케이션을 생성하자.


필수사항을 입력하면 된다. GitHub는 Callback URL를 하나만 입력할 수 있는 것 같다.

2. Client Secret 생성하기

생성한 애플리케이션을 클릭한다. [Generate a new client secret]을 클릭하여 새로운 Client Secret을 발급받는다. 암호 확인 과정이 필요하다.

생성 직후 키를 보여주며, 창을 닫게 되면 해당 키는 다시 확인할 수 없으므로 적당한 곳에 임시로 기록해두던지 하자.

잊어버릴 경우 다시 발급받아야한다.

3. API 키 확인

메인 화면인 [General]에서 확인할 수 있다.

Client ID는 상시 확인 가능하고, Client Secret은 발급 직후에만 일시적으로 확인 가능하다는 점을 주의하자

GitHub OAuth는 이게 끝이다. 별도의 정보 동의 과정도 요구하지 않는다. 그도 그럴 것이, GitHub의 OAuth 키는 프로필 정보만 불러올 수 있기 때문.

GitHub 인증 모듈 구현하기

필요한 모든 준비가 갖춰졌으니, GitHub 인증 모듈을 구현해보자. 이전에 구현한 AuthModule을 상속받아 구현할 것이다.

JAVA

1
2
3
4
public class GithubAuthModule extends AuthModule
{
	// GitHub 인증 모듈
}

객체의 기본 형식은 위와 같다.

메서드메서드 타입내용구현 필요 여부
getAuthorizationUrl추상인증 URL 반환 메서드Y
getAccessToken접근 토큰 반환 메서드Y
getRefreshAccessToken접근 토큰 갱신 및 반환 메서드
getUserInfo사용자 정보 응답 반환 메서드Y
getRefreshTokenEndpoint접근 토큰 재발급 요청 URL 반환 메서드
getApiKeyBeanAPI 키 객체 반환 메서드
getUserInfoEndPoint사용자 정보 요청 URL 반환 메서드
getUserInfoBean추상유저 정보 객체 반환 메서드Y
deleteInfo추상연동 해제 결과 반환 메서드Y
getUpdateAuthorizationUrl추상정보 제공 동의 갱신 URL 반환 메서드Y
getAccessTokenEndpoint추상접근 토큰 요청 URL 반환 메서드Y
getAuthorizationBaseUrl추상인증 API 요청 URL 반환 메서드Y

GutHub 모듈이 구현해야하는 대상은 위와 같다. 이전 플랫폼과 다르게 getAccessToken, getUserInfo의 오버라이딩이 필요하다.

properties 파일 생성하기

WEB-INF 아래 github.properties 파일을 생성한다. 기 생성된 sample.properties를 복사해서 사용해도 된다.

PROPERTIES

1
2
3
api=API_KEY
secret=SECRET_KEY
callback=CALLBACK_URL

기본적인 형식은 위와 같으며, 각 항목에 해당하는 값을 입력하면 된다.

인증 모듈 기본 메서드 및 변수 할당하기

인증 모듈이 정상적으로 동작하기 위해선 기본적으로 지정해줘야할 메서드와 변수들이 존재한다. API 정보 설정, 인스턴스 반환같은 것들이다.

JAVA

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
private static final String MODULE_NAME = "github";
	
private static final String API_KEY;
private static final String SECRET_KEY;
private static final String CALLBACK_URL;

static
{
	ApiKeyBean apiKeyBean = getApiKeyBean(MODULE_NAME);
	
	API_KEY = apiKeyBean.getApi();
	SECRET_KEY = apiKeyBean.getSecret();
	CALLBACK_URL = apiKeyBean.getCallback();
}

private static final ServiceBuilderOAuth20 SERVICE_BUILDER = new ServiceBuilder(API_KEY).apiSecret(SECRET_KEY).callback(CALLBACK_URL);

private static final GithubAuthModule INSTANCE = new GithubAuthModule(SERVICE_BUILDER);

private GithubAuthModule(ServiceBuilderOAuth20 serviceBuilder)
{
	super(serviceBuilder);
}

public static GithubAuthModule getInstance()
{
	return INSTANCE;
}
구분형식내용
MODULE_NAMEString모듈 이름
API_KEYStringAPI키
SECRET_KEYStringSecret키
CALLBACK_URLString콜백 URL
SERVICE_BUILDERServiceBuilderOAuth20OAuth2.0 서비스 빌더
INSTANCEGithubAuthModule인스턴스

정의된 변수는 전부 static final로 선언되어 있어서, 인스턴스 생성 시 한 번만 선언되며 재할당이 불가능하도록 관리한다.

static{ } 구문을 통해 인스턴스 생성 시 API 정보를 할당하도록 구성했다.

API 할당 시 getApiKeyBean() 메서드를 통해 제공된 이름을 갖는 properties를 분석하여 ApiKeyBean 객체를 반환받아 사용한다.

API URL 할당하기

각 API 별 요청 URL을 반환하는 메서드를 구현하자.

JAVA

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Override
public String getAccessTokenEndpoint()
{
	return "https://github.com/login/oauth/access_token";
}

@Override
protected String getAuthorizationBaseUrl()
{
	return "https://github.com/login/oauth/authorize";
}

@Override
protected String getUserInfoEndPoint()
{
	return "https://api.github.com/users/RWB0104";
}
  • getAccessTokenEndpoint() - 토큰과 관련된 API는 해당 메소드가 반환하는 URL을 토대로 사용한다.
  • getAuthorizationBaseUrl() - 인증과 관련된 API는 해당 메소드가 반환하는 URL을 토대로 사용한다.
  • getUserInfoEndPoint() - 사용자 정보와 관련된 API는 해당 메소드가 반환하는 URL을 토대로 사용한다.

OAuth2.0 서비스를 수행함에 있어서 필요한 URL은 위와 같다. 이 중 getAccessTokenEndpoint()getAuthorizationBaseUrl()는 scribeJAVA 라이브러리의 객체인 DefaultApi20의 추상 메서드고 나머지 하나가 AuthModule의 추상 메서드다.

DefaultApi20는 사용자 계정 API에 관련된 메서드를 별도로 제공하지 않는다. 하지만 AuthModule에서 사용자 정보 확인 공통 메서드를 사용할 때 사용자 계정 API가 반드시 필요하므로 AuthModule의 추상 메서드로 관리한다.

인증 URL 반환 메서드

GitHub 플랫폼 로그인 URL을 반환하는 기능을 구현한다.

우선 API를 살펴보자.


  • 요청

TXT

1
GET https://github.com/login/oauth/authorize?response_type=code&client_id={:client_id}&redirect_uri={:redirect_uri}&state={:state}
parametertypedatarequireddescription
{:response_type}pathStringY응답 타입. code로 고정
{:client_id}pathStringYAPI키
{:redirect_uri}pathStringYCallback URL
{:state}pathStringY고유 상태값

  • 응답

GitHub 플랫폼 로그인 페이지


GitHub 플랫폼 로그인 API는 위와 같다. 메서드가 요청의 URL을 반환하도록 설계하면 된다.

문자열 연산으로 URL을 직접 설계할 수도 있지만, service.getAuthorizationUrl() 메서드를 통해 URL을 간편하게 생성할 수 있다.


이미 AuthModule에 공통 메서드로 선언된 게 있으므로, 따로 구현하지 않아도 된다.

접근 토큰 반환 메서드

로그인 결과로 Code를 전달받으므로 Access Token으로 교환하는 기능을 구현한다.

GitHub는 Accept 헤더로 JSON MIME를 지정해야하는데, 아쉽게도 scribeJAVA에는 임의 헤더를 넣어 인증 URL을 생성하는 API는 존재하지 않는다.

Access Token도 없으므로 HttpURLConnection으로 직접 구현해야한다.


  • 요청

TXT

1
2
POST https://kauth.kakao.com/oauth/token?grant_type=authorization_code&client_id={:client_id}&client_secret={:client_secret}&redirect_uri={:redirect_uri}&code={:code}
Accept: application/json
parametertypedatarequireddescription
{:grant_type}pathStringY인증 타입. authorization_code로 고정
{:client_id}pathStringYAPI키
{:client_secret}pathStringYSecret키
{:redirect_uri}pathStringYCallback URL
{:code}pathStringY인가 코드

  • 응답

JSON

1
2
3
4
5
{
	"access_token": "gho_16C7e42F292c6912E7710c838347Ae178B4a",
	"scope": "repo,gist",
	"token_type": "bearer"
}
parameterdatadescription
access_tokenString인증 토큰
token_typeString토큰 타입
scopeString접근 권한

AccessTokenRequestParams 객체로 요청을 생성하여 service.getAccessToken으로 응답을 받는다.

특이하게 응답 헤더를 반드시 지정해야하므로, 별도로 오버라이딩해서 사용한다.

접근 토큰 갱신 및 반환 메서드

위 응답을 보면 알 수 있듯이, GitHub는 Refresh Token이 따로 존재하지 않는다. Access Token의 만료시간도 없다. GitHub는 그냥 Access Token 하나만 다루게 된다.

Refresh Token이 없으므로 기능 자체가 쓸모가 없다. 따라서 Github에선 건들지 않는다.

사용자 정보 응답 반환 메서드

Access Token으로 유저 정보를 호출하는 기능을 구현한다. 발급받은 Access Token을 실제로 유의미하게 쓰는 부분이다.

GitHub API는 아래와 같다.


  • 요청

TXT

1
2
GET/POST https://api.github.com/user
Authorization: token {:access_token}
parametertypedatarequireddescription
{:access_token}headerStringY접근 토큰

  • 응답

JSON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{
	"login": "octocat",
	"id": 1,
	"node_id": "MDQ6VXNlcjE=",
	"avatar_url": "https://github.com/images/error/octocat_happy.gif",
	"gravatar_id": "",
	"url": "https://api.github.com/users/octocat",
	"html_url": "https://github.com/octocat",
	"followers_url": "https://api.github.com/users/octocat/followers",
	"following_url": "https://api.github.com/users/octocat/following{/other_user}",
	"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
	"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
	"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
	"organizations_url": "https://api.github.com/users/octocat/orgs",
	"repos_url": "https://api.github.com/users/octocat/repos",
	"events_url": "https://api.github.com/users/octocat/events{/privacy}",
	"received_events_url": "https://api.github.com/users/octocat/received_events",
	"type": "User",
	"site_admin": false,
	"name": "monalisa octocat",
	"company": "GitHub",
	"blog": "https://github.com/blog",
	"location": "San Francisco",
	"email": "octocat@github.com",
	"hireable": false,
	"bio": "There once was...",
	"twitter_username": "monatheoctocat",
	"public_repos": 2,
	"public_gists": 1,
	"followers": 20,
	"following": 0,
	"created_at": "2008-01-14T04:33:35Z",
	"updated_at": "2008-01-14T04:33:35Z",
	"private_gists": 81,
	"total_private_repos": 100,
	"owned_private_repos": 100,
	"disk_usage": 10000,
	"collaborators": 8,
	"two_factor_authentication": true,
	"plan": {
		"name": "Medium",
		"space": 400,
		"private_repos": 20,
		"collaborators": 0
	}
}

응답 명세는 GitHub에서 명확하게 제공하지 않는다. 확실한건 email, login, avatar_url를 쓰면 될 것 같다.

유저 정보 객체 반환 메서드

GitHub의 유저 정보 호출 API 응답 형식에 맞게끔 응답을 파싱하여 UserInfoBean로 반환하는 메서드를 구현한다.

이 프로젝트에선 이름, 이메일, 프로필사진 URL만을 사용하므로, 응답에서 해당 값을 빼내어 객체에 담는다.


  • 코드

JAVA

1
2
3
4
5
6
7
8
9
10
11
12
13
@Override
public UserInfoBean getUserInfoBean(String body) throws JsonProcessingException
{
	ObjectMapper mapper = new ObjectMapper();
	
	JsonNode node = mapper.readTree(body);
	
	String email = node.get("email") == null ? "미동의" : node.get("email").textValue();
	String name = node.get("name") == null ? "미동의" : node.get("name").textValue();
	String picture = node.get("avatar_url") == null ? "/oauth2/assets/images/logo.png" : node.get("avatar_url").textValue();
	
	return new UserInfoBean(email, name, picture, MODULE_NAME);
}

응답 형식에 맞추어 필요한 값을 추출한다. 만약, 사용자가 정보 제공에 동의하지 않았을 경우 대상 객체가 null을 반환한다. 데이터의 누락 시 오류를 방지하기 위해 데이터에 대한 null 처리를 반드시 해야한다.

연동 해제 결과 반환 메서드

카카오 아이디로 처음 로그인을 하면 정보 제공 동의를 수행하는데, 나중에 다시 로그인을 하면 이러한 동의 과정이 생략된다. 즉, 플랫폼에서 첫 로그인 시 정보 제공 동의를 받아 어딘가로부터 저장한다는 뜻이다. 만약 사용자가 서비스로부터 회원 탈퇴를 수행한다면 GitHub와의 연동을 해제하여 정보를 완전히 삭제할 필요가 있다.

GitHub API는 아래와 같다.


  • 요청

TXT

1
2
3
4
DELETE https://api.github.com/applications/{:client_id}/grant
Authorization: Basic {:auth}
Accept: application/vnd.github.v3+json
Content-Type: application/x-www-form-urlencoded
parametertypedatarequireddescription
{:client_id}pathStringYAPI 키
{:auth}headerStringYAPI 키와 Secret 키의 Basic 인증

  • 응답

응답은 204로, 아무도 오지 않는다.


  • 코드

JAVA

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Override
public boolean deleteInfo(String access) throws IOException, ExecutionException, InterruptedException
{
	HashMap<String, String> params = new HashMap<>();
	params.put("access_token", access);
	
	ObjectMapper mapper = new ObjectMapper();
	
	byte[] paramBytes = mapper.writeValueAsString(params).getBytes(StandardCharsets.UTF_8);
	
	URL url = new URL(Util.builder("https://api.github.com/applications/", API_KEY, "/grant"));
	
	HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	connection.setRequestMethod("DELETE");
	connection.addRequestProperty("Authorization", Util.builder("Basic ", Base64.getEncoder().encodeToString(Util.builder(API_KEY, ":", SECRET_KEY).getBytes())));
	connection.addRequestProperty("Accept", "application/vnd.github.v3+json");
	connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
	connection.setDoOutput(true);
	connection.getOutputStream().write(paramBytes);
	
	int status = connection.getResponseCode();
	
	connection.disconnect();
	
	return status == 204;
}

구현은 간단하다. OAuthRequest 객체를 활용하면 요청을 쉽게 생성할 수 있다. 응답 자체는 중요하지 않다. 이번엔 응답이 204이므로, 응답 상태값이 204인지 비교하여 결과를 반환한다.

정보 제공 동의 갱신 URL 반환 메서드

GitHub는 별도의 동의가 이루어지지 않으므로 무시한다.

전체 코드

JAVA

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package oauth.account.module;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.builder.ServiceBuilderOAuth20;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Verb;
import global.module.Util;
import oauth.account.bean.ApiKeyBean;
import oauth.account.bean.UserInfoBean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * GitHub 인증 모듈 클래스
 *
 * @author RWB
 * @since 2021.10.05 Tue 00:22:10
 */
public class GithubAuthModule extends AuthModule
{
	private static final String MODULE_NAME = "github";
	
	private static final String API_KEY;
	private static final String SECRET_KEY;
	private static final String CALLBACK_URL;
	
	static
	{
		ApiKeyBean apiKeyBean = getApiKeyBean(MODULE_NAME);
		
		API_KEY = apiKeyBean.getApi();
		SECRET_KEY = apiKeyBean.getSecret();
		CALLBACK_URL = apiKeyBean.getCallback();
	}
	
	private static final ServiceBuilderOAuth20 SERVICE_BUILDER = new ServiceBuilder(API_KEY).apiSecret(SECRET_KEY).callback(CALLBACK_URL);
	
	private static final GithubAuthModule INSTANCE = new GithubAuthModule(SERVICE_BUILDER);
	
	/**
	 * 생성자 메서드
	 *
	 * @param serviceBuilder: [ServiceBuilderOAuth20] API 서비스 빌더
	 */
	private GithubAuthModule(ServiceBuilderOAuth20 serviceBuilder)
	{
		super(serviceBuilder);
	}
	
	/**
	 * 인스턴스 반환 메서드
	 *
	 * @return [GithubAuthModule] 인스턴스
	 */
	public static GithubAuthModule getInstance()
	{
		return INSTANCE;
	}
	
	/**
	 * 접근 토큰 반환 메서드
	 *
	 * @param code: [String] 인증 코드
	 *
	 * @return [OAuth2AccessToken] 접근 토큰
	 *
	 * @throws IOException 데이터 입출력 예외
	 */
	@Override
	public OAuth2AccessToken getAccessToken(String code) throws IOException
	{
		HashMap<String, String> params = new HashMap<>();
		params.put("client_id", API_KEY);
		params.put("client_secret", SECRET_KEY);
		params.put("redirect_uri", CALLBACK_URL);
		params.put("code", code);
		
		StringBuilder builder = new StringBuilder();
		
		for (Map.Entry<String, String> param : params.entrySet())
		{
			String pre = builder.length() == 0 ? "" : "&";
			
			builder.append(pre).append(URLEncoder.encode(param.getKey(), StandardCharsets.UTF_8)).append("=").append(URLEncoder.encode(param.getValue(), StandardCharsets.UTF_8));
		}
		
		byte[] paramBytes = builder.toString().getBytes(StandardCharsets.UTF_8);
		
		URL url = new URL(getAccessTokenEndpoint());
		
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod("POST");
		connection.setRequestProperty("Accept", "application/json");
		connection.setDoOutput(true);
		connection.getOutputStream().write(paramBytes);
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
		
		StringBuilder responseBuilder = new StringBuilder();
		String temp;
		
		while ((temp = reader.readLine()) != null)
		{
			responseBuilder.append(temp);
		}
		
		reader.close();
		connection.disconnect();
		
		ObjectMapper mapper = new ObjectMapper();
		
		JsonNode node = mapper.readTree(responseBuilder.toString());
		
		String access_token = node.get("access_token") == null ? "미동의" : node.get("access_token").textValue();
		String token_type = node.get("token_type") == null ? "미동의" : node.get("token_type").textValue();
		String scope = node.get("scope") == null ? "미동의" : node.get("scope").textValue();
		
		return new OAuth2AccessToken(access_token, token_type, 0, null, scope, responseBuilder.toString());
	}
	
	/**
	 * 사용자 정보 응답 반환 메서드
	 *
	 * @param access: [String] 접근 토큰
	 *
	 * @return [Response] 사용자 정보 응답
	 *
	 * @throws IOException 데이터 입출력 예외
	 * @throws ExecutionException 실행 예외
	 * @throws InterruptedException 인터럽트 예외
	 */
	@Override
	public Response getUserInfo(String access) throws IOException, ExecutionException, InterruptedException
	{
		OAuthRequest oAuthRequest = new OAuthRequest(Verb.GET, getUserInfoEndPoint());
		oAuthRequest.addHeader("Authorization", Util.builder("token ", access));
		
		service.signRequest(access, oAuthRequest);
		
		return service.execute(oAuthRequest);
	}
	
	/**
	 * 유저 정보 객체 반환 메서드
	 *
	 * @param body: [String] OAuth 응답 내용
	 *
	 * @return [UserInfoBean] 유저 정보 객체
	 *
	 * @throws JsonProcessingException JSON 파싱 예외
	 */
	@Override
	public UserInfoBean getUserInfoBean(String body) throws JsonProcessingException
	{
		ObjectMapper mapper = new ObjectMapper();
		
		JsonNode node = mapper.readTree(body);
		
		String email = node.get("email") == null ? "미동의" : node.get("email").textValue();
		String name = node.get("name") == null ? "미동의" : node.get("name").textValue();
		String picture = node.get("avatar_url") == null ? "/oauth2/assets/images/logo.png" : node.get("avatar_url").textValue();
		
		return new UserInfoBean(email, name, picture, MODULE_NAME);
	}
	
	/**
	 * 연동 해제 결과 반환 메서드
	 *
	 * @param access: [String] 접근 토큰
	 *
	 * @return [boolean] 연동 해제 결과
	 *
	 * @throws IOException 데이터 입출력 예외
	 */
	@Override
	public boolean deleteInfo(String access) throws IOException
	{
		HashMap<String, String> params = new HashMap<>();
		params.put("access_token", access);
		
		ObjectMapper mapper = new ObjectMapper();
		
		byte[] paramBytes = mapper.writeValueAsString(params).getBytes(StandardCharsets.UTF_8);
		
		URL url = new URL(Util.builder("https://api.github.com/applications/", API_KEY, "/grant"));
		
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod("DELETE");
		connection.addRequestProperty("Authorization", Util.builder("Basic ", Base64.getEncoder().encodeToString(Util.builder(API_KEY, ":", SECRET_KEY).getBytes())));
		connection.addRequestProperty("Accept", "application/vnd.github.v3+json");
		connection.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		connection.setDoOutput(true);
		connection.getOutputStream().write(paramBytes);
		
		int status = connection.getResponseCode();
		
		connection.disconnect();
		
		return status == 204;
	}
	
	/**
	 * 정보 제공 동의 갱신 URL 반환 메서드
	 *
	 * @param state: [String] 고유 상태값
	 *
	 * @return [String] 정보 제공 동의 갱신 URL
	 */
	@Override
	public String getUpdateAuthorizationUrl(String state)
	{
		return null;
	}
	
	/**
	 * 접근 토큰 요청 URL 반환 메서드
	 *
	 * @return [String] 접근 토큰 요청 URL
	 */
	@Override
	public String getAccessTokenEndpoint()
	{
		return "https://github.com/login/oauth/access_token";
	}
	
	/**
	 * 인증 API 요청 URL 반환 메서드
	 *
	 * @return [String] 인증 API 요청 URL
	 */
	@Override
	protected String getAuthorizationBaseUrl()
	{
		return "https://github.com/login/oauth/authorize";
	}
	
	/**
	 * 사용자 정보 요청 URL 반환 메서드
	 *
	 * @return [String] 사용자 정보 요청 URL
	 */
	@Override
	protected String getUserInfoEndPoint()
	{
		return "https://api.github.com/user";
	}
}

정리한 전체 코드는 위와 같다.

정리

이로써 모든 플랫폼에 대한 인증 모듈 구현이 끝났다. AuthModule를 활용함으로써 최소한의 코드로 각 플랫폼에 대응하는 모듈을 구현했다. 만약 추후 다른 OAuth를 붙일 경우, 위와 같은 방식으로 모듈을 추가 구성하면 된다.

다음 장에서는 모듈을 호출해서 사용하는 영역인 프로세스를 구현할 예정이다.

🏷️ Related Tag

# JAVA
# OAuth2.0
# scribeJAVA

😍 읽어주셔서 감사합니다!
도움이 되셨다면, 💝공감이나 🗨️댓글을 달아주시는 건 어떤가요?
블로그 운영에 큰 힘이 됩니다!
https://blog.itcode.dev/posts/2021/10/23/oauth2-java-server-7