View Javadoc
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  
11  package org.eclipse.jgit.transport;
12  
13  import java.util.Arrays;
14  
15  import org.eclipse.jgit.errors.UnsupportedCredentialItem;
16  
17  /**
18   * Simple {@link org.eclipse.jgit.transport.CredentialsProvider} that always
19   * uses the same information.
20   */
21  public class UsernamePasswordCredentialsProvider extends CredentialsProvider {
22  	private String username;
23  
24  	private char[] password;
25  
26  	/**
27  	 * Initialize the provider with a single username and password.
28  	 *
29  	 * @param username
30  	 *            user name
31  	 * @param password
32  	 *            password
33  	 */
34  	public UsernamePasswordCredentialsProvider(String username, String password) {
35  		this(username, password.toCharArray());
36  	}
37  
38  	/**
39  	 * Initialize the provider with a single username and password.
40  	 *
41  	 * @param username
42  	 *            user name
43  	 * @param password
44  	 *            password
45  	 */
46  	public UsernamePasswordCredentialsProvider(String username, char[] password) {
47  		this.username = username;
48  		this.password = password;
49  	}
50  
51  	/** {@inheritDoc} */
52  	@Override
53  	public boolean isInteractive() {
54  		return false;
55  	}
56  
57  	/** {@inheritDoc} */
58  	@Override
59  	public boolean supports(CredentialItem... items) {
60  		for (CredentialItem i : items) {
61  			if (i instanceof CredentialItem.Username)
62  				continue;
63  
64  			else if (i instanceof CredentialItem.Password)
65  				continue;
66  
67  			else
68  				return false;
69  		}
70  		return true;
71  	}
72  
73  	/** {@inheritDoc} */
74  	@Override
75  	public boolean get(URIish uri, CredentialItem... items)
76  			throws UnsupportedCredentialItem {
77  		for (CredentialItem i : items) {
78  			if (i instanceof CredentialItem.Username) {
79  				((CredentialItem.Username) i).setValue(username);
80  				continue;
81  			}
82  			if (i instanceof CredentialItem.Password) {
83  				((CredentialItem.Password) i).setValue(password);
84  				continue;
85  			}
86  			if (i instanceof CredentialItem.StringType) {
87  				if (i.getPromptText().equals("Password: ")) { //$NON-NLS-1$
88  					((CredentialItem.StringType) i).setValue(new String(
89  							password));
90  					continue;
91  				}
92  			}
93  			throw new UnsupportedCredentialItem(uri, i.getClass().getName()
94  					+ ":" + i.getPromptText()); //$NON-NLS-1$
95  		}
96  		return true;
97  	}
98  
99  	/**
100 	 * Destroy the saved username and password..
101 	 */
102 	public void clear() {
103 		username = null;
104 
105 		if (password != null) {
106 			Arrays.fill(password, (char) 0);
107 			password = null;
108 		}
109 	}
110 }