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  	/** Install this implementation as the default. */
65  	public static void install() {
66  		final ConsoleCredentialsProvider c = new ConsoleCredentialsProvider();
67  		if (c.cons == null)
68  			throw new NoClassDefFoundError(
69  					CLIText.get().noSystemConsoleAvailable);
70  		CredentialsProvider cp = new ChainingCredentialsProvider(
71  				new NetRCCredentialsProvider(), c);
72  		CredentialsProvider.setDefault(cp);
73  	}
74  
75  	private final Console cons = System.console();
76  
77  	@Override
78  	public boolean isInteractive() {
79  		return true;
80  	}
81  
82  	@Override
83  	public boolean supports(CredentialItem... items) {
84  		for (CredentialItem i : items) {
85  			if (i instanceof CredentialItem.StringType)
86  				continue;
87  
88  			else if (i instanceof CredentialItem.CharArrayType)
89  				continue;
90  
91  			else if (i instanceof CredentialItem.YesNoType)
92  				continue;
93  
94  			else if (i instanceof CredentialItem.InformationalMessage)
95  				continue;
96  
97  			else
98  				return false;
99  		}
100 		return true;
101 	}
102 
103 	@Override
104 	public boolean get(URIish uri, CredentialItem... items)
105 			throws UnsupportedCredentialItem {
106 		boolean ok = true;
107 		for (int i = 0; i < items.length && ok; i++) {
108 			CredentialItem item = items[i];
109 
110 			if (item instanceof CredentialItem.StringType)
111 				ok = get((CredentialItem.StringType) item);
112 
113 			else if (item instanceof CredentialItem.CharArrayType)
114 				ok = get((CredentialItem.CharArrayType) item);
115 
116 			else if (item instanceof CredentialItem.YesNoType)
117 				ok = get((CredentialItem.YesNoType) item);
118 
119 			else if (item instanceof CredentialItem.InformationalMessage)
120 				ok = get((CredentialItem.InformationalMessage) item);
121 
122 			else
123 				throw new UnsupportedCredentialItem(uri, item.getPromptText());
124 		}
125 		return ok;
126 	}
127 
128 	private boolean get(CredentialItem.StringType item) {
129 		if (item.isValueSecure()) {
130 			char[] v = cons.readPassword("%s: ", item.getPromptText()); //$NON-NLS-1$
131 			if (v != null) {
132 				item.setValue(new String(v));
133 				return true;
134 			} else {
135 				return false;
136 			}
137 		} else {
138 			String v = cons.readLine("%s: ", item.getPromptText()); //$NON-NLS-1$
139 			if (v != null) {
140 				item.setValue(v);
141 				return true;
142 			} else {
143 				return false;
144 			}
145 		}
146 	}
147 
148 	private boolean get(CredentialItem.CharArrayType item) {
149 		if (item.isValueSecure()) {
150 			char[] v = cons.readPassword("%s: ", item.getPromptText()); //$NON-NLS-1$
151 			if (v != null) {
152 				item.setValueNoCopy(v);
153 				return true;
154 			} else {
155 				return false;
156 			}
157 		} else {
158 			String v = cons.readLine("%s: ", item.getPromptText()); //$NON-NLS-1$
159 			if (v != null) {
160 				item.setValueNoCopy(v.toCharArray());
161 				return true;
162 			} else {
163 				return false;
164 			}
165 		}
166 	}
167 
168 	private boolean get(CredentialItem.InformationalMessage item) {
169 		cons.printf("%s\n", item.getPromptText()); //$NON-NLS-1$
170 		cons.flush();
171 		return true;
172 	}
173 
174 	private boolean get(CredentialItem.YesNoType item) {
175 		String r = cons.readLine("%s [%s/%s]? ", item.getPromptText(), //$NON-NLS-1$
176 				CLIText.get().answerYes, CLIText.get().answerNo);
177 		if (r != null) {
178 			item.setValue(CLIText.get().answerYes.equalsIgnoreCase(r));
179 			return true;
180 		} else {
181 			return false;
182 		}
183 	}
184 }