Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKBJavaConnector/AuthenticationService/src/authservice/handler/UserNameHandler.java @ 10015

Last change on this file since 10015 was 6147, checked in by mholper, 14 years ago

added UserNameHandler for all serviceProject, central in ECJClient #1441

File size: 4.2 KB
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package authservice.handler;
6
7import java.io.BufferedReader;
8import java.io.IOException;
9import java.io.InputStream;
10import java.io.InputStreamReader;
11import java.io.PushbackInputStream;
12import java.util.Arrays;
13import javax.security.auth.callback.Callback;
14import javax.security.auth.callback.CallbackHandler;
15import javax.security.auth.callback.NameCallback;
16import javax.security.auth.callback.PasswordCallback;
17import javax.security.auth.callback.UnsupportedCallbackException;
18
19/**
20 *  Username/Password Callbackhandler
21 * @author MartinH
22 */
23public class UserNameHandler implements CallbackHandler {
24
25    static String user;
26    static String pwd;
27
28    public UserNameHandler(String userName, String passwd) {
29        setUser(userName);
30        setPassword(passwd);
31    }
32
33    static {
34        setUser("okbtester");
35        setPassword("okbtester");
36    }
37
38    public static void setUser(String userName) {
39        user = userName;
40    }
41
42    public static void setPassword(String passwd) {
43        pwd = passwd;
44    }
45
46    /**
47     * Invoke an array of Callbacks.
48     *
49     * <p>
50     *
51     * @param callbacks an array of <code>Callback</code> objects which contain
52     *      the information requested by an underlying security
53     *      service to be retrieved or displayed.
54     *
55     * @exception java.io.IOException if an input or output error occurs. <p>
56     *
57     * @exception UnsupportedCallbackException if the implementation of this
58     *      method does not support one or more of the Callbacks
59     *      specified in the <code>callbacks</code> parameter.
60     */
61    public void handle(Callback[] callbacks)
62            throws IOException, UnsupportedCallbackException {
63
64        for (int i = 0; i < callbacks.length; i++) {
65            if (callbacks[i] instanceof NameCallback) {
66
67                // prompt the user for a username
68                NameCallback nc = (NameCallback) callbacks[i];
69
70                System.err.print(nc.getPrompt());
71                System.err.flush();
72                nc.setName(user);
73                //nc.setName((new BufferedReader(new InputStreamReader(System.in))).readLine());
74
75            } else if (callbacks[i] instanceof PasswordCallback) {
76
77                // prompt the user for sensitive information
78                PasswordCallback pc = (PasswordCallback) callbacks[i];
79                System.err.print(pc.getPrompt());
80                System.err.flush();
81
82                pc.setPassword(pwd.toCharArray());
83                //pc.setPassword(readPassword(System.in));
84
85            } else {
86                throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
87            }
88        }
89    }
90
91    // Reads user password from given input stream.
92    private char[] readPassword(InputStream in) throws IOException {
93
94        char[] lineBuffer;
95        char[] buf;
96        int i;
97
98        buf = lineBuffer = new char[128];
99
100        int room = buf.length;
101        int offset = 0;
102        int c;
103
104        loop:
105        while (true) {
106            switch (c = in.read()) {
107                case -1:
108                case '\n':
109                    break loop;
110
111                case '\r':
112                    int c2 = in.read();
113                    if ((c2 != '\n') && (c2 != -1)) {
114                        if (!(in instanceof PushbackInputStream)) {
115                            in = new PushbackInputStream(in);
116                        }
117                        ((PushbackInputStream) in).unread(c2);
118                    } else {
119                        break loop;
120                    }
121
122                default:
123                    if (--room < 0) {
124                        buf = new char[offset + 128];
125                        room = buf.length - offset - 1;
126                        System.arraycopy(lineBuffer, 0, buf, 0, offset);
127                        Arrays.fill(lineBuffer, ' ');
128                        lineBuffer = buf;
129                    }
130                    buf[offset++] = (char) c;
131                    break;
132            }
133        }
134
135        if (offset == 0) {
136            return null;
137        }
138
139        char[] ret = new char[offset];
140        System.arraycopy(buf, 0, ret, 0, offset);
141        Arrays.fill(buf, ' ');
142
143        return ret;
144    }
145}
Note: See TracBrowser for help on using the repository browser.