You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AwtAuthenticator.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.awtui;
  44. import java.awt.Container;
  45. import java.awt.GridBagConstraints;
  46. import java.awt.GridBagLayout;
  47. import java.awt.Insets;
  48. import java.net.Authenticator;
  49. import java.net.PasswordAuthentication;
  50. import java.util.ArrayList;
  51. import java.util.Collection;
  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. /** Basic network prompt for username/password when using AWT. */
  58. public class AwtAuthenticator extends Authenticator {
  59. private static final AwtAuthenticator me = new AwtAuthenticator();
  60. /** Install this authenticator implementation into the JVM. */
  61. public static void install() {
  62. setDefault(me);
  63. }
  64. /**
  65. * Add a cached authentication for future use.
  66. *
  67. * @param ca
  68. * the information we should remember.
  69. */
  70. public static void add(final CachedAuthentication ca) {
  71. synchronized (me) {
  72. me.cached.add(ca);
  73. }
  74. }
  75. private final Collection<CachedAuthentication> cached = new ArrayList<CachedAuthentication>();
  76. @Override
  77. protected PasswordAuthentication getPasswordAuthentication() {
  78. for (final CachedAuthentication ca : cached) {
  79. if (ca.host.equals(getRequestingHost())
  80. && ca.port == getRequestingPort())
  81. return ca.toPasswordAuthentication();
  82. }
  83. final GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1,
  84. GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
  85. new Insets(0, 0, 0, 0), 0, 0);
  86. final Container panel = new JPanel();
  87. panel.setLayout(new GridBagLayout());
  88. final StringBuilder instruction = new StringBuilder();
  89. instruction.append("Enter username and password for ");
  90. if (getRequestorType() == RequestorType.PROXY) {
  91. instruction.append(getRequestorType());
  92. instruction.append(" ");
  93. instruction.append(getRequestingHost());
  94. if (getRequestingPort() > 0) {
  95. instruction.append(":");
  96. instruction.append(getRequestingPort());
  97. }
  98. } else {
  99. instruction.append(getRequestingURL());
  100. }
  101. gbc.weightx = 1.0;
  102. gbc.gridwidth = GridBagConstraints.REMAINDER;
  103. gbc.gridx = 0;
  104. panel.add(new JLabel(instruction.toString()), gbc);
  105. gbc.gridy++;
  106. gbc.gridwidth = GridBagConstraints.RELATIVE;
  107. // Username
  108. //
  109. final JTextField username;
  110. gbc.fill = GridBagConstraints.NONE;
  111. gbc.gridx = 0;
  112. gbc.weightx = 1;
  113. panel.add(new JLabel("Username:"), gbc);
  114. gbc.gridx = 1;
  115. gbc.fill = GridBagConstraints.HORIZONTAL;
  116. gbc.weighty = 1;
  117. username = new JTextField(20);
  118. panel.add(username, gbc);
  119. gbc.gridy++;
  120. // Password
  121. //
  122. final JPasswordField password;
  123. gbc.fill = GridBagConstraints.NONE;
  124. gbc.gridx = 0;
  125. gbc.weightx = 1;
  126. panel.add(new JLabel("Password:"), gbc);
  127. gbc.gridx = 1;
  128. gbc.fill = GridBagConstraints.HORIZONTAL;
  129. gbc.weighty = 1;
  130. password = new JPasswordField(20);
  131. panel.add(password, gbc);
  132. gbc.gridy++;
  133. if (JOptionPane.showConfirmDialog(null, panel,
  134. "Authentication Required", JOptionPane.OK_CANCEL_OPTION,
  135. JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
  136. final CachedAuthentication ca = new CachedAuthentication(
  137. getRequestingHost(), getRequestingPort(), username
  138. .getText(), new String(password.getPassword()));
  139. cached.add(ca);
  140. return ca.toPasswordAuthentication();
  141. }
  142. return null; // cancel
  143. }
  144. /** Authentication data to remember and reuse. */
  145. public static class CachedAuthentication {
  146. final String host;
  147. final int port;
  148. final String user;
  149. final String pass;
  150. /**
  151. * Create a new cached authentication.
  152. *
  153. * @param aHost
  154. * system this is for.
  155. * @param aPort
  156. * port number of the service.
  157. * @param aUser
  158. * username at the service.
  159. * @param aPass
  160. * password at the service.
  161. */
  162. public CachedAuthentication(final String aHost, final int aPort,
  163. final String aUser, final String aPass) {
  164. host = aHost;
  165. port = aPort;
  166. user = aUser;
  167. pass = aPass;
  168. }
  169. PasswordAuthentication toPasswordAuthentication() {
  170. return new PasswordAuthentication(user, pass.toCharArray());
  171. }
  172. }
  173. }