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  /**
55   * A JSch {@link com.jcraft.jsch.UserInfo} adapter for a
56   * {@link org.eclipse.jgit.transport.CredentialsProvider}.
57   */
58  public class CredentialsProviderUserInfo implements UserInfo,
59  		UIKeyboardInteractive {
60  	private final URIish uri;
61  
62  	private final CredentialsProvider provider;
63  
64  	private String password;
65  
66  	private String passphrase;
67  
68  	/**
69  	 * Wrap a CredentialsProvider to make it suitable for use with JSch.
70  	 *
71  	 * @param session
72  	 *            the JSch session this UserInfo will support authentication on.
73  	 * @param credentialsProvider
74  	 *            the provider that will perform the authentication.
75  	 */
76  	public CredentialsProviderUserInfo(Session session,
77  			CredentialsProvider credentialsProvider) {
78  		this.uri = createURI(session);
79  		this.provider = credentialsProvider;
80  	}
81  
82  	private static URIish createURI(Session session) {
83  		URIish uri = new URIish();
84  		uri = uri.setScheme("ssh"); //$NON-NLS-1$
85  		uri = uri.setUser(session.getUserName());
86  		uri = uri.setHost(session.getHost());
87  		uri = uri.setPort(session.getPort());
88  		return uri;
89  	}
90  
91  	/** {@inheritDoc} */
92  	@Override
93  	public String getPassword() {
94  		return password;
95  	}
96  
97  	/** {@inheritDoc} */
98  	@Override
99  	public String getPassphrase() {
100 		return passphrase;
101 	}
102 
103 	/** {@inheritDoc} */
104 	@Override
105 	public boolean promptPassphrase(String msg) {
106 		CredentialItem.StringType v = newPrompt(msg);
107 		if (provider.get(uri, v)) {
108 			passphrase = v.getValue();
109 			return true;
110 		} else {
111 			passphrase = null;
112 			return false;
113 		}
114 	}
115 
116 	/** {@inheritDoc} */
117 	@Override
118 	public boolean promptPassword(String msg) {
119 		CredentialItem.Password p = new CredentialItem.Password(msg);
120 		if (provider.get(uri, p)) {
121 			password = new String(p.getValue());
122 			return true;
123 		} else {
124 			password = null;
125 			return false;
126 		}
127 	}
128 
129 	private CredentialItem.StringType newPrompt(String msg) {
130 		return new CredentialItem.StringType(msg, true);
131 	}
132 
133 	/** {@inheritDoc} */
134 	@Override
135 	public boolean promptYesNo(String msg) {
136 		CredentialItem.YesNoType v = new CredentialItem.YesNoType(msg);
137 		return provider.get(uri, v) && v.getValue();
138 	}
139 
140 	/** {@inheritDoc} */
141 	@Override
142 	public void showMessage(String msg) {
143 		provider.get(uri, new CredentialItem.InformationalMessage(msg));
144 	}
145 
146 	/** {@inheritDoc} */
147 	@Override
148 	public String[] promptKeyboardInteractive(String destination, String name,
149 			String instruction, String[] prompt, boolean[] echo) {
150 		CredentialItem.StringType[] v = new CredentialItem.StringType[prompt.length];
151 		for (int i = 0; i < prompt.length; i++)
152 			v[i] = new CredentialItem.StringType(prompt[i], !echo[i]);
153 
154 		List<CredentialItem> items = new ArrayList<>();
155 		if (instruction != null && instruction.length() > 0)
156 			items.add(new CredentialItem.InformationalMessage(instruction));
157 		items.addAll(Arrays.asList(v));
158 
159 		if (!provider.get(uri, items))
160 			return null; // cancel
161 
162 		String[] result = new String[v.length];
163 		for (int i = 0; i < v.length; i++)
164 			result[i] = v[i].getValue();
165 		return result;
166 	}
167 }