SVN auth
[fedora-idea.git] / plugins / svn4idea / src / org / jetbrains / idea / svn / dialogs / SvnInteractiveAuthenticationProvider.java
blob7b9964eb6f1b0de17e6a88315551fcba4035c5c2
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.jetbrains.idea.svn.dialogs;
18 import com.intellij.openapi.application.ApplicationManager;
19 import com.intellij.openapi.diagnostic.Logger;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.ui.GuiUtils;
22 import com.intellij.util.SystemProperties;
23 import org.jetbrains.idea.svn.SvnBundle;
24 import org.jetbrains.idea.svn.SvnConfiguration;
25 import org.jetbrains.idea.svn.SvnVcs;
26 import org.tmatesoft.svn.core.SVNErrorMessage;
27 import org.tmatesoft.svn.core.SVNURL;
28 import org.tmatesoft.svn.core.auth.*;
30 import java.io.File;
31 import java.lang.reflect.InvocationTargetException;
33 public class SvnInteractiveAuthenticationProvider implements ISVNAuthenticationProvider {
34 private static final Logger LOG = Logger.getInstance("#org.jetbrains.idea.svn.dialogs.SvnInteractiveAuthenticationProvider");
35 private final Project myProject;
36 private static ThreadLocal<MyCallState> myCallState = new ThreadLocal<MyCallState>();
37 private final SvnVcs myVcs;
39 public SvnInteractiveAuthenticationProvider(final SvnVcs vcs) {
40 myVcs = vcs;
41 myProject = vcs.getProject();
44 public static void clearCallState() {
45 myCallState.set(null);
48 public static boolean wasCalled() {
49 return myCallState.get() != null && myCallState.get().isWasCalled();
52 public static boolean wasCancelled() {
53 return myCallState.get() != null && myCallState.get().isWasCancelled();
56 public SVNAuthentication requestClientAuthentication(String kind,
57 final SVNURL url,
58 final String realm,
59 SVNErrorMessage errorMessage,
60 final SVNAuthentication previousAuth,
61 final boolean authMayBeStored) {
62 final MyCallState callState = new MyCallState(true, false);
63 myCallState.set(callState);
64 // once we came here, we don't know _correct_ auth todo +-
65 final SvnConfiguration configuration = SvnConfiguration.getInstance(myProject);
66 configuration.clearCredentials(kind, realm);
68 final SVNAuthentication[] result = new SVNAuthentication[1];
69 Runnable command = null;
71 final String userName =
72 previousAuth != null && previousAuth.getUserName() != null ? previousAuth.getUserName() : SystemProperties.getUserName();
73 if (ISVNAuthenticationManager.PASSWORD.equals(kind)) {// || ISVNAuthenticationManager.USERNAME.equals(kind)) {
74 command = new Runnable() {
75 public void run() {
76 SimpleCredentialsDialog dialog = new SimpleCredentialsDialog(myProject);
77 dialog.setup(realm, userName, authMayBeStored);
78 if (previousAuth == null) {
79 dialog.setTitle(SvnBundle.message("dialog.title.authentication.required"));
81 else {
82 dialog.setTitle(SvnBundle.message("dialog.title.authentication.required.was.failed"));
84 dialog.show();
85 if (dialog.isOK()) {
86 result[0] = new SVNPasswordAuthentication(dialog.getUserName(), dialog.getPassword(), dialog.isSaveAllowed());
91 else if (ISVNAuthenticationManager.USERNAME.equals(kind)) {
92 if (ApplicationManager.getApplication().isUnitTestMode()) {
93 return new SVNUserNameAuthentication(userName, false);
95 command = new Runnable() {
96 public void run() {
97 UserNameCredentialsDialog dialog = new UserNameCredentialsDialog(myProject);
98 dialog.setup(realm, userName, authMayBeStored);
99 if (previousAuth == null) {
100 dialog.setTitle(SvnBundle.message("dialog.title.authentication.required"));
102 else {
103 dialog.setTitle(SvnBundle.message("dialog.title.authentication.required.was.failed"));
105 dialog.show();
106 if (dialog.isOK()) {
107 result[0] = new SVNUserNameAuthentication(dialog.getUserName(), dialog.isSaveAllowed());
112 else if (ISVNAuthenticationManager.SSH.equals(kind)) {
113 command = new Runnable() {
114 public void run() {
115 SSHCredentialsDialog dialog = new SSHCredentialsDialog(myProject);
116 dialog.setup(realm, userName, authMayBeStored, url.getPort());
117 if (previousAuth == null) {
118 dialog.setTitle(SvnBundle.message("dialog.title.authentication.required"));
120 else {
121 dialog.setTitle(SvnBundle.message("dialog.title.authentication.required.was.failed"));
123 dialog.show();
124 if (dialog.isOK()) {
125 int port = dialog.getPortNumber();
126 if (dialog.getKeyFile() != null && dialog.getKeyFile().trim().length() > 0) {
127 String passphrase = dialog.getPassphrase();
128 if (passphrase != null && passphrase.length() == 0) {
129 passphrase = null;
131 result[0] =
132 new SVNSSHAuthentication(dialog.getUserName(), new File(dialog.getKeyFile()), passphrase, port, dialog.isSaveAllowed());
134 else {
135 result[0] = new SVNSSHAuthentication(dialog.getUserName(), dialog.getPassword(), port, dialog.isSaveAllowed());
140 } else if (ISVNAuthenticationManager.SSL.equals(kind)) {
141 command = new Runnable() {
142 public void run() {
143 final SSLCredentialsDialog dialog = new SSLCredentialsDialog(myProject, realm, authMayBeStored);
144 if (previousAuth == null) {
145 dialog.setTitle(SvnBundle.message("dialog.title.authentication.required"));
147 else {
148 dialog.setTitle(SvnBundle.message("dialog.title.authentication.required.was.failed"));
150 dialog.show();
151 if (dialog.isOK()) {
152 result[0] = new SVNSSLAuthentication(new File(dialog.getCertificatePath()), String.valueOf(dialog.getCertificatePassword()),
153 dialog.getSaveAuth());
159 if (command != null) {
160 try {
161 GuiUtils.runOrInvokeAndWait(command);
163 catch (InterruptedException e) {
166 catch (InvocationTargetException e) {
169 log("3 authentication result: " + result[0]);
171 callState.setWasCancelled(result[0] == null);
172 return result[0];
175 public int acceptServerAuthentication(SVNURL url, String realm, Object certificate, boolean resultMayBeStored) {
176 // do-not-need-it-here
177 return ISVNAuthenticationProvider.REJECTED;
180 private void log(final String s) {
181 LOG.debug(s);
184 public static class MyCallState {
185 private boolean myWasCalled;
186 private boolean myWasCancelled;
188 public MyCallState(boolean wasCalled, boolean wasCancelled) {
189 myWasCalled = wasCalled;
190 myWasCancelled = wasCancelled;
193 public boolean isWasCalled() {
194 return myWasCalled;
197 public boolean isWasCancelled() {
198 return myWasCancelled;
201 public void setWasCancelled(boolean wasCancelled) {
202 myWasCancelled = wasCancelled;