1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.mail;
  6. import java.net.InetAddress;
  7. /**
  8. * The class Authenticator represents an object that knows how to obtain
  9. * authentication for a network connection. Usually, it will do this
  10. * by prompting the user for information.
  11. * <p>
  12. * Applications use this class by creating a subclass, and registering
  13. * an instance of that subclass with the session when it is created.
  14. * When authentication is required, the system will invoke a method
  15. * on the subclass (like getPasswordAuthentication). The subclass's
  16. * method can query about the authentication being requested with a
  17. * number of inherited methods (getRequestingXXX()), and form an
  18. * appropriate message for the user.
  19. * <p>
  20. * All methods that request authentication have a default implementation
  21. * that fails.
  22. *
  23. * @see java.net.Authenticator
  24. * @see javax.mail.Session#getInstance(java.util.Properties,
  25. * javax.mail.Authenticator)
  26. * @see javax.mail.Session#getDefaultInstance(java.util.Properties,
  27. * javax.mail.Authenticator)
  28. * @see javax.mail.Session#requestPasswordAuthentication
  29. * @see javax.mail.PasswordAuthentication
  30. *
  31. * @author Bill Foote
  32. * @author Bill Shannon
  33. * @version 1.5, 12/06/99
  34. */
  35. // There are no abstract methods, but to be useful the user must
  36. // subclass.
  37. public abstract class Authenticator {
  38. private InetAddress requestingSite;
  39. private int requestingPort;
  40. private String requestingProtocol;
  41. private String requestingPrompt;
  42. private String requestingUserName;
  43. private void reset() {
  44. requestingSite = null;
  45. requestingPort = -1;
  46. requestingProtocol = null;
  47. requestingPrompt = null;
  48. requestingUserName = null;
  49. }
  50. /**
  51. * Ask the authenticator for a password.
  52. * <p>
  53. *
  54. * @param addr The InetAddress of the site requesting authorization,
  55. * or null if not known.
  56. * @param port the port for the requested connection
  57. * @param protocol The protocol that's requesting the connection
  58. * (@see java.net.Authenticator.getProtocol())
  59. * @param prompt A prompt string for the user
  60. *
  61. * @return The username/password, or null if one can't be gotten.
  62. */
  63. final PasswordAuthentication requestPasswordAuthentication(
  64. InetAddress addr, int port, String protocol,
  65. String prompt, String defaultUserName) {
  66. reset();
  67. requestingSite = addr;
  68. requestingPort = port;
  69. requestingProtocol = protocol;
  70. requestingPrompt = prompt;
  71. requestingUserName = defaultUserName;
  72. return getPasswordAuthentication();
  73. }
  74. /**
  75. * @return the InetAddress of the site requesting authorization, or null
  76. * if it's not available.
  77. */
  78. protected final InetAddress getRequestingSite() {
  79. return requestingSite;
  80. }
  81. /**
  82. * @return the port for the requested connection
  83. */
  84. protected final int getRequestingPort() {
  85. return requestingPort;
  86. }
  87. /**
  88. * Give the protocol that's requesting the connection. Often this
  89. * will be based on a URLName.
  90. *
  91. * @return the protcol
  92. *
  93. * @see javax.mail.URLName#getProtocol
  94. */
  95. protected final String getRequestingProtocol() {
  96. return requestingProtocol;
  97. }
  98. /**
  99. * @return the prompt string given by the requestor
  100. */
  101. protected final String getRequestingPrompt() {
  102. return requestingPrompt;
  103. }
  104. /**
  105. * @return the default user name given by the requestor
  106. */
  107. protected final String getDefaultUserName() {
  108. return requestingUserName;
  109. }
  110. /**
  111. * Called when password authentication is needed. Subclasses should
  112. * override the default implementation, which returns null. <p>
  113. *
  114. * Note that if this method uses a dialog to prompt the user for this
  115. * information, the dialog needs to block until the user supplies the
  116. * information. This method can not simply return after showing the
  117. * dialog.
  118. * @return The PasswordAuthentication collected from the
  119. * user, or null if none is provided.
  120. */
  121. protected PasswordAuthentication getPasswordAuthentication() {
  122. return null;
  123. }
  124. }