Debian Codegraph @

demo/src/main/java/internal/TtyAuthenticator.java

raw
1 /*
2 * Copyright (c) 1997, 2020 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 java.io.*;
12 import java.net.*;
13 import jakarta.mail.*;
14 import jakarta.mail.PasswordAuthentication;
15 import jakarta.mail.Authenticator;
16
17 /**
18 * A simple Authenticator that prompts for the user name and password on stdin.
19 * Puts up a dialog something like:
20 * <p> <pre>
21 * Connecting to &lt;protocol&gt; mail service on host &lt;addr&gt;, port &lt;port&gt;.
22 * &lt;prompt&gt;
23 *
24 * User Name: [defaultUserName]
25 * Password:
26 * </pre> <p>
27 *
28 * @author Bill Shannon
29 */
30
31 public class TtyAuthenticator extends Authenticator {
32
33 /**
34 * @return The PasswordAuthentication collected from the
35 * user, or null if none is provided.
36 */
37 protected PasswordAuthentication getPasswordAuthentication() {
38 BufferedReader in = new BufferedReader(
39 new InputStreamReader((System.in)));
40 StringBuffer sb = new StringBuffer();
41 sb.append("Connecting to ");
42 sb.append(getRequestingProtocol());
43 sb.append(" mail service on host ");
44 sb.append(getRequestingSite().getHostName());
45 int port = getRequestingPort();
46 if (port > 0) {
47 sb.append(", port ");
48 sb.append(port);
49 }
50 sb.append(".");
51 System.out.println(sb.toString());
52 String prompt = getRequestingPrompt();
53 if (prompt != null)
54 System.out.println(prompt);
55 System.out.println();
56 String userName = get(in, "User Name", getDefaultUserName());
57 String password = getpw("Password");
58 if (userName == null)
59 return null;
60 else
61 return new PasswordAuthentication(userName, password);
62 }
63
64 private static final String get(BufferedReader in,
65 String name, String value) {
66 PrintStream p = System.out;
67
68 p.print(name + ": ");
69 if (value != null)
70 p.print("[" + value + "] ");
71 p.flush();
72
73 try {
74 String s = in.readLine();
75 if (s.length() == 0)
76 return value;
77 else
78 return s;
79 } catch (IOException e) {
80 return value;
81 }
82 }
83
84 private static final String getpw(String name) {
85 Console cons;
86 char[] passwd;
87 if ((cons = System.console()) != null &&
88 (passwd = cons.readPassword("[%s] ", name)) != null)
89 return new String(passwd);
90 return "";
91 }
92
93 // main program, for debugging.
94 // Usage: java TtyAuthenticator host port protocol prompt defaultUser
95 public static void main(String argv[]) throws Exception {
96 Session sess = Session.getInstance(System.getProperties(),
97 new TtyAuthenticator());
98 PasswordAuthentication pw = sess.requestPasswordAuthentication(
99 InetAddress.getByName(argv[0]),
100 Integer.parseInt(argv[1]), argv[2], z(argv[3]), z(argv[4]));
101 System.out.println("User: " + n(pw.getUserName()));
102 System.out.println("Password: " + n(pw.getPassword()));
103 }
104
105 private static final String n(String s) {
106 return s == null ? "<null>" : s;
107 }
108
109 private static final String z(String s) {
110 return s.length() > 0 ? s : null;
111 }
112 }