1. /*
  2. * @(#)PasswordCallback.java 1.16 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> Underlying security services instantiate and pass a
  10. * <code>PasswordCallback</code> to the <code>handle</code>
  11. * method of a <code>CallbackHandler</code> to retrieve password information.
  12. *
  13. * @version 1.16, 01/23/03
  14. * @see javax.security.auth.callback.CallbackHandler
  15. */
  16. public class PasswordCallback implements Callback, java.io.Serializable {
  17. /**
  18. * @serial
  19. * @since 1.4
  20. */
  21. private String prompt;
  22. /**
  23. * @serial
  24. * @since 1.4
  25. */
  26. private boolean echoOn;
  27. /**
  28. * @serial
  29. * @since 1.4
  30. */
  31. private char[] inputPassword;
  32. /**
  33. * Construct a <code>PasswordCallback</code> with a prompt
  34. * and a boolean specifying whether the password should be displayed
  35. * as it is being typed.
  36. *
  37. * <p>
  38. *
  39. * @param prompt the prompt used to request the password. <p>
  40. *
  41. * @param echoOn true if the password should be displayed
  42. * as it is being typed.
  43. *
  44. * @exception IllegalArgumentException if <code>prompt</code> is null or
  45. * if <code>prompt</code> has a length of 0.
  46. */
  47. public PasswordCallback(String prompt, boolean echoOn) {
  48. if (prompt == null || prompt.length() == 0)
  49. throw new IllegalArgumentException();
  50. this.prompt = prompt;
  51. this.echoOn = echoOn;
  52. }
  53. /**
  54. * Get the prompt.
  55. *
  56. * <p>
  57. *
  58. * @return the prompt.
  59. */
  60. public String getPrompt() {
  61. return prompt;
  62. }
  63. /**
  64. * Return whether the password
  65. * should be displayed as it is being typed.
  66. *
  67. * <p>
  68. *
  69. * @return the whether the password
  70. * should be displayed as it is being typed.
  71. */
  72. public boolean isEchoOn() {
  73. return echoOn;
  74. }
  75. /**
  76. * Set the retrieved password.
  77. *
  78. * <p> This method makes a copy of the input <i>password</i>
  79. * before storing it.
  80. *
  81. * <p>
  82. *
  83. * @param password the retrieved password, which may be null.
  84. *
  85. * @see #getPassword
  86. */
  87. public void setPassword(char[] password) {
  88. this.inputPassword = (password == null ?
  89. null : (char[])password.clone());
  90. }
  91. /**
  92. * Get the retrieved password.
  93. *
  94. * <p> This method returns a copy of the retrieved password.
  95. *
  96. * <p>
  97. *
  98. * @return the retrieved password, which may be null.
  99. *
  100. * @see #setPassword
  101. */
  102. public char[] getPassword() {
  103. return (inputPassword == null?
  104. null : (char[])inputPassword.clone());
  105. }
  106. /**
  107. * Clear the retrieved password.
  108. */
  109. public void clearPassword() {
  110. if (inputPassword != null) {
  111. for (int i = 0; i < inputPassword.length; i++)
  112. inputPassword[i] = ' ';
  113. }
  114. }
  115. }