Skip to content

Package: SimpleAuthenticator

SimpleAuthenticator

nameinstructionbranchcomplexitylinemethod
SimpleAuthenticator(Frame)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
constrain(Component, GridBagLayout, GridBagConstraints)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getPasswordAuthentication()
M: 249 C: 0
0%
M: 16 C: 0
0%
M: 9 C: 0
0%
M: 58 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: import jakarta.mail.*;
12: import java.net.InetAddress;
13: import java.awt.*;
14: import javax.swing.*;
15:
16: /**
17: * Simple Authenticator for requesting password information.
18: *
19: * @author        Christopher Cotton
20: * @author        Bill Shannon
21: */
22:
23: public class SimpleAuthenticator extends Authenticator {
24:
25: Frame frame;
26: String username;
27: String password;
28:
29: public SimpleAuthenticator(Frame f) {
30:         this.frame = f;
31: }
32:
33: protected PasswordAuthentication getPasswordAuthentication() {
34:
35:         // given a prompt?
36:         String prompt = getRequestingPrompt();
37:•        if (prompt == null)
38:          prompt = "Please login...";
39:
40:         // protocol
41:         String protocol = getRequestingProtocol();
42:•        if (protocol == null)
43:          protocol = "Unknown protocol";
44:
45:         // get the host
46:         String host = null;
47:         InetAddress inet = getRequestingSite();
48:•        if (inet != null)
49:          host = inet.getHostName();
50:•        if (host == null)
51:          host = "Unknown host";
52:
53:         // port
54:         String port = "";
55:         int portnum = getRequestingPort();
56:•        if (portnum != -1)
57:          port = ", port " + portnum + " ";
58:
59:         // Build the info string
60:         String info = "Connecting to " + protocol + " mail service on host " +
61:                                                                 host + port;
62:
63:         //JPanel d = new JPanel();
64:         // XXX - for some reason using a JPanel here causes JOptionPane
65:         // to display incorrectly, so we workaround the problem using
66:         // an anonymous JComponent.
67:         JComponent d = new JComponent() { };
68:
69:         GridBagLayout gb = new GridBagLayout();
70:         GridBagConstraints c = new GridBagConstraints();
71:         d.setLayout(gb);
72:         c.insets = new Insets(2, 2, 2, 2);
73:
74:         c.anchor = GridBagConstraints.WEST;
75:         c.gridwidth = GridBagConstraints.REMAINDER;
76:         c.weightx = 0.0;
77:         d.add(constrain(new JLabel(info), gb, c));
78:         d.add(constrain(new JLabel(prompt), gb, c));
79:
80:         c.gridwidth = 1;
81:         c.anchor = GridBagConstraints.EAST;
82:         c.fill = GridBagConstraints.NONE;
83:         c.weightx = 0.0;
84:         d.add(constrain(new JLabel("Username:"), gb, c));
85:
86:         c.anchor = GridBagConstraints.EAST;
87:         c.fill = GridBagConstraints.HORIZONTAL;
88:         c.gridwidth = GridBagConstraints.REMAINDER;
89:         c.weightx = 1.0;
90:         String user = getDefaultUserName();
91:         JTextField username = new JTextField(user, 20);
92:         d.add(constrain(username, gb, c));
93:
94:         c.gridwidth = 1;
95:         c.fill = GridBagConstraints.NONE;
96:         c.anchor = GridBagConstraints.EAST;
97:         c.weightx = 0.0;
98:         d.add(constrain(new JLabel("Password:"), gb, c));
99:
100:         c.anchor = GridBagConstraints.EAST;
101:         c.fill = GridBagConstraints.HORIZONTAL;
102:         c.gridwidth = GridBagConstraints.REMAINDER;
103:         c.weightx = 1.0;
104:         JPasswordField password = new JPasswordField("", 20);
105:         d.add(constrain(password, gb, c));
106:         // XXX - following doesn't work
107:•        if (user != null && user.length() > 0)
108:          password.requestFocus();
109:         else
110:          username.requestFocus();
111:         
112:         int result = JOptionPane.showConfirmDialog(frame, d, "Login",
113:          JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
114:         
115:•        if (result == JOptionPane.OK_OPTION)
116:          return new PasswordAuthentication(username.getText(),
117:                                                 password.getText());
118:         else
119:          return null;
120: }
121:
122: private Component constrain(Component cmp,
123:                                 GridBagLayout gb, GridBagConstraints c) {
124:         gb.setConstraints(cmp, c);
125:         return (cmp);
126: }
127: }