2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.awtui
;
40 import java
.awt
.Container
;
41 import java
.awt
.GridBagConstraints
;
42 import java
.awt
.GridBagLayout
;
43 import java
.awt
.Insets
;
44 import java
.net
.Authenticator
;
45 import java
.net
.PasswordAuthentication
;
46 import java
.util
.ArrayList
;
47 import java
.util
.Collection
;
49 import javax
.swing
.JLabel
;
50 import javax
.swing
.JOptionPane
;
51 import javax
.swing
.JPanel
;
52 import javax
.swing
.JPasswordField
;
53 import javax
.swing
.JTextField
;
55 /** Basic network prompt for username/password when using AWT. */
56 public class AwtAuthenticator
extends Authenticator
{
57 private static final AwtAuthenticator me
= new AwtAuthenticator();
59 /** Install this authenticator implementation into the JVM. */
60 public static void install() {
65 * Add a cached authentication for future use.
68 * the information we should remember.
70 public static void add(final CachedAuthentication ca
) {
76 private final Collection
<CachedAuthentication
> cached
= new ArrayList
<CachedAuthentication
>();
79 protected PasswordAuthentication
getPasswordAuthentication() {
80 for (final CachedAuthentication ca
: cached
) {
81 if (ca
.host
.equals(getRequestingHost())
82 && ca
.port
== getRequestingPort())
83 return ca
.toPasswordAuthentication();
86 final GridBagConstraints gbc
= new GridBagConstraints(0, 0, 1, 1, 1, 1,
87 GridBagConstraints
.NORTHWEST
, GridBagConstraints
.NONE
,
88 new Insets(0, 0, 0, 0), 0, 0);
89 final Container panel
= new JPanel();
90 panel
.setLayout(new GridBagLayout());
92 final StringBuilder instruction
= new StringBuilder();
93 instruction
.append("Enter username and password for ");
94 if (getRequestorType() == RequestorType
.PROXY
) {
95 instruction
.append(getRequestorType());
96 instruction
.append(" ");
97 instruction
.append(getRequestingHost());
98 if (getRequestingPort() > 0) {
99 instruction
.append(":");
100 instruction
.append(getRequestingPort());
103 instruction
.append(getRequestingURL());
107 gbc
.gridwidth
= GridBagConstraints
.REMAINDER
;
109 panel
.add(new JLabel(instruction
.toString()), gbc
);
112 gbc
.gridwidth
= GridBagConstraints
.RELATIVE
;
116 final JTextField username
;
117 gbc
.fill
= GridBagConstraints
.NONE
;
120 panel
.add(new JLabel("Username:"), gbc
);
123 gbc
.fill
= GridBagConstraints
.HORIZONTAL
;
125 username
= new JTextField(20);
126 panel
.add(username
, gbc
);
131 final JPasswordField password
;
132 gbc
.fill
= GridBagConstraints
.NONE
;
135 panel
.add(new JLabel("Password:"), gbc
);
138 gbc
.fill
= GridBagConstraints
.HORIZONTAL
;
140 password
= new JPasswordField(20);
141 panel
.add(password
, gbc
);
144 if (JOptionPane
.showConfirmDialog(null, panel
,
145 "Authentication Required", JOptionPane
.OK_CANCEL_OPTION
,
146 JOptionPane
.QUESTION_MESSAGE
) == JOptionPane
.OK_OPTION
) {
147 final CachedAuthentication ca
= new CachedAuthentication(
148 getRequestingHost(), getRequestingPort(), username
149 .getText(), new String(password
.getPassword()));
151 return ca
.toPasswordAuthentication();
154 return null; // cancel
157 /** Authentication data to remember and reuse. */
158 public static class CachedAuthentication
{
168 * Create a new cached authentication.
171 * system this is for.
173 * port number of the service.
175 * username at the service.
177 * password at the service.
179 public CachedAuthentication(final String aHost
, final int aPort
,
180 final String aUser
, final String aPass
) {
187 PasswordAuthentication
toPasswordAuthentication() {
188 return new PasswordAuthentication(user
, pass
.toCharArray());