View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc.
3    * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5    * and other copyright owners as documented in the project's IP log.
6    *
7    * This program and the accompanying materials are made available
8    * under the terms of the Eclipse Distribution License v1.0 which
9    * accompanies this distribution, is reproduced below, and is
10   * available at http://www.eclipse.org/org/documents/edl-v10.php
11   *
12   * All rights reserved.
13   *
14   * Redistribution and use in source and binary forms, with or
15   * without modification, are permitted provided that the following
16   * conditions are met:
17   *
18   * - Redistributions of source code must retain the above copyright
19   *   notice, this list of conditions and the following disclaimer.
20   *
21   * - Redistributions in binary form must reproduce the above
22   *   copyright notice, this list of conditions and the following
23   *   disclaimer in the documentation and/or other materials provided
24   *   with the distribution.
25   *
26   * - Neither the name of the Eclipse Foundation, Inc. nor the
27   *   names of its contributors may be used to endorse or promote
28   *   products derived from this software without specific prior
29   *   written permission.
30   *
31   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44   */
45  
46  package org.eclipse.jgit.console;
47  
48  import java.io.Console;
49  
50  import org.eclipse.jgit.errors.UnsupportedCredentialItem;
51  import org.eclipse.jgit.pgm.internal.CLIText;
52  import org.eclipse.jgit.transport.ChainingCredentialsProvider;
53  import org.eclipse.jgit.transport.CredentialItem;
54  import org.eclipse.jgit.transport.CredentialsProvider;
55  import org.eclipse.jgit.transport.NetRCCredentialsProvider;
56  import org.eclipse.jgit.transport.URIish;
57  
58  /**
59   * Interacts with the user during authentication by using the text console.
60   *
61   * @since 4.0
62   */
63  public class ConsoleCredentialsProvider extends CredentialsProvider {
64  	/**
65  	 * Install this implementation as the default.
66  	 */
67  	public static void install() {
68  		final ConsoleCredentialsProviderProvider.html#ConsoleCredentialsProvider">ConsoleCredentialsProvider c = new ConsoleCredentialsProvider();
69  		if (c.cons == null)
70  			throw new NoClassDefFoundError(
71  					CLIText.get().noSystemConsoleAvailable);
72  		CredentialsProvider cp = new ChainingCredentialsProvider(
73  				new NetRCCredentialsProvider(), c);
74  		CredentialsProvider.setDefault(cp);
75  	}
76  
77  	private final Console cons = System.console();
78  
79  	/** {@inheritDoc} */
80  	@Override
81  	public boolean isInteractive() {
82  		return true;
83  	}
84  
85  	/** {@inheritDoc} */
86  	@Override
87  	public boolean supports(CredentialItem... items) {
88  		for (CredentialItem i : items) {
89  			if (i instanceof CredentialItem.StringType)
90  				continue;
91  
92  			else if (i instanceof CredentialItem.CharArrayType)
93  				continue;
94  
95  			else if (i instanceof CredentialItem.YesNoType)
96  				continue;
97  
98  			else if (i instanceof CredentialItem.InformationalMessage)
99  				continue;
100 
101 			else
102 				return false;
103 		}
104 		return true;
105 	}
106 
107 	/** {@inheritDoc} */
108 	@Override
109 	public boolean get(URIish uri, CredentialItem... items)
110 			throws UnsupportedCredentialItem {
111 		boolean ok = true;
112 		for (int i = 0; i < items.length && ok; i++) {
113 			CredentialItem item = items[i];
114 
115 			if (item instanceof CredentialItem.StringType)
116 				ok = get((CredentialItem.StringType) item);
117 
118 			else if (item instanceof CredentialItem.CharArrayType)
119 				ok = get((CredentialItem.CharArrayType) item);
120 
121 			else if (item instanceof CredentialItem.YesNoType)
122 				ok = get((CredentialItem.YesNoType) item);
123 
124 			else if (item instanceof CredentialItem.InformationalMessage)
125 				ok = get((CredentialItem.InformationalMessage) item);
126 
127 			else
128 				throw new UnsupportedCredentialItem(uri, item.getPromptText());
129 		}
130 		return ok;
131 	}
132 
133 	private boolean get(CredentialItem.StringType item) {
134 		if (item.isValueSecure()) {
135 			char[] v = cons.readPassword("%s: ", item.getPromptText()); //$NON-NLS-1$
136 			if (v != null) {
137 				item.setValue(new String(v));
138 				return true;
139 			} else {
140 				return false;
141 			}
142 		} else {
143 			String v = cons.readLine("%s: ", item.getPromptText()); //$NON-NLS-1$
144 			if (v != null) {
145 				item.setValue(v);
146 				return true;
147 			} else {
148 				return false;
149 			}
150 		}
151 	}
152 
153 	private boolean get(CredentialItem.CharArrayType item) {
154 		if (item.isValueSecure()) {
155 			char[] v = cons.readPassword("%s: ", item.getPromptText()); //$NON-NLS-1$
156 			if (v != null) {
157 				item.setValueNoCopy(v);
158 				return true;
159 			} else {
160 				return false;
161 			}
162 		} else {
163 			String v = cons.readLine("%s: ", item.getPromptText()); //$NON-NLS-1$
164 			if (v != null) {
165 				item.setValueNoCopy(v.toCharArray());
166 				return true;
167 			} else {
168 				return false;
169 			}
170 		}
171 	}
172 
173 	private boolean get(CredentialItem.InformationalMessage item) {
174 		cons.printf("%s\n", item.getPromptText()); //$NON-NLS-1$
175 		cons.flush();
176 		return true;
177 	}
178 
179 	private boolean get(CredentialItem.YesNoType item) {
180 		String r = cons.readLine("%s [%s/%s]? ", item.getPromptText(), //$NON-NLS-1$
181 				CLIText.get().answerYes, CLIText.get().answerNo);
182 		if (r != null) {
183 			item.setValue(CLIText.get().answerYes.equalsIgnoreCase(r));
184 			return true;
185 		} else {
186 			return false;
187 		}
188 	}
189 }