view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/AuthSettings.java @ 979:2b1bd4bca6ad Indore-2017.2.x

TW-50054 support custom clone path whitelist
author Dmitry Neverov <dmitry.neverov@gmail.com>
date Wed, 24 Jan 2018 13:49:01 +0100
parents 38adef4f1b8f
children 6b69c7a12f76
line wrap: on
line source
/*
 * Copyright 2000-2018 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jetbrains.buildServer.buildTriggers.vcs.mercurial.command;

import jetbrains.buildServer.log.Loggers;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.net.*;
import java.util.HashSet;
import java.util.Set;

import static com.intellij.openapi.util.text.StringUtil.isEmpty;
import static java.util.Arrays.asList;

/**
 * @author dmitry.neverov
 */
public class AuthSettings {

  private final static Set<String> AUTH_PROTOS = new HashSet<String>(asList("http", "https", "ssh"));
  private final String myUsername;
  private final String myPassword;

  public AuthSettings() {
    this(null, null);
  }

  public AuthSettings(@Nullable String username, @Nullable String password) {
    myUsername = username;
    myPassword = password;
  }

  public String getUsername() {
    return myUsername;
  }

  public String getPassword() {
    return myPassword;
  }

  public String getRepositoryUrlWithCredentials(@NotNull String repositoryUrl) {
    if (isRequireCredentials(repositoryUrl)) {
      if (containsCredentials(repositoryUrl))
        return repositoryUrl;
      try {
        return createURLWithCredentials(repositoryUrl);
      } catch (MalformedURLException e) {
        Loggers.VCS.warn("Error while parsing url " + repositoryUrl, e);
      }
      return repositoryUrl;
    } else {
      return repositoryUrl;
    }
  }

  public String getRepositoryUrlWithHiddenPassword(@NotNull String repositoryUrl) {
    if (isEmpty(myPassword))
      return repositoryUrl;
    return repositoryUrl.replace(myPassword, "******");
  }

  private boolean isRequireCredentials(@NotNull String repositoryUrl) {
    for (String scheme : AUTH_PROTOS) {
      if (repositoryUrl.startsWith(scheme + ":"))
        return true;
    }
    return false;
  }

  private boolean containsCredentials(final String repositoryUrl) {
    try {
      URL url = new URL(null, repositoryUrl, new FakeStreamHandler());
      String userInfo = url.getUserInfo();
      return userInfo != null && userInfo.contains(":");
    } catch (MalformedURLException e) {
      return false;
    }
  }

  private String createURLWithCredentials(String originalUrl) throws MalformedURLException {
    String userInfo = createUserInfo();
    if (!isEmpty(userInfo)) {
      URL url = new URL(null, originalUrl, new FakeStreamHandler());
      return url.getProtocol() + "://"
              + userInfo + "@"
              + url.getHost()
              + (url.getPort() != -1 ? ":" + url.getPort() : "")
              + url.getFile()
              + (url.getRef() != null ? url.getRef() : "");
    } else {
      return originalUrl;
    }
  }

  private String createUserInfo() {
    String userInfo = "";
    if (!isEmpty(myUsername)) {
      userInfo += myUsername;
      if (!isEmpty(myPassword)) {
        userInfo += ":" + myPassword;
      }
    }
    return getEscapedUserInfo(userInfo);
  }

  private static String getEscapedUserInfo(String userInfo) {
    try {
      URI uri = new URI("http", userInfo, "somewhere.com", 80, "", "", "");
      String escapedURI = uri.toASCIIString();
      int from = "http://".length();
      int to = escapedURI.indexOf("somewhere.com") - 1;
      String escapedUserInfo = escapedURI.substring(from, to);
      escapedUserInfo = escapedUserInfo.replaceAll("&", "%26");
      return escapedUserInfo;
   } catch (URISyntaxException e) {
      assert false;
    }
    return userInfo;
  }

  @NotNull
  public static String escapePassword(@NotNull String password) {
    String escaped = getEscapedUserInfo("user:" + password);
    return escaped.substring(5);
  }

  private class FakeStreamHandler extends URLStreamHandler {
    @Override
    protected URLConnection openConnection(URL u) throws IOException {
      throw new UnsupportedOperationException();
    }
  }
}