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> and others
5    *
6    * This program and the accompanying materials are made available under the
7    * terms of the Eclipse Distribution License v. 1.0 which is available at
8    * https://www.eclipse.org/org/documents/edl-v10.php.
9    *
10   * SPDX-License-Identifier: BSD-3-Clause
11   */
12  
13  package org.eclipse.jgit.console;
14  
15  import java.io.Console;
16  
17  import org.eclipse.jgit.errors.UnsupportedCredentialItem;
18  import org.eclipse.jgit.pgm.internal.CLIText;
19  import org.eclipse.jgit.transport.ChainingCredentialsProvider;
20  import org.eclipse.jgit.transport.CredentialItem;
21  import org.eclipse.jgit.transport.CredentialsProvider;
22  import org.eclipse.jgit.transport.NetRCCredentialsProvider;
23  import org.eclipse.jgit.transport.URIish;
24  
25  /**
26   * Interacts with the user during authentication by using the text console.
27   *
28   * @since 4.0
29   */
30  public class ConsoleCredentialsProvider extends CredentialsProvider {
31  	/**
32  	 * Install this implementation as the default.
33  	 */
34  	public static void install() {
35  		final ConsoleCredentialsProvider c = new ConsoleCredentialsProvider();
36  		if (c.cons == null)
37  			throw new NoClassDefFoundError(
38  					CLIText.get().noSystemConsoleAvailable);
39  		CredentialsProvider cp = new ChainingCredentialsProvider(
40  				new NetRCCredentialsProvider(), c);
41  		CredentialsProvider.setDefault(cp);
42  	}
43  
44  	private final Console cons = System.console();
45  
46  	/** {@inheritDoc} */
47  	@Override
48  	public boolean isInteractive() {
49  		return true;
50  	}
51  
52  	/** {@inheritDoc} */
53  	@Override
54  	public boolean supports(CredentialItem... items) {
55  		for (CredentialItem i : items) {
56  			if (i instanceof CredentialItem.StringType)
57  				continue;
58  
59  			else if (i instanceof CredentialItem.CharArrayType)
60  				continue;
61  
62  			else if (i instanceof CredentialItem.YesNoType)
63  				continue;
64  
65  			else if (i instanceof CredentialItem.InformationalMessage)
66  				continue;
67  
68  			else
69  				return false;
70  		}
71  		return true;
72  	}
73  
74  	/** {@inheritDoc} */
75  	@Override
76  	public boolean get(URIish uri, CredentialItem... items)
77  			throws UnsupportedCredentialItem {
78  		boolean ok = true;
79  		for (int i = 0; i < items.length && ok; i++) {
80  			CredentialItem item = items[i];
81  
82  			if (item instanceof CredentialItem.StringType)
83  				ok = get((CredentialItem.StringType) item);
84  
85  			else if (item instanceof CredentialItem.CharArrayType)
86  				ok = get((CredentialItem.CharArrayType) item);
87  
88  			else if (item instanceof CredentialItem.YesNoType)
89  				ok = get((CredentialItem.YesNoType) item);
90  
91  			else if (item instanceof CredentialItem.InformationalMessage)
92  				ok = get((CredentialItem.InformationalMessage) item);
93  
94  			else
95  				throw new UnsupportedCredentialItem(uri, item.getPromptText());
96  		}
97  		return ok;
98  	}
99  
100 	private boolean get(CredentialItem.StringType item) {
101 		if (item.isValueSecure()) {
102 			char[] v = cons.readPassword("%s: ", item.getPromptText()); //$NON-NLS-1$
103 			if (v != null) {
104 				item.setValue(new String(v));
105 				return true;
106 			}
107 			return false;
108 		}
109 		String v = cons.readLine("%s: ", item.getPromptText()); //$NON-NLS-1$
110 		if (v != null) {
111 			item.setValue(v);
112 			return true;
113 		}
114 		return false;
115 	}
116 
117 	private boolean get(CredentialItem.CharArrayType item) {
118 		if (item.isValueSecure()) {
119 			char[] v = cons.readPassword("%s: ", item.getPromptText()); //$NON-NLS-1$
120 			if (v != null) {
121 				item.setValueNoCopy(v);
122 				return true;
123 			}
124 			return false;
125 		}
126 		String v = cons.readLine("%s: ", item.getPromptText()); //$NON-NLS-1$
127 		if (v != null) {
128 			item.setValueNoCopy(v.toCharArray());
129 			return true;
130 		}
131 		return false;
132 	}
133 
134 	private boolean get(CredentialItem.InformationalMessage item) {
135 		cons.printf("%s\n", item.getPromptText()); //$NON-NLS-1$
136 		cons.flush();
137 		return true;
138 	}
139 
140 	private boolean get(CredentialItem.YesNoType item) {
141 		String r = cons.readLine("%s [%s/%s]? ", item.getPromptText(), //$NON-NLS-1$
142 				CLIText.get().answerYes, CLIText.get().answerNo);
143 		if (r != null) {
144 			item.setValue(CLIText.get().answerYes.equalsIgnoreCase(r));
145 			return true;
146 		}
147 		return false;
148 	}
149 }