CredentialsProvider.java

  1. /*
  2.  * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>,
  3.  * Copyright (C) 2010, Stefan Lay <stefan.lay@sap.com> and others
  4.  *
  5.  * This program and the accompanying materials are made available under the
  6.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  7.  * https://www.eclipse.org/org/documents/edl-v10.php.
  8.  *
  9.  * SPDX-License-Identifier: BSD-3-Clause
  10.  */

  11. package org.eclipse.jgit.transport;

  12. import java.util.List;

  13. import org.eclipse.jgit.errors.UnsupportedCredentialItem;

  14. /**
  15.  * Provide credentials for use in connecting to Git repositories.
  16.  *
  17.  * Implementors are strongly encouraged to support at least the minimal
  18.  * {@link org.eclipse.jgit.transport.CredentialItem.Username} and
  19.  * {@link org.eclipse.jgit.transport.CredentialItem.Password} items. More
  20.  * sophisticated implementors may implement additional types, such as
  21.  * {@link org.eclipse.jgit.transport.CredentialItem.StringType}.
  22.  *
  23.  * CredentialItems are usually presented in bulk, allowing implementors to
  24.  * combine them into a single UI widget and streamline the authentication
  25.  * process for an end-user.
  26.  *
  27.  * @see UsernamePasswordCredentialsProvider
  28.  */
  29. public abstract class CredentialsProvider {
  30.     private static volatile CredentialsProvider defaultProvider;

  31.     /**
  32.      * Get the default credentials provider, or null.
  33.      *
  34.      * @return the default credentials provider, or null.
  35.      */
  36.     public static CredentialsProvider getDefault() {
  37.         return defaultProvider;
  38.     }

  39.     /**
  40.      * Set the default credentials provider.
  41.      *
  42.      * @param p
  43.      *            the new default provider, may be null to select no default.
  44.      */
  45.     public static void setDefault(CredentialsProvider p) {
  46.         defaultProvider = p;
  47.     }

  48.     /**
  49.      * Whether any of the passed items is null
  50.      *
  51.      * @param items
  52.      *            credential items to check
  53.      * @return {@code true} if any of the passed items is null, {@code false}
  54.      *         otherwise
  55.      * @since 4.2
  56.      */
  57.     protected static boolean isAnyNull(CredentialItem... items) {
  58.         for (CredentialItem i : items)
  59.             if (i == null)
  60.                 return true;
  61.         return false;
  62.     }

  63.     /**
  64.      * Check if the provider is interactive with the end-user.
  65.      *
  66.      * An interactive provider may try to open a dialog box, or prompt for input
  67.      * on the terminal, and will wait for a user response. A non-interactive
  68.      * provider will either populate CredentialItems, or fail.
  69.      *
  70.      * @return {@code true} if the provider is interactive with the end-user.
  71.      */
  72.     public abstract boolean isInteractive();

  73.     /**
  74.      * Check if the provider can supply the necessary
  75.      * {@link org.eclipse.jgit.transport.CredentialItem}s.
  76.      *
  77.      * @param items
  78.      *            the items the application requires to complete authentication.
  79.      * @return {@code true} if this
  80.      *         {@link org.eclipse.jgit.transport.CredentialsProvider} supports
  81.      *         all of the items supplied.
  82.      */
  83.     public abstract boolean supports(CredentialItem... items);

  84.     /**
  85.      * Ask for the credential items to be populated.
  86.      *
  87.      * @param uri
  88.      *            the URI of the remote resource that needs authentication.
  89.      * @param items
  90.      *            the items the application requires to complete authentication.
  91.      * @return {@code true} if the request was successful and values were
  92.      *         supplied; {@code false} if the user canceled the request and did
  93.      *         not supply all requested values.
  94.      * @throws org.eclipse.jgit.errors.UnsupportedCredentialItem
  95.      *             if one of the items supplied is not supported.
  96.      */
  97.     public abstract boolean get(URIish uri, CredentialItem... items)
  98.             throws UnsupportedCredentialItem;

  99.     /**
  100.      * Ask for the credential items to be populated.
  101.      *
  102.      * @param uri
  103.      *            the URI of the remote resource that needs authentication.
  104.      * @param items
  105.      *            the items the application requires to complete authentication.
  106.      * @return {@code true} if the request was successful and values were
  107.      *         supplied; {@code false} if the user canceled the request and did
  108.      *         not supply all requested values.
  109.      * @throws org.eclipse.jgit.errors.UnsupportedCredentialItem
  110.      *             if one of the items supplied is not supported.
  111.      */
  112.     public boolean get(URIish uri, List<CredentialItem> items)
  113.             throws UnsupportedCredentialItem {
  114.         return get(uri, items.toArray(new CredentialItem[0]));
  115.     }

  116.     /**
  117.      * Reset the credentials provider for the given URI
  118.      *
  119.      * @param uri
  120.      *            a {@link org.eclipse.jgit.transport.URIish} object.
  121.      */
  122.     public void reset(URIish uri) {
  123.         // default does nothing
  124.     }
  125. }