View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.awtui;
13  
14  import java.awt.Container;
15  import java.awt.GridBagConstraints;
16  import java.awt.GridBagLayout;
17  import java.awt.Insets;
18  import java.net.PasswordAuthentication;
19  
20  import javax.swing.JLabel;
21  import javax.swing.JOptionPane;
22  import javax.swing.JPanel;
23  import javax.swing.JPasswordField;
24  import javax.swing.JTextField;
25  
26  import org.eclipse.jgit.util.CachedAuthenticator;
27  
28  /**
29   * Basic network prompt for username/password when using AWT.
30   */
31  public class AwtAuthenticator extends CachedAuthenticator {
32  	/**
33  	 * Install this authenticator implementation into the JVM.
34  	 */
35  	public static void install() {
36  		setDefault(new AwtAuthenticator());
37  	}
38  
39  	/** {@inheritDoc} */
40  	@Override
41  	protected PasswordAuthentication promptPasswordAuthentication() {
42  		final GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1,
43  				GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
44  				new Insets(0, 0, 0, 0), 0, 0);
45  		final Container panel = new JPanel();
46  		panel.setLayout(new GridBagLayout());
47  
48  		final StringBuilder instruction = new StringBuilder();
49  		instruction.append(UIText.get().enterUsernameAndPasswordFor);
50  		instruction.append(" "); //$NON-NLS-1$
51  		if (getRequestorType() == RequestorType.PROXY) {
52  			instruction.append(getRequestorType());
53  			instruction.append(" "); //$NON-NLS-1$
54  			instruction.append(getRequestingHost());
55  			if (getRequestingPort() > 0) {
56  				instruction.append(":"); //$NON-NLS-1$
57  				instruction.append(getRequestingPort());
58  			}
59  		} else {
60  			instruction.append(getRequestingURL());
61  		}
62  
63  		gbc.weightx = 1.0;
64  		gbc.gridwidth = GridBagConstraints.REMAINDER;
65  		gbc.gridx = 0;
66  		panel.add(new JLabel(instruction.toString()), gbc);
67  		gbc.gridy++;
68  
69  		gbc.gridwidth = GridBagConstraints.RELATIVE;
70  
71  		// Username
72  		//
73  		final JTextField username;
74  		gbc.fill = GridBagConstraints.NONE;
75  		gbc.gridx = 0;
76  		gbc.weightx = 1;
77  		panel.add(new JLabel(UIText.get().username), gbc);
78  
79  		gbc.gridx = 1;
80  		gbc.fill = GridBagConstraints.HORIZONTAL;
81  		gbc.weighty = 1;
82  		username = new JTextField(20);
83  		panel.add(username, gbc);
84  		gbc.gridy++;
85  
86  		// Password
87  		//
88  		final JPasswordField password;
89  		gbc.fill = GridBagConstraints.NONE;
90  		gbc.gridx = 0;
91  		gbc.weightx = 1;
92  		panel.add(new JLabel(UIText.get().password), gbc);
93  
94  		gbc.gridx = 1;
95  		gbc.fill = GridBagConstraints.HORIZONTAL;
96  		gbc.weighty = 1;
97  		password = new JPasswordField(20);
98  		panel.add(password, gbc);
99  		gbc.gridy++;
100 
101 		if (JOptionPane.showConfirmDialog(null, panel,
102 				UIText.get().authenticationRequired, JOptionPane.OK_CANCEL_OPTION,
103 				JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
104 			return new PasswordAuthentication(username.getText(), password
105 					.getPassword());
106 		}
107 
108 		return null; // cancel
109 	}
110 }