1. /*
  2. * @(#)CallbackHandler.java 1.15 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 javax.security.auth.callback;
  8. /**
  9. * <p> An application implements a <code>CallbackHandler</code> and passes
  10. * it to underlying security services so that they may interact with
  11. * the application to retrieve specific authentication data,
  12. * such as usernames and passwords, or to display certain information,
  13. * such as error and warning messages.
  14. *
  15. * <p> CallbackHandlers are implemented in an application-dependent fashion.
  16. * For example, implementations for an application with a graphical user
  17. * interface (GUI) may pop up windows to prompt for requested information
  18. * or to display error messages. An implementation may also choose to obtain
  19. * requested information from an alternate source without asking the end user.
  20. *
  21. * <p> Underlying security services make requests for different types
  22. * of information by passing individual Callbacks to the
  23. * <code>CallbackHandler</code>. The <code>CallbackHandler</code>
  24. * implementation decides how to retrieve and display information
  25. * depending on the Callbacks passed to it. For example,
  26. * if the underlying service needs a username and password to
  27. * authenticate a user, it uses a <code>NameCallback</code> and
  28. * <code>PasswordCallback</code>. The <code>CallbackHandler</code>
  29. * can then choose to prompt for a username and password serially,
  30. * or to prompt for both in a single window.
  31. *
  32. * <p> A default <code>CallbackHandler</code> class implementation
  33. * may be specified in the <i>auth.login.defaultCallbackHandler</i>
  34. * security property. The security property can be set
  35. * in the Java security properties file located in the file named
  36. * <JAVA_HOME>/lib/security/java.security, where <JAVA_HOME>
  37. * refers to the directory where the SDK was installed.
  38. *
  39. * <p> If the security property is set to the fully qualified name of a
  40. * <code>CallbackHandler</code> implementation class,
  41. * then a <code>LoginContext</code> will load the specified
  42. * <code>CallbackHandler</code> and pass it to the underlying LoginModules.
  43. * The <code>LoginContext</code> only loads the default handler
  44. * if it was not provided one.
  45. *
  46. * <p> All default handler implementations must provide a public
  47. * zero-argument constructor.
  48. *
  49. * @version 1.15, 01/23/03
  50. */
  51. public interface CallbackHandler {
  52. /**
  53. * <p> Retrieve or display the information requested in the
  54. * provided Callbacks.
  55. *
  56. * <p> The <code>handle</code> method implementation checks the
  57. * instance(s) of the <code>Callback</code> object(s) passed in
  58. * to retrieve or display the requested information.
  59. * The following example is provided to help demonstrate what an
  60. * <code>handle</code> method implementation might look like.
  61. * This example code is for guidance only. Many details,
  62. * including proper error handling, are left out for simplicity.
  63. *
  64. * <pre>
  65. * public void handle(Callback[] callbacks)
  66. * throws IOException, UnsupportedCallbackException {
  67. *
  68. * for (int i = 0; i < callbacks.length; i++) {
  69. * if (callbacks[i] instanceof TextOutputCallback) {
  70. *
  71. * // display the message according to the specified type
  72. * TextOutputCallback toc = (TextOutputCallback)callbacks[i];
  73. * switch (toc.getMessageType()) {
  74. * case TextOutputCallback.INFORMATION:
  75. * System.out.println(toc.getMessage());
  76. * break;
  77. * case TextOutputCallback.ERROR:
  78. * System.out.println("ERROR: " + toc.getMessage());
  79. * break;
  80. * case TextOutputCallback.WARNING:
  81. * System.out.println("WARNING: " + toc.getMessage());
  82. * break;
  83. * default:
  84. * throw new IOException("Unsupported message type: " +
  85. * toc.getMessageType());
  86. * }
  87. *
  88. * } else if (callbacks[i] instanceof NameCallback) {
  89. *
  90. * // prompt the user for a username
  91. * NameCallback nc = (NameCallback)callbacks[i];
  92. *
  93. * // ignore the provided defaultName
  94. * System.err.print(nc.getPrompt());
  95. * System.err.flush();
  96. * nc.setName((new BufferedReader
  97. * (new InputStreamReader(System.in))).readLine());
  98. *
  99. * } else if (callbacks[i] instanceof PasswordCallback) {
  100. *
  101. * // prompt the user for sensitive information
  102. * PasswordCallback pc = (PasswordCallback)callbacks[i];
  103. * System.err.print(pc.getPrompt());
  104. * System.err.flush();
  105. * pc.setPassword(readPassword(System.in));
  106. *
  107. * } else {
  108. * throw new UnsupportedCallbackException
  109. * (callbacks[i], "Unrecognized Callback");
  110. * }
  111. * }
  112. * }
  113. *
  114. * // Reads user password from given input stream.
  115. * private char[] readPassword(InputStream in) throws IOException {
  116. * // insert code to read a user password from the input stream
  117. * }
  118. * </pre>
  119. *
  120. * @param callbacks an array of <code>Callback</code> objects provided
  121. * by an underlying security service which contains
  122. * the information requested to be retrieved or displayed.
  123. *
  124. * @exception java.io.IOException if an input or output error occurs. <p>
  125. *
  126. * @exception UnsupportedCallbackException if the implementation of this
  127. * method does not support one or more of the Callbacks
  128. * specified in the <code>callbacks</code> parameter.
  129. */
  130. void handle(Callback[] callbacks)
  131. throws java.io.IOException, UnsupportedCallbackException;
  132. }