Skip to content

Commit

Permalink
fix: Improve GitHub token validation check (#327) (#328)
Browse files Browse the repository at this point in the history
Backport from main, see #327
  • Loading branch information
vinokurig authored Jul 14, 2022
1 parent 7ef4966 commit 37d7d98
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,27 +200,22 @@ public Optional<Boolean> isValid(PersonalAccessToken personalAccessToken)
return Optional.empty();
}

if (personalAccessToken.getScmTokenName() != null
&& personalAccessToken.getScmTokenName().startsWith(OAUTH_2_PREFIX)) {
try {
try {
if (personalAccessToken.getScmTokenName() != null
&& personalAccessToken.getScmTokenName().startsWith(OAUTH_2_PREFIX)) {
String[] scopes = githubApiClient.getTokenScopes(personalAccessToken.getToken());
return Optional.of(containsScopes(scopes, DEFAULT_TOKEN_SCOPES));
} catch (ScmItemNotFoundException | ScmCommunicationException | ScmBadRequestException e) {
LOG.error(e.getMessage(), e);
throw new ScmCommunicationException(e.getMessage(), e);
}
} else {
// No REST API for PAT-s in Github found yet. Just try to do some action.
try {
} else {
// No REST API for PAT-s in Github found yet. Just try to do some action.
GithubUser user = githubApiClient.getUser(personalAccessToken.getToken());
if (personalAccessToken.getScmUserId().equals(Long.toString(user.getId()))) {
return Optional.of(Boolean.TRUE);
} else {
return Optional.of(Boolean.FALSE);
}
} catch (ScmItemNotFoundException | ScmCommunicationException | ScmBadRequestException e) {
return Optional.of(Boolean.FALSE);
}
} catch (ScmItemNotFoundException | ScmCommunicationException | ScmBadRequestException e) {
return Optional.of(Boolean.FALSE);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2022 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand All @@ -17,6 +17,8 @@
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static java.net.HttpURLConnection.HTTP_FORBIDDEN;
import static org.eclipse.che.api.factory.server.scm.PersonalAccessTokenFetcher.OAUTH_2_PREFIX;
import static org.eclipse.che.dto.server.DtoFactory.newDto;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -204,4 +206,45 @@ public void shouldValidatePersonalToken() throws Exception {

assertTrue(githubPATFetcher.isValid(token).get());
}

@Test
public void shouldValidateOauthToken() throws Exception {
stubFor(
get(urlEqualTo("/user"))
.withHeader(HttpHeaders.AUTHORIZATION, equalTo("token " + githubOauthToken))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/json; charset=utf-8")
.withHeader(GithubApiClient.GITHUB_OAUTH_SCOPES_HEADER, "repo")
.withBodyFile("github/rest/user/response.json")));

PersonalAccessToken token =
new PersonalAccessToken(
"https://github.com",
"cheUser",
"username",
"123456789",
OAUTH_2_PREFIX + "-token-name",
"tid-23434",
githubOauthToken);

assertTrue(githubPATFetcher.isValid(token).get());
}

@Test
public void shouldNotValidateExpiredOauthToken() throws Exception {
stubFor(get(urlEqualTo("/user")).willReturn(aResponse().withStatus(HTTP_FORBIDDEN)));

PersonalAccessToken token =
new PersonalAccessToken(
"https://github.com",
"cheUser",
"username",
"123456789",
OAUTH_2_PREFIX + "-token-name",
"tid-23434",
githubOauthToken);

assertFalse(githubPATFetcher.isValid(token).get());
}
}

0 comments on commit 37d7d98

Please sign in to comment.