1   /* Copyright 2002-2025 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 javax.swing.JButton;
20  import javax.swing.JDialog;
21  import javax.swing.JLabel;
22  import javax.swing.JPasswordField;
23  import javax.swing.JTextField;
24  import javax.swing.Spring;
25  import javax.swing.SpringLayout;
26  import java.awt.Component;
27  import java.awt.event.ActionListener;
28  import java.net.Authenticator;
29  import java.net.PasswordAuthentication;
30  
31  /** Simple swing-based dialog window to ask username/password.
32   * <p>
33   * In order to use this class, it should be registered as a default authenticator.
34   * This can be done by calling:
35   * <pre>
36   *   Authenticator.setDefault(new AuthenticatorDialog());
37   * </pre>
38   * </p>
39   * @author Luc Maisonobe
40   */
41  public class AuthenticatorDialog extends Authenticator {
42  
43      /** User name. */
44      private String userName;
45  
46      /** Password. */
47      private char[] password;
48  
49      /** Simple constructor.
50       */
51      public AuthenticatorDialog() {
52          userName = "";
53          password = new char[0];
54      }
55  
56      /** {@inheritDoc} */
57      protected PasswordAuthentication getPasswordAuthentication() {
58  
59          synchronized (AuthenticatorDialog.class) {
60          final JDialog dialog = new JDialog();
61          dialog.setTitle("enter password");
62          dialog.setModal(true);
63          dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
64          final Component cp = dialog.getContentPane();
65          final SpringLayout layout = new SpringLayout();
66          dialog.setLayout(layout);
67  
68          final JLabel messageLabel = new JLabel(getRequestingPrompt());
69          dialog.add(messageLabel);
70          layout.putConstraint(SpringLayout.NORTH, messageLabel, 5,
71                               SpringLayout.NORTH, cp);
72          // SpringLayout.HORIZONTAL_CENTER is not available in Java 5, we do it the hard way
73          SpringLayout.Constraints c = layout.getConstraints(messageLabel);
74          c.setX(Spring.scale(Spring.sum(Spring.width(cp),
75                                         Spring.minus(Spring.width(messageLabel))),
76                              0.5f));
77  
78          final JLabel userNameLabel = new JLabel("username");
79          dialog.add(userNameLabel);
80          layout.putConstraint(SpringLayout.NORTH, userNameLabel, 5,
81                               SpringLayout.SOUTH, messageLabel);
82          layout.putConstraint(SpringLayout.WEST,  userNameLabel, 10,
83                               SpringLayout.WEST,  cp);
84  
85          final JTextField userNameField = new JTextField(10);
86          dialog.add(userNameField);
87          layout.putConstraint(SpringLayout.SOUTH, userNameField, 0,
88                               SpringLayout.SOUTH, userNameLabel);
89          layout.putConstraint(SpringLayout.WEST,  userNameField, 20,
90                               SpringLayout.EAST,  userNameLabel);
91  
92          final JLabel passwordLabel = new JLabel("password");
93          dialog.add(passwordLabel);
94          layout.putConstraint(SpringLayout.NORTH, passwordLabel, 5,
95                               SpringLayout.SOUTH, userNameLabel);
96          layout.putConstraint(SpringLayout.WEST,  passwordLabel, 0,
97                               SpringLayout.WEST,  userNameLabel);
98  
99          final JPasswordField passwordField = new JPasswordField(10);
100         dialog.add(passwordField);
101         layout.putConstraint(SpringLayout.SOUTH, passwordField, 0,
102                              SpringLayout.SOUTH, passwordLabel);
103         layout.putConstraint(SpringLayout.WEST,  passwordField, 0,
104                              SpringLayout.WEST,  userNameField);
105         layout.putConstraint(SpringLayout.EAST,  passwordField, 0,
106                              SpringLayout.EAST,  userNameField);
107 
108         final JButton okButton = new JButton("OK");
109         dialog.add(okButton);
110         layout.putConstraint(SpringLayout.NORTH, okButton, 15,
111                              SpringLayout.SOUTH, passwordLabel);
112         // SpringLayout.HORIZONTAL_CENTER is not available in Java 5, we do it the hard way
113         c = layout.getConstraints(okButton);
114         c.setX(Spring.sum(Spring.sum(Spring.scale(Spring.width(cp), 0.5f), Spring.constant(-15)),
115                           Spring.minus(Spring.width(okButton))));
116 
117         final JButton cancelButton = new JButton("Cancel");
118         dialog.add(cancelButton);
119         layout.putConstraint(SpringLayout.SOUTH, cancelButton, 0,
120                              SpringLayout.SOUTH, okButton);
121         // SpringLayout.HORIZONTAL_CENTER is not available in Java 5, we do it the hard way
122         c = layout.getConstraints(cancelButton);
123         c.setX(Spring.sum(Spring.scale(Spring.width(cp), 0.5f), Spring.constant(15)));
124 
125         layout.putConstraint(SpringLayout.SOUTH, cp, 0,
126                              SpringLayout.SOUTH, cancelButton);
127         layout.putConstraint(SpringLayout.EAST,  cp, 10,
128                              SpringLayout.EAST,  passwordField);
129         dialog.pack();
130 
131         ActionListener al = e -> {
132             if (e.getSource() == cancelButton) {
133                 userName = "";
134                 password = new char[0];
135             } else {
136                 userName = userNameField.getText();
137                 password = passwordField.getPassword();
138             }
139             userNameField.setText(null);
140             passwordField.setText(null);
141             dialog.setVisible(false);
142         };
143         passwordField.addActionListener(al);
144         okButton.addActionListener(al);
145         cancelButton.addActionListener(al);
146 
147         dialog.setVisible(true);
148 
149         // retrieve user input and reset everything to empty
150         // to prevent credentials lying around in memory
151         PasswordAuthentication authentication =
152             new PasswordAuthentication(userName, password);
153         userName = "";
154         password = new char[0];
155 
156         return authentication;
157         }
158 
159     }
160 
161 }