<commit_before>/*
 * Copyright 2000-2013 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 org.jetbrains.idea.svn.commandLine;

import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;

/**
 * @author <NAME>.
 */
public class CertificateCallbackCase extends AuthCallbackCase {

  private static final String CERTIFICATE_ERROR = "Error validating server certificate for";
  private static final String UNTRUSTED_SERVER_CERTIFICATE = "Server SSL certificate untrusted";

  private boolean accepted;

  CertificateCallbackCase(@NotNull AuthenticationCallback callback, SVNURL url) {
    super(callback, url);
  }

  @Override
  public boolean canHandle(String error) {
    return error.startsWith(CERTIFICATE_ERROR) ||
           // https one-way protocol untrusted server certificate
           error.contains(UNTRUSTED_SERVER_CERTIFICATE);
  }

  @Override
  public boolean getCredentials(final String errText) throws SvnBindException {
    String realm = getRealm(errText);

    if (!errText.startsWith(CERTIFICATE_ERROR)) {
      // if we do not have explicit realm in error message - use server url and not full repository url
      // as SVNKit lifecycle resolves ssl realm (for saving certificate to runtime storage) as server url
      SVNURL serverUrl = getServerUrl(realm);
      realm = serverUrl != null ? serverUrl.toString() : realm;
    }

    if (!myTried && myAuthenticationCallback.acceptSSLServerCertificate(myUrl, realm)) {
      accepted = true;
      myTried = true;
      return true;
    }
    throw new SvnBindException("Server SSL certificate rejected");
  }

  private static String getRealm(String errText) throws SvnBindException {
    String realm = errText;
    final int idx1 = realm.indexOf('\'');
    if (idx1 == -1) {<commit_msg>IDEA-113732 Handled https 'certificate issued for a different hostname, issuer is not trusted' error for command line using SVNKit authentication lifecycle<commit_after>/*
 * Copyright 2000-2013 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 org.jetbrains.idea.svn.commandLine;

import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;

/**
 * @author <NAME>.
 */
public class CertificateCallbackCase extends AuthCallbackCase {

  private static final String CERTIFICATE_ERROR = "Error validating server certificate for";
  private static final String UNTRUSTED_SERVER_CERTIFICATE = "Server SSL certificate untrusted";
  private static final String CERTIFICATE_VERIFICATION_FAILED = "certificate verification failed";

  private boolean accepted;

  CertificateCallbackCase(@NotNull AuthenticationCallback callback, SVNURL url) {
    super(callback, url);
  }

  @Override
  public boolean canHandle(String error) {
    return error.startsWith(CERTIFICATE_ERROR) ||
           // https one-way protocol untrusted server certificate
           error.contains(UNTRUSTED_SERVER_CERTIFICATE) ||
           // for instance, certificate issued for a different hostname, issuer is not trusted - for both 1.7 and 1.8
           error.contains(CERTIFICATE_VERIFICATION_FAILED);
  }

  @Override
  public boolean getCredentials(final String errText) throws SvnBindException {
    String realm = getRealm(errText);

    if (!errText.startsWith(CERTIFICATE_ERROR)) {
      // if we do not have explicit realm in error message - use server url and not full repository url
      // as SVNKit lifecycle resolves ssl realm (for saving certificate to runtime storage) as server url
      SVNURL serverUrl = getServerUrl(realm);
      realm = serverUrl != null ? serverUrl.toString() : realm;
    }

    if (!myTried && myAuthenticationCallback.acceptSSLServerCertificate(myUrl, realm)) {
      accepted = true;
      myTried = true;
      return true;
    }
    throw new SvnBindException("Server SSL certificate rejected");
  }

  private static String getRealm(String errText) throws SvnBindException {
    String realm = errText;
    final int idx1 = realm.indexOf('\'');
    if (idx1 == -1) {
{'fraction_non_alphanumeric': 0.07934388708754529, 'fraction_numerical': 0.007629219912263971, 'mean_word_length': 3.9101123595505616, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1377512', 'n_tokens_mistral': 1479, 'n_tokens_neox': 1421, 'n_words': 506}