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