home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / net / Authenticator.java next >
Encoding:
Java Source  |  1998-03-20  |  6.0 KB  |  193 lines

  1. /*
  2.  * @(#)Authenticator.java    1.9 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.net;
  16.  
  17. /**
  18.  * The class Authenticator represents an object that knows how to obtain
  19.  * authentication for a network connection.  Usually, it will do this
  20.  * by prompting the user for information.
  21.  * <p>
  22.  * Applications use this class by creating a subclass, and registering
  23.  * an instance of that subclass with the system with setDefault().
  24.  * When authentication is required, the system will invoke a method
  25.  * on the subclass (like getPasswordAuthentication).  The subclass's
  26.  * method can query about the authentication being requested with a
  27.  * number of inherited methods (getRequestingXXX()), and form an
  28.  * appropriate message for the user.
  29.  * <p>
  30.  * All methods that request authentication have a default implementation
  31.  * that fails.
  32.  *
  33.  * @see java.net.Authenticator.setDefault(java.net.ConnectionAuthenticator)
  34.  * @see java.net.getPasswordAuthentication()
  35.  *
  36.  * @author  Bill Foote
  37.  * @version 1.9, 03/18/98
  38.  * @since   JDK1.2
  39.  */
  40.  
  41. // There are no abstract methods, but to be useful the user must
  42. // subclass.
  43. public abstract 
  44. class Authenticator {
  45.  
  46.     // The system-wide authenticator object.  See setDefault().
  47.     private static Authenticator theAuthenticator;
  48.  
  49.     private InetAddress requestingSite;
  50.     private int requestingPort;
  51.     private String requestingProtocol;
  52.     private String requestingPrompt;
  53.     private String requestingScheme;
  54.  
  55.     private void reset() {
  56.     requestingSite = null;
  57.     requestingPort = -1;
  58.     requestingProtocol = null;
  59.     requestingPrompt = null;
  60.     requestingScheme = null;
  61.     }
  62.  
  63.  
  64.     /**
  65.      * Sets the authenticator that will be used by the networking code
  66.      * when a proxy or an HTTP server asks for authenticator.
  67.      * If an Authenticator has already been established
  68.      * as the current authenticator, no action will be taken.
  69.      * If the argument is <code>null</code> and no
  70.      * authenticator has been established, then no action is taken
  71.      * and the method simply returns.
  72.      * <p>
  73.      * If the caller does not have the java.net.Net "Authenticator.setDefault"
  74.      * permission, then a java.lang.SecurityException will be thrown.
  75.      * <p>
  76.      * Typically, this will be called exactly once, at system startup.
  77.      *
  78.      * @param    a    The authorizer
  79.      */
  80.     public static void setDefault(Authenticator a) {
  81.     NetPermission setDefaultPermission
  82.         = new NetPermission("Authenticator.setDefault");
  83.     SecurityManager sm = System.getSecurityManager();
  84.     if (sm != null) sm.checkPermission(setDefaultPermission);
  85.     if (theAuthenticator != null) {
  86.         return;
  87.     }
  88.     theAuthenticator = a;
  89.     }
  90.  
  91.     /**
  92.      * Ask the authenticator that has been registered with the system
  93.      * for a password.
  94.      * <p>
  95.      * If the caller does not have
  96.      * java.net.Net "Authenticator.requestPasswordAuthentication" permission,
  97.      * a java.lang.SecurityException will be thrown.
  98.      *
  99.      * @param addr The InetAddress of the site requesting authorization,
  100.      *             or null if not known.
  101.      * @param port the port for the requested connection
  102.      * @param protocol The protocol that's requesting the connection
  103.      *          (@see java.net.Authenticator.getProtocol())
  104.      * @param prompt A prompt string for the user
  105.      * @param scheme The authentication scheme
  106.      *
  107.      * @return The username/password, or null if one can't be gotten.
  108.      */
  109.     public static PasswordAuthentication requestPasswordAuthentication(
  110.                         InetAddress addr,
  111.                         int port,
  112.                         String protocol,
  113.                         String prompt,
  114.                         String scheme) {
  115.  
  116.     NetPermission requestPermission
  117.         = new NetPermission("Authenticator.requestPasswordAuthentication");
  118.     SecurityManager sm = System.getSecurityManager();
  119.     if (sm != null) sm.checkPermission(requestPermission);
  120.  
  121.     Authenticator a = theAuthenticator;
  122.     if (a == null) {
  123.         return null;
  124.     } else {
  125.         synchronized(a) {
  126.         a.reset();
  127.         a.requestingSite = addr;
  128.         a.requestingPort = port;
  129.         a.requestingProtocol = protocol;
  130.         a.requestingPrompt = prompt;
  131.         a.requestingScheme = scheme;
  132.         return a.getPasswordAuthentication();
  133.         }
  134.     }
  135.     }
  136.  
  137.     /**
  138.      * @return the InetAddress of the site requesting authorization, or null
  139.      *        if it's not available.
  140.      */
  141.     protected final InetAddress getRequestingSite() {
  142.     return requestingSite;
  143.     }
  144.  
  145.     /**
  146.      * @return the port for the requested connection
  147.      */
  148.     protected final int getRequestingPort() {
  149.     return requestingPort;
  150.     }
  151.  
  152.     /**
  153.      * Give the protocol that's requesting the connection.  Often this
  154.      * will be based on a URL, but in a future JDK it could be, for
  155.      * example, "SOCKS" for a password-protected SOCKS5 firewall.
  156.      *
  157.      * @return the protcol, optionally followed by "/version", where
  158.      *        version is a version number.
  159.      *
  160.      * @see java.net.URL.getProtocol()
  161.      */
  162.     protected final String getRequestingProtocol() {
  163.     return requestingProtocol;
  164.     }
  165.  
  166.     /**
  167.      * @return the prompt string given by the requestor (realm for
  168.      *        http requests)
  169.      */
  170.     protected final String getRequestingPrompt() {
  171.     return requestingPrompt;
  172.     }
  173.  
  174.     /**
  175.      * @return the scheme of the requestor (the HTTP scheme
  176.      *        for an HTTP firewall, for example)
  177.      */
  178.     protected final String getRequestingScheme() {
  179.     return requestingScheme;
  180.     }
  181.  
  182.     /**
  183.      * Called when password authorization is needed.  Subclasses should
  184.      * override the default implementation, which returns null.
  185.      * @return The PasswordAuthentication collected from the
  186.      *        user, or null if none is provided.
  187.      */
  188.     protected PasswordAuthentication getPasswordAuthentication() {
  189.     return null;
  190.     }
  191.  
  192. }
  193.