Mercurial > hg > mercurial
view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/AuthSettings.java @ 1027:10dc26b32c35
Update code according to new Java
author | nikolai.kulakov@DESKTOP-Q4QCGIH |
---|---|
date | Wed, 05 Aug 2020 13:19:53 +0300 |
parents | 9c8525c5af12 |
children |
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 jetbrains.buildServer.serverSide.TeamCityProperties; 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<>(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) { String preparedUrl = escapeUnsafeChars(repositoryUrl); if (isRequireCredentials(preparedUrl)) { if (containsCredentials(preparedUrl)) return preparedUrl; try { return createURLWithCredentials(preparedUrl); } catch (MalformedURLException e) { Loggers.VCS.warn("Error while parsing url " + preparedUrl, e); } return preparedUrl; } else { return preparedUrl; } } @NotNull private String escapeUnsafeChars(@NotNull String repositoryUrl) { if (!TeamCityProperties.getBooleanOrTrue("teamcity.mercurial.identifyCommand.escapeUnsafeChars")) return repositoryUrl; StringBuilder res = new StringBuilder(); for (char c: repositoryUrl.toCharArray()) { switch (c) { case '"': res.append("%22"); break; case '\'': res.append("%27"); break; case '`': res.append("%60"); break; case ' ': res.append("%20"); break; case ';': res.append("%3B"); break; default: res.append(c); } } return res.toString(); } 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 static class FakeStreamHandler extends URLStreamHandler { @Override protected URLConnection openConnection(URL u) throws IOException { throw new UnsupportedOperationException(); } } }