You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CredentialItem.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.transport;
  44. import java.util.Arrays;
  45. import org.eclipse.jgit.internal.JGitText;
  46. /**
  47. * A credential requested from a {@link CredentialsProvider}.
  48. *
  49. * Most users should work with the specialized subclasses:
  50. * <ul>
  51. * <li>{@link Username} for usernames</li>
  52. * <li>{@link Password} for passwords</li>
  53. * <li>{@link StringType} for other general string information</li>
  54. * <li>{@link CharArrayType} for other general secret information</li>
  55. * </ul>
  56. *
  57. * This class is not thread-safe. Applications should construct their own
  58. * instance for each use, as the value is held within the CredentialItem object.
  59. */
  60. public abstract class CredentialItem {
  61. private final String promptText;
  62. private final boolean valueSecure;
  63. /**
  64. * Initialize a prompt.
  65. *
  66. * @param promptText
  67. * prompt to display to the user alongside of the input field.
  68. * Should be sufficient text to indicate what to supply for this
  69. * item.
  70. * @param maskValue
  71. * true if the value should be masked from displaying during
  72. * input. This should be true for passwords and other secrets,
  73. * false for names and other public data.
  74. */
  75. public CredentialItem(String promptText, boolean maskValue) {
  76. this.promptText = promptText;
  77. this.valueSecure = maskValue;
  78. }
  79. /** @return prompt to display to the user. */
  80. public String getPromptText() {
  81. return promptText;
  82. }
  83. /** @return true if the value should be masked when entered. */
  84. public boolean isValueSecure() {
  85. return valueSecure;
  86. }
  87. /** Clear the stored value, destroying it as much as possible. */
  88. public abstract void clear();
  89. /**
  90. * An item whose value is stored as a string.
  91. *
  92. * When working with secret data, consider {@link CharArrayType} instead, as
  93. * the internal members of the array can be cleared, reducing the chances
  94. * that the password is left in memory after authentication is completed.
  95. */
  96. public static class StringType extends CredentialItem {
  97. private String value;
  98. /**
  99. * Initialize a prompt for a single string.
  100. *
  101. * @param promptText
  102. * prompt to display to the user alongside of the input
  103. * field. Should be sufficient text to indicate what to
  104. * supply for this item.
  105. * @param maskValue
  106. * true if the value should be masked from displaying during
  107. * input. This should be true for passwords and other
  108. * secrets, false for names and other public data.
  109. */
  110. public StringType(String promptText, boolean maskValue) {
  111. super(promptText, maskValue);
  112. }
  113. @Override
  114. public void clear() {
  115. value = null;
  116. }
  117. /** @return the current value */
  118. public String getValue() {
  119. return value;
  120. }
  121. /**
  122. *
  123. * @param newValue
  124. */
  125. public void setValue(String newValue) {
  126. value = newValue;
  127. }
  128. }
  129. /** An item whose value is stored as a char[] and is therefore clearable. */
  130. public static class CharArrayType extends CredentialItem {
  131. private char[] value;
  132. /**
  133. * Initialize a prompt for a secure value stored in a character array.
  134. *
  135. * @param promptText
  136. * prompt to display to the user alongside of the input
  137. * field. Should be sufficient text to indicate what to
  138. * supply for this item.
  139. * @param maskValue
  140. * true if the value should be masked from displaying during
  141. * input. This should be true for passwords and other
  142. * secrets, false for names and other public data.
  143. */
  144. public CharArrayType(String promptText, boolean maskValue) {
  145. super(promptText, maskValue);
  146. }
  147. /** Destroys the current value, clearing the internal array. */
  148. @Override
  149. public void clear() {
  150. if (value != null) {
  151. Arrays.fill(value, (char) 0);
  152. value = null;
  153. }
  154. }
  155. /**
  156. * Get the current value.
  157. *
  158. * The returned array will be cleared out when {@link #clear()} is
  159. * called. Callers that need the array elements to survive should delay
  160. * invoking {@code clear()} until the value is no longer necessary.
  161. *
  162. * @return the current value array. The actual internal array is
  163. * returned, reducing the number of copies present in memory.
  164. */
  165. public char[] getValue() {
  166. return value;
  167. }
  168. /**
  169. * Set the new value, clearing the old value array.
  170. *
  171. * @param newValue
  172. * if not null, the array is copied.
  173. */
  174. public void setValue(char[] newValue) {
  175. clear();
  176. if (newValue != null) {
  177. value = new char[newValue.length];
  178. System.arraycopy(newValue, 0, value, 0, newValue.length);
  179. }
  180. }
  181. /**
  182. * Set the new value, clearing the old value array.
  183. *
  184. * @param newValue
  185. * the new internal array. The array is <b>NOT</b> copied.
  186. */
  187. public void setValueNoCopy(char[] newValue) {
  188. clear();
  189. value = newValue;
  190. }
  191. }
  192. /** An item whose value is a boolean choice, presented as Yes/No. */
  193. public static class YesNoType extends CredentialItem {
  194. private boolean value;
  195. /**
  196. * Initialize a prompt for a single boolean answer.
  197. *
  198. * @param promptText
  199. * prompt to display to the user alongside of the input
  200. * field. Should be sufficient text to indicate what to
  201. * supply for this item.
  202. */
  203. public YesNoType(String promptText) {
  204. super(promptText, false);
  205. }
  206. @Override
  207. public void clear() {
  208. value = false;
  209. }
  210. /** @return the current value */
  211. public boolean getValue() {
  212. return value;
  213. }
  214. /**
  215. * Set the new value.
  216. *
  217. * @param newValue
  218. */
  219. public void setValue(boolean newValue) {
  220. value = newValue;
  221. }
  222. }
  223. /** An advice message presented to the user, with no response required. */
  224. public static class InformationalMessage extends CredentialItem {
  225. /**
  226. * Initialize an informational message.
  227. *
  228. * @param messageText
  229. * message to display to the user.
  230. */
  231. public InformationalMessage(String messageText) {
  232. super(messageText, false);
  233. }
  234. @Override
  235. public void clear() {
  236. // Nothing to clear.
  237. }
  238. }
  239. /** Prompt for a username, which is not masked on input. */
  240. public static class Username extends StringType {
  241. /** Initialize a new username item, with a default username prompt. */
  242. public Username() {
  243. super(JGitText.get().credentialUsername, false);
  244. }
  245. }
  246. /** Prompt for a password, which is masked on input. */
  247. public static class Password extends CharArrayType {
  248. /** Initialize a new password item, with a default password prompt. */
  249. public Password() {
  250. super(JGitText.get().credentialPassword, true);
  251. }
  252. /**
  253. * Initialize a new password item, with given prompt.
  254. *
  255. * @param msg
  256. * prompt message
  257. */
  258. public Password(String msg) {
  259. super(msg, true);
  260. }
  261. }
  262. }