ChainingCredentialsProvider.java

  1. /*
  2.  * Copyright (C) 2014, Matthias Sohn <matthias.sohn@sap.com> 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.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.List;

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

  15. /**
  16.  * A credentials provider chaining multiple credentials providers
  17.  *
  18.  * @since 3.5
  19.  */
  20. public class ChainingCredentialsProvider extends CredentialsProvider {

  21.     private List<CredentialsProvider> credentialProviders;

  22.     /**
  23.      * Create a new chaining credential provider. This provider tries to
  24.      * retrieve credentials from the chained credential providers in the order
  25.      * they are given here. If multiple providers support the requested items
  26.      * and have non-null credentials the first of them will be used.
  27.      *
  28.      * @param providers
  29.      *            credential providers asked for credentials in the order given
  30.      *            here
  31.      */
  32.     public ChainingCredentialsProvider(CredentialsProvider... providers) {
  33.         this.credentialProviders = new ArrayList<>(
  34.                 Arrays.asList(providers));
  35.     }

  36.     /** {@inheritDoc} */
  37.     @Override
  38.     public boolean isInteractive() {
  39.         for (CredentialsProvider p : credentialProviders)
  40.             if (p.isInteractive())
  41.                 return true;
  42.         return false;
  43.     }

  44.     /** {@inheritDoc} */
  45.     @Override
  46.     public boolean supports(CredentialItem... items) {
  47.         for (CredentialsProvider p : credentialProviders)
  48.             if (p.supports(items))
  49.                 return true;
  50.         return false;
  51.     }

  52.     /**
  53.      * {@inheritDoc}
  54.      * <p>
  55.      * Populates the credential items with the credentials provided by the first
  56.      * credential provider in the list which populates them with non-null values
  57.      *
  58.      * @see org.eclipse.jgit.transport.CredentialsProvider#supports(org.eclipse.jgit.transport.CredentialItem[])
  59.      */
  60.     @Override
  61.     public boolean get(URIish uri, CredentialItem... items)
  62.             throws UnsupportedCredentialItem {
  63.         for (CredentialsProvider p : credentialProviders) {
  64.             if (p.supports(items)) {
  65.                 if (!p.get(uri, items)) {
  66.                     if (p.isInteractive()) {
  67.                         return false; // user cancelled the request
  68.                     }
  69.                     continue;
  70.                 }
  71.                 if (isAnyNull(items)) {
  72.                     continue;
  73.                 }
  74.                 return true;
  75.             }
  76.         }
  77.         return false;
  78.     }
  79. }