PasswordCallback.java.svn-base 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package javax.security.auth.callback;
  18. import java.io.Serializable;
  19. import java.util.Arrays;
  20. import org.apache.harmony.auth.internal.nls.Messages;
  21. /**
  22. * Is used in conjunction with a {@link CallbackHandler} to retrieve a password
  23. * when needed.
  24. */
  25. public class PasswordCallback implements Callback, Serializable {
  26. private static final long serialVersionUID = 2267422647454909926L;
  27. private String prompt;
  28. boolean echoOn;
  29. private char[] inputPassword;
  30. private void setPrompt(String prompt) throws IllegalArgumentException {
  31. if (prompt == null || prompt.length() == 0) {
  32. throw new IllegalArgumentException(Messages.getString("auth.14")); //$NON-NLS-1$
  33. }
  34. this.prompt = prompt;
  35. }
  36. /**
  37. * Creates a new {@code PasswordCallback} instance.
  38. *
  39. * @param prompt
  40. * the message that should be displayed to the user
  41. * @param echoOn
  42. * determines whether the user input should be echoed
  43. */
  44. public PasswordCallback(String prompt, boolean echoOn) {
  45. super();
  46. setPrompt(prompt);
  47. this.echoOn = echoOn;
  48. }
  49. /**
  50. * Returns the prompt that was specified when creating this {@code
  51. * PasswordCallback}
  52. *
  53. * @return the prompt
  54. */
  55. public String getPrompt() {
  56. return prompt;
  57. }
  58. /**
  59. * Queries whether this {@code PasswordCallback} expects user input to be
  60. * echoed, which is specified during the creation of the object.
  61. *
  62. * @return {@code true} if (and only if) user input should be echoed
  63. */
  64. public boolean isEchoOn() {
  65. return echoOn;
  66. }
  67. /**
  68. * Sets the password. The {@link CallbackHandler} that performs the actual
  69. * provisioning or input of the password needs to call this method to hand
  70. * back the password to the security service that requested it.
  71. *
  72. * @param password
  73. * the password. A copy of this is stored, so subsequent changes
  74. * to the input array do not affect the {@code PasswordCallback}.
  75. */
  76. public void setPassword(char[] password) {
  77. if (password == null) {
  78. this.inputPassword = password;
  79. } else {
  80. inputPassword = new char[password.length];
  81. System.arraycopy(password, 0, inputPassword, 0, inputPassword.length);
  82. }
  83. }
  84. /**
  85. * Returns the password. The security service that needs the password
  86. * usually calls this method once the {@link CallbackHandler} has finished
  87. * its work.
  88. *
  89. * @return the password. A copy of the internal password is created and
  90. * returned, so subsequent changes to the internal password do not
  91. * affect the result.
  92. */
  93. public char[] getPassword() {
  94. if (inputPassword != null) {
  95. char[] tmp = new char[inputPassword.length];
  96. System.arraycopy(inputPassword, 0, tmp, 0, tmp.length);
  97. return tmp;
  98. }
  99. return null;
  100. }
  101. /**
  102. * Clears the password stored in this {@code PasswordCallback}.
  103. */
  104. public void clearPassword() {
  105. if (inputPassword != null) {
  106. Arrays.fill(inputPassword, '\u0000');
  107. }
  108. }
  109. }