View Javadoc
1   package com.jsql.view.swing.panel.preferences;
2   
3   import com.jsql.view.swing.panel.PanelPreferences;
4   import com.jsql.view.swing.text.JPopupTextField;
5   import com.jsql.view.swing.text.listener.DocumentListenerEditing;
6   import com.jsql.view.swing.util.MediatorHelper;
7   
8   import javax.swing.*;
9   import javax.swing.event.DocumentListener;
10  import java.awt.*;
11  import java.util.stream.Stream;
12  
13  public class PanelAuthentication extends JPanel {
14  
15      private final JCheckBox checkboxUseDigestAuthentication = new JCheckBox("<html>Enable <b>Basic</b> and <b>NTLM</b> (for <b>Digest</b>: do not enable but just set the user and password):</html>", MediatorHelper.model().getMediatorUtils().getAuthenticationUtil().isAuthentEnabled());
16      private final JCheckBox checkboxUseKerberos = new JCheckBox("Enable Kerberos:", MediatorHelper.model().getMediatorUtils().getAuthenticationUtil().isKerberos());
17  
18      private final JTextField textDigestAuthenticationUsername = new JPopupTextField("Host system user", MediatorHelper.model().getMediatorUtils().getAuthenticationUtil().getUsernameAuthentication()).getProxy();
19      private final JTextField textDigestAuthenticationPassword = new JPopupTextField("Host system password", MediatorHelper.model().getMediatorUtils().getAuthenticationUtil().getPasswordAuthentication()).getProxy();
20      private final JTextField textKerberosLoginConf = new JPopupTextField("Path to login.conf", MediatorHelper.model().getMediatorUtils().getAuthenticationUtil().getPathKerberosLogin()).getProxy();
21      private final JTextField textKerberosKrb5Conf = new JPopupTextField("Path to krb5.conf", MediatorHelper.model().getMediatorUtils().getAuthenticationUtil().getPathKerberosKrb5()).getProxy();
22  
23      private static final String TAG_HTML_ON = "<html>";
24      private static final String TAG_HTML_OFF = "</html>";
25      
26      public PanelAuthentication(PanelPreferences panelPreferences) {
27          this.checkboxUseDigestAuthentication.setToolTipText(
28              PanelAuthentication.TAG_HTML_ON
29              + "Enable <b>Basic</b>, <b>Digest</b>, <b>NTLM</b> authentication (e.g. WWW-Authenticate).<br>"
30              + "Then define username and password for the host.<br>"
31              + "<i><b>Negotiate</b> authentication is defined in URL.</i>"
32              + PanelAuthentication.TAG_HTML_OFF
33          );
34  
35          var panelUsername = new JPanel();
36          panelUsername.setLayout(new BoxLayout(panelUsername, BoxLayout.LINE_AXIS));
37          panelUsername.add(new JLabel("Username "));
38          panelUsername.add(this.textDigestAuthenticationUsername);
39          panelUsername.setMaximumSize(new Dimension(325, this.textDigestAuthenticationUsername.getPreferredSize().height));
40  
41          var panelPassword = new JPanel();
42          panelPassword.setLayout(new BoxLayout(panelPassword, BoxLayout.LINE_AXIS));
43          panelPassword.add(new JLabel("Password "));
44          panelPassword.add(this.textDigestAuthenticationPassword);
45          panelPassword.setMaximumSize(new Dimension(325, this.textDigestAuthenticationPassword.getPreferredSize().height));
46          panelPassword.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
47  
48          String tooltipUseKerberos = PanelAuthentication.TAG_HTML_ON
49              + "Enable Kerberos authentication, then define path to <b>login.conf</b> and <b>krb5.conf</b>.<br>"
50              + "Path to <b>.keytab</b> file is defined in login.conf ; name of <b>principal</b> must be correct.<br>"
51              + "<b>Realm</b> and <b>kdc</b> are defined in krb5.conf.<br>"
52              + "Finally use the <b>correct hostname</b> in URL, e.g. http://servicename.corp.test/[..]"
53              + PanelAuthentication.TAG_HTML_OFF;
54  
55          this.textKerberosLoginConf.setToolTipText(
56              PanelAuthentication.TAG_HTML_ON
57              + "Define the path to <b>login.conf</b>. Sample:<br>"
58              + "&emsp;<b>entry-name</b> {<br>"
59              + "&emsp;&emsp;com.sun.security.auth.module.Krb5LoginModule<br>"
60              + "&emsp;&emsp;required<br>"
61              + "&emsp;&emsp;useKeyTab=true<br>"
62              + "&emsp;&emsp;keyTab=\"<b>/path/to/my.keytab</b>\"<br>"
63              + "&emsp;&emsp;principal=\"<b>HTTP/SERVICENAME.CORP.TEST@CORP.TEST</b>\"<br>"
64              + "&emsp;&emsp;debug=false;<br>"
65              + "&emsp;}<br>"
66              + "<i>Principal name is case sensitive ; entry-name is read automatically.</i>"
67              + PanelAuthentication.TAG_HTML_OFF);
68          this.textKerberosKrb5Conf.setToolTipText(
69              PanelAuthentication.TAG_HTML_ON
70              + "Define the path to <b>krb5.conf</b>. Sample:<br>"
71              + "&emsp;[libdefaults]<br>"
72              + "&emsp;&emsp;default_realm = <b>CORP.TEST</b><br>"
73              + "&emsp;&emsp;udp_preference_limit = 1<br>"
74              + "&emsp;[realms]<br>"
75              + "&emsp;&emsp;<b>CORP.TEST</b> = {<br>"
76              + "&emsp;&emsp;&emsp;kdc = <b>127.0.0.1:88</b><br>"
77              + "&emsp;&emsp;}<br>"
78              + "<i>Realm and kdc are case sensitives.</i>"
79              + PanelAuthentication.TAG_HTML_OFF);
80          this.checkboxUseKerberos.setToolTipText(tooltipUseKerberos);
81  
82          var panelLoginConf = new JPanel();
83          panelLoginConf.setLayout(new BoxLayout(panelLoginConf, BoxLayout.LINE_AXIS));
84          panelLoginConf.add(new JLabel("login.conf "));
85          panelLoginConf.add(this.textKerberosLoginConf);
86          panelLoginConf.setMaximumSize(new Dimension(325, this.textKerberosLoginConf.getPreferredSize().height));
87  
88          var panelKrb5Conf = new JPanel();
89          panelKrb5Conf.setLayout(new BoxLayout(panelKrb5Conf, BoxLayout.LINE_AXIS));
90          panelKrb5Conf.add(new JLabel("krb5.conf "));
91          panelKrb5Conf.add(this.textKerberosKrb5Conf);
92          panelKrb5Conf.setMaximumSize(new Dimension(325, this.textKerberosKrb5Conf.getPreferredSize().height));
93          
94          Stream.of(
95              this.checkboxUseDigestAuthentication,
96              this.checkboxUseKerberos
97          )
98          .forEach(button -> button.addActionListener(panelPreferences.getActionListenerSave()));
99          
100         DocumentListener documentListenerSave = new DocumentListenerEditing() {
101             @Override
102             public void process() {
103                 panelPreferences.getActionListenerSave().actionPerformed(null);
104             }
105         };
106 
107         Stream.of(
108             this.textDigestAuthenticationPassword,
109             this.textDigestAuthenticationUsername,
110             this.textKerberosKrb5Conf,
111             this.textKerberosLoginConf
112         )
113         .forEach(textField -> textField.getDocument().addDocumentListener(documentListenerSave));
114 
115         var labelOrigin = new JLabel("<html><b>Network secured connection</b></html>");
116         labelOrigin.setBorder(PanelGeneral.MARGIN);
117 
118         var groupLayout = new GroupLayout(this);
119         this.setLayout(groupLayout);
120 
121         groupLayout.setHorizontalGroup(
122             groupLayout
123             .createSequentialGroup()
124             .addGroup(
125                 groupLayout
126                 .createParallelGroup(GroupLayout.Alignment.LEADING, false)
127                 .addComponent(labelOrigin)
128                 .addComponent(this.checkboxUseDigestAuthentication)
129                 .addComponent(panelUsername)
130                 .addComponent(panelPassword)
131                 .addComponent(this.checkboxUseKerberos)
132                 .addComponent(panelLoginConf)
133                 .addComponent(panelKrb5Conf)
134             )
135         );
136         
137         groupLayout.setVerticalGroup(
138             groupLayout
139             .createSequentialGroup()
140             .addGroup(
141                 groupLayout
142                 .createParallelGroup(GroupLayout.Alignment.BASELINE)
143                 .addComponent(labelOrigin)
144             )
145             .addGroup(
146                 groupLayout
147                 .createParallelGroup(GroupLayout.Alignment.BASELINE)
148                 .addComponent(this.checkboxUseDigestAuthentication)
149             )
150             .addGroup(
151                 groupLayout
152                 .createParallelGroup(GroupLayout.Alignment.BASELINE)
153                 .addComponent(panelUsername)
154             )
155             .addGroup(
156                 groupLayout
157                 .createParallelGroup(GroupLayout.Alignment.BASELINE)
158                 .addComponent(panelPassword)
159             )
160             .addGroup(
161                 groupLayout
162                 .createParallelGroup(GroupLayout.Alignment.BASELINE)
163                 .addComponent(this.checkboxUseKerberos)
164             )
165             .addGroup(
166                 groupLayout
167                 .createParallelGroup(GroupLayout.Alignment.BASELINE)
168                 .addComponent(panelLoginConf)
169             )
170             .addGroup(
171                 groupLayout
172                 .createParallelGroup(GroupLayout.Alignment.BASELINE)
173                 .addComponent(panelKrb5Conf)
174             )
175         );
176     }
177     
178     
179     // Getter and setter
180     
181     public JCheckBox getCheckboxUseDigestAuthentication() {
182         return this.checkboxUseDigestAuthentication;
183     }
184 
185     public JTextField getTextDigestAuthenticationUsername() {
186         return this.textDigestAuthenticationUsername;
187     }
188 
189     public JTextField getTextDigestAuthenticationPassword() {
190         return this.textDigestAuthenticationPassword;
191     }
192 
193     public JCheckBox getCheckboxUseKerberos() {
194         return this.checkboxUseKerberos;
195     }
196 
197     public JTextField getTextKerberosKrb5Conf() {
198         return this.textKerberosKrb5Conf;
199     }
200 
201     public JTextField getTextKerberosLoginConf() {
202         return this.textKerberosLoginConf;
203     }
204 }