summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/auth/CredentialsAuthenticator.java
blob: 410cb0997201ced51bf13938faabd1c927ac27af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright 2000-2014 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.auth;

import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.tmatesoft.svn.core.SVNCancelException;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.auth.SVNAuthentication;
import org.tmatesoft.svn.core.auth.SVNPasswordAuthentication;
import org.tmatesoft.svn.core.auth.SVNSSLAuthentication;

import java.util.List;

/**
 * @author Konstantin Kolosovsky.
 */
class CredentialsAuthenticator extends AbstractAuthenticator {
  private String myKind;
  // sometimes realm string is different (with <>), so store credentials for both strings..
  private String myRealm2;
  private SVNAuthentication myAuthentication;

  CredentialsAuthenticator(@NotNull IdeaSvnkitBasedAuthenticationCallback authService,
                           @NotNull SVNURL url,
                           @Nullable String realm) {
    super(authService, url, realm == null ? url.getHost() : realm);
  }

  public boolean tryAuthenticate(boolean passwordRequest) {
    final List<String> kinds = IdeaSvnkitBasedAuthenticationCallback.getKinds(myUrl, passwordRequest);
    for (String kind : kinds) {
      myKind = kind;
      if (!tryAuthenticate()) {
        return false;
      }
    }
    return true;
  }

  @Override
  protected boolean getWithPassive(SvnAuthenticationManager passive) throws SVNException {
    myAuthentication = getWithPassiveImpl(passive);
    if (myAuthentication != null && !checkAuthOk(myAuthentication)) {
      myAuthService.clearPassiveCredentials(myRealm, myUrl,
                                            myAuthentication instanceof SVNPasswordAuthentication);  //clear passive also take into acconut ssl filepath
      myAuthentication = null;
    }
    return myAuthentication != null;
  }

  private SVNAuthentication getWithPassiveImpl(SvnAuthenticationManager passive) throws SVNException {
    try {
      return passive.getFirstAuthentication(myKind, myRealm, myUrl);
    }
    catch (SVNCancelException e) {
      return null;
    }
  }

  private boolean checkAuthOk(SVNAuthentication authentication) {
    if (authentication instanceof SVNPasswordAuthentication && StringUtil.isEmptyOrSpaces(authentication.getUserName())) return false;
    if (authentication instanceof SVNSSLAuthentication) {
      if (StringUtil.isEmptyOrSpaces(((SVNSSLAuthentication)authentication).getPassword())) return false;
    }
    return true;
  }

  @Override
  protected boolean getWithActive(final SvnAuthenticationManager active) throws SVNException {
    if (ISVNAuthenticationManager.SSL.equals(myKind)) {
      if (super.getWithActive(active)) return true;
    }
    myAuthentication = active.getProvider().requestClientAuthentication(myKind, myUrl, myRealm, null, null, true);
    myStoreInUsual = myAuthService.getTempDirectory() == null && myAuthentication != null && myAuthentication.isStorageAllowed();

    return myAuthentication != null;
  }

  public void requestClientAuthentication(SVNURL url, String realm, SVNAuthentication authentication) {
    if (!myUrl.equals(url)) return;
    myAuthentication = authentication;
    myRealm2 = realm;
    myStoreInUsual = myAuthentication != null && myAuthentication.isStorageAllowed();
  }

  @Override
  protected boolean afterAuthCall() {
    return myAuthentication != null;
  }

  @Override
  protected boolean acknowledge(SvnAuthenticationManager manager) throws SVNException {
    if (!StringUtil.isEmptyOrSpaces(myRealm2) && !myRealm2.equals(myRealm)) {
      storeCredentials(manager, myAuthentication, myRealm2);
    }
    return storeCredentials(manager, myAuthentication, myRealm);
  }
}