UsernamePasswordCredentialsProvider.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.errors.UnsupportedCredentialItem;

  13. /**
  14.  * Simple {@link org.eclipse.jgit.transport.CredentialsProvider} that always
  15.  * uses the same information.
  16.  */
  17. public class UsernamePasswordCredentialsProvider extends CredentialsProvider {
  18.     private String username;

  19.     private char[] password;

  20.     /**
  21.      * Initialize the provider with a single username and password.
  22.      *
  23.      * @param username
  24.      *            user name
  25.      * @param password
  26.      *            password
  27.      */
  28.     public UsernamePasswordCredentialsProvider(String username, String password) {
  29.         this(username, password.toCharArray());
  30.     }

  31.     /**
  32.      * Initialize the provider with a single username and password.
  33.      *
  34.      * @param username
  35.      *            user name
  36.      * @param password
  37.      *            password
  38.      */
  39.     public UsernamePasswordCredentialsProvider(String username, char[] password) {
  40.         this.username = username;
  41.         this.password = password;
  42.     }

  43.     /** {@inheritDoc} */
  44.     @Override
  45.     public boolean isInteractive() {
  46.         return false;
  47.     }

  48.     /** {@inheritDoc} */
  49.     @Override
  50.     public boolean supports(CredentialItem... items) {
  51.         for (CredentialItem i : items) {
  52.             if (i instanceof CredentialItem.Username)
  53.                 continue;

  54.             else if (i instanceof CredentialItem.Password)
  55.                 continue;

  56.             else
  57.                 return false;
  58.         }
  59.         return true;
  60.     }

  61.     /** {@inheritDoc} */
  62.     @Override
  63.     public boolean get(URIish uri, CredentialItem... items)
  64.             throws UnsupportedCredentialItem {
  65.         for (CredentialItem i : items) {
  66.             if (i instanceof CredentialItem.Username) {
  67.                 ((CredentialItem.Username) i).setValue(username);
  68.                 continue;
  69.             }
  70.             if (i instanceof CredentialItem.Password) {
  71.                 ((CredentialItem.Password) i).setValue(password);
  72.                 continue;
  73.             }
  74.             if (i instanceof CredentialItem.StringType) {
  75.                 if (i.getPromptText().equals("Password: ")) { //$NON-NLS-1$
  76.                     ((CredentialItem.StringType) i).setValue(new String(
  77.                             password));
  78.                     continue;
  79.                 }
  80.             }
  81.             throw new UnsupportedCredentialItem(uri, i.getClass().getName()
  82.                     + ":" + i.getPromptText()); //$NON-NLS-1$
  83.         }
  84.         return true;
  85.     }

  86.     /**
  87.      * Destroy the saved username and password..
  88.      */
  89.     public void clear() {
  90.         username = null;

  91.         if (password != null) {
  92.             Arrays.fill(password, (char) 0);
  93.             password = null;
  94.         }
  95.     }
  96. }