-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGitHubOAuthTest.java
More file actions
358 lines (301 loc) · 18.4 KB
/
Copy pathGitHubOAuthTest.java
File metadata and controls
358 lines (301 loc) · 18.4 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* Copyright 2000-2020 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.teamcity.githubauth;
import com.intellij.openapi.diagnostic.Log4jLogger;
import jetbrains.buildServer.ExtensionHolder;
import jetbrains.buildServer.TestInternalProperties;
import jetbrains.buildServer.controllers.interceptors.auth.HttpAuthenticationResult;
import jetbrains.buildServer.controllers.interceptors.auth.util.HttpAuthUtil;
import jetbrains.buildServer.serverSide.SProject;
import jetbrains.buildServer.serverSide.SProjectFeatureDescriptor;
import jetbrains.buildServer.serverSide.oauth.OAuthConnectionDescriptor;
import jetbrains.buildServer.serverSide.oauth.github.GitHubConstants;
import jetbrains.buildServer.users.DuplicateUserAccountException;
import jetbrains.buildServer.users.SUser;
import jetbrains.buildServer.users.UserSet;
import jetbrains.buildServer.util.StringUtil;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.jetbrains.annotations.NotNull;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;
import static java.util.Collections.*;
import static java.util.stream.Collectors.toMap;
import static org.assertj.core.api.BDDAssertions.then;
import static org.jetbrains.teamcity.githubauth.GitHubOAuth.*;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.eq;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
@Test
public class GitHubOAuthTest {
private static final String CLIENT_ID = "321";
private static final String CLIENT_SECRET = "123";
private static final String TC_URL = "http://teamcity.com";
private GitHubOAuth gitHubOAuth;
private TeamCityCoreFacade teamCityCoreMock;
private OAuthConnectionDescriptor rootProjectConnection;
private SUser tcUser;
private MockHttpSession session;
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private MockRestServiceServer server;
@BeforeMethod
public void setUp() throws Exception {
TestInternalProperties.init();
RestTemplate restTemplate = new RestTemplate();
server = MockRestServiceServer.bindTo(restTemplate).build();
GitHubOAuthClient gitHubClient = new GitHubOAuthClient(restTemplate);
teamCityCoreMock = mock(TeamCityCoreFacade.class);
gitHubOAuth = new GitHubOAuth(gitHubClient, teamCityCoreMock);
Logger logger = Logger.getLogger("oauth");
logger.setLevel(Level.DEBUG);
logger.addAppender(new ConsoleAppender(new SimpleLayout()));
gitHubOAuth.setLogger(new Log4jLogger(logger));
tcUser = mock(SUser.class);
when(tcUser.describe(anyBoolean())).thenReturn("TeamCity User");
rootProjectConnection = new OAuthConnectionDescriptor(mock(SProject.class), mock(SProjectFeatureDescriptor.class), mock(ExtensionHolder.class));
Map<String, String> connectionParams = new HashMap<>();
connectionParams.put(GitHubConstants.CLIENT_ID_PARAM, CLIENT_ID);
connectionParams.put(GitHubConstants.CLIENT_SECRET_PARAM, CLIENT_SECRET);
when(rootProjectConnection.getParameters()).thenReturn(connectionParams);
when(teamCityCoreMock.getRootProjectGitHubConnection()).thenReturn(rootProjectConnection);
when(teamCityCoreMock.getRootUrl()).thenReturn(TC_URL);
when(teamCityCoreMock.isAuthModuleConfigured(GitHubOAuth.class)).thenReturn(true);
session = new MockHttpSession();
newRequest();
}
private void setupGitHubApiAnswers(String code, String token, String tokenScope, String userJson) {
server.reset();
MultiValueMap<String, String> expectedTokenBody = createTokenRequestBody(CLIENT_ID, CLIENT_SECRET, code);
server.expect(requestTo("https://github.com/login/oauth/access_token")).andExpect(method(POST))
.andExpect(content().formData(expectedTokenBody))
.andRespond(withSuccess(createTokenJson(token, tokenScope), APPLICATION_JSON));
server.expect(requestTo("https://api.github.com/user?access_token=" + token)).andExpect(method(GET))
.andRespond(withSuccess(userJson, APPLICATION_JSON));
}
private void setupGitHubApiErrorTokenAnswers(String code, String clientId, String clientSecret) {
server.reset();
MultiValueMap<String, String> expectedTokenBody = createTokenRequestBody(clientId, clientSecret, code);
server.expect(requestTo("https://github.com/login/oauth/access_token")).andExpect(method(POST))
.andExpect(content().formData(expectedTokenBody))
.andRespond(withSuccess(createErrorTokenJson(), APPLICATION_JSON));
}
@NotNull
private MultiValueMap<String, String> createTokenRequestBody(String clientId, String clientSecret, String code) {
MultiValueMap<String, String> expectedTokenBody = new LinkedMultiValueMap<>();
expectedTokenBody.put("client_id", singletonList(clientId));
expectedTokenBody.put("client_secret", singletonList(clientSecret));
expectedTokenBody.put("code", singletonList(code));
expectedTokenBody.put("redirect_uri", singletonList(TC_URL + TOKEN_REDIRECT_URL));
return expectedTokenBody;
}
private void newRequest() {
request = new MockHttpServletRequest();
request.setSession(session);
response = new MockHttpServletResponse();
}
@Test
public void should_not_accept__foreign_requests() throws IOException {
emulateFirstOAuthStep();
newRequest();
request.setRequestURI("/foreignPath.html");
HttpAuthenticationResult result = gitHubOAuth.processAuthenticationRequest(request, response, emptyMap());
then(result.getType()).isEqualTo(HttpAuthenticationResult.Type.NOT_APPLICABLE);
}
@Test
public void successful_login__new_user_created() throws Exception {
String code = emulateFirstOAuthStep();
setupGitHubApiAnswers(code, "token1", DEFAULT_SCOPE, createUserJson("1", "octocat", "monalisa octocat", "octocat@github.com"));
when(teamCityCoreMock.findUserByPropertyValue(GITHUB_USER_ID_PROPERTY_KEY, "1")).thenReturn(UserSet.EMPTY);
when(teamCityCoreMock.createUser("octocat", "octocat@github.com", "monalisa octocat", singletonMap(GITHUB_USER_ID_PROPERTY_KEY, "1"))).thenReturn(tcUser);
HttpAuthenticationResult result = gitHubOAuth.processAuthenticationRequest(request, response, emptyMap());
then(result.getType()).isEqualTo(HttpAuthenticationResult.Type.AUTHENTICATED);
then(result.getPrincipal().getName()).isEqualTo("octocat");
verify(teamCityCoreMock).rememberToken(rootProjectConnection, tcUser, "octocat", "token1", DEFAULT_SCOPE);
}
@Test
public void successful_login__new_user_created_with_empty_email() throws Exception {
String code = emulateFirstOAuthStep();
setupGitHubApiAnswers(code, "token1", DEFAULT_SCOPE, createUserJson("1", "octocat", "monalisa octocat", null));
when(teamCityCoreMock.findUserByPropertyValue(GITHUB_USER_ID_PROPERTY_KEY, "1")).thenReturn(UserSet.EMPTY);
when(teamCityCoreMock.createUser("octocat", null, "monalisa octocat", singletonMap(GITHUB_USER_ID_PROPERTY_KEY, "1"))).thenReturn(tcUser);
HttpAuthenticationResult result = gitHubOAuth.processAuthenticationRequest(request, response, emptyMap());
then(result.getType()).isEqualTo(HttpAuthenticationResult.Type.AUTHENTICATED);
}
@Test
public void successful_login__user_exists() throws Exception {
String code = emulateFirstOAuthStep();
setupGitHubApiAnswers(code, "token1", DEFAULT_SCOPE, createUserJson("1", "octocat", "monalisa octocat", "octocat@github.com"));
when(teamCityCoreMock.findUserByPropertyValue(GITHUB_USER_ID_PROPERTY_KEY, "1")).thenReturn(() -> singleton(tcUser));
when(tcUser.getUsername()).thenReturn("octocat");
HttpAuthenticationResult result = gitHubOAuth.processAuthenticationRequest(request, response, emptyMap());
then(result.getType()).isEqualTo(HttpAuthenticationResult.Type.AUTHENTICATED);
then(result.getPrincipal().getName()).isEqualTo("octocat");
verify(teamCityCoreMock).rememberToken(rootProjectConnection, tcUser, "octocat", "token1", DEFAULT_SCOPE);
verify(teamCityCoreMock, never()).createUser(anyString(), anyString(), anyString(), anyMap());
}
@Test
public void failed_login__username_exists_and_correspond_to_different_user() throws Exception {
String code = emulateFirstOAuthStep();
setupGitHubApiAnswers(code, "token1", DEFAULT_SCOPE, createUserJson("1", "octocat", "monalisa octocat", "octocat@github.com"));
when(teamCityCoreMock.findUserByPropertyValue(GITHUB_USER_ID_PROPERTY_KEY, "1")).thenReturn(UserSet.EMPTY);
when(teamCityCoreMock.createUser(eq("octocat"), anyString(), anyString(), anyMap())).thenThrow(new DuplicateUserAccountException("octocat"));
HttpAuthenticationResult result = gitHubOAuth.processAuthenticationRequest(request, response, emptyMap());
then(result.getType()).isEqualTo(HttpAuthenticationResult.Type.UNAUTHENTICATED);
then(HttpAuthUtil.getUnauthenticatedReason(request)).isEqualTo("User with username 'octocat' already exist");
verify(teamCityCoreMock, never()).rememberToken(any(), any(), anyString(), anyString(), anyString());
}
@Test
public void should_not_accept_incorrect_state_param() throws IOException {
emulateFirstOAuthStep();
request.setParameter("state", "incorrect_state");
HttpAuthenticationResult result = gitHubOAuth.processAuthenticationRequest(request, response, emptyMap());
then(result.getType()).isEqualTo(HttpAuthenticationResult.Type.UNAUTHENTICATED);
then(HttpAuthUtil.getUnauthenticatedReason(request)).isEqualTo("GitHub login error: 'state' parameter is invalid");
then(response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
}
@Test
public void should_not_accept_missing_state_param() throws IOException {
emulateFirstOAuthStep();
request.removeParameter("state");
HttpAuthenticationResult result = gitHubOAuth.processAuthenticationRequest(request, response, emptyMap());
then(result.getType()).isEqualTo(HttpAuthenticationResult.Type.UNAUTHENTICATED);
then(HttpAuthUtil.getUnauthenticatedReason(request)).isEqualTo("GitHub login error: 'state' parameter is empty");
then(response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
}
@Test
public void should_not_accept_missing_code_param() throws IOException {
emulateFirstOAuthStep();
request.removeParameter("code");
HttpAuthenticationResult result = gitHubOAuth.processAuthenticationRequest(request, response, emptyMap());
then(result.getType()).isEqualTo(HttpAuthenticationResult.Type.UNAUTHENTICATED);
then(HttpAuthUtil.getUnauthenticatedReason(request)).isEqualTo("GitHub login error: 'code' parameter is empty");
then(response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
}
@Test
public void should_handle_error_when_user_redirected_back() throws IOException {
emulateFirstOAuthStep();
request.removeParameter("code");
request.addParameter("error", "application_suspended");
request.addParameter("error_description", "Your+application+has+been+suspended.+Contact+support@github.com.");
HttpAuthenticationResult result = gitHubOAuth.processAuthenticationRequest(request, response, emptyMap());
then(result.getType()).isEqualTo(HttpAuthenticationResult.Type.UNAUTHENTICATED);
then(HttpAuthUtil.getUnauthenticatedReason(request)).isEqualTo("GitHub login error: user was redirected with 'error' param.");
then(response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
}
@Test
public void should_handle_error_when_token_request_failed___invalid_credentials() throws IOException {
String code = emulateFirstOAuthStep();
rootProjectConnection.getParameters().put(GitHubConstants.CLIENT_SECRET_PARAM, "incorrectSecret");
setupGitHubApiErrorTokenAnswers(code, CLIENT_ID, "incorrectSecret");
HttpAuthenticationResult result = gitHubOAuth.processAuthenticationRequest(request, response, emptyMap());
then(result.getType()).isEqualTo(HttpAuthenticationResult.Type.UNAUTHENTICATED);
then(HttpAuthUtil.getUnauthenticatedReason(request)).isEqualTo("GitHub login error: The client_id and/or client_secret passed are incorrect.");
}
private String emulateFirstOAuthStep() {
String redirect = gitHubOAuth.getUserRedirect(request);
Map<String, String> params = verifyRedirectUrlAndFetchQueryParams(redirect);
String code = StringUtil.generateUniqueHash();
newRequest();
request.addParameter("code", code);
request.addParameter("state", params.get("state"));
request.setRequestURI(params.get("redirect_uri"));
return code;
}
@NotNull
private Map<String, String> verifyRedirectUrlAndFetchQueryParams(String redirect) {
then(redirect).isNotNull();
String queryString = redirect.substring(redirect.indexOf("?") + 1);
Map<String, String> parsedQueryString = Stream.of(queryString.split("&")).collect(toMap(param -> param.substring(0, param.indexOf("=")),
param -> param.substring(param.indexOf("=") + 1)));
then(parsedQueryString).containsEntry("client_id", CLIENT_ID).containsEntry("redirect_uri", TC_URL + TOKEN_REDIRECT_URL).containsKey("state");
return parsedQueryString;
}
private String createTokenJson(String token, String scope) {
return "{\"access_token\":\"" + token + "\", \"scope\":\"" + scope + "\", \"token_type\":\"bearer\"}";
}
private String createErrorTokenJson() {
return "{" +
" \"error\": \"incorrect_client_credentials\",\n" +
" \"error_description\": \"The client_id and/or client_secret passed are incorrect.\",\n" +
" \"error_uri\": \"https://developer.github.com/v3/oauth/#incorrect-client-credentials\"" +
"}";
}
private String createUserJson(String id, String login, String name, String email) {
return
"{\n" +
" \"login\": \"" + login + "\",\n" +
" \"id\": " + id + ",\n" +
" \"avatar_url\": \"https://github.com/images/error/octocat_happy.gif\",\n" +
" \"gravatar_id\": \"\",\n" +
" \"url\": \"https://api.github.com/users/octocat\",\n" +
" \"html_url\": \"https://github.com/octocat\",\n" +
" \"followers_url\": \"https://api.github.com/users/octocat/followers\",\n" +
" \"following_url\": \"https://api.github.com/users/octocat/following{/other_user}\",\n" +
" \"gists_url\": \"https://api.github.com/users/octocat/gists{/gist_id}\",\n" +
" \"starred_url\": \"https://api.github.com/users/octocat/starred{/owner}{/repo}\",\n" +
" \"subscriptions_url\": \"https://api.github.com/users/octocat/subscriptions\",\n" +
" \"organizations_url\": \"https://api.github.com/users/octocat/orgs\",\n" +
" \"repos_url\": \"https://api.github.com/users/octocat/repos\",\n" +
" \"events_url\": \"https://api.github.com/users/octocat/events{/privacy}\",\n" +
" \"received_events_url\": \"https://api.github.com/users/octocat/received_events\",\n" +
" \"type\": \"User\",\n" +
" \"site_admin\": false,\n" +
" \"name\": \"" + name + "\",\n" +
" \"company\": \"GitHub\",\n" +
" \"blog\": \"https://github.com/blog\",\n" +
" \"location\": \"San Francisco\",\n" +
" \"email\": " + (email != null ? "\"" + email + "\"" : "null") + ",\n" +
" \"hireable\": false,\n" +
" \"bio\": \"There once was...\",\n" +
" \"public_repos\": 2,\n" +
" \"public_gists\": 1,\n" +
" \"followers\": 20,\n" +
" \"following\": 0,\n" +
" \"created_at\": \"2008-01-14T04:33:35Z\",\n" +
" \"updated_at\": \"2008-01-14T04:33:35Z\",\n" +
" \"total_private_repos\": 100,\n" +
" \"owned_private_repos\": 100,\n" +
" \"private_gists\": 81,\n" +
" \"disk_usage\": 10000,\n" +
" \"collaborators\": 8,\n" +
" \"plan\": {\n" +
" \"name\": \"Medium\",\n" +
" \"space\": 400,\n" +
" \"private_repos\": 20,\n" +
" \"collaborators\": 0\n" +
" }\n" +
"}";
}
}