Search in sources :

Example 31 with PersonalAccessToken

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"));
}
Also used : PersonalAccessToken(org.eclipse.che.api.factory.server.scm.PersonalAccessToken) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Subject(org.eclipse.che.commons.subject.Subject) Test(org.testng.annotations.Test)

Example 32 with PersonalAccessToken

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);
}
Also used : PersonalAccessToken(org.eclipse.che.api.factory.server.scm.PersonalAccessToken) BitbucketPersonalAccessToken(org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken) Test(org.testng.annotations.Test)

Example 33 with PersonalAccessToken

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);
    }
}
Also used : ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException) OAuthToken(org.eclipse.che.api.auth.shared.dto.OAuthToken) PersonalAccessToken(org.eclipse.che.api.factory.server.scm.PersonalAccessToken) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException) BadRequestException(org.eclipse.che.api.core.BadRequestException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException)

Example 34 with PersonalAccessToken

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());
}
Also used : PersonalAccessToken(org.eclipse.che.api.factory.server.scm.PersonalAccessToken) Test(org.testng.annotations.Test)

Example 35 with PersonalAccessToken

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;
}
Also used : PersonalAccessToken(org.eclipse.che.api.factory.server.scm.PersonalAccessToken)

Aggregations

PersonalAccessToken (org.eclipse.che.api.factory.server.scm.PersonalAccessToken)54 Test (org.testng.annotations.Test)42 Secret (io.fabric8.kubernetes.api.model.Secret)20 KubernetesNamespaceMeta (org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta)20 Subject (org.eclipse.che.commons.subject.Subject)16 KubernetesNamespaceMetaImpl (org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl)16 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)12 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)12 SecretBuilder (io.fabric8.kubernetes.api.model.SecretBuilder)12 SubjectImpl (org.eclipse.che.commons.subject.SubjectImpl)12 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)8 OAuthToken (org.eclipse.che.api.auth.shared.dto.OAuthToken)8 BitbucketPersonalAccessToken (org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken)8 KubernetesNamespace (org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace)8 KubernetesSecrets (org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesSecrets)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)8 Optional (java.util.Optional)6 ScmBadRequestException (org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException)6 ScmCommunicationException (org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException)6 ScmItemNotFoundException (org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException)6