View Javadoc
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  
12  package org.eclipse.jgit.transport;
13  
14  import java.util.List;
15  
16  import org.eclipse.jgit.errors.UnsupportedCredentialItem;
17  
18  /**
19   * Provide credentials for use in connecting to Git repositories.
20   *
21   * Implementors are strongly encouraged to support at least the minimal
22   * {@link org.eclipse.jgit.transport.CredentialItem.Username} and
23   * {@link org.eclipse.jgit.transport.CredentialItem.Password} items. More
24   * sophisticated implementors may implement additional types, such as
25   * {@link org.eclipse.jgit.transport.CredentialItem.StringType}.
26   *
27   * CredentialItems are usually presented in bulk, allowing implementors to
28   * combine them into a single UI widget and streamline the authentication
29   * process for an end-user.
30   *
31   * @see UsernamePasswordCredentialsProvider
32   */
33  public abstract class CredentialsProvider {
34  	private static volatile CredentialsProvider defaultProvider;
35  
36  	/**
37  	 * Get the default credentials provider, or null.
38  	 *
39  	 * @return the default credentials provider, or null.
40  	 */
41  	public static CredentialsProvider getDefault() {
42  		return defaultProvider;
43  	}
44  
45  	/**
46  	 * Set the default credentials provider.
47  	 *
48  	 * @param p
49  	 *            the new default provider, may be null to select no default.
50  	 */
51  	public static void setDefault(CredentialsProvider p) {
52  		defaultProvider = p;
53  	}
54  
55  	/**
56  	 * Whether any of the passed items is null
57  	 *
58  	 * @param items
59  	 *            credential items to check
60  	 * @return {@code true} if any of the passed items is null, {@code false}
61  	 *         otherwise
62  	 * @since 4.2
63  	 */
64  	protected static boolean isAnyNull(CredentialItem... items) {
65  		for (CredentialItem i : items)
66  			if (i == null)
67  				return true;
68  		return false;
69  	}
70  
71  	/**
72  	 * Check if the provider is interactive with the end-user.
73  	 *
74  	 * An interactive provider may try to open a dialog box, or prompt for input
75  	 * on the terminal, and will wait for a user response. A non-interactive
76  	 * provider will either populate CredentialItems, or fail.
77  	 *
78  	 * @return {@code true} if the provider is interactive with the end-user.
79  	 */
80  	public abstract boolean isInteractive();
81  
82  	/**
83  	 * Check if the provider can supply the necessary
84  	 * {@link org.eclipse.jgit.transport.CredentialItem}s.
85  	 *
86  	 * @param items
87  	 *            the items the application requires to complete authentication.
88  	 * @return {@code true} if this
89  	 *         {@link org.eclipse.jgit.transport.CredentialsProvider} supports
90  	 *         all of the items supplied.
91  	 */
92  	public abstract boolean supports(CredentialItem... items);
93  
94  	/**
95  	 * Ask for the credential items to be populated.
96  	 *
97  	 * @param uri
98  	 *            the URI of the remote resource that needs authentication.
99  	 * @param items
100 	 *            the items the application requires to complete authentication.
101 	 * @return {@code true} if the request was successful and values were
102 	 *         supplied; {@code false} if the user canceled the request and did
103 	 *         not supply all requested values.
104 	 * @throws org.eclipse.jgit.errors.UnsupportedCredentialItem
105 	 *             if one of the items supplied is not supported.
106 	 */
107 	public abstract boolean get(URIish uri, CredentialItem... items)
108 			throws UnsupportedCredentialItem;
109 
110 	/**
111 	 * Ask for the credential items to be populated.
112 	 *
113 	 * @param uri
114 	 *            the URI of the remote resource that needs authentication.
115 	 * @param items
116 	 *            the items the application requires to complete authentication.
117 	 * @return {@code true} if the request was successful and values were
118 	 *         supplied; {@code false} if the user canceled the request and did
119 	 *         not supply all requested values.
120 	 * @throws org.eclipse.jgit.errors.UnsupportedCredentialItem
121 	 *             if one of the items supplied is not supported.
122 	 */
123 	public boolean get(URIish uri, List<CredentialItem> items)
124 			throws UnsupportedCredentialItem {
125 		return get(uri, items.toArray(new CredentialItem[0]));
126 	}
127 
128 	/**
129 	 * Reset the credentials provider for the given URI
130 	 *
131 	 * @param uri
132 	 *            a {@link org.eclipse.jgit.transport.URIish} object.
133 	 */
134 	public void reset(URIish uri) {
135 		// default does nothing
136 	}
137 }