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