1 |
/* Authenticator.java -- Abstract class for obtaining authentication info |
/* Authenticator.java -- Abstract class for obtaining authentication info |
2 |
Copyright (C) 1998,2000 Free Software Foundation, Inc. |
Copyright (C) 1998, 2000, 2003 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
43 |
* some network operations (such as hitting a password protected |
* some network operations (such as hitting a password protected |
44 |
* web site). |
* web site). |
45 |
* <p> |
* <p> |
46 |
* To make use of this feature, a programmer must create a subclass of |
* To make use of this feature, a programmer must create a subclass |
47 |
* Authenticator that knows how to obtain the necessary info. An example |
* that knows how to obtain the necessary info. An example |
48 |
* would be a class that popped up a dialog box to prompt the user. |
* would be a class that popped up a dialog box to prompt the user. |
49 |
* After creating an instance of that subclass, the static setDefault |
* After creating an instance of that subclass, the static |
50 |
* method of this class is called to set up that instance as the object |
* <code>setDefault</code> method of this class is called to set up |
51 |
* to use on subsequent calls to obtain authorization. |
* that instance as the object to use on subsequent calls to obtain |
52 |
|
* authorization. |
53 |
* |
* |
54 |
* @since 1.2 |
* @since 1.2 |
55 |
* |
* |
58 |
*/ |
*/ |
59 |
public abstract class Authenticator |
public abstract class Authenticator |
60 |
{ |
{ |
61 |
|
/* |
62 |
/*************************************************************************/ |
* Class Variables |
63 |
|
*/ |
64 |
/* |
|
65 |
* Class Variables |
/** |
66 |
*/ |
* This is the default Authenticator object to use for password requests |
67 |
|
*/ |
68 |
/** |
private static Authenticator defaultAuthenticator; |
69 |
* This is the default Authenticator object to use for password requests |
|
70 |
*/ |
/* |
71 |
private static Authenticator default_authenticator; |
* Instance Variables |
72 |
|
*/ |
73 |
/*************************************************************************/ |
|
74 |
|
/** |
75 |
/* |
* The hostname of the site requesting authentication |
76 |
* Instance Variables |
*/ |
77 |
*/ |
private String host; |
78 |
|
|
79 |
/** |
/** |
80 |
* The hostname of the site requesting authentication |
* InternetAddress of the site requesting authentication |
81 |
*/ |
*/ |
82 |
private String host; |
private InetAddress addr; |
83 |
|
|
84 |
/** |
/** |
85 |
* InternetAddress of the site requesting authentication |
* The port number of the site requesting authentication |
86 |
*/ |
*/ |
87 |
private InetAddress addr; |
private int port; |
88 |
|
|
89 |
/** |
/** |
90 |
* The port number of the site requesting authentication |
* The protocol name of the site requesting authentication |
91 |
*/ |
*/ |
92 |
private int port; |
private String protocol; |
93 |
|
|
94 |
/** |
/** |
95 |
* The protocol name of the site requesting authentication |
* The prompt to display to the user when requesting authentication info |
96 |
*/ |
*/ |
97 |
private String protocol; |
private String prompt; |
98 |
|
|
99 |
/** |
/** |
100 |
* The prompt to display to the user when requesting authentication info |
* The authentication scheme in use |
101 |
*/ |
*/ |
102 |
private String prompt; |
private String scheme; |
103 |
|
|
104 |
/** |
/* |
105 |
* The authentication scheme in use |
* Class Methods |
106 |
*/ |
*/ |
107 |
private String scheme; |
|
108 |
|
/** |
109 |
/*************************************************************************/ |
* This method sets the default <code>Authenticator</code> object (an |
110 |
|
* instance of a subclass of <code>Authenticator</code>) to use when |
111 |
/* |
* prompting the user for |
112 |
* Class Methods |
* information. Note that this method checks to see if the caller is |
113 |
*/ |
* allowed to set this value (the "setDefaultAuthenticator" permission) |
114 |
|
* and throws a <code>SecurityException</code> if it is not. |
115 |
/** |
* |
116 |
* This method sets the default <code>Authenticator</code> object (an |
* @param defAuth The new default <code>Authenticator</code> object to use |
117 |
* instance of a |
* |
118 |
* subclass of <code>Authenticator</code>) to use when prompting the user for |
* @exception SecurityException If the caller does not have permission |
119 |
* information. Note that this method checks to see if the caller is |
* to perform this operation |
120 |
* allowed to set this value (the "setDefaultAuthenticator" permission) |
*/ |
121 |
* and throws a <code>SecurityException</code> if it is not. |
public static void setDefault(Authenticator defAuth) |
122 |
* |
{ |
123 |
* @param def_auth The new default <code>Authenticator</code> object to use |
SecurityManager sm = System.getSecurityManager(); |
124 |
* |
if (sm != null) |
125 |
* @exception SecurityException If the caller does not have permission |
sm.checkPermission(new NetPermission("setDefaultAuthenticator")); |
126 |
* to perform this operation |
|
127 |
*/ |
defaultAuthenticator = defAuth; |
128 |
public static void |
} |
129 |
setDefault(Authenticator def_auth) |
|
130 |
{ |
/** |
131 |
SecurityManager sm = System.getSecurityManager(); |
* This method is called whenever a username and password for a given |
132 |
if (sm != null) |
* network operation is required. First, a security check is made to see |
133 |
sm.checkPermission(new NetPermission("setDefaultAuthenticator")); |
* if the caller has the "requestPasswordAuthentication" |
134 |
|
* permission. If not, the method thows an exception. If there is no |
135 |
default_authenticator = def_auth; |
* default <code>Authenticator</code> object, the method then returns |
136 |
} |
* <code>null</code>. Otherwise, the default authenticators's instance |
137 |
|
* variables are initialized and it's <code>getPasswordAuthentication</code> |
138 |
/*************************************************************************/ |
* method is called to get the actual authentication information to return. |
139 |
|
* |
140 |
/** |
* @param addr The address requesting authentication |
141 |
* This method is called whenever a username and password for a given |
* @param port The port requesting authentication |
142 |
* network operation is required. First, a security check is made to see |
* @param protocol The protocol requesting authentication |
143 |
* if the caller has the "requestPasswordAuthentication" |
* @param prompt The prompt to display to the user when requesting |
144 |
* permission. If not, the method thows an exception. If there is no |
* authentication info |
145 |
* default <code>Authenticator</code> object, the method then returns |
* @param scheme The authentication scheme in use |
146 |
* <code>null</code>. Otherwise, the default authenticators's instance |
* |
147 |
* variables are initialized and it's <code>getPasswordAuthentication</code> |
* @return A <code>PasswordAuthentication</code> object with the user's |
148 |
* method is called to get the actual authentication information to return. |
* authentication info. |
149 |
* |
* |
150 |
* @param addr The address requesting authentication |
* @exception SecurityException If the caller does not have permission to |
151 |
* @param port The port requesting authentication |
* perform this operation |
152 |
* @param protocol The protocol requesting authentication |
*/ |
153 |
* @param prompt The prompt to display to the user when requesting |
public static PasswordAuthentication |
154 |
* authentication info |
requestPasswordAuthentication(InetAddress addr, int port, String protocol, |
155 |
* @param scheme The authentication scheme in use |
String prompt, String scheme) |
156 |
* |
throws SecurityException |
157 |
* @return A <code>PasswordAuthentication</code> object with the user's |
{ |
158 |
* authentication info. |
return(requestPasswordAuthentication (null, addr, port, protocol, |
159 |
* |
prompt, scheme)); |
160 |
* @exception SecurityException If the caller does not have permission to |
} |
161 |
* perform this operation |
|
162 |
*/ |
/** |
163 |
public static PasswordAuthentication |
* This method is called whenever a username and password for a given |
164 |
requestPasswordAuthentication(InetAddress addr, int port, String protocol, |
* network operation is required. First, a security check is made to see |
165 |
String prompt, String scheme) |
* if the caller has the "requestPasswordAuthentication" |
166 |
throws SecurityException |
* permission. If not, the method thows an exception. If there is no |
167 |
{ |
* default <code>Authenticator</code> object, the method then returns |
168 |
return(requestPasswordAuthentication (null, addr, port, protocol, |
* <code>null</code>. Otherwise, the default authenticators's instance |
169 |
prompt, scheme)); |
* variables are initialized and it's <code>getPasswordAuthentication</code> |
170 |
} |
* method is called to get the actual authentication information to return. |
171 |
|
* This method is the preferred one as it can be used with hostname |
172 |
/** |
* when addr is unknown. |
173 |
* This method is called whenever a username and password for a given |
* |
174 |
* network operation is required. First, a security check is made to see |
* @param host The hostname requesting authentication |
175 |
* if the caller has the "requestPasswordAuthentication" |
* @param addr The address requesting authentication |
176 |
* permission. If not, the method thows an exception. If there is no |
* @param port The port requesting authentication |
177 |
* default <code>Authenticator</code> object, the method then returns |
* @param protocol The protocol requesting authentication |
178 |
* <code>null</code>. Otherwise, the default authenticators's instance |
* @param prompt The prompt to display to the user when requesting |
179 |
* variables are initialized and it's <code>getPasswordAuthentication</code> |
* authentication info |
180 |
* method is called to get the actual authentication information to return. |
* @param scheme The authentication scheme in use |
181 |
* This method is the preferred one as it can be used with hostname |
* |
182 |
* when addr is unknown. |
* @return A <code>PasswordAuthentication</code> object with the user's |
183 |
* |
* authentication info. |
184 |
* @param host The hostname requesting authentication |
* |
185 |
* @param addr The address requesting authentication |
* @exception SecurityException If the caller does not have permission to |
186 |
* @param port The port requesting authentication |
* perform this operation |
187 |
* @param protocol The protocol requesting authentication |
* |
188 |
* @param prompt The prompt to display to the user when requesting |
* @since 1.4 |
189 |
* authentication info |
*/ |
190 |
* @param scheme The authentication scheme in use |
public static PasswordAuthentication |
191 |
* |
requestPasswordAuthentication(String host, InetAddress addr, int port, |
192 |
* @return A <code>PasswordAuthentication</code> object with the user's |
String protocol, String prompt, String scheme) |
193 |
* authentication info. |
throws SecurityException |
194 |
* |
{ |
195 |
* @exception SecurityException If the caller does not have permission to |
SecurityManager sm = System.getSecurityManager(); |
196 |
* perform this operation |
if (sm != null) |
197 |
* |
sm.checkPermission(new NetPermission("requestPasswordAuthentication")); |
198 |
* @since 1.4 |
|
199 |
*/ |
if (defaultAuthenticator == null) |
200 |
public static PasswordAuthentication |
return(null); |
201 |
requestPasswordAuthentication(String host, InetAddress addr, int port, |
|
202 |
String protocol, String prompt, String scheme) |
defaultAuthenticator.host = host; |
203 |
throws SecurityException |
defaultAuthenticator.addr = addr; |
204 |
{ |
defaultAuthenticator.port = port; |
205 |
SecurityManager sm = System.getSecurityManager(); |
defaultAuthenticator.protocol = protocol; |
206 |
if (sm != null) |
defaultAuthenticator.prompt = prompt; |
207 |
sm.checkPermission(new NetPermission("requestPasswordAuthentication")); |
defaultAuthenticator.scheme = scheme; |
208 |
|
|
209 |
if (default_authenticator == null) |
return(defaultAuthenticator.getPasswordAuthentication()); |
210 |
|
} |
211 |
|
|
212 |
|
/* |
213 |
|
* Constructors |
214 |
|
*/ |
215 |
|
|
216 |
|
/** |
217 |
|
* Default, no-argument constructor for subclasses to call. |
218 |
|
*/ |
219 |
|
public Authenticator() |
220 |
|
{ |
221 |
|
} |
222 |
|
|
223 |
|
/* |
224 |
|
* Instance Methods |
225 |
|
*/ |
226 |
|
|
227 |
|
/** |
228 |
|
* This method returns the address of the site that is requesting |
229 |
|
* authentication. |
230 |
|
* |
231 |
|
* @return The requesting site's address |
232 |
|
*/ |
233 |
|
protected final InetAddress getRequestingSite() |
234 |
|
{ |
235 |
|
return(addr); |
236 |
|
} |
237 |
|
|
238 |
|
/** |
239 |
|
* Returns the hostname of the host or proxy requesting authorization, |
240 |
|
* or <code>null</code> if not available. |
241 |
|
* |
242 |
|
* @return The name of the host requesting authentication, or |
243 |
|
* </code>null</code> if it is not available. |
244 |
|
* |
245 |
|
* @since 1.4 |
246 |
|
*/ |
247 |
|
protected final String getRequestingHost() |
248 |
|
{ |
249 |
|
return(host); |
250 |
|
} |
251 |
|
|
252 |
|
/** |
253 |
|
* This method returns the port of the site that is requesting |
254 |
|
* authentication. |
255 |
|
* |
256 |
|
* @return The requesting port |
257 |
|
*/ |
258 |
|
protected final int getRequestingPort() |
259 |
|
{ |
260 |
|
return(port); |
261 |
|
} |
262 |
|
|
263 |
|
/** |
264 |
|
* This method returns the requesting protocol of the operation that is |
265 |
|
* requesting authentication |
266 |
|
* |
267 |
|
* @return The requesting protocol |
268 |
|
*/ |
269 |
|
protected final String getRequestingProtocol() |
270 |
|
{ |
271 |
|
return(protocol); |
272 |
|
} |
273 |
|
|
274 |
|
/** |
275 |
|
* Returns the prompt that should be used when requesting authentication |
276 |
|
* information from the user |
277 |
|
* |
278 |
|
* @return The user prompt |
279 |
|
*/ |
280 |
|
protected final String getRequestingPrompt() |
281 |
|
{ |
282 |
|
return(prompt); |
283 |
|
} |
284 |
|
|
285 |
|
/** |
286 |
|
* This method returns the authentication scheme in use |
287 |
|
* |
288 |
|
* @return The authentication scheme |
289 |
|
*/ |
290 |
|
protected final String getRequestingScheme() |
291 |
|
{ |
292 |
|
return(scheme); |
293 |
|
} |
294 |
|
|
295 |
|
/** |
296 |
|
* This method is called whenever a request for authentication is made. It |
297 |
|
* can call the other getXXX methods to determine the information relevant |
298 |
|
* to this request. Subclasses should override this method, which returns |
299 |
|
* <code>null</code> by default. |
300 |
|
* |
301 |
|
* @return The <code>PasswordAuthentication</code> information |
302 |
|
*/ |
303 |
|
protected PasswordAuthentication getPasswordAuthentication() |
304 |
|
{ |
305 |
return(null); |
return(null); |
306 |
|
} |
|
default_authenticator.host = host; |
|
|
default_authenticator.addr = addr; |
|
|
default_authenticator.port = port; |
|
|
default_authenticator.protocol = protocol; |
|
|
default_authenticator.prompt = prompt; |
|
|
default_authenticator.scheme = scheme; |
|
|
|
|
|
return(default_authenticator.getPasswordAuthentication()); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Returns the hostname of the host or proxy requesting authorization, |
|
|
* or null if not available. |
|
|
* |
|
|
* @since 1.4 |
|
|
*/ |
|
|
protected final String getRequestingHost() |
|
|
{ |
|
|
return(host); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Constructors |
|
|
*/ |
|
|
|
|
|
/** |
|
|
* Default, no-argument constructor for subclasses to call. |
|
|
*/ |
|
|
public |
|
|
Authenticator() |
|
|
{ |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Instance Methods |
|
|
*/ |
|
|
|
|
|
/** |
|
|
* This method returns the address of the site that is requesting |
|
|
* authentication. |
|
|
* |
|
|
* @return The requesting site |
|
|
*/ |
|
|
protected final InetAddress |
|
|
getRequestingSite() |
|
|
{ |
|
|
return(addr); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* This method returns the port of the site that is requesting |
|
|
* authentication. |
|
|
* |
|
|
* @return The requesting port |
|
|
*/ |
|
|
protected final int |
|
|
getRequestingPort() |
|
|
{ |
|
|
return(port); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* This method returns the requesting protocol of the operation that is |
|
|
* requesting authentication |
|
|
* |
|
|
* @return The requesting protocol |
|
|
*/ |
|
|
protected final String |
|
|
getRequestingProtocol() |
|
|
{ |
|
|
return(protocol); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* Returns the prompt that should be used when requesting authentication |
|
|
* information from the user |
|
|
* |
|
|
* @return The user prompt |
|
|
*/ |
|
|
protected final String |
|
|
getRequestingPrompt() |
|
|
{ |
|
|
return(prompt); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* This method returns the authentication scheme in use |
|
|
* |
|
|
* @return The authentication scheme |
|
|
*/ |
|
|
protected final String |
|
|
getRequestingScheme() |
|
|
{ |
|
|
return(scheme); |
|
|
} |
|
|
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/** |
|
|
* This method is called whenever a request for authentication is made. It |
|
|
* can call the other getXXX methods to determine the information relevant |
|
|
* to this request. Subclasses should override this method, which returns |
|
|
* <code>null</code> by default. |
|
|
* |
|
|
* @return The PasswordAuthentication information |
|
|
*/ |
|
|
protected PasswordAuthentication |
|
|
getPasswordAuthentication() |
|
|
{ |
|
|
return(null); |
|
|
} |
|
307 |
|
|
308 |
} // class Authenticator |
} // class Authenticator |
309 |
|
|