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.awtui;
47  
48  import java.awt.GridBagConstraints;
49  import java.awt.GridBagLayout;
50  import java.awt.Insets;
51  
52  import javax.swing.JLabel;
53  import javax.swing.JOptionPane;
54  import javax.swing.JPanel;
55  import javax.swing.JPasswordField;
56  import javax.swing.JTextField;
57  
58  import org.eclipse.jgit.errors.UnsupportedCredentialItem;
59  import org.eclipse.jgit.transport.ChainingCredentialsProvider;
60  import org.eclipse.jgit.transport.CredentialItem;
61  import org.eclipse.jgit.transport.CredentialsProvider;
62  import org.eclipse.jgit.transport.NetRCCredentialsProvider;
63  import org.eclipse.jgit.transport.URIish;
64  
65  /**
66   * Interacts with the user during authentication by using AWT/Swing dialogs.
67   */
68  public class AwtCredentialsProvider extends CredentialsProvider {
69  	/**
70  	 * Install this implementation as the default.
71  	 */
72  	public static void install() {
73  		final AwtCredentialsProviderovider.html#AwtCredentialsProvider">AwtCredentialsProvider c = new AwtCredentialsProvider();
74  		CredentialsProvider cp = new ChainingCredentialsProvider(
75  				new NetRCCredentialsProvider(), c);
76  		CredentialsProvider.setDefault(cp);
77  	}
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 		if (items.length == 0) {
112 			return true;
113 
114 		} else if (items.length == 1) {
115 			final CredentialItem item = items[0];
116 
117 			if (item instanceof CredentialItem.InformationalMessage) {
118 				JOptionPane.showMessageDialog(null, item.getPromptText(),
119 						UIText.get().warning, JOptionPane.INFORMATION_MESSAGE);
120 				return true;
121 
122 			} else if (item instanceof CredentialItem.YesNoType) {
123 				CredentialItem.YesNoType v = (CredentialItem.YesNoType) item;
124 				int r = JOptionPane.showConfirmDialog(null, v.getPromptText(),
125 						UIText.get().warning, JOptionPane.YES_NO_OPTION);
126 				switch (r) {
127 				case JOptionPane.YES_OPTION:
128 					v.setValue(true);
129 					return true;
130 
131 				case JOptionPane.NO_OPTION:
132 					v.setValue(false);
133 					return true;
134 
135 				case JOptionPane.CANCEL_OPTION:
136 				case JOptionPane.CLOSED_OPTION:
137 				default:
138 					return false;
139 				}
140 
141 			} else {
142 				return interactive(uri, items);
143 			}
144 
145 		} else {
146 			return interactive(uri, items);
147 		}
148 	}
149 
150 	private static boolean interactive(URIish uri, CredentialItem[] items) {
151 		final GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1,
152 				GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
153 				new Insets(0, 0, 0, 0), 0, 0);
154 		final JPanel panel = new JPanel();
155 		panel.setLayout(new GridBagLayout());
156 
157 		final JTextField[] texts = new JTextField[items.length];
158 		for (int i = 0; i < items.length; i++) {
159 			CredentialItem item = items[i];
160 
161 			if (item instanceof CredentialItem.StringType
162 					|| item instanceof CredentialItem.CharArrayType) {
163 				gbc.fill = GridBagConstraints.NONE;
164 				gbc.gridwidth = GridBagConstraints.RELATIVE;
165 				gbc.gridx = 0;
166 				panel.add(new JLabel(item.getPromptText()), gbc);
167 
168 				gbc.fill = GridBagConstraints.HORIZONTAL;
169 				gbc.gridwidth = GridBagConstraints.RELATIVE;
170 				gbc.gridx = 1;
171 				if (item.isValueSecure())
172 					texts[i] = new JPasswordField(20);
173 				else
174 					texts[i] = new JTextField(20);
175 				panel.add(texts[i], gbc);
176 				gbc.gridy++;
177 
178 			} else if (item instanceof CredentialItem.InformationalMessage) {
179 				gbc.fill = GridBagConstraints.NONE;
180 				gbc.gridwidth = GridBagConstraints.REMAINDER;
181 				gbc.gridx = 0;
182 				panel.add(new JLabel(item.getPromptText()), gbc);
183 				gbc.gridy++;
184 
185 			} else {
186 				throw new UnsupportedCredentialItem(uri, item.getPromptText());
187 			}
188 		}
189 
190 		if (JOptionPane.showConfirmDialog(null, panel,
191 				UIText.get().authenticationRequired,
192 				JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) != JOptionPane.OK_OPTION)
193 			return false; // cancel
194 
195 		for (int i = 0; i < items.length; i++) {
196 			CredentialItem item = items[i];
197 			JTextField f = texts[i];
198 
199 			if (item instanceof CredentialItem.StringType) {
200 				CredentialItem.StringType v = (CredentialItem.StringType) item;
201 				if (f instanceof JPasswordField)
202 					v.setValue(new String(((JPasswordField) f).getPassword()));
203 				else
204 					v.setValue(f.getText());
205 
206 			} else if (item instanceof CredentialItem.CharArrayType) {
207 				CredentialItem.CharArrayType v = (CredentialItem.CharArrayType) item;
208 				if (f instanceof JPasswordField)
209 					v.setValueNoCopy(((JPasswordField) f).getPassword());
210 				else
211 					v.setValueNoCopy(f.getText().toCharArray());
212 			}
213 		}
214 		return true;
215 	}
216 }