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  /** Interacts with the user during authentication by using AWT/Swing dialogs. */
66  public class AwtCredentialsProvider extends CredentialsProvider {
67  	/** Install this implementation as the default. */
68  	public static void install() {
69  		final AwtCredentialsProvider c = new AwtCredentialsProvider();
70  		CredentialsProvider cp = new ChainingCredentialsProvider(
71  				new NetRCCredentialsProvider(), c);
72  		CredentialsProvider.setDefault(cp);
73  	}
74  
75  	@Override
76  	public boolean isInteractive() {
77  		return true;
78  	}
79  
80  	@Override
81  	public boolean supports(CredentialItem... items) {
82  		for (CredentialItem i : items) {
83  			if (i instanceof CredentialItem.StringType)
84  				continue;
85  
86  			else if (i instanceof CredentialItem.CharArrayType)
87  				continue;
88  
89  			else if (i instanceof CredentialItem.YesNoType)
90  				continue;
91  
92  			else if (i instanceof CredentialItem.InformationalMessage)
93  				continue;
94  
95  			else
96  				return false;
97  		}
98  		return true;
99  	}
100 
101 	@Override
102 	public boolean get(URIish uri, CredentialItem... items)
103 			throws UnsupportedCredentialItem {
104 		if (items.length == 0) {
105 			return true;
106 
107 		} else if (items.length == 1) {
108 			final CredentialItem item = items[0];
109 
110 			if (item instanceof CredentialItem.InformationalMessage) {
111 				JOptionPane.showMessageDialog(null, item.getPromptText(),
112 						UIText.get().warning, JOptionPane.INFORMATION_MESSAGE);
113 				return true;
114 
115 			} else if (item instanceof CredentialItem.YesNoType) {
116 				CredentialItem.YesNoType v = (CredentialItem.YesNoType) item;
117 				int r = JOptionPane.showConfirmDialog(null, v.getPromptText(),
118 						UIText.get().warning, JOptionPane.YES_NO_OPTION);
119 				switch (r) {
120 				case JOptionPane.YES_OPTION:
121 					v.setValue(true);
122 					return true;
123 
124 				case JOptionPane.NO_OPTION:
125 					v.setValue(false);
126 					return true;
127 
128 				case JOptionPane.CANCEL_OPTION:
129 				case JOptionPane.CLOSED_OPTION:
130 				default:
131 					return false;
132 				}
133 
134 			} else {
135 				return interactive(uri, items);
136 			}
137 
138 		} else {
139 			return interactive(uri, items);
140 		}
141 	}
142 
143 	private static boolean interactive(URIish uri, CredentialItem[] items) {
144 		final GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1,
145 				GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
146 				new Insets(0, 0, 0, 0), 0, 0);
147 		final JPanel panel = new JPanel();
148 		panel.setLayout(new GridBagLayout());
149 
150 		final JTextField[] texts = new JTextField[items.length];
151 		for (int i = 0; i < items.length; i++) {
152 			CredentialItem item = items[i];
153 
154 			if (item instanceof CredentialItem.StringType
155 					|| item instanceof CredentialItem.CharArrayType) {
156 				gbc.fill = GridBagConstraints.NONE;
157 				gbc.gridwidth = GridBagConstraints.RELATIVE;
158 				gbc.gridx = 0;
159 				panel.add(new JLabel(item.getPromptText()), gbc);
160 
161 				gbc.fill = GridBagConstraints.HORIZONTAL;
162 				gbc.gridwidth = GridBagConstraints.RELATIVE;
163 				gbc.gridx = 1;
164 				if (item.isValueSecure())
165 					texts[i] = new JPasswordField(20);
166 				else
167 					texts[i] = new JTextField(20);
168 				panel.add(texts[i], gbc);
169 				gbc.gridy++;
170 
171 			} else if (item instanceof CredentialItem.InformationalMessage) {
172 				gbc.fill = GridBagConstraints.NONE;
173 				gbc.gridwidth = GridBagConstraints.REMAINDER;
174 				gbc.gridx = 0;
175 				panel.add(new JLabel(item.getPromptText()), gbc);
176 				gbc.gridy++;
177 
178 			} else {
179 				throw new UnsupportedCredentialItem(uri, item.getPromptText());
180 			}
181 		}
182 
183 		if (JOptionPane.showConfirmDialog(null, panel,
184 				UIText.get().authenticationRequired,
185 				JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) != JOptionPane.OK_OPTION)
186 			return false; // cancel
187 
188 		for (int i = 0; i < items.length; i++) {
189 			CredentialItem item = items[i];
190 			JTextField f = texts[i];
191 
192 			if (item instanceof CredentialItem.StringType) {
193 				CredentialItem.StringType v = (CredentialItem.StringType) item;
194 				if (f instanceof JPasswordField)
195 					v.setValue(new String(((JPasswordField) f).getPassword()));
196 				else
197 					v.setValue(f.getText());
198 
199 			} else if (item instanceof CredentialItem.CharArrayType) {
200 				CredentialItem.CharArrayType v = (CredentialItem.CharArrayType) item;
201 				if (f instanceof JPasswordField)
202 					v.setValueNoCopy(((JPasswordField) f).getPassword());
203 				else
204 					v.setValueNoCopy(f.getText().toCharArray());
205 			}
206 		}
207 		return true;
208 	}
209 }