| 1 | /* Authenticator.java -- Abstract class for obtaining authentication info
|
|---|
| 2 | Copyright (C) 1998,2000 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 | package java.net;
|
|---|
| 39 |
|
|---|
| 40 | /**
|
|---|
| 41 | * This abstract class provides a model for obtaining authentication
|
|---|
| 42 | * information (in the form of a username and password) required by
|
|---|
| 43 | * some network operations (such as hitting a password protected
|
|---|
| 44 | * web site).
|
|---|
| 45 | * <p>
|
|---|
| 46 | * To make use of this feature, a programmer must create a subclass of
|
|---|
| 47 | * Authenticator 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.
|
|---|
| 49 | * After creating an instance of that subclass, the static setDefault
|
|---|
| 50 | * method of this class is called to set up that instance as the object
|
|---|
| 51 | * to use on subsequent calls to obtain authorization.
|
|---|
| 52 | *
|
|---|
| 53 | * @since 1.2
|
|---|
| 54 | *
|
|---|
| 55 | * @author Aaron M. Renn ([email protected])
|
|---|
| 56 | * @status Believed to be JDK 1.4 complete
|
|---|
| 57 | */
|
|---|
| 58 | public abstract class Authenticator
|
|---|
| 59 | {
|
|---|
| 60 |
|
|---|
| 61 | /*************************************************************************/
|
|---|
| 62 |
|
|---|
| 63 | /*
|
|---|
| 64 | * Class Variables
|
|---|
| 65 | */
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * This is the default Authenticator object to use for password requests
|
|---|
| 69 | */
|
|---|
| 70 | private static Authenticator default_authenticator;
|
|---|
| 71 |
|
|---|
| 72 | /*************************************************************************/
|
|---|
| 73 |
|
|---|
| 74 | /*
|
|---|
| 75 | * Instance Variables
|
|---|
| 76 | */
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * The hostname of the site requesting authentication
|
|---|
| 80 | */
|
|---|
| 81 | private String host;
|
|---|
| 82 |
|
|---|
| 83 | /**
|
|---|
| 84 | * InternetAddress of the site requesting authentication
|
|---|
| 85 | */
|
|---|
| 86 | private InetAddress addr;
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * The port number of the site requesting authentication
|
|---|
| 90 | */
|
|---|
| 91 | private int port;
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * The protocol name of the site requesting authentication
|
|---|
| 95 | */
|
|---|
| 96 | private String protocol;
|
|---|
| 97 |
|
|---|
| 98 | /**
|
|---|
| 99 | * The prompt to display to the user when requesting authentication info
|
|---|
| 100 | */
|
|---|
| 101 | private String prompt;
|
|---|
| 102 |
|
|---|
| 103 | /**
|
|---|
| 104 | * The authentication scheme in use
|
|---|
| 105 | */
|
|---|
| 106 | private String scheme;
|
|---|
| 107 |
|
|---|
| 108 | /*************************************************************************/
|
|---|
| 109 |
|
|---|
| 110 | /*
|
|---|
| 111 | * Class Methods
|
|---|
| 112 | */
|
|---|
| 113 |
|
|---|
| 114 | /**
|
|---|
| 115 | * This method sets the default <code>Authenticator</code> object (an
|
|---|
| 116 | * instance of a
|
|---|
| 117 | * subclass of <code>Authenticator</code>) to use when prompting the user for
|
|---|
| 118 | * information. Note that this method checks to see if the caller is
|
|---|
| 119 | * allowed to set this value (the "setDefaultAuthenticator" permission)
|
|---|
| 120 | * and throws a <code>SecurityException</code> if it is not.
|
|---|
| 121 | *
|
|---|
| 122 | * @param def_auth The new default <code>Authenticator</code> object to use
|
|---|
| 123 | *
|
|---|
| 124 | * @exception SecurityException If the caller does not have permission
|
|---|
| 125 | * to perform this operation
|
|---|
| 126 | */
|
|---|
| 127 | public static void
|
|---|
| 128 | setDefault(Authenticator def_auth)
|
|---|
| 129 | {
|
|---|
| 130 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 131 | if (sm != null)
|
|---|
| 132 | sm.checkPermission(new NetPermission("setDefaultAuthenticator"));
|
|---|
| 133 |
|
|---|
| 134 | default_authenticator = def_auth;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /*************************************************************************/
|
|---|
| 138 |
|
|---|
| 139 | /**
|
|---|
| 140 | * This method is called whenever a username and password for a given
|
|---|
| 141 | * network operation is required. First, a security check is made to see
|
|---|
| 142 | * if the caller has the "requestPasswordAuthentication"
|
|---|
| 143 | * permission. If not, the method thows an exception. If there is no
|
|---|
| 144 | * default <code>Authenticator</code> object, the method then returns
|
|---|
| 145 | * <code>null</code>. Otherwise, the default authenticators's instance
|
|---|
| 146 | * variables are initialized and it's <code>getPasswordAuthentication</code>
|
|---|
| 147 | * method is called to get the actual authentication information to return.
|
|---|
| 148 | *
|
|---|
| 149 | * @param addr The address requesting authentication
|
|---|
| 150 | * @param port The port requesting authentication
|
|---|
| 151 | * @param protocol The protocol requesting authentication
|
|---|
| 152 | * @param prompt The prompt to display to the user when requesting
|
|---|
| 153 | * authentication info
|
|---|
| 154 | * @param scheme The authentication scheme in use
|
|---|
| 155 | *
|
|---|
| 156 | * @return A <code>PasswordAuthentication</code> object with the user's
|
|---|
| 157 | * authentication info.
|
|---|
| 158 | *
|
|---|
| 159 | * @exception SecurityException If the caller does not have permission to
|
|---|
| 160 | * perform this operation
|
|---|
| 161 | */
|
|---|
| 162 | public static PasswordAuthentication
|
|---|
| 163 | requestPasswordAuthentication(InetAddress addr, int port, String protocol,
|
|---|
| 164 | String prompt, String scheme)
|
|---|
| 165 | throws SecurityException
|
|---|
| 166 | {
|
|---|
| 167 | return(requestPasswordAuthentication (null, addr, port, protocol,
|
|---|
| 168 | prompt, scheme));
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /**
|
|---|
| 172 | * This method is called whenever a username and password for a given
|
|---|
| 173 | * network operation is required. First, a security check is made to see
|
|---|
| 174 | * if the caller has the "requestPasswordAuthentication"
|
|---|
| 175 | * permission. If not, the method thows an exception. If there is no
|
|---|
| 176 | * default <code>Authenticator</code> object, the method then returns
|
|---|
| 177 | * <code>null</code>. Otherwise, the default authenticators's instance
|
|---|
| 178 | * variables are initialized and it's <code>getPasswordAuthentication</code>
|
|---|
| 179 | * method is called to get the actual authentication information to return.
|
|---|
| 180 | * This method is the preferred one as it can be used with hostname
|
|---|
| 181 | * when addr is unknown.
|
|---|
| 182 | *
|
|---|
| 183 | * @param host The hostname requesting authentication
|
|---|
| 184 | * @param addr The address requesting authentication
|
|---|
| 185 | * @param port The port requesting authentication
|
|---|
| 186 | * @param protocol The protocol requesting authentication
|
|---|
| 187 | * @param prompt The prompt to display to the user when requesting
|
|---|
| 188 | * authentication info
|
|---|
| 189 | * @param scheme The authentication scheme in use
|
|---|
| 190 | *
|
|---|
| 191 | * @return A <code>PasswordAuthentication</code> object with the user's
|
|---|
| 192 | * authentication info.
|
|---|
| 193 | *
|
|---|
| 194 | * @exception SecurityException If the caller does not have permission to
|
|---|
| 195 | * perform this operation
|
|---|
| 196 | *
|
|---|
| 197 | * @since 1.4
|
|---|
| 198 | */
|
|---|
| 199 | public static PasswordAuthentication
|
|---|
| 200 | requestPasswordAuthentication(String host, InetAddress addr, int port,
|
|---|
| 201 | String protocol, String prompt, String scheme)
|
|---|
| 202 | throws SecurityException
|
|---|
| 203 | {
|
|---|
| 204 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 205 | if (sm != null)
|
|---|
| 206 | sm.checkPermission(new NetPermission("requestPasswordAuthentication"));
|
|---|
| 207 |
|
|---|
| 208 | if (default_authenticator == null)
|
|---|
| 209 | return(null);
|
|---|
| 210 |
|
|---|
| 211 | default_authenticator.host = host;
|
|---|
| 212 | default_authenticator.addr = addr;
|
|---|
| 213 | default_authenticator.port = port;
|
|---|
| 214 | default_authenticator.protocol = protocol;
|
|---|
| 215 | default_authenticator.prompt = prompt;
|
|---|
| 216 | default_authenticator.scheme = scheme;
|
|---|
| 217 |
|
|---|
| 218 | return(default_authenticator.getPasswordAuthentication());
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | /**
|
|---|
| 222 | * Returns the hostname of the host or proxy requesting authorization,
|
|---|
| 223 | * or null if not available.
|
|---|
| 224 | *
|
|---|
| 225 | * @since 1.4
|
|---|
| 226 | */
|
|---|
| 227 | protected final String getRequestingHost()
|
|---|
| 228 | {
|
|---|
| 229 | return(host);
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | /*************************************************************************/
|
|---|
| 233 |
|
|---|
| 234 | /*
|
|---|
| 235 | * Constructors
|
|---|
| 236 | */
|
|---|
| 237 |
|
|---|
| 238 | /**
|
|---|
| 239 | * Default, no-argument constructor for subclasses to call.
|
|---|
| 240 | */
|
|---|
| 241 | public
|
|---|
| 242 | Authenticator()
|
|---|
| 243 | {
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | /*************************************************************************/
|
|---|
| 247 |
|
|---|
| 248 | /*
|
|---|
| 249 | * Instance Methods
|
|---|
| 250 | */
|
|---|
| 251 |
|
|---|
| 252 | /**
|
|---|
| 253 | * This method returns the address of the site that is requesting
|
|---|
| 254 | * authentication.
|
|---|
| 255 | *
|
|---|
| 256 | * @return The requesting site
|
|---|
| 257 | */
|
|---|
| 258 | protected final InetAddress
|
|---|
| 259 | getRequestingSite()
|
|---|
| 260 | {
|
|---|
| 261 | return(addr);
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | /*************************************************************************/
|
|---|
| 265 |
|
|---|
| 266 | /**
|
|---|
| 267 | * This method returns the port of the site that is requesting
|
|---|
| 268 | * authentication.
|
|---|
| 269 | *
|
|---|
| 270 | * @return The requesting port
|
|---|
| 271 | */
|
|---|
| 272 | protected final int
|
|---|
| 273 | getRequestingPort()
|
|---|
| 274 | {
|
|---|
| 275 | return(port);
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | /*************************************************************************/
|
|---|
| 279 |
|
|---|
| 280 | /**
|
|---|
| 281 | * This method returns the requesting protocol of the operation that is
|
|---|
| 282 | * requesting authentication
|
|---|
| 283 | *
|
|---|
| 284 | * @return The requesting protocol
|
|---|
| 285 | */
|
|---|
| 286 | protected final String
|
|---|
| 287 | getRequestingProtocol()
|
|---|
| 288 | {
|
|---|
| 289 | return(protocol);
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | /*************************************************************************/
|
|---|
| 293 |
|
|---|
| 294 | /**
|
|---|
| 295 | * Returns the prompt that should be used when requesting authentication
|
|---|
| 296 | * information from the user
|
|---|
| 297 | *
|
|---|
| 298 | * @return The user prompt
|
|---|
| 299 | */
|
|---|
| 300 | protected final String
|
|---|
| 301 | getRequestingPrompt()
|
|---|
| 302 | {
|
|---|
| 303 | return(prompt);
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | /*************************************************************************/
|
|---|
| 307 |
|
|---|
| 308 | /**
|
|---|
| 309 | * This method returns the authentication scheme in use
|
|---|
| 310 | *
|
|---|
| 311 | * @return The authentication scheme
|
|---|
| 312 | */
|
|---|
| 313 | protected final String
|
|---|
| 314 | getRequestingScheme()
|
|---|
| 315 | {
|
|---|
| 316 | return(scheme);
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | /*************************************************************************/
|
|---|
| 320 |
|
|---|
| 321 | /**
|
|---|
| 322 | * This method is called whenever a request for authentication is made. It
|
|---|
| 323 | * can call the other getXXX methods to determine the information relevant
|
|---|
| 324 | * to this request. Subclasses should override this method, which returns
|
|---|
| 325 | * <code>null</code> by default.
|
|---|
| 326 | *
|
|---|
| 327 | * @return The PasswordAuthentication information
|
|---|
| 328 | */
|
|---|
| 329 | protected PasswordAuthentication
|
|---|
| 330 | getPasswordAuthentication()
|
|---|
| 331 | {
|
|---|
| 332 | return(null);
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | } // class Authenticator
|
|---|
| 336 |
|
|---|