Search in sources :

Example 6 with BitbucketPersonalAccessToken

use of org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken in project devspaces-images by redhat-developer.

the class BitbucketServerPersonalAccessTokenFetcher method fetchPersonalAccessToken.

@Override
public PersonalAccessToken fetchPersonalAccessToken(Subject cheUser, String scmServerUrl) throws ScmUnauthorizedException, ScmCommunicationException {
    if (!bitbucketServerApiClient.isConnected(scmServerUrl)) {
        LOG.debug("not a  valid url {} for current fetcher ", scmServerUrl);
        return null;
    }
    final String tokenName = format(TOKEN_NAME_TEMPLATE, cheUser.getUserId(), apiEndpoint.getHost());
    try {
        BitbucketUser user = bitbucketServerApiClient.getUser(EnvironmentContext.getCurrent().getSubject());
        LOG.debug("Current bitbucket user {} ", user);
        // cleanup existed
        List<BitbucketPersonalAccessToken> existingTokens = bitbucketServerApiClient.getPersonalAccessTokens(user.getSlug()).stream().filter(p -> p.getName().equals(tokenName)).collect(Collectors.toList());
        for (BitbucketPersonalAccessToken existedToken : existingTokens) {
            LOG.debug("Deleting existed che token {} {}", existedToken.getId(), existedToken.getName());
            bitbucketServerApiClient.deletePersonalAccessTokens(user.getSlug(), existedToken.getId());
        }
        BitbucketPersonalAccessToken token = bitbucketServerApiClient.createPersonalAccessTokens(user.getSlug(), tokenName, DEFAULT_TOKEN_SCOPE);
        LOG.debug("Token created = {} for {}", token.getId(), token.getUser());
        return new PersonalAccessToken(scmServerUrl, EnvironmentContext.getCurrent().getSubject().getUserId(), user.getName(), valueOf(user.getId()), token.getName(), valueOf(token.getId()), token.getToken());
    } catch (ScmBadRequestException | ScmItemNotFoundException e) {
        throw new ScmCommunicationException(e.getMessage(), e);
    }
}
Also used : ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) URL(java.net.URL) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) BitbucketServerApiClient(org.eclipse.che.api.factory.server.bitbucket.server.BitbucketServerApiClient) PersonalAccessTokenFetcher(org.eclipse.che.api.factory.server.scm.PersonalAccessTokenFetcher) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) Inject(javax.inject.Inject) List(java.util.List) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) String.valueOf(java.lang.String.valueOf) BitbucketUser(org.eclipse.che.api.factory.server.bitbucket.server.BitbucketUser) PersonalAccessToken(org.eclipse.che.api.factory.server.scm.PersonalAccessToken) Subject(org.eclipse.che.commons.subject.Subject) Optional(java.util.Optional) Named(javax.inject.Named) BitbucketPersonalAccessToken(org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) PersonalAccessToken(org.eclipse.che.api.factory.server.scm.PersonalAccessToken) BitbucketPersonalAccessToken(org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) BitbucketPersonalAccessToken(org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken) BitbucketUser(org.eclipse.che.api.factory.server.bitbucket.server.BitbucketUser) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException)

Example 7 with BitbucketPersonalAccessToken

use of org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken in project devspaces-images by redhat-developer.

the class BitbucketServerPersonalAccessTokenFetcherTest method setup.

@BeforeMethod
public void setup() throws MalformedURLException {
    URL apiEndpoint = new URL("https://che.server.com");
    subject = new SubjectImpl("another_user", "user987", "token111", false);
    bitbucketUser = new BitbucketUser("User", "user", 32423523, "NORMAL", true, "user", "user@users.com");
    bitbucketPersonalAccessToken = new BitbucketPersonalAccessToken(234234, 234345345, 23534534, "che-token-<user987>-<che.server.com>", "2340590skdf3<0>945i0923i4jasoidfj934ui50", bitbucketUser, ImmutableSet.of("PROJECT_WRITE", "REPO_WRITE"));
    bitbucketPersonalAccessToken2 = new BitbucketPersonalAccessToken(3647456, 234345345, 23534534, "che-token-<user987>-<che.server.com>", "34545<0>945i0923i4jasoidfj934ui50", bitbucketUser, ImmutableSet.of("REPO_READ"));
    bitbucketPersonalAccessToken3 = new BitbucketPersonalAccessToken(132423, 234345345, 23534534, "che-token-<user987>-<che.server.com>", "3456\\<0>945//i0923i4jasoidfj934ui50", bitbucketUser, ImmutableSet.of("PROJECT_READ", "REPO_READ"));
    fetcher = new BitbucketServerPersonalAccessTokenFetcher(bitbucketServerApiClient, apiEndpoint);
    EnvironmentContext context = new EnvironmentContext();
    context.setSubject(subject);
    EnvironmentContext.setCurrent(context);
}
Also used : EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) BitbucketPersonalAccessToken(org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken) SubjectImpl(org.eclipse.che.commons.subject.SubjectImpl) URL(java.net.URL) BitbucketUser(org.eclipse.che.api.factory.server.bitbucket.server.BitbucketUser) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 8 with BitbucketPersonalAccessToken

use of org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken 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 9 with BitbucketPersonalAccessToken

use of org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken in project che-server by eclipse-che.

the class HttpBitbucketServerApiClientTest method shouldBeAbleToGetExistedPAT.

@Test
public void shouldBeAbleToGetExistedPAT() throws ScmCommunicationException, ScmUnauthorizedException, ScmItemNotFoundException {
    // given
    stubFor(get(urlPathEqualTo("/rest/access-tokens/1.0/users/ksmster/5")).withHeader(HttpHeaders.AUTHORIZATION, equalTo(AUTHORIZATION_TOKEN)).withHeader(HttpHeaders.ACCEPT, equalTo(MediaType.APPLICATION_JSON)).willReturn(ok().withBodyFile("bitbucket/rest/access-tokens/1.0/users/ksmster/newtoken.json")));
    // when
    BitbucketPersonalAccessToken result = bitbucketServer.getPersonalAccessToken("ksmster", 5L);
    // then
    assertNotNull(result);
    assertEquals(result.getToken(), "MTU4OTEwNTMyOTA5Ohc88HcY8k7gWOzl2mP5TtdtY5Qs");
}
Also used : BitbucketPersonalAccessToken(org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken) Test(org.testng.annotations.Test)

Example 10 with BitbucketPersonalAccessToken

use of org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken in project devspaces-images by redhat-developer.

the class BitbucketServerPersonalAccessTokenFetcherTest method shouldDeleteExistedCheTokenBeforeCreatingNew.

@Test
public void shouldDeleteExistedCheTokenBeforeCreatingNew() throws ScmUnauthorizedException, ScmCommunicationException, ScmItemNotFoundException, ScmBadRequestException {
    when(bitbucketServerApiClient.isConnected(eq(someBitbucketURL))).thenReturn(true);
    when(bitbucketServerApiClient.getUser(eq(subject))).thenReturn(bitbucketUser);
    when(bitbucketServerApiClient.getPersonalAccessTokens(eq(bitbucketUser.getSlug()))).thenReturn(ImmutableList.of(bitbucketPersonalAccessToken, bitbucketPersonalAccessToken2));
    when(bitbucketServerApiClient.createPersonalAccessTokens(eq(bitbucketUser.getSlug()), eq("che-token-<user987>-<che.server.com>"), eq(ImmutableSet.of("PROJECT_WRITE", "REPO_WRITE")))).thenReturn(bitbucketPersonalAccessToken3);
    // when
    PersonalAccessToken result = fetcher.fetchPersonalAccessToken(subject, someBitbucketURL);
    // then
    assertNotNull(result);
    verify(bitbucketServerApiClient).deletePersonalAccessTokens(eq(bitbucketUser.getSlug()), eq(bitbucketPersonalAccessToken.getId()));
    verify(bitbucketServerApiClient).deletePersonalAccessTokens(eq(bitbucketUser.getSlug()), eq(bitbucketPersonalAccessToken2.getId()));
}
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)

Aggregations

BitbucketPersonalAccessToken (org.eclipse.che.api.factory.server.bitbucket.server.BitbucketPersonalAccessToken)12 Test (org.testng.annotations.Test)8 PersonalAccessToken (org.eclipse.che.api.factory.server.scm.PersonalAccessToken)6 URL (java.net.URL)4 BitbucketUser (org.eclipse.che.api.factory.server.bitbucket.server.BitbucketUser)4 EnvironmentContext (org.eclipse.che.commons.env.EnvironmentContext)4 ImmutableSet (com.google.common.collect.ImmutableSet)2 String.format (java.lang.String.format)2 String.valueOf (java.lang.String.valueOf)2 List (java.util.List)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 Named (javax.inject.Named)2 BitbucketServerApiClient (org.eclipse.che.api.factory.server.bitbucket.server.BitbucketServerApiClient)2 PersonalAccessTokenFetcher (org.eclipse.che.api.factory.server.scm.PersonalAccessTokenFetcher)2 ScmBadRequestException (org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException)2 ScmCommunicationException (org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException)2 ScmItemNotFoundException (org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException)2