Authenticator.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF 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
18package java.net;
19
20/**
21 * An implementation of this class is able to obtain authentication information
22 * for a connection in several ways. For this purpose it has to set the default
23 * authenticator which extends {@code Authenticator} by {@code
24 * setDefault(Authenticator a)}. Then it should override {@code
25 * getPasswordAuthentication()} which dictates how the authentication info is
26 * obtained. Usually, it prompts the user for the required input.
27 *
28 * @see #setDefault
29 * @see #getPasswordAuthentication
30 * @since Android 1.0
31 */
32public abstract class Authenticator {
33
34    // the default authenticator that needs to be set
35    private static Authenticator thisAuthenticator;
36
37    private static final NetPermission requestPasswordAuthenticationPermission = new NetPermission(
38            "requestPasswordAuthentication"); //$NON-NLS-1$
39
40    private static final NetPermission setDefaultAuthenticatorPermission = new NetPermission(
41            "setDefaultAuthenticator"); //$NON-NLS-1$
42
43    // the requester connection info
44    private String host;
45
46    private InetAddress addr;
47
48    private int port;
49
50    private String protocol;
51
52    private String prompt;
53
54    private String scheme;
55
56    private URL url;
57
58    private RequestorType rt;
59
60    /**
61     * Returns the collected username and password for authorization. The
62     * subclass has to override this method to return a value different to the
63     * default which is {@code null}.
64     * <p>
65     * Returns {@code null} by default.
66     * </p>
67     *
68     * @return collected password authentication data.
69     * @since Android 1.0
70     */
71    protected PasswordAuthentication getPasswordAuthentication() {
72        return null;
73    }
74
75    /**
76     * Returns the port of the connection that requests authorization.
77     *
78     * @return port of the connection.
79     * @since Android 1.0
80     */
81    protected final int getRequestingPort() {
82        return this.port;
83    }
84
85    /**
86     * Returns the address of the connection that requests authorization or
87     * {@code null} if unknown.
88     *
89     * @return address of the connection.
90     * @since Android 1.0
91     */
92    protected final InetAddress getRequestingSite() {
93        return this.addr;
94    }
95
96    /**
97     * Returns the realm (prompt string) of the connection that requests
98     * authorization.
99     *
100     * @return prompt string of the connection.
101     * @since Android 1.0
102     */
103    protected final String getRequestingPrompt() {
104        return this.prompt;
105    }
106
107    /**
108     * Returns the protocol of the connection that requests authorization.
109     *
110     * @return protocol of the connection.
111     * @since Android 1.0
112     */
113    protected final String getRequestingProtocol() {
114        return this.protocol;
115    }
116
117    /**
118     * Returns the scheme of the connection that requests authorization, for
119     * example HTTP Basic Authentication.
120     *
121     * @return scheme of the connection.
122     * @since Android 1.0
123     */
124    protected final String getRequestingScheme() {
125        return this.scheme;
126    }
127
128    /**
129     * If the permission check of the security manager does not result in a
130     * security exception, this method invokes the methods of the registered
131     * authenticator to get the authentication info.
132     *
133     * @return password authentication info or {@code null} if no authenticator
134     *         exists.
135     * @param rAddr
136     *            address of the connection that requests authentication.
137     * @param rPort
138     *            port of the connection that requests authentication.
139     * @param rProtocol
140     *            protocol of the connection that requests authentication.
141     * @param rPrompt
142     *            realm of the connection that requests authentication.
143     * @param rScheme
144     *            scheme of the connection that requests authentication.
145     * @throws SecurityException
146     *             if a security manager denies the password authentication
147     *             permission.
148     * @since Android 1.0
149     */
150    public static synchronized PasswordAuthentication requestPasswordAuthentication(
151            InetAddress rAddr, int rPort, String rProtocol, String rPrompt,
152            String rScheme) {
153        SecurityManager sm = System.getSecurityManager();
154        if (sm != null) {
155            sm.checkPermission(requestPasswordAuthenticationPermission);
156        }
157        if (thisAuthenticator == null) {
158            return null;
159        }
160        // set the requester info so it knows what it is requesting
161        // authentication for
162        thisAuthenticator.addr = rAddr;
163        thisAuthenticator.port = rPort;
164        thisAuthenticator.protocol = rProtocol;
165        thisAuthenticator.prompt = rPrompt;
166        thisAuthenticator.scheme = rScheme;
167        thisAuthenticator.rt = RequestorType.SERVER;
168
169        // returns the authentication info obtained by the registered
170        // Authenticator
171        return thisAuthenticator.getPasswordAuthentication();
172    }
173
174    /**
175     * Sets {@code a} as the default authenticator. It will be called whenever
176     * the realm that the URL is pointing to requires authorization. If there is
177     * a security manager set then the caller must have the appropriate {@code
178     * NetPermission}.
179     *
180     * @param a
181     *            authenticator which has to be set as default.
182     * @throws SecurityException
183     *             if a security manager denies the password authentication
184     *             permission.
185     * @since Android 1.0
186     */
187    public static void setDefault(Authenticator a) {
188        SecurityManager sm = System.getSecurityManager();
189        if (sm != null) {
190            sm.checkPermission(setDefaultAuthenticatorPermission);
191        }
192        thisAuthenticator = a;
193    }
194
195    /**
196     * If the permission check of the security manager does not result in a
197     * security exception, this method invokes the methods of the registered
198     * authenticator to get the authentication info.
199     *
200     * @return password authentication info or {@code null} if no authenticator
201     *         exists.
202     * @param rHost
203     *            host name of the connection that requests authentication.
204     * @param rAddr
205     *            address of the connection that requests authentication.
206     * @param rPort
207     *            port of the connection that requests authentication.
208     * @param rProtocol
209     *            protocol of the connection that requests authentication.
210     * @param rPrompt
211     *            realm of the connection that requests authentication.
212     * @param rScheme
213     *            scheme of the connection that requests authentication.
214     * @throws SecurityException
215     *             if a security manager denies the password authentication
216     *             permission.
217     * @since Android 1.0
218     */
219    public static synchronized PasswordAuthentication requestPasswordAuthentication(
220            String rHost, InetAddress rAddr, int rPort, String rProtocol,
221            String rPrompt, String rScheme) {
222        SecurityManager sm = System.getSecurityManager();
223        if (sm != null) {
224            sm.checkPermission(requestPasswordAuthenticationPermission);
225        }
226        if (thisAuthenticator == null) {
227            return null;
228        }
229        // set the requester info so it knows what it is requesting
230        // authentication for
231        thisAuthenticator.host = rHost;
232        thisAuthenticator.addr = rAddr;
233        thisAuthenticator.port = rPort;
234        thisAuthenticator.protocol = rProtocol;
235        thisAuthenticator.prompt = rPrompt;
236        thisAuthenticator.scheme = rScheme;
237        thisAuthenticator.rt = RequestorType.SERVER;
238
239        // returns the authentication info obtained by the registered
240        // Authenticator
241        return thisAuthenticator.getPasswordAuthentication();
242    }
243
244    /**
245     * Returns the host name of the connection that requests authentication or
246     * {@code null} if unknown.
247     *
248     * @return name of the requesting host or {@code null}.
249     * @since Android 1.0
250     */
251    protected final String getRequestingHost() {
252        return host;
253    }
254
255    /**
256     * If the permission check of the security manager does not result in a
257     * security exception, this method invokes the methods of the registered
258     * authenticator to get the authentication info.
259     *
260     * @return password authentication info or {@code null} if no authenticator
261     *         exists.
262     * @param rHost
263     *            host name of the connection that requests authentication.
264     * @param rAddr
265     *            address of the connection that requests authentication.
266     * @param rPort
267     *            port of the connection that requests authentication.
268     * @param rProtocol
269     *            protocol of the connection that requests authentication.
270     * @param rPrompt
271     *            realm of the connection that requests authentication.
272     * @param rScheme
273     *            scheme of the connection that requests authentication.
274     * @param rURL
275     *            url of the connection that requests authentication.
276     * @param reqType
277     *            requestor type of the connection that requests authentication.
278     * @throws SecurityException
279     *             if a security manager denies the password authentication
280     *             permission.
281     * @since Android 1.0
282     */
283    public static PasswordAuthentication requestPasswordAuthentication(
284            String rHost, InetAddress rAddr, int rPort, String rProtocol,
285            String rPrompt, String rScheme, URL rURL,
286            Authenticator.RequestorType reqType) {
287        SecurityManager sm = System.getSecurityManager();
288        if (null != sm) {
289            sm.checkPermission(requestPasswordAuthenticationPermission);
290        }
291        if (null == thisAuthenticator) {
292            return null;
293        }
294        // sets the requester info so it knows what it is requesting
295        // authentication for
296        thisAuthenticator.host = rHost;
297        thisAuthenticator.addr = rAddr;
298        thisAuthenticator.port = rPort;
299        thisAuthenticator.protocol = rProtocol;
300        thisAuthenticator.prompt = rPrompt;
301        thisAuthenticator.scheme = rScheme;
302        thisAuthenticator.url = rURL;
303        thisAuthenticator.rt = reqType;
304
305        // returns the authentication info obtained by the registered
306        // Authenticator
307        return thisAuthenticator.getPasswordAuthentication();
308
309    }
310
311    /**
312     * Returns the URL of the authentication request.
313     *
314     * @return authentication request url.
315     * @since Android 1.0
316     */
317    protected URL getRequestingURL() {
318        return url;
319    }
320
321    /**
322     * Returns the type of this request, it can be {@code PROXY} or {@code SERVER}.
323     *
324     * @return RequestorType of the authentication request.
325     * @since Android 1.0
326     */
327    protected Authenticator.RequestorType getRequestorType() {
328        return rt;
329    }
330
331    /**
332     * Enumeration class for the origin of the authentication request.
333     *
334     * @since Android 1.0
335     */
336    public enum RequestorType {
337
338        /**
339         * Type of proxy server
340         */
341        PROXY,
342
343        /**
344         * Type of origin server
345         */
346        SERVER
347    }
348}
349