Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4acee60e8d88217fdafa676ad057795afc95e496 (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
/*******************************************************************************
 * Copyright (c) 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.security.ui.storage;

import java.net.URL;
import javax.crypto.spec.PBEKeySpec;
import org.eclipse.equinox.internal.security.storage.friends.InternalExchangeUtils;
import org.eclipse.equinox.internal.security.ui.nls.SecUIMessages;
import org.eclipse.equinox.security.storage.provider.IPreferencesContainer;
import org.eclipse.equinox.security.storage.provider.PasswordProvider;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.PlatformUI;

/**
 * This password provider prompts user for the password. This provider uses the same password for
 * all secure preferences.
 */
public class DefaultPasswordProvider extends PasswordProvider {

	public PBEKeySpec getPassword(IPreferencesContainer container, int passwordType) {
		if (!StorageUtils.showUI(container))
			return null;

		boolean newPassword = ((passwordType & CREATE_NEW_PASSWORD) != 0);
		boolean passwordChange = ((passwordType & PASSWORD_CHANGE) != 0);

		String location = container.getLocation().getFile();
		URL defaultURL = InternalExchangeUtils.defaultStorageLocation();
		if (defaultURL != null) { // remove default location from the dialog
			String defaultFile = defaultURL.getFile();
			if (defaultFile != null && defaultFile.equals(location))
				location = null;
		}

		final StorageLoginDialog loginDialog = new StorageLoginDialog(newPassword, passwordChange, location);

		final PBEKeySpec[] result = new PBEKeySpec[1];
		PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
			public void run() {
				if (loginDialog.open() == Window.OK)
					result[0] = loginDialog.getGeneratedPassword();
				else
					result[0] = null;
			}
		});
		return result[0];
	}

	public boolean retryOnError(Exception e, IPreferencesContainer container) {
		if (!StorageUtils.showUI(container))
			return false;

		final Boolean[] result = new Boolean[1];
		PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
			public void run() {
				boolean reply = MessageDialog.openConfirm(StorageUtils.getShell(), SecUIMessages.exceptionTitle, SecUIMessages.exceptionDecode);
				result[0] = Boolean.valueOf(reply);
			}
		});
		return result[0].booleanValue();
	}
}

Back to the top