Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4d543df394ef72fb48d2208354a0722790fc35a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*******************************************************************************
 * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com>
 * Copyright (C) 2010, Edwin Kempin <edwin.kempin@sap.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.ui.internal.credentials;

import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.egit.core.securestorage.UserPasswordCredentials;
import org.eclipse.egit.ui.internal.SecureStoreUtils;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.egit.ui.internal.dialogs.CustomPromptDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.jgit.errors.UnsupportedCredentialItem;
import org.eclipse.jgit.transport.CredentialItem;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

/**
 * This class implements a {@link CredentialsProvider} for EGit. The provider
 * tries to retrieve the credentials (user, password) for a given URI from the
 * secure store. A login popup is shown if no credentials are available.
 */
public class EGitCredentialsProvider extends CredentialsProvider {

	private String user;
	private String password;

	/**
	 * Default constructor
	 */
	public EGitCredentialsProvider() {
		// empty
	}

	/**
	 * @param user
	 * @param password
	 */
	public EGitCredentialsProvider(String user, String password) {
		this.user = user;
		// If the password is empty try secure store or ask the user
		this.password = password != null && password.isEmpty() ? null
				: password;
	}

	@Override
	public boolean isInteractive() {
		return true;
	}

	@Override
	public boolean supports(CredentialItem... items) {
		for (CredentialItem i : items) {
			if (i instanceof CredentialItem.StringType)
				continue;
			else if (i instanceof CredentialItem.CharArrayType)
				continue;
			else if (i instanceof CredentialItem.YesNoType)
				continue;
			else if (i instanceof CredentialItem.InformationalMessage)
				continue;
			else
				return false;
		}
		return true;
	}

	@Override
	public boolean get(final URIish uri, final CredentialItem... items)
			throws UnsupportedCredentialItem {

		if (items.length == 0) {
			return true;
		}

		CredentialItem.Username userItem = null;
		CredentialItem.Password passwordItem = null;
		boolean isSpecial = false;

		for (CredentialItem item : items) {
			if (item instanceof CredentialItem.Username)
				userItem = (CredentialItem.Username) item;
			else if (item instanceof CredentialItem.Password)
				passwordItem = (CredentialItem.Password) item;
			else
				isSpecial = true;
		}

		if (!isSpecial && (userItem != null || passwordItem != null)) {
			UserPasswordCredentials credentials = null;
			if ((user != null) && (password != null))
				credentials = new UserPasswordCredentials(user, password);
			else
				credentials = SecureStoreUtils.getCredentials(uri);

			if (credentials == null) {
				credentials = getCredentialsFromUser(uri);
				if (credentials == null)
					return false;
			}
			if (userItem != null)
				userItem.setValue(credentials.getUser());
			if (passwordItem != null)
				passwordItem.setValue(credentials.getPassword().toCharArray());
			return true;
		}

		// special handling for non-user,non-password type items
		final boolean[] result = new boolean[1];

		PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
			@Override
			public void run() {
				Shell shell = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow().getShell();

				if (items.length == 1) {
					CredentialItem item = items[0];
					result[0] = getSingleSpecial(shell, uri, item);
				} else {
					result[0] = getMultiSpecial(shell, uri, items);
				}
			}
		});

		return result[0];
	}

	@Override
	public void reset(URIish uri) {
		SecureStoreUtils.clearCredentials(uri);
		user = null;
		password = null;
	}

	/**
	 * Opens a dialog for a single non-user, non-password type item.
	 * @param shell the shell to use
	 * @param uri the uri of the get request
	 * @param item the item to handle
	 * @return <code>true</code> if the request was successful and values were supplied;
	 * 		<code>false</code> if the user canceled the request and did not supply all requested values.
	 */
	private boolean getSingleSpecial(Shell shell, URIish uri, CredentialItem item) {
		if (item instanceof CredentialItem.InformationalMessage) {
			MessageDialog.openInformation(shell, UIText.EGitCredentialsProvider_information, item.getPromptText());
			return true;
		} else if (item instanceof CredentialItem.YesNoType) {
			CredentialItem.YesNoType v = (CredentialItem.YesNoType) item;
			String[] labels = new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL };
			int[] resultIDs = new int[] { IDialogConstants.YES_ID, IDialogConstants.NO_ID, IDialogConstants.CANCEL_ID };

			MessageDialog dialog = new MessageDialog(
					shell,
					UIText.EGitCredentialsProvider_question,
					null,
					item.getPromptText(),
					MessageDialog.QUESTION_WITH_CANCEL,
					labels,
					0);
			dialog.setBlockOnOpen(true);
			int r = dialog.open();
			if (r < 0) {
				return false;
			}

			switch (resultIDs[r]) {
			case IDialogConstants.YES_ID: {
				v.setValue(true);
				return true;
			}
			case IDialogConstants.NO_ID: {
				v.setValue(false);
				return true;
			}
			default:
				// abort
				return false;
			}
		} else {
			// generically handles all other types of items
			return getMultiSpecial(shell, uri, item);
		}
	}

	/**
	 * Opens a generic dialog presenting all CredentialItems to the user.
	 * @param shell the shell to use
	 * @param uri the uri of the get request
	 * @param items the items to handle
	 * @return <code>true</code> if the request was successful and values were supplied;
	 * 		<code>false</code> if the user canceled the request and did not supply all requested values.
	 */
	private boolean getMultiSpecial(Shell shell, URIish uri, CredentialItem... items) {
		CustomPromptDialog dialog = new CustomPromptDialog(shell, uri, UIText.EGitCredentialsProvider_information, items);
		dialog.setBlockOnOpen(true);
		int r = dialog.open();
		if (r == Window.OK) {
			return true;
		}
		return false;
	}

	private UserPasswordCredentials getCredentialsFromUser(final URIish uri) {
		final AtomicReference<UserPasswordCredentials> aRef = new AtomicReference<>(
				null);
		PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
			@Override
			public void run() {
				Shell shell = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow().getShell();
				aRef.set(LoginService.login(shell, uri));
			}
		});
		return aRef.get();
	}
}

Back to the top