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