1   /* Copyright 2002-2020 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.orekit.data;
18  
19  import java.awt.Component;
20  import java.awt.event.ActionEvent;
21  import java.awt.event.ActionListener;
22  import java.net.Authenticator;
23  import java.net.PasswordAuthentication;
24  
25  import javax.swing.JButton;
26  import javax.swing.JDialog;
27  import javax.swing.JLabel;
28  import javax.swing.JPasswordField;
29  import javax.swing.JTextField;
30  import javax.swing.Spring;
31  import javax.swing.SpringLayout;
32  
33  /** Simple swing-based dialog window to ask username/password.
34   * <p>
35   * In order to use this class, it should be registered as a default authenticator.
36   * This can be done by calling:
37   * <pre>
38   *   Authenticator.setDefault(new AuthenticatorDialog());
39   * </pre>
40   * </p>
41   * @author Luc Maisonobe
42   */
43  public class AuthenticatorDialog extends Authenticator {
44  
45      /** User name. */
46      private String userName;
47  
48      /** Password. */
49      private char[] password;
50  
51      /** Simple constructor.
52       */
53      public AuthenticatorDialog() {
54          userName = new String();
55          password = new char[0];
56      }
57  
58      /** {@inheritDoc} */
59      protected PasswordAuthentication getPasswordAuthentication() {
60  
61          synchronized (AuthenticatorDialog.class) {
62          final JDialog dialog = new JDialog();
63          dialog.setTitle("enter password");
64          dialog.setModal(true);
65          dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
66          final Component cp = dialog.getContentPane();
67          final SpringLayout layout = new SpringLayout();
68          dialog.setLayout(layout);
69  
70          final JLabel messageLabel = new JLabel(getRequestingPrompt());
71          dialog.add(messageLabel);
72          layout.putConstraint(SpringLayout.NORTH, messageLabel, 5,
73                               SpringLayout.NORTH, cp);
74          // SpringLayout.HORIZONTAL_CENTER is not available in Java 5, we do it the hard way
75          SpringLayout.Constraints c = layout.getConstraints(messageLabel);
76          c.setX(Spring.scale(Spring.sum(Spring.width(cp),
77                                         Spring.minus(Spring.width(messageLabel))),
78                              0.5f));
79  
80          final JLabel userNameLabel = new JLabel("username");
81          dialog.add(userNameLabel);
82          layout.putConstraint(SpringLayout.NORTH, userNameLabel, 5,
83                               SpringLayout.SOUTH, messageLabel);
84          layout.putConstraint(SpringLayout.WEST,  userNameLabel, 10,
85                               SpringLayout.WEST,  cp);
86  
87          final JTextField userNameField = new JTextField(10);
88          dialog.add(userNameField);
89          layout.putConstraint(SpringLayout.SOUTH, userNameField, 0,
90                               SpringLayout.SOUTH, userNameLabel);
91          layout.putConstraint(SpringLayout.WEST,  userNameField, 20,
92                               SpringLayout.EAST,  userNameLabel);
93  
94          final JLabel passwordLabel = new JLabel("password");
95          dialog.add(passwordLabel);
96          layout.putConstraint(SpringLayout.NORTH, passwordLabel, 5,
97                               SpringLayout.SOUTH, userNameLabel);
98          layout.putConstraint(SpringLayout.WEST,  passwordLabel, 0,
99                               SpringLayout.WEST,  userNameLabel);
100 
101         final JPasswordField passwordField = new JPasswordField(10);
102         dialog.add(passwordField);
103         layout.putConstraint(SpringLayout.SOUTH, passwordField, 0,
104                              SpringLayout.SOUTH, passwordLabel);
105         layout.putConstraint(SpringLayout.WEST,  passwordField, 0,
106                              SpringLayout.WEST,  userNameField);
107         layout.putConstraint(SpringLayout.EAST,  passwordField, 0,
108                              SpringLayout.EAST,  userNameField);
109 
110         final JButton okButton = new JButton("OK");
111         dialog.add(okButton);
112         layout.putConstraint(SpringLayout.NORTH, okButton, 15,
113                              SpringLayout.SOUTH, passwordLabel);
114         // SpringLayout.HORIZONTAL_CENTER is not available in Java 5, we do it the hard way
115         c = layout.getConstraints(okButton);
116         c.setX(Spring.sum(Spring.sum(Spring.scale(Spring.width(cp), 0.5f), Spring.constant(-15)),
117                           Spring.minus(Spring.width(okButton))));
118 
119         final JButton cancelButton = new JButton("Cancel");
120         dialog.add(cancelButton);
121         layout.putConstraint(SpringLayout.SOUTH, cancelButton, 0,
122                              SpringLayout.SOUTH, okButton);
123         // SpringLayout.HORIZONTAL_CENTER is not available in Java 5, we do it the hard way
124         c = layout.getConstraints(cancelButton);
125         c.setX(Spring.sum(Spring.scale(Spring.width(cp), 0.5f), Spring.constant(15)));
126 
127         layout.putConstraint(SpringLayout.SOUTH, cp, 0,
128                              SpringLayout.SOUTH, cancelButton);
129         layout.putConstraint(SpringLayout.EAST,  cp, 10,
130                              SpringLayout.EAST,  passwordField);
131         dialog.pack();
132 
133         ActionListener al = new ActionListener() {
134             public void actionPerformed(ActionEvent e) {
135                 if (e.getSource() == cancelButton) {
136                     userName = new String();
137                     password = new char[0];
138                 } else {
139                     userName = userNameField.getText();
140                     password = passwordField.getPassword();
141                 }
142                 userNameField.setText(null);
143                 passwordField.setText(null);
144                 dialog.setVisible(false);
145             }
146         };
147         passwordField.addActionListener(al);
148         okButton.addActionListener(al);
149         cancelButton.addActionListener(al);
150 
151         dialog.setVisible(true);
152 
153         // retrieve user input and reset everything to empty
154         // to prevent credentials lying around in memory
155         PasswordAuthentication authentication =
156             new PasswordAuthentication(userName, password);
157         userName = new String();
158         password = new char[0];
159 
160         return authentication;
161         }
162 
163     }
164 
165 }