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