| 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 | * Sometimes a network operation (such as hitting a password protected
|
|---|
| 42 | * web site) will require authentication information in the form of a
|
|---|
| 43 | * username and password. This abstract class provides a model for
|
|---|
| 44 | * obtaining that information.
|
|---|
| 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 | */
|
|---|
| 57 | public abstract class Authenticator
|
|---|
| 58 | {
|
|---|
| 59 |
|
|---|
| 60 | /*************************************************************************/
|
|---|
| 61 |
|
|---|
| 62 | /*
|
|---|
| 63 | * Class Variables
|
|---|
| 64 | */
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * This is the default Authenticator object to use for password requests
|
|---|
| 68 | */
|
|---|
| 69 | private static Authenticator default_authenticator;
|
|---|
| 70 |
|
|---|
| 71 | /*************************************************************************/
|
|---|
| 72 |
|
|---|
| 73 | /*
|
|---|
| 74 | * Instance Variables
|
|---|
| 75 | */
|
|---|
| 76 |
|
|---|
| 77 | /**
|
|---|
| 78 | * InternetAddress of the site requesting authentication
|
|---|
| 79 | */
|
|---|
| 80 | private InetAddress addr;
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * The port number of the site requesting authentication
|
|---|
| 84 | */
|
|---|
| 85 | private int port;
|
|---|
| 86 |
|
|---|
| 87 | /**
|
|---|
| 88 | * The protocol name of the site requesting authentication
|
|---|
| 89 | */
|
|---|
| 90 | private String protocol;
|
|---|
| 91 |
|
|---|
| 92 | /**
|
|---|
| 93 | * The prompt to display to the user when requesting authentication info
|
|---|
| 94 | */
|
|---|
| 95 | private String prompt;
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | * The authentication scheme in use
|
|---|
| 99 | */
|
|---|
| 100 | private String scheme;
|
|---|
| 101 |
|
|---|
| 102 | /*************************************************************************/
|
|---|
| 103 |
|
|---|
| 104 | /*
|
|---|
| 105 | * Class Methods
|
|---|
| 106 | */
|
|---|
| 107 |
|
|---|
| 108 | /**
|
|---|
| 109 | * This method sets the default <code>Authenticator</code> object (an
|
|---|
| 110 | * instance of a
|
|---|
| 111 | * subclass of <code>Authenticator</code>) to use when prompting the user for
|
|---|
| 112 | * 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 | * @param def_auth The new default <code>Authenticator</code> object to use
|
|---|
| 117 | *
|
|---|
| 118 | * @exception SecurityException If the caller does not have permission
|
|---|
| 119 | * to perform this operation
|
|---|
| 120 | */
|
|---|
| 121 | public static void
|
|---|
| 122 | setDefault(Authenticator def_auth)
|
|---|
| 123 | {
|
|---|
| 124 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 125 | if (sm != null)
|
|---|
| 126 | sm.checkPermission(new NetPermission("setDefaultAuthenticator"));
|
|---|
| 127 |
|
|---|
| 128 | default_authenticator = def_auth;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /*************************************************************************/
|
|---|
| 132 |
|
|---|
| 133 | /**
|
|---|
| 134 | * This method is called whenever a username and password for a given
|
|---|
| 135 | * network operation is required. First, a security check is made to see
|
|---|
| 136 | * if the caller has the "requestPasswordAuthentication"
|
|---|
| 137 | * permission. If not, the method thows an exception. If there is no
|
|---|
| 138 | * default <code>Authenticator</code> object, the method then returns
|
|---|
| 139 | * <code>null</code>. Otherwise, the default authenticators's instance
|
|---|
| 140 | * variables are initialized and it's <code>getPasswordAuthentication</code>
|
|---|
| 141 | * method is called to get the actual authentication information to return.
|
|---|
| 142 | *
|
|---|
| 143 | * @param addr The address requesting authentication
|
|---|
| 144 | * @param port The port requesting authentication
|
|---|
| 145 | * @param protocol The protocol requesting authentication
|
|---|
| 146 | * @param prompt The prompt to display to the user when requesting
|
|---|
| 147 | authentication info
|
|---|
| 148 | * @param scheme The authentication scheme in use
|
|---|
| 149 | *
|
|---|
| 150 | * @return A <code>PasswordAuthentication</code> object with the user's
|
|---|
| 151 | * authentication info.
|
|---|
| 152 | *
|
|---|
| 153 | * @exception SecurityException If the caller does not have permission to
|
|---|
| 154 | * perform this operation
|
|---|
| 155 | */
|
|---|
| 156 | public static PasswordAuthentication
|
|---|
| 157 | requestPasswordAuthentication(InetAddress addr, int port, String protocol,
|
|---|
| 158 | String prompt, String scheme)
|
|---|
| 159 | {
|
|---|
| 160 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 161 | if (sm != null)
|
|---|
| 162 | sm.checkPermission(new NetPermission("requestPasswordAuthentication"));
|
|---|
| 163 |
|
|---|
| 164 | if (default_authenticator == null)
|
|---|
| 165 | return(null);
|
|---|
| 166 |
|
|---|
| 167 | default_authenticator.addr = addr;
|
|---|
| 168 | default_authenticator.port = port;
|
|---|
| 169 | default_authenticator.protocol = protocol;
|
|---|
| 170 | default_authenticator.prompt = prompt;
|
|---|
| 171 | default_authenticator.scheme = scheme;
|
|---|
| 172 |
|
|---|
| 173 | return(default_authenticator.getPasswordAuthentication());
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | /*************************************************************************/
|
|---|
| 177 |
|
|---|
| 178 | /*
|
|---|
| 179 | * Constructors
|
|---|
| 180 | */
|
|---|
| 181 |
|
|---|
| 182 | /**
|
|---|
| 183 | * Default, no-argument constructor for subclasses to call.
|
|---|
| 184 | */
|
|---|
| 185 | public
|
|---|
| 186 | Authenticator()
|
|---|
| 187 | {
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /*************************************************************************/
|
|---|
| 191 |
|
|---|
| 192 | /*
|
|---|
| 193 | * Instance Methods
|
|---|
| 194 | */
|
|---|
| 195 |
|
|---|
| 196 | /**
|
|---|
| 197 | * This method returns the address of the site that is requesting
|
|---|
| 198 | * authentication.
|
|---|
| 199 | *
|
|---|
| 200 | * @return The requesting site
|
|---|
| 201 | */
|
|---|
| 202 | protected final InetAddress
|
|---|
| 203 | getRequestingSite()
|
|---|
| 204 | {
|
|---|
| 205 | return(addr);
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | /*************************************************************************/
|
|---|
| 209 |
|
|---|
| 210 | /**
|
|---|
| 211 | * This method returns the port of the site that is requesting
|
|---|
| 212 | * authentication.
|
|---|
| 213 | *
|
|---|
| 214 | * @return The requesting port
|
|---|
| 215 | */
|
|---|
| 216 | protected final int
|
|---|
| 217 | getRequestingPort()
|
|---|
| 218 | {
|
|---|
| 219 | return(port);
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | /*************************************************************************/
|
|---|
| 223 |
|
|---|
| 224 | /**
|
|---|
| 225 | * This method returns the requesting protocol of the operation that is
|
|---|
| 226 | * requesting authentication
|
|---|
| 227 | *
|
|---|
| 228 | * @return The requesting protocol
|
|---|
| 229 | */
|
|---|
| 230 | protected final String
|
|---|
| 231 | getRequestingProtocol()
|
|---|
| 232 | {
|
|---|
| 233 | return(protocol);
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | /*************************************************************************/
|
|---|
| 237 |
|
|---|
| 238 | /**
|
|---|
| 239 | * Returns the prompt that should be used when requesting authentication
|
|---|
| 240 | * information from the user
|
|---|
| 241 | *
|
|---|
| 242 | * @return The user prompt
|
|---|
| 243 | */
|
|---|
| 244 | protected final String
|
|---|
| 245 | getRequestingPrompt()
|
|---|
| 246 | {
|
|---|
| 247 | return(prompt);
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | /*************************************************************************/
|
|---|
| 251 |
|
|---|
| 252 | /**
|
|---|
| 253 | * This method returns the authentication scheme in use
|
|---|
| 254 | *
|
|---|
| 255 | * @return The authentication scheme
|
|---|
| 256 | */
|
|---|
| 257 | protected final String
|
|---|
| 258 | getRequestingScheme()
|
|---|
| 259 | {
|
|---|
| 260 | return(scheme);
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | /*************************************************************************/
|
|---|
| 264 |
|
|---|
| 265 | /**
|
|---|
| 266 | * This method is called whenever a request for authentication is made. It
|
|---|
| 267 | * can call the other getXXX methods to determine the information relevant
|
|---|
| 268 | * to this request. Subclasses should override this method, which returns
|
|---|
| 269 | * <code>null</code> by default.
|
|---|
| 270 | *
|
|---|
| 271 | * @return The PasswordAuthentication information
|
|---|
| 272 | */
|
|---|
| 273 | protected PasswordAuthentication
|
|---|
| 274 | getPasswordAuthentication()
|
|---|
| 275 | {
|
|---|
| 276 | return(null);
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | } // class Authenticator
|
|---|
| 280 |
|
|---|