1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.security.auth.callback;
  6. /**
  7. * <p> Underlying security services instantiate and pass a
  8. * <code>PasswordCallback</code> to the <code>invokeCallback</code>
  9. * method of a <code>CallbackHandler</code> to retrieve password information.
  10. *
  11. * @version 1.10, 01/11/00
  12. * @see javax.security.auth.callback.CallbackHandler
  13. */
  14. public class PasswordCallback implements Callback {
  15. private String prompt;
  16. private boolean echoOn;
  17. private char[] inputPassword;
  18. /**
  19. * Construct a <code>PasswordCallback</code> with a prompt
  20. * and a boolean specifying whether the password should be displayed
  21. * as it is being typed.
  22. *
  23. * <p>
  24. *
  25. * @param prompt the prompt used to request the password. <p>
  26. *
  27. * @param echoOn true if the password should be displayed
  28. * as it is being typed.
  29. *
  30. * @exception IllegalArgumentException if <code>prompt</code> is null or
  31. * if <code>prompt</code> has a length of 0.
  32. */
  33. public PasswordCallback(String prompt, boolean echoOn) {
  34. if (prompt == null || prompt.length() == 0)
  35. throw new IllegalArgumentException();
  36. this.prompt = prompt;
  37. this.echoOn = echoOn;
  38. }
  39. /**
  40. * Get the prompt.
  41. *
  42. * <p>
  43. *
  44. * @return the prompt.
  45. */
  46. public String getPrompt() {
  47. return prompt;
  48. }
  49. /**
  50. * Return whether the password
  51. * should be displayed as it is being typed.
  52. *
  53. * <p>
  54. *
  55. * @return the whether the password
  56. * should be displayed as it is being typed.
  57. */
  58. public boolean isEchoOn() {
  59. return echoOn;
  60. }
  61. /**
  62. * Set the retrieved password.
  63. *
  64. * <p> This method makes a copy of the input <i>password</i>
  65. * before storing it.
  66. *
  67. * <p>
  68. *
  69. * @param password the retrieved password, which may be null.
  70. */
  71. public void setPassword(char[] password) {
  72. this.inputPassword = (password == null ?
  73. null : (char[])password.clone());
  74. }
  75. /**
  76. * Get the retrieved password.
  77. *
  78. * <p> This method returns a copy of the retrieved password.
  79. *
  80. * <p>
  81. *
  82. * @return the retrieved password, which may be null.
  83. */
  84. public char[] getPassword() {
  85. return (inputPassword == null?
  86. null : (char[])inputPassword.clone());
  87. }
  88. /**
  89. * Clear the retrieved password.
  90. */
  91. public void clearPassword() {
  92. if (inputPassword != null) {
  93. for (int i = 0; i < inputPassword.length; i++)
  94. inputPassword[i] = ' ';
  95. }
  96. }
  97. }