use of org.eclipse.che.api.factory.server.scm.PersonalAccessToken in project devspaces-images by redhat-developer.
the class BitbucketServerAuthorizingFileContentProviderTest method shouldFetchContentWithTokenIfPresent.
@Test
public void shouldFetchContentWithTokenIfPresent() throws Exception {
BitbucketUrl url = new BitbucketUrl().withHostName(TEST_HOSTNAME);
BitbucketServerAuthorizingFileContentProvider fileContentProvider = new BitbucketServerAuthorizingFileContentProvider(url, urlFetcher, gitCredentialManager, personalAccessTokenManager);
PersonalAccessToken token = new PersonalAccessToken(TEST_HOSTNAME, "user1", "token");
when(personalAccessTokenManager.get(any(Subject.class), anyString())).thenReturn(Optional.of(token));
String fileURL = "https://foo.bar/scm/repo/.devfile";
// when
fileContentProvider.fetchContent(fileURL);
// then
verify(urlFetcher).fetch(eq(fileURL), eq("Bearer token"));
}
use of org.eclipse.che.api.factory.server.scm.PersonalAccessToken in project devspaces-images by redhat-developer.
the class BitbucketServerPersonalAccessTokenFetcherTest method shouldBeAbleToFetchPersonalAccessToken.
@Test
public void shouldBeAbleToFetchPersonalAccessToken() throws ScmUnauthorizedException, ScmCommunicationException, ScmItemNotFoundException, ScmBadRequestException {
// given
when(bitbucketServerApiClient.isConnected(eq(someBitbucketURL))).thenReturn(true);
when(bitbucketServerApiClient.getUser(eq(subject))).thenReturn(bitbucketUser);
when(bitbucketServerApiClient.getPersonalAccessTokens(eq(bitbucketUser.getSlug()))).thenReturn(Collections.emptyList());
when(bitbucketServerApiClient.createPersonalAccessTokens(eq(bitbucketUser.getSlug()), eq("che-token-<user987>-<che.server.com>"), eq(ImmutableSet.of("PROJECT_WRITE", "REPO_WRITE")))).thenReturn(bitbucketPersonalAccessToken);
// when
PersonalAccessToken result = fetcher.fetchPersonalAccessToken(subject, someBitbucketURL);
// then
assertNotNull(result);
}
use of org.eclipse.che.api.factory.server.scm.PersonalAccessToken in project devspaces-images by redhat-developer.
the class GithubPersonalAccessTokenFetcher method fetchPersonalAccessToken.
@Override
public PersonalAccessToken fetchPersonalAccessToken(Subject cheSubject, String scmServerUrl) throws ScmUnauthorizedException, ScmCommunicationException {
OAuthToken oAuthToken;
if (githubApiClient == null || !githubApiClient.isConnected(scmServerUrl)) {
LOG.debug("not a valid url {} for current fetcher ", scmServerUrl);
return null;
}
try {
oAuthToken = oAuthAPI.getToken(OAUTH_PROVIDER_NAME);
// Find the user associated to the OAuth token by querying the GitHub API.
GithubUser user = githubApiClient.getUser(oAuthToken.getToken());
PersonalAccessToken token = new PersonalAccessToken(scmServerUrl, cheSubject.getUserId(), user.getLogin(), Long.toString(user.getId()), NameGenerator.generate(OAUTH_2_PREFIX, 5), NameGenerator.generate("id-", 5), oAuthToken.getToken());
Optional<Boolean> valid = isValid(token);
if (valid.isEmpty()) {
throw new ScmCommunicationException("Unable to verify if current token is a valid GitHub token. Token's scm-url needs to be '" + GithubApiClient.GITHUB_SERVER + "' and was '" + token.getScmProviderUrl() + "'");
} else if (!valid.get()) {
throw new ScmCommunicationException("Current token doesn't have the necessary privileges. Please make sure Che app scopes are correct and containing at least: " + DEFAULT_TOKEN_SCOPES.toString());
}
return token;
} catch (UnauthorizedException e) {
throw new ScmUnauthorizedException(cheSubject.getUserName() + " is not authorized in " + OAUTH_PROVIDER_NAME + " OAuth provider.", OAUTH_PROVIDER_NAME, "2.0", getLocalAuthenticateUrl());
} catch (NotFoundException | ServerException | ForbiddenException | BadRequestException | ScmItemNotFoundException | ScmBadRequestException | ConflictException e) {
LOG.error(e.getMessage());
throw new ScmCommunicationException(e.getMessage(), e);
}
}
use of org.eclipse.che.api.factory.server.scm.PersonalAccessToken in project devspaces-images by redhat-developer.
the class GitlabOAuthTokenFetcherTest method shouldValidatePersonalToken.
@Test
public void shouldValidatePersonalToken() throws Exception {
stubFor(get(urlEqualTo("/api/v4/user")).withHeader(HttpHeaders.AUTHORIZATION, equalTo("Bearer token123")).willReturn(aResponse().withHeader("Content-Type", "application/json; charset=utf-8").withBodyFile("gitlab/rest/api/v4/user/response.json")));
PersonalAccessToken token = new PersonalAccessToken(wireMockServer.baseUrl(), "cheUser", "username", "1", "token-name", "tid-23434", "token123");
assertTrue(oAuthTokenFetcher.isValid(token).get());
}
use of org.eclipse.che.api.factory.server.scm.PersonalAccessToken in project che-server by eclipse-che.
the class KubernetesPersonalAccessTokenManager method fetchAndSave.
@Override
public PersonalAccessToken fetchAndSave(Subject cheUser, String scmServerUrl) throws UnsatisfiedScmPreconditionException, ScmConfigurationPersistenceException, ScmUnauthorizedException, ScmCommunicationException, UnknownScmProviderException {
PersonalAccessToken personalAccessToken = scmPersonalAccessTokenFetcher.fetchPersonalAccessToken(cheUser, scmServerUrl);
save(personalAccessToken);
return personalAccessToken;
}
Aggregations