Switch jgit library to the EDL (3-clause BSD)
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / awtui / AwtAuthenticator.java
blob258ffb8262bddce355e0f56679f2fe7548e1d41f
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
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
21 * written permission.
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() {
61 setDefault(me);
64 /**
65 * Add a cached authentication for future use.
67 * @param ca
68 * the information we should remember.
70 public static void add(final CachedAuthentication ca) {
71 synchronized (me) {
72 me.cached.add(ca);
76 private final Collection<CachedAuthentication> cached = new ArrayList<CachedAuthentication>();
78 @Override
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());
102 } else {
103 instruction.append(getRequestingURL());
106 gbc.weightx = 1.0;
107 gbc.gridwidth = GridBagConstraints.REMAINDER;
108 gbc.gridx = 0;
109 panel.add(new JLabel(instruction.toString()), gbc);
110 gbc.gridy++;
112 gbc.gridwidth = GridBagConstraints.RELATIVE;
114 // Username
116 final JTextField username;
117 gbc.fill = GridBagConstraints.NONE;
118 gbc.gridx = 0;
119 gbc.weightx = 1;
120 panel.add(new JLabel("Username:"), gbc);
122 gbc.gridx = 1;
123 gbc.fill = GridBagConstraints.HORIZONTAL;
124 gbc.weighty = 1;
125 username = new JTextField(20);
126 panel.add(username, gbc);
127 gbc.gridy++;
129 // Password
131 final JPasswordField password;
132 gbc.fill = GridBagConstraints.NONE;
133 gbc.gridx = 0;
134 gbc.weightx = 1;
135 panel.add(new JLabel("Password:"), gbc);
137 gbc.gridx = 1;
138 gbc.fill = GridBagConstraints.HORIZONTAL;
139 gbc.weighty = 1;
140 password = new JPasswordField(20);
141 panel.add(password, gbc);
142 gbc.gridy++;
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()));
150 cached.add(ca);
151 return ca.toPasswordAuthentication();
154 return null; // cancel
157 /** Authentication data to remember and reuse. */
158 public static class CachedAuthentication {
159 final String host;
161 final int port;
163 final String user;
165 final String pass;
168 * Create a new cached authentication.
170 * @param aHost
171 * system this is for.
172 * @param aPort
173 * port number of the service.
174 * @param aUser
175 * username at the service.
176 * @param aPass
177 * password at the service.
179 public CachedAuthentication(final String aHost, final int aPort,
180 final String aUser, final String aPass) {
181 host = aHost;
182 port = aPort;
183 user = aUser;
184 pass = aPass;
187 PasswordAuthentication toPasswordAuthentication() {
188 return new PasswordAuthentication(user, pass.toCharArray());