Mercurial > hg > mercurial
view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/AuthSettings.java @ 669:c32869bd757b
Merge Gaya-8.0.x
author | Dmitry Neverov <dmitry.neverov@jetbrains.com> |
---|---|
date | Sat, 12 Oct 2013 19:38:36 +0400 |
parents | 45f25ca68312 |
children | 31a1aca3305c |
line wrap: on
line source
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; } private class FakeStreamHandler extends URLStreamHandler { @Override protected URLConnection openConnection(URL u) throws IOException { throw new UnsupportedOperationException(); } } }