AwtAuthenticator.java

  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. package org.eclipse.jgit.awtui;

  12. import java.awt.Container;
  13. import java.awt.GridBagConstraints;
  14. import java.awt.GridBagLayout;
  15. import java.awt.Insets;
  16. import java.net.PasswordAuthentication;

  17. import javax.swing.JLabel;
  18. import javax.swing.JOptionPane;
  19. import javax.swing.JPanel;
  20. import javax.swing.JPasswordField;
  21. import javax.swing.JTextField;

  22. import org.eclipse.jgit.util.CachedAuthenticator;

  23. /**
  24.  * Basic network prompt for username/password when using AWT.
  25.  */
  26. public class AwtAuthenticator extends CachedAuthenticator {
  27.     /**
  28.      * Install this authenticator implementation into the JVM.
  29.      */
  30.     public static void install() {
  31.         setDefault(new AwtAuthenticator());
  32.     }

  33.     /** {@inheritDoc} */
  34.     @Override
  35.     protected PasswordAuthentication promptPasswordAuthentication() {
  36.         final GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1,
  37.                 GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,
  38.                 new Insets(0, 0, 0, 0), 0, 0);
  39.         final Container panel = new JPanel();
  40.         panel.setLayout(new GridBagLayout());

  41.         final StringBuilder instruction = new StringBuilder();
  42.         instruction.append(UIText.get().enterUsernameAndPasswordFor);
  43.         instruction.append(" "); //$NON-NLS-1$
  44.         if (getRequestorType() == RequestorType.PROXY) {
  45.             instruction.append(getRequestorType());
  46.             instruction.append(" "); //$NON-NLS-1$
  47.             instruction.append(getRequestingHost());
  48.             if (getRequestingPort() > 0) {
  49.                 instruction.append(":"); //$NON-NLS-1$
  50.                 instruction.append(getRequestingPort());
  51.             }
  52.         } else {
  53.             instruction.append(getRequestingURL());
  54.         }

  55.         gbc.weightx = 1.0;
  56.         gbc.gridwidth = GridBagConstraints.REMAINDER;
  57.         gbc.gridx = 0;
  58.         panel.add(new JLabel(instruction.toString()), gbc);
  59.         gbc.gridy++;

  60.         gbc.gridwidth = GridBagConstraints.RELATIVE;

  61.         // Username
  62.         //
  63.         final JTextField username;
  64.         gbc.fill = GridBagConstraints.NONE;
  65.         gbc.gridx = 0;
  66.         gbc.weightx = 1;
  67.         panel.add(new JLabel(UIText.get().username), gbc);

  68.         gbc.gridx = 1;
  69.         gbc.fill = GridBagConstraints.HORIZONTAL;
  70.         gbc.weighty = 1;
  71.         username = new JTextField(20);
  72.         panel.add(username, gbc);
  73.         gbc.gridy++;

  74.         // Password
  75.         //
  76.         final JPasswordField password;
  77.         gbc.fill = GridBagConstraints.NONE;
  78.         gbc.gridx = 0;
  79.         gbc.weightx = 1;
  80.         panel.add(new JLabel(UIText.get().password), gbc);

  81.         gbc.gridx = 1;
  82.         gbc.fill = GridBagConstraints.HORIZONTAL;
  83.         gbc.weighty = 1;
  84.         password = new JPasswordField(20);
  85.         panel.add(password, gbc);
  86.         gbc.gridy++;

  87.         if (JOptionPane.showConfirmDialog(null, panel,
  88.                 UIText.get().authenticationRequired, JOptionPane.OK_CANCEL_OPTION,
  89.                 JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
  90.             return new PasswordAuthentication(username.getText(), password
  91.                     .getPassword());
  92.         }

  93.         return null; // cancel
  94.     }
  95. }