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