View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.transport;
45  
46  import java.util.ArrayList;
47  import java.util.Arrays;
48  import java.util.List;
49  
50  import com.jcraft.jsch.Session;
51  import com.jcraft.jsch.UIKeyboardInteractive;
52  import com.jcraft.jsch.UserInfo;
53  
54  /** A JSch {@link UserInfo} adapter for a {@link CredentialsProvider}. */
55  public class CredentialsProviderUserInfo implements UserInfo,
56  		UIKeyboardInteractive {
57  	private final URIish uri;
58  
59  	private final CredentialsProvider provider;
60  
61  	private String password;
62  
63  	private String passphrase;
64  
65  	/**
66  	 * Wrap a CredentialsProvider to make it suitable for use with JSch.
67  	 *
68  	 * @param session
69  	 *            the JSch session this UserInfo will support authentication on.
70  	 * @param credentialsProvider
71  	 *            the provider that will perform the authentication.
72  	 */
73  	public CredentialsProviderUserInfo(Session session,
74  			CredentialsProvider credentialsProvider) {
75  		this.uri = createURI(session);
76  		this.provider = credentialsProvider;
77  	}
78  
79  	private static URIish createURI(Session session) {
80  		URIish uri = new URIish();
81  		uri = uri.setScheme("ssh"); //$NON-NLS-1$
82  		uri = uri.setUser(session.getUserName());
83  		uri = uri.setHost(session.getHost());
84  		uri = uri.setPort(session.getPort());
85  		return uri;
86  	}
87  
88  	public String getPassword() {
89  		return password;
90  	}
91  
92  	public String getPassphrase() {
93  		return passphrase;
94  	}
95  
96  	public boolean promptPassphrase(String msg) {
97  		CredentialItem.StringType v = newPrompt(msg);
98  		if (provider.get(uri, v)) {
99  			passphrase = v.getValue();
100 			return true;
101 		} else {
102 			passphrase = null;
103 			return false;
104 		}
105 	}
106 
107 	public boolean promptPassword(String msg) {
108 		CredentialItem.Password p = new CredentialItem.Password(msg);
109 		if (provider.get(uri, p)) {
110 			password = new String(p.getValue());
111 			return true;
112 		} else {
113 			password = null;
114 			return false;
115 		}
116 	}
117 
118 	private CredentialItem.StringType newPrompt(String msg) {
119 		return new CredentialItem.StringType(msg, true);
120 	}
121 
122 	public boolean promptYesNo(String msg) {
123 		CredentialItem.YesNoType v = new CredentialItem.YesNoType(msg);
124 		return provider.get(uri, v) && v.getValue();
125 	}
126 
127 	public void showMessage(String msg) {
128 		provider.get(uri, new CredentialItem.InformationalMessage(msg));
129 	}
130 
131 	public String[] promptKeyboardInteractive(String destination, String name,
132 			String instruction, String[] prompt, boolean[] echo) {
133 		CredentialItem.StringType[] v = new CredentialItem.StringType[prompt.length];
134 		for (int i = 0; i < prompt.length; i++)
135 			v[i] = new CredentialItem.StringType(prompt[i], !echo[i]);
136 
137 		List<CredentialItem> items = new ArrayList<CredentialItem>();
138 		if (instruction != null && instruction.length() > 0)
139 			items.add(new CredentialItem.InformationalMessage(instruction));
140 		items.addAll(Arrays.asList(v));
141 
142 		if (!provider.get(uri, items))
143 			return null; // cancel
144 
145 		String[] result = new String[v.length];
146 		for (int i = 0; i < v.length; i++)
147 			result[i] = v[i].getValue();
148 		return result;
149 	}
150 }