/**
   * Use this method to create a ServerContext from a remote git url. Note that this will require
   * server calls and should be done on a background thread.
   *
   * @param gitRemoteUrl
   * @return
   */
  public ServerContext createContextFromRemoteUrl(final String gitRemoteUrl) {
    assert !StringUtils.isEmpty(gitRemoteUrl);

    try {
      // Get matching context from manager
      ServerContext context = getActiveContext();
      if (context == ServerContext.NO_CONTEXT
          || context.getGitRepository() == null
          || !StringUtils.equalsIgnoreCase(
              context.getGitRepository().getRemoteUrl(), gitRemoteUrl)) {
        context = null;
      }

      if (context == null) {
        // Manager didn't have a matching context, so create one
        final AuthenticationProvider authenticationProvider =
            getAuthenticationProvider(gitRemoteUrl);
        final AuthenticationInfo authenticationInfo =
            AuthHelper.getAuthenticationInfoSynchronously(authenticationProvider, gitRemoteUrl);
        if (authenticationInfo != null) {
          // Create a new context object and store it back in the manager
          context = createServerContext(gitRemoteUrl, authenticationInfo);
        }
      }

      return context;
    } catch (Throwable ex) {
      throw new RuntimeException(ex.getLocalizedMessage(), ex);
    }
  }
Ejemplo n.º 2
0
 public synchronized HttpClient getHttpClient() {
   checkDisposed();
   if (httpClient == null && authenticationInfo != null) {
     final Credentials credentials = AuthHelper.getCredentials(type, authenticationInfo);
     final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
     credentialsProvider.setCredentials(AuthScope.ANY, credentials);
     httpClient =
         HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
   }
   return httpClient;
 }
  /**
   * This method takes a VSO_DEPLOYMENT context and returns the VSO context that has the correct PAT
   *
   * @param originalContext the VSO_DEPLOYMENT context to use to generate the PAT
   * @param authenticationProvider the provider used to create the original context
   * @param tokenDescription the description to use for the generated PAT
   * @return a new VSO context object
   */
  public ServerContext createVsoContext(
      ServerContext originalContext,
      VsoAuthenticationProvider authenticationProvider,
      String tokenDescription) {
    // If the context is a VSO_DEPLOYMENT, then we can generate the PAT and create a new context
    // Otherwise, throw an exception
    if (originalContext == null || originalContext.getType() != ServerContext.Type.VSO_DEPLOYMENT) {
      throw new IllegalArgumentException("originalContext must be a VSO_DEPLOYMENT context");
    }
    if (authenticationProvider == null) {
      throw new IllegalArgumentException("authenticationProvider must be set");
    }

    // generate PAT
    final AuthenticationResult result = authenticationProvider.getAuthenticationResult();
    final PersonalAccessTokenFactory patFactory = new PersonalAccessTokenFactoryImpl(result);

    final String accountName = UrlHelper.getVSOAccountName(originalContext.getUri());
    final Account account = AccountLookupOperation.getAccount(result, accountName);

    if (account != null) {
      // TODO: handle case where session token cannot be created
      SessionToken sessionToken =
          patFactory.createSessionToken(
              tokenDescription,
              Arrays.asList(TokenScope.CODE_READ, TokenScope.CODE_WRITE, TokenScope.CODE_MANAGE),
              account.getAccountId());
      // create a VSO context with session token (remove the original client and allow that to be
      // recreated)
      final AuthenticationInfo finalAuthenticationInfo =
          AuthHelper.createAuthenticationInfo(
              originalContext.getUri().toString(), result, sessionToken);
      final ServerContext newContext =
          new ServerContextBuilder(originalContext)
              .type(ServerContext.Type.VSO)
              .authentication(finalAuthenticationInfo)
              .buildWithClient(null);
      return newContext;
    }

    logger.debug("Account not found: " + accountName);
    return null;
  }
Ejemplo n.º 4
0
  protected static ClientConfig getClientConfig(
      final Type type,
      final AuthenticationInfo authenticationInfo,
      final boolean includeProxySettings) {
    final Credentials credentials = AuthHelper.getCredentials(type, authenticationInfo);

    final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, credentials);

    final ConnectorProvider connectorProvider = new ApacheConnectorProvider();

    final ClientConfig clientConfig = new ClientConfig().connectorProvider(connectorProvider);
    clientConfig.property(ApacheClientProperties.CREDENTIALS_PROVIDER, credentialsProvider);

    clientConfig.property(ApacheClientProperties.PREEMPTIVE_BASIC_AUTHENTICATION, true);
    clientConfig.property(
        ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED);

    // Define fiddler as a local HTTP proxy
    if (includeProxySettings) {
      final String proxyHost;
      if (System.getProperty("proxyHost") != null) {
        proxyHost = System.getProperty("proxyHost");
      } else {
        proxyHost = "127.0.0.1";
      }

      final String proxyPort;
      if (System.getProperty("proxyPort") != null) {
        proxyPort = System.getProperty("proxyPort");
      } else {
        proxyPort = "8888";
      }

      final String proxyUrl = String.format("http://%s:%s", proxyHost, proxyPort);

      clientConfig.property(ClientProperties.PROXY_URI, proxyUrl);
      clientConfig.property(ApacheClientProperties.SSL_CONFIG, getSslConfigurator());
    }
    return clientConfig;
  }