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