IDEA-25467 (CVS is set to Offline mode on project open) - integration to trunk
[fedora-idea.git] / plugins / cvs / cvs-core / src / com / intellij / cvsSupport2 / connections / ssh / SshPasswordAuthentication.java
blob80c1985a9b3afcef8ddb70e4fc0b410e0d10282a
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 com.intellij.cvsSupport2.connections.ssh;
18 import com.trilead.ssh2.Connection;
19 import com.trilead.ssh2.InteractiveCallback;
20 import org.netbeans.lib.cvsclient.connection.AuthenticationException;
22 import java.io.IOException;
23 import java.util.Arrays;
24 import java.util.List;
26 public class SshPasswordAuthentication implements SshAuthentication {
27 private final static String PASSWORD_METHOD = "password";
28 private final static String KEYBOARD_METHOD = "keyboard-interactive";
30 private final String myLogin;
31 private final SSHPasswordProvider myPasswordProvider;
32 private final String myCvsRootAsString;
34 public SshPasswordAuthentication(final String login, final SSHPasswordProvider passwordProvider, final String cvsRootAsString) {
35 myLogin = login;
36 myPasswordProvider = passwordProvider;
37 myCvsRootAsString = cvsRootAsString;
40 public void authenticate(final Connection connection) throws AuthenticationException, SolveableAuthenticationException {
41 final String password = myPasswordProvider.getPasswordForCvsRoot(myCvsRootAsString);
42 if (password == null) {
43 throw new SolveableAuthenticationException("Authentication rejected.");
45 try {
46 final String[] methodsArr = connection.getRemainingAuthMethods(myLogin);
47 if ((methodsArr == null) || (methodsArr.length == 0)) return;
48 final List<String> methods = Arrays.asList(methodsArr);
50 if (methods.contains(PASSWORD_METHOD)) {
51 if (connection.authenticateWithPassword(myLogin, password)) return;
54 if (methods.contains(KEYBOARD_METHOD)) {
55 final boolean wasAuthenticated = connection.authenticateWithKeyboardInteractive(myLogin, new InteractiveCallback() {
56 public String[] replyToChallenge(String s, String instruction, int numPrompts, String[] strings, boolean[] booleans) throws Exception {
57 final String[] result = new String[numPrompts];
58 if (numPrompts > 0) {
59 Arrays.fill(result, password);
61 return result;
63 });
64 if (wasAuthenticated) return;
67 throw new SolveableAuthenticationException("Authentication rejected.");
69 catch (IOException e) {
70 throw new SolveableAuthenticationException(e.getMessage(), e);