1. /*
  2. * @(#)Authenticator.java 1.32 04/06/28
  3. *
  4. * Copyright 2004 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 overriding {@link
  14. * #getPasswordAuthentication()} in a sub-class. This method will
  15. * typically use the various getXXX() accessor methods to get information
  16. * about the entity requesting authentication. It must then acquire a
  17. * username and password either by interacting with the user or through
  18. * some other non-interactive means. The credentials are then returned
  19. * as a {@link PasswordAuthentication} return value.
  20. * <p>
  21. * An instance of this concrete sub-class is then registered
  22. * with the system by calling {@link #setDefault(Authenticator)}.
  23. * When authentication is required, the system will invoke one of the
  24. * requestPasswordAuthentication() methods which in turn will call the
  25. * getPasswordAuthentication() method of the registered object.
  26. * <p>
  27. * All methods that request authentication have a default implementation
  28. * that fails.
  29. *
  30. * @see java.net.Authenticator#setDefault(java.net.Authenticator)
  31. * @see java.net.Authenticator#getPasswordAuthentication()
  32. *
  33. * @author Bill Foote
  34. * @version 1.32, 06/28/04
  35. * @since 1.2
  36. */
  37. // There are no abstract methods, but to be useful the user must
  38. // subclass.
  39. public abstract
  40. class Authenticator {
  41. // The system-wide authenticator object. See setDefault().
  42. private static Authenticator theAuthenticator;
  43. private String requestingHost;
  44. private InetAddress requestingSite;
  45. private int requestingPort;
  46. private String requestingProtocol;
  47. private String requestingPrompt;
  48. private String requestingScheme;
  49. private URL requestingURL;
  50. private RequestorType requestingAuthType;
  51. /**
  52. * The type of the entity requesting authentication.
  53. *
  54. * @since 1.5
  55. */
  56. public enum RequestorType {
  57. /**
  58. * Entity requesting authentication is a HTTP proxy server.
  59. */
  60. PROXY,
  61. /**
  62. * Entity requesting authentication is a HTTP origin server.
  63. */
  64. SERVER
  65. }
  66. private void reset() {
  67. requestingHost = null;
  68. requestingSite = null;
  69. requestingPort = -1;
  70. requestingProtocol = null;
  71. requestingPrompt = null;
  72. requestingScheme = null;
  73. requestingURL = null;
  74. requestingAuthType = RequestorType.SERVER;
  75. }
  76. /**
  77. * Sets the authenticator that will be used by the networking code
  78. * when a proxy or an HTTP server asks for authentication.
  79. * <p>
  80. * First, if there is a security manager, its <code>checkPermission</code>
  81. * method is called with a
  82. * <code>NetPermission("setDefaultAuthenticator")</code> permission.
  83. * This may result in a java.lang.SecurityException.
  84. *
  85. * @param a The authenticator to be set. If a is <code>null</code> then
  86. * any previously set authenticator is removed.
  87. *
  88. * @throws SecurityException
  89. * if a security manager exists and its
  90. * <code>checkPermission</code> method doesn't allow
  91. * setting the default authenticator.
  92. *
  93. * @see SecurityManager#checkPermission
  94. * @see java.net.NetPermission
  95. */
  96. public synchronized static void setDefault(Authenticator a) {
  97. SecurityManager sm = System.getSecurityManager();
  98. if (sm != null) {
  99. NetPermission setDefaultPermission
  100. = new NetPermission("setDefaultAuthenticator");
  101. sm.checkPermission(setDefaultPermission);
  102. }
  103. theAuthenticator = a;
  104. }
  105. /**
  106. * Ask the authenticator that has been registered with the system
  107. * for a password.
  108. * <p>
  109. * First, if there is a security manager, its <code>checkPermission</code>
  110. * method is called with a
  111. * <code>NetPermission("requestPasswordAuthentication")</code> permission.
  112. * This may result in a java.lang.SecurityException.
  113. *
  114. * @param addr The InetAddress of the site requesting authorization,
  115. * or null if not known.
  116. * @param port the port for the requested connection
  117. * @param protocol The protocol that's requesting the connection
  118. * ({@link java.net.Authenticator#getRequestingProtocol()})
  119. * @param prompt A prompt string for the user
  120. * @param scheme The authentication scheme
  121. *
  122. * @return The username/password, or null if one can't be gotten.
  123. *
  124. * @throws SecurityException
  125. * if a security manager exists and its
  126. * <code>checkPermission</code> method doesn't allow
  127. * the password authentication request.
  128. *
  129. * @see SecurityManager#checkPermission
  130. * @see java.net.NetPermission
  131. */
  132. public static PasswordAuthentication requestPasswordAuthentication(
  133. InetAddress addr,
  134. int port,
  135. String protocol,
  136. String prompt,
  137. String scheme) {
  138. SecurityManager sm = System.getSecurityManager();
  139. if (sm != null) {
  140. NetPermission requestPermission
  141. = new NetPermission("requestPasswordAuthentication");
  142. sm.checkPermission(requestPermission);
  143. }
  144. Authenticator a = theAuthenticator;
  145. if (a == null) {
  146. return null;
  147. } else {
  148. synchronized(a) {
  149. a.reset();
  150. a.requestingSite = addr;
  151. a.requestingPort = port;
  152. a.requestingProtocol = protocol;
  153. a.requestingPrompt = prompt;
  154. a.requestingScheme = scheme;
  155. return a.getPasswordAuthentication();
  156. }
  157. }
  158. }
  159. /**
  160. * Ask the authenticator that has been registered with the system
  161. * for a password. This is the preferred method for requesting a password
  162. * because the hostname can be provided in cases where the InetAddress
  163. * is not available.
  164. * <p>
  165. * First, if there is a security manager, its <code>checkPermission</code>
  166. * method is called with a
  167. * <code>NetPermission("requestPasswordAuthentication")</code> permission.
  168. * This may result in a java.lang.SecurityException.
  169. *
  170. * @param host The hostname of the site requesting authentication.
  171. * @param addr The InetAddress of the site requesting authentication,
  172. * or null if not known.
  173. * @param port the port for the requested connection.
  174. * @param protocol The protocol that's requesting the connection
  175. * ({@link java.net.Authenticator#getRequestingProtocol()})
  176. * @param prompt A prompt string for the user which identifies the authentication realm.
  177. * @param scheme The authentication scheme
  178. *
  179. * @return The username/password, or null if one can't be gotten.
  180. *
  181. * @throws SecurityException
  182. * if a security manager exists and its
  183. * <code>checkPermission</code> method doesn't allow
  184. * the password authentication request.
  185. *
  186. * @see SecurityManager#checkPermission
  187. * @see java.net.NetPermission
  188. * @since 1.4
  189. */
  190. public static PasswordAuthentication requestPasswordAuthentication(
  191. String host,
  192. InetAddress addr,
  193. int port,
  194. String protocol,
  195. String prompt,
  196. String scheme) {
  197. SecurityManager sm = System.getSecurityManager();
  198. if (sm != null) {
  199. NetPermission requestPermission
  200. = new NetPermission("requestPasswordAuthentication");
  201. sm.checkPermission(requestPermission);
  202. }
  203. Authenticator a = theAuthenticator;
  204. if (a == null) {
  205. return null;
  206. } else {
  207. synchronized(a) {
  208. a.reset();
  209. a.requestingHost = host;
  210. a.requestingSite = addr;
  211. a.requestingPort = port;
  212. a.requestingProtocol = protocol;
  213. a.requestingPrompt = prompt;
  214. a.requestingScheme = scheme;
  215. return a.getPasswordAuthentication();
  216. }
  217. }
  218. }
  219. /**
  220. * Ask the authenticator that has been registered with the system
  221. * for a password.
  222. * <p>
  223. * First, if there is a security manager, its <code>checkPermission</code>
  224. * method is called with a
  225. * <code>NetPermission("requestPasswordAuthentication")</code> permission.
  226. * This may result in a java.lang.SecurityException.
  227. *
  228. * @param host The hostname of the site requesting authentication.
  229. * @param addr The InetAddress of the site requesting authorization,
  230. * or null if not known.
  231. * @param port the port for the requested connection
  232. * @param protocol The protocol that's requesting the connection
  233. * ({@link java.net.Authenticator#getRequestingProtocol()})
  234. * @param prompt A prompt string for the user
  235. * @param scheme The authentication scheme
  236. * @param url The requesting URL that caused the authentication
  237. * @param reqType The type (server or proxy) of the entity requesting
  238. * authentication.
  239. *
  240. * @return The username/password, or null if one can't be gotten.
  241. *
  242. * @throws SecurityException
  243. * if a security manager exists and its
  244. * <code>checkPermission</code> method doesn't allow
  245. * the password authentication request.
  246. *
  247. * @see SecurityManager#checkPermission
  248. * @see java.net.NetPermission
  249. *
  250. * @since 1.5
  251. */
  252. public static PasswordAuthentication requestPasswordAuthentication(
  253. String host,
  254. InetAddress addr,
  255. int port,
  256. String protocol,
  257. String prompt,
  258. String scheme,
  259. URL url,
  260. RequestorType reqType) {
  261. SecurityManager sm = System.getSecurityManager();
  262. if (sm != null) {
  263. NetPermission requestPermission
  264. = new NetPermission("requestPasswordAuthentication");
  265. sm.checkPermission(requestPermission);
  266. }
  267. Authenticator a = theAuthenticator;
  268. if (a == null) {
  269. return null;
  270. } else {
  271. synchronized(a) {
  272. a.reset();
  273. a.requestingHost = host;
  274. a.requestingSite = addr;
  275. a.requestingPort = port;
  276. a.requestingProtocol = protocol;
  277. a.requestingPrompt = prompt;
  278. a.requestingScheme = scheme;
  279. a.requestingURL = url;
  280. a.requestingAuthType = reqType;
  281. return a.getPasswordAuthentication();
  282. }
  283. }
  284. }
  285. /**
  286. * Gets the <code>hostname</code> of the
  287. * site or proxy requesting authentication, or <code>null</code>
  288. * if not available.
  289. *
  290. * @return the hostname of the connection requiring authentication, or null
  291. * if it's not available.
  292. * @since 1.4
  293. */
  294. protected final String getRequestingHost() {
  295. return requestingHost;
  296. }
  297. /**
  298. * Gets the <code>InetAddress</code> of the
  299. * site requesting authorization, or <code>null</code>
  300. * if not available.
  301. *
  302. * @return the InetAddress of the site requesting authorization, or null
  303. * if it's not available.
  304. */
  305. protected final InetAddress getRequestingSite() {
  306. return requestingSite;
  307. }
  308. /**
  309. * Gets the port number for the requested connection.
  310. * @return an <code>int</code> indicating the
  311. * port for the requested connection.
  312. */
  313. protected final int getRequestingPort() {
  314. return requestingPort;
  315. }
  316. /**
  317. * Give the protocol that's requesting the connection. Often this
  318. * will be based on a URL, but in a future JDK it could be, for
  319. * example, "SOCKS" for a password-protected SOCKS5 firewall.
  320. *
  321. * @return the protcol, optionally followed by "/version", where
  322. * version is a version number.
  323. *
  324. * @see java.net.URL#getProtocol()
  325. */
  326. protected final String getRequestingProtocol() {
  327. return requestingProtocol;
  328. }
  329. /**
  330. * Gets the prompt string given by the requestor.
  331. *
  332. * @return the prompt string given by the requestor (realm for
  333. * http requests)
  334. */
  335. protected final String getRequestingPrompt() {
  336. return requestingPrompt;
  337. }
  338. /**
  339. * Gets the scheme of the requestor (the HTTP scheme
  340. * for an HTTP firewall, for example).
  341. *
  342. * @return the scheme of the requestor
  343. *
  344. */
  345. protected final String getRequestingScheme() {
  346. return requestingScheme;
  347. }
  348. /**
  349. * Called when password authorization is needed. Subclasses should
  350. * override the default implementation, which returns null.
  351. * @return The PasswordAuthentication collected from the
  352. * user, or null if none is provided.
  353. */
  354. protected PasswordAuthentication getPasswordAuthentication() {
  355. return null;
  356. }
  357. /**
  358. * Returns the URL that resulted in this
  359. * request for authentication.
  360. *
  361. * @since 1.5
  362. *
  363. * @return the requesting URL
  364. *
  365. */
  366. protected URL getRequestingURL () {
  367. return requestingURL;
  368. }
  369. /**
  370. * Returns whether the requestor is a Proxy or a Server.
  371. *
  372. * @since 1.5
  373. *
  374. * @return the authentication type of the requestor
  375. *
  376. */
  377. protected RequestorType getRequestorType () {
  378. return requestingAuthType;
  379. }
  380. }