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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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
  48. * {@link org.eclipse.jgit.transport.CredentialsProvider}.
  49. *
  50. * Most users should work with the specialized subclasses:
  51. * <ul>
  52. * <li>{@link org.eclipse.jgit.transport.CredentialItem.Username} for
  53. * usernames</li>
  54. * <li>{@link org.eclipse.jgit.transport.CredentialItem.Password} for
  55. * passwords</li>
  56. * <li>{@link org.eclipse.jgit.transport.CredentialItem.StringType} for other
  57. * general string information</li>
  58. * <li>{@link org.eclipse.jgit.transport.CredentialItem.CharArrayType} for other
  59. * general secret information</li>
  60. * </ul>
  61. *
  62. * This class is not thread-safe. Applications should construct their own
  63. * instance for each use, as the value is held within the CredentialItem object.
  64. */
  65. public abstract class CredentialItem {
  66. private final String promptText;
  67. private final boolean valueSecure;
  68. /**
  69. * Initialize a prompt.
  70. *
  71. * @param promptText
  72. * prompt to display to the user alongside of the input field.
  73. * Should be sufficient text to indicate what to supply for this
  74. * item.
  75. * @param maskValue
  76. * true if the value should be masked from displaying during
  77. * input. This should be true for passwords and other secrets,
  78. * false for names and other public data.
  79. */
  80. public CredentialItem(String promptText, boolean maskValue) {
  81. this.promptText = promptText;
  82. this.valueSecure = maskValue;
  83. }
  84. /**
  85. * Get prompt to display to the user.
  86. *
  87. * @return prompt to display to the user.
  88. */
  89. public String getPromptText() {
  90. return promptText;
  91. }
  92. /**
  93. * Whether the value should be masked when entered.
  94. *
  95. * @return true if the value should be masked when entered.
  96. */
  97. public boolean isValueSecure() {
  98. return valueSecure;
  99. }
  100. /**
  101. * Clear the stored value, destroying it as much as possible.
  102. */
  103. public abstract void clear();
  104. /**
  105. * An item whose value is stored as a string.
  106. *
  107. * When working with secret data, consider {@link CharArrayType} instead, as
  108. * the internal members of the array can be cleared, reducing the chances
  109. * that the password is left in memory after authentication is completed.
  110. */
  111. public static class StringType extends CredentialItem {
  112. private String value;
  113. /**
  114. * Initialize a prompt for a single string.
  115. *
  116. * @param promptText
  117. * prompt to display to the user alongside of the input
  118. * field. Should be sufficient text to indicate what to
  119. * supply for this item.
  120. * @param maskValue
  121. * true if the value should be masked from displaying during
  122. * input. This should be true for passwords and other
  123. * secrets, false for names and other public data.
  124. */
  125. public StringType(String promptText, boolean maskValue) {
  126. super(promptText, maskValue);
  127. }
  128. @Override
  129. public void clear() {
  130. value = null;
  131. }
  132. /** @return the current value */
  133. public String getValue() {
  134. return value;
  135. }
  136. /**
  137. *
  138. * @param newValue
  139. */
  140. public void setValue(String newValue) {
  141. value = newValue;
  142. }
  143. }
  144. /** An item whose value is stored as a char[] and is therefore clearable. */
  145. public static class CharArrayType extends CredentialItem {
  146. private char[] value;
  147. /**
  148. * Initialize a prompt for a secure value stored in a character array.
  149. *
  150. * @param promptText
  151. * prompt to display to the user alongside of the input
  152. * field. Should be sufficient text to indicate what to
  153. * supply for this item.
  154. * @param maskValue
  155. * true if the value should be masked from displaying during
  156. * input. This should be true for passwords and other
  157. * secrets, false for names and other public data.
  158. */
  159. public CharArrayType(String promptText, boolean maskValue) {
  160. super(promptText, maskValue);
  161. }
  162. /** Destroys the current value, clearing the internal array. */
  163. @Override
  164. public void clear() {
  165. if (value != null) {
  166. Arrays.fill(value, (char) 0);
  167. value = null;
  168. }
  169. }
  170. /**
  171. * Get the current value.
  172. *
  173. * The returned array will be cleared out when {@link #clear()} is
  174. * called. Callers that need the array elements to survive should delay
  175. * invoking {@code clear()} until the value is no longer necessary.
  176. *
  177. * @return the current value array. The actual internal array is
  178. * returned, reducing the number of copies present in memory.
  179. */
  180. public char[] getValue() {
  181. return value;
  182. }
  183. /**
  184. * Set the new value, clearing the old value array.
  185. *
  186. * @param newValue
  187. * if not null, the array is copied.
  188. */
  189. public void setValue(char[] newValue) {
  190. clear();
  191. if (newValue != null) {
  192. value = new char[newValue.length];
  193. System.arraycopy(newValue, 0, value, 0, newValue.length);
  194. }
  195. }
  196. /**
  197. * Set the new value, clearing the old value array.
  198. *
  199. * @param newValue
  200. * the new internal array. The array is <b>NOT</b> copied.
  201. */
  202. public void setValueNoCopy(char[] newValue) {
  203. clear();
  204. value = newValue;
  205. }
  206. }
  207. /** An item whose value is a boolean choice, presented as Yes/No. */
  208. public static class YesNoType extends CredentialItem {
  209. private boolean value;
  210. /**
  211. * Initialize a prompt for a single boolean answer.
  212. *
  213. * @param promptText
  214. * prompt to display to the user alongside of the input
  215. * field. Should be sufficient text to indicate what to
  216. * supply for this item.
  217. */
  218. public YesNoType(String promptText) {
  219. super(promptText, false);
  220. }
  221. @Override
  222. public void clear() {
  223. value = false;
  224. }
  225. /** @return the current value */
  226. public boolean getValue() {
  227. return value;
  228. }
  229. /**
  230. * Set the new value.
  231. *
  232. * @param newValue
  233. */
  234. public void setValue(boolean newValue) {
  235. value = newValue;
  236. }
  237. }
  238. /** An advice message presented to the user, with no response required. */
  239. public static class InformationalMessage extends CredentialItem {
  240. /**
  241. * Initialize an informational message.
  242. *
  243. * @param messageText
  244. * message to display to the user.
  245. */
  246. public InformationalMessage(String messageText) {
  247. super(messageText, false);
  248. }
  249. @Override
  250. public void clear() {
  251. // Nothing to clear.
  252. }
  253. }
  254. /** Prompt for a username, which is not masked on input. */
  255. public static class Username extends StringType {
  256. /** Initialize a new username item, with a default username prompt. */
  257. public Username() {
  258. super(JGitText.get().credentialUsername, false);
  259. }
  260. }
  261. /** Prompt for a password, which is masked on input. */
  262. public static class Password extends CharArrayType {
  263. /** Initialize a new password item, with a default password prompt. */
  264. public Password() {
  265. super(JGitText.get().credentialPassword, true);
  266. }
  267. /**
  268. * Initialize a new password item, with given prompt.
  269. *
  270. * @param msg
  271. * prompt message
  272. */
  273. public Password(String msg) {
  274. super(msg, true);
  275. }
  276. }
  277. }