CredentialItem.java

  1. /*
  2.  * Copyright (C) 2010, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.transport;

  11. import java.util.Arrays;

  12. import org.eclipse.jgit.internal.JGitText;

  13. /**
  14.  * A credential requested from a
  15.  * {@link org.eclipse.jgit.transport.CredentialsProvider}.
  16.  *
  17.  * Most users should work with the specialized subclasses:
  18.  * <ul>
  19.  * <li>{@link org.eclipse.jgit.transport.CredentialItem.Username} for
  20.  * usernames</li>
  21.  * <li>{@link org.eclipse.jgit.transport.CredentialItem.Password} for
  22.  * passwords</li>
  23.  * <li>{@link org.eclipse.jgit.transport.CredentialItem.StringType} for other
  24.  * general string information</li>
  25.  * <li>{@link org.eclipse.jgit.transport.CredentialItem.CharArrayType} for other
  26.  * general secret information</li>
  27.  * </ul>
  28.  *
  29.  * This class is not thread-safe. Applications should construct their own
  30.  * instance for each use, as the value is held within the CredentialItem object.
  31.  */
  32. public abstract class CredentialItem {
  33.     private final String promptText;

  34.     private final boolean valueSecure;

  35.     /**
  36.      * Initialize a prompt.
  37.      *
  38.      * @param promptText
  39.      *            prompt to display to the user alongside of the input field.
  40.      *            Should be sufficient text to indicate what to supply for this
  41.      *            item.
  42.      * @param maskValue
  43.      *            true if the value should be masked from displaying during
  44.      *            input. This should be true for passwords and other secrets,
  45.      *            false for names and other public data.
  46.      */
  47.     public CredentialItem(String promptText, boolean maskValue) {
  48.         this.promptText = promptText;
  49.         this.valueSecure = maskValue;
  50.     }

  51.     /**
  52.      * Get prompt to display to the user.
  53.      *
  54.      * @return prompt to display to the user.
  55.      */
  56.     public String getPromptText() {
  57.         return promptText;
  58.     }

  59.     /**
  60.      * Whether the value should be masked when entered.
  61.      *
  62.      * @return true if the value should be masked when entered.
  63.      */
  64.     public boolean isValueSecure() {
  65.         return valueSecure;
  66.     }

  67.     /**
  68.      * Clear the stored value, destroying it as much as possible.
  69.      */
  70.     public abstract void clear();

  71.     /**
  72.      * An item whose value is stored as a string.
  73.      *
  74.      * When working with secret data, consider {@link CharArrayType} instead, as
  75.      * the internal members of the array can be cleared, reducing the chances
  76.      * that the password is left in memory after authentication is completed.
  77.      */
  78.     public static class StringType extends CredentialItem {
  79.         private String value;

  80.         /**
  81.          * Initialize a prompt for a single string.
  82.          *
  83.          * @param promptText
  84.          *            prompt to display to the user alongside of the input
  85.          *            field. Should be sufficient text to indicate what to
  86.          *            supply for this item.
  87.          * @param maskValue
  88.          *            true if the value should be masked from displaying during
  89.          *            input. This should be true for passwords and other
  90.          *            secrets, false for names and other public data.
  91.          */
  92.         public StringType(String promptText, boolean maskValue) {
  93.             super(promptText, maskValue);
  94.         }

  95.         @Override
  96.         public void clear() {
  97.             value = null;
  98.         }

  99.         /** @return the current value */
  100.         public String getValue() {
  101.             return value;
  102.         }

  103.         /**
  104.          *
  105.          * @param newValue
  106.          */
  107.         public void setValue(String newValue) {
  108.             value = newValue;
  109.         }
  110.     }

  111.     /** An item whose value is stored as a char[] and is therefore clearable. */
  112.     public static class CharArrayType extends CredentialItem {
  113.         private char[] value;

  114.         /**
  115.          * Initialize a prompt for a secure value stored in a character array.
  116.          *
  117.          * @param promptText
  118.          *            prompt to display to the user alongside of the input
  119.          *            field. Should be sufficient text to indicate what to
  120.          *            supply for this item.
  121.          * @param maskValue
  122.          *            true if the value should be masked from displaying during
  123.          *            input. This should be true for passwords and other
  124.          *            secrets, false for names and other public data.
  125.          */
  126.         public CharArrayType(String promptText, boolean maskValue) {
  127.             super(promptText, maskValue);
  128.         }

  129.         /** Destroys the current value, clearing the internal array. */
  130.         @Override
  131.         public void clear() {
  132.             if (value != null) {
  133.                 Arrays.fill(value, (char) 0);
  134.                 value = null;
  135.             }
  136.         }

  137.         /**
  138.          * Get the current value.
  139.          *
  140.          * The returned array will be cleared out when {@link #clear()} is
  141.          * called. Callers that need the array elements to survive should delay
  142.          * invoking {@code clear()} until the value is no longer necessary.
  143.          *
  144.          * @return the current value array. The actual internal array is
  145.          *         returned, reducing the number of copies present in memory.
  146.          */
  147.         public char[] getValue() {
  148.             return value;
  149.         }

  150.         /**
  151.          * Set the new value, clearing the old value array.
  152.          *
  153.          * @param newValue
  154.          *            if not null, the array is copied.
  155.          */
  156.         public void setValue(char[] newValue) {
  157.             clear();

  158.             if (newValue != null) {
  159.                 value = new char[newValue.length];
  160.                 System.arraycopy(newValue, 0, value, 0, newValue.length);
  161.             }
  162.         }

  163.         /**
  164.          * Set the new value, clearing the old value array.
  165.          *
  166.          * @param newValue
  167.          *            the new internal array. The array is <b>NOT</b> copied.
  168.          */
  169.         public void setValueNoCopy(char[] newValue) {
  170.             clear();
  171.             value = newValue;
  172.         }
  173.     }

  174.     /** An item whose value is a boolean choice, presented as Yes/No. */
  175.     public static class YesNoType extends CredentialItem {
  176.         private boolean value;

  177.         /**
  178.          * Initialize a prompt for a single boolean answer.
  179.          *
  180.          * @param promptText
  181.          *            prompt to display to the user alongside of the input
  182.          *            field. Should be sufficient text to indicate what to
  183.          *            supply for this item.
  184.          */
  185.         public YesNoType(String promptText) {
  186.             super(promptText, false);
  187.         }

  188.         @Override
  189.         public void clear() {
  190.             value = false;
  191.         }

  192.         /** @return the current value */
  193.         public boolean getValue() {
  194.             return value;
  195.         }

  196.         /**
  197.          * Set the new value.
  198.          *
  199.          * @param newValue
  200.          */
  201.         public void setValue(boolean newValue) {
  202.             value = newValue;
  203.         }
  204.     }

  205.     /** An advice message presented to the user, with no response required. */
  206.     public static class InformationalMessage extends CredentialItem {
  207.         /**
  208.          * Initialize an informational message.
  209.          *
  210.          * @param messageText
  211.          *            message to display to the user.
  212.          */
  213.         public InformationalMessage(String messageText) {
  214.             super(messageText, false);
  215.         }

  216.         @Override
  217.         public void clear() {
  218.             // Nothing to clear.
  219.         }
  220.     }

  221.     /** Prompt for a username, which is not masked on input. */
  222.     public static class Username extends StringType {
  223.         /** Initialize a new username item, with a default username prompt. */
  224.         public Username() {
  225.             super(JGitText.get().credentialUsername, false);
  226.         }
  227.     }

  228.     /** Prompt for a password, which is masked on input. */
  229.     public static class Password extends CharArrayType {
  230.         /** Initialize a new password item, with a default password prompt. */
  231.         public Password() {
  232.             super(JGitText.get().credentialPassword, true);
  233.         }

  234.         /**
  235.          * Initialize a new password item, with given prompt.
  236.          *
  237.          * @param msg
  238.          *            prompt message
  239.          */
  240.         public Password(String msg) {
  241.             super(msg, true);
  242.         }
  243.     }
  244. }