1. /*
  2. * @(#)Authenticator.java 1.27 03/01/23
  3. *
  4. * Copyright 2003 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.Authenticator)
  25. * @see java.net.Authenticator#getPasswordAuthentication()
  26. *
  27. * @author Bill Foote
  28. * @version 1.27, 01/23/03
  29. * @since 1.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 String requestingHost;
  38. private InetAddress requestingSite;
  39. private int requestingPort;
  40. private String requestingProtocol;
  41. private String requestingPrompt;
  42. private String requestingScheme;
  43. private void reset() {
  44. requestingHost = null;
  45. requestingSite = null;
  46. requestingPort = -1;
  47. requestingProtocol = null;
  48. requestingPrompt = null;
  49. requestingScheme = null;
  50. }
  51. /**
  52. * Sets the authenticator that will be used by the networking code
  53. * when a proxy or an HTTP server asks for authentication.
  54. * <p>
  55. * First, if there is a security manager, its <code>checkPermission</code>
  56. * method is called with a
  57. * <code>NetPermission("setDefaultAuthenticator")</code> permission.
  58. * This may result in a java.lang.SecurityException.
  59. *
  60. * @param a The authenticator to be set. If a is <code>null</code> then
  61. * any previously set authenticator is removed.
  62. *
  63. * @throws SecurityException
  64. * if a security manager exists and its
  65. * <code>checkPermission</code> method doesn't allow
  66. * setting the default authenticator.
  67. *
  68. * @see SecurityManager#checkPermission
  69. * @see java.net.NetPermission
  70. */
  71. public synchronized static void setDefault(Authenticator a) {
  72. SecurityManager sm = System.getSecurityManager();
  73. if (sm != null) {
  74. NetPermission setDefaultPermission
  75. = new NetPermission("setDefaultAuthenticator");
  76. sm.checkPermission(setDefaultPermission);
  77. }
  78. theAuthenticator = a;
  79. }
  80. /**
  81. * Ask the authenticator that has been registered with the system
  82. * for a password.
  83. * <p>
  84. * First, if there is a security manager, its <code>checkPermission</code>
  85. * method is called with a
  86. * <code>NetPermission("requestPasswordAuthentication")</code> permission.
  87. * This may result in a java.lang.SecurityException.
  88. *
  89. * @param addr The InetAddress of the site requesting authorization,
  90. * or null if not known.
  91. * @param port the port for the requested connection
  92. * @param protocol The protocol that's requesting the connection
  93. * ({@link java.net.Authenticator#getRequestingProtocol()})
  94. * @param prompt A prompt string for the user
  95. * @param scheme The authentication scheme
  96. *
  97. * @return The username/password, or null if one can't be gotten.
  98. *
  99. * @throws SecurityException
  100. * if a security manager exists and its
  101. * <code>checkPermission</code> method doesn't allow
  102. * the password authentication request.
  103. *
  104. * @see SecurityManager#checkPermission
  105. * @see java.net.NetPermission
  106. */
  107. public static PasswordAuthentication requestPasswordAuthentication(
  108. InetAddress addr,
  109. int port,
  110. String protocol,
  111. String prompt,
  112. String scheme) {
  113. SecurityManager sm = System.getSecurityManager();
  114. if (sm != null) {
  115. NetPermission requestPermission
  116. = new NetPermission("requestPasswordAuthentication");
  117. sm.checkPermission(requestPermission);
  118. }
  119. Authenticator a = theAuthenticator;
  120. if (a == null) {
  121. return null;
  122. } else {
  123. synchronized(a) {
  124. a.reset();
  125. a.requestingSite = addr;
  126. a.requestingPort = port;
  127. a.requestingProtocol = protocol;
  128. a.requestingPrompt = prompt;
  129. a.requestingScheme = scheme;
  130. return a.getPasswordAuthentication();
  131. }
  132. }
  133. }
  134. /**
  135. * Ask the authenticator that has been registered with the system
  136. * for a password. This is the preferred method for requesting a password
  137. * because the hostname can be provided in cases where the InetAddress
  138. * is not available.
  139. * <p>
  140. * First, if there is a security manager, its <code>checkPermission</code>
  141. * method is called with a
  142. * <code>NetPermission("requestPasswordAuthentication")</code> permission.
  143. * This may result in a java.lang.SecurityException.
  144. *
  145. * @param host The hostname of the site requesting authentication.
  146. * @param addr The InetAddress of the site requesting authentication,
  147. * or null if not known.
  148. * @param port the port for the requested connection.
  149. * @param protocol The protocol that's requesting the connection
  150. * ({@link java.net.Authenticator#getRequestingProtocol()})
  151. * @param prompt A prompt string for the user which identifies the authentication realm.
  152. * @param scheme The authentication scheme
  153. *
  154. * @return The username/password, or null if one can't be gotten.
  155. *
  156. * @throws SecurityException
  157. * if a security manager exists and its
  158. * <code>checkPermission</code> method doesn't allow
  159. * the password authentication request.
  160. *
  161. * @see SecurityManager#checkPermission
  162. * @see java.net.NetPermission
  163. * @since 1.4
  164. */
  165. public static PasswordAuthentication requestPasswordAuthentication(
  166. String host,
  167. InetAddress addr,
  168. int port,
  169. String protocol,
  170. String prompt,
  171. String scheme) {
  172. SecurityManager sm = System.getSecurityManager();
  173. if (sm != null) {
  174. NetPermission requestPermission
  175. = new NetPermission("requestPasswordAuthentication");
  176. sm.checkPermission(requestPermission);
  177. }
  178. Authenticator a = theAuthenticator;
  179. if (a == null) {
  180. return null;
  181. } else {
  182. synchronized(a) {
  183. a.reset();
  184. a.requestingHost = host;
  185. a.requestingSite = addr;
  186. a.requestingPort = port;
  187. a.requestingProtocol = protocol;
  188. a.requestingPrompt = prompt;
  189. a.requestingScheme = scheme;
  190. return a.getPasswordAuthentication();
  191. }
  192. }
  193. }
  194. /**
  195. * Gets the <code>hostname</code> of the
  196. * site or proxy requesting authentication, or <code>null</code>
  197. * if not available.
  198. *
  199. * @return the hostname of the connection requiring authentication, or null
  200. * if it's not available.
  201. * @since 1.4
  202. */
  203. protected final String getRequestingHost() {
  204. return requestingHost;
  205. }
  206. /**
  207. * Gets the <code>InetAddress</code> of the
  208. * site requesting authorization, or <code>null</code>
  209. * if not available.
  210. *
  211. * @return the InetAddress of the site requesting authorization, or null
  212. * if it's not available.
  213. */
  214. protected final InetAddress getRequestingSite() {
  215. return requestingSite;
  216. }
  217. /**
  218. * Gets the port number for the requested connection.
  219. * @return an <code>int</code> indicating the
  220. * port for the requested connection.
  221. */
  222. protected final int getRequestingPort() {
  223. return requestingPort;
  224. }
  225. /**
  226. * Give the protocol that's requesting the connection. Often this
  227. * will be based on a URL, but in a future SDK it could be, for
  228. * example, "SOCKS" for a password-protected SOCKS5 firewall.
  229. *
  230. * @return the protcol, optionally followed by "/version", where
  231. * version is a version number.
  232. *
  233. * @see java.net.URL#getProtocol()
  234. */
  235. protected final String getRequestingProtocol() {
  236. return requestingProtocol;
  237. }
  238. /**
  239. * Gets the prompt string given by the requestor.
  240. *
  241. * @return the prompt string given by the requestor (realm for
  242. * http requests)
  243. */
  244. protected final String getRequestingPrompt() {
  245. return requestingPrompt;
  246. }
  247. /**
  248. * Gets the scheme of the requestor (the HTTP scheme
  249. * for an HTTP firewall, for example).
  250. *
  251. * @return the scheme of the requestor
  252. *
  253. */
  254. protected final String getRequestingScheme() {
  255. return requestingScheme;
  256. }
  257. /**
  258. * Called when password authorization is needed. Subclasses should
  259. * override the default implementation, which returns null.
  260. * @return The PasswordAuthentication collected from the
  261. * user, or null if none is provided.
  262. */
  263. protected PasswordAuthentication getPasswordAuthentication() {
  264. return null;
  265. }
  266. }