View Javadoc
1   /*
2    * Copyright (C) 2010, 2021 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.InformationalMessage) {
62  				continue;
63  			}
64  			if (i instanceof CredentialItem.Username) {
65  				continue;
66  			}
67  			if (i instanceof CredentialItem.Password) {
68  				continue;
69  			}
70  			if (i instanceof CredentialItem.StringType) {
71  				if (i.getPromptText().equals("Password: ")) { //$NON-NLS-1$
72  					continue;
73  				}
74  			}
75  			return false;
76  		}
77  		return true;
78  	}
79  
80  	/** {@inheritDoc} */
81  	@Override
82  	public boolean get(URIish uri, CredentialItem... items)
83  			throws UnsupportedCredentialItem {
84  		for (CredentialItem i : items) {
85  			if (i instanceof CredentialItem.InformationalMessage) {
86  				continue;
87  			}
88  			if (i instanceof CredentialItem.Username) {
89  				((CredentialItem.Username) i).setValue(username);
90  				continue;
91  			}
92  			if (i instanceof CredentialItem.Password) {
93  				((CredentialItem.Password) i).setValue(password);
94  				continue;
95  			}
96  			if (i instanceof CredentialItem.StringType) {
97  				if (i.getPromptText().equals("Password: ")) { //$NON-NLS-1$
98  					((CredentialItem.StringType) i).setValue(new String(
99  							password));
100 					continue;
101 				}
102 			}
103 			throw new UnsupportedCredentialItem(uri, i.getClass().getName()
104 					+ ":" + i.getPromptText()); //$NON-NLS-1$
105 		}
106 		return true;
107 	}
108 
109 	/**
110 	 * Destroy the saved username and password..
111 	 */
112 	public void clear() {
113 		username = null;
114 
115 		if (password != null) {
116 			Arrays.fill(password, (char) 0);
117 			password = null;
118 		}
119 	}
120 }