Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8c7318054b2c45016ee3417b5f5dd9d1ee94da38 (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
/*******************************************************************************
 * Copyright (c) 2008, 2017 IBM Corporation and others.
 *
 * 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
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.security.ui.storage;

import org.eclipse.equinox.internal.security.storage.friends.InternalExchangeUtils;
import org.eclipse.equinox.internal.security.ui.nls.SecUIMessages;
import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
import org.eclipse.jface.dialogs.*;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.PlatformUI;

public class PasswordRecoveryDialog extends TitleAreaDialog {

	private static final String HELP_ID = "org.eclipse.equinox.security.ui.password_recovery_dialog"; //$NON-NLS-1$

	protected Text[] answers;
	protected String moduleID;
	protected String[] answersText = null;
	protected Button okButton;

	final protected String[] questionsText;

	public PasswordRecoveryDialog(String[] questionsText, Shell parentShell, String moduleID) {
		super(parentShell);
		this.questionsText = questionsText;
		this.moduleID = moduleID;
		answers = new Text[questionsText.length];
	}

	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText(SecUIMessages.pswdRecoveryTitle);
		PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, HELP_ID);
	}

	protected void createButtonsForButtonBar(Composite parent) {
		okButton = createButton(parent, IDialogConstants.OK_ID, SecUIMessages.pswRecoveryButtonOK, true);
		okButton.setEnabled(false);
		createButton(parent, IDialogConstants.CANCEL_ID, SecUIMessages.pswRecoveryButtonCancel, false);
	}

	protected boolean isResizable() {
		return true;
	}

	protected Control createDialogArea(Composite parent) {
		Composite compositeTop = (Composite) super.createDialogArea(parent);
		Composite composite = new Composite(compositeTop, SWT.NONE);

		setMessage(SecUIMessages.pswRecoveryMsg, IMessageProvider.INFORMATION);

		for (int i = 0; i < questionsText.length; i++) {
			Group group = new Group(composite, SWT.NONE);
			group.setText(NLS.bind(SecUIMessages.passwordGroup, Integer.toString(i + 1)));
			group.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
			group.setLayout(new GridLayout());

			String question = NLS.bind(SecUIMessages.pswRecoveryQuestion, questionsText[i]);
			new Label(group, SWT.LEFT).setText(question);
			answers[i] = new Text(group, SWT.LEFT | SWT.BORDER);
			answers[i].setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
			answers[i].addModifyListener(event -> validateOK());
		}

		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		GridLayoutFactory.swtDefaults().generateLayout(composite);

		return composite;
	}

	protected void validateOK() {
		boolean valid = true;
		for (int i = 0; i < questionsText.length; i++) {
			if (answers[i] == null)
				continue;
			String question = answers[i].getText();
			if (question == null || question.length() == 0) {
				valid = false;
				break;
			}
		}
		if (valid)
			setMessage(SecUIMessages.pswRecoveryMsg, IMessageProvider.INFORMATION);
		else
			setMessage(SecUIMessages.pswRecoveryWarning, IMessageProvider.WARNING);
		okButton.setEnabled(valid);
	}

	protected void okPressed() {
		answersText = new String[questionsText.length];
		for (int i = 0; i < questionsText.length; i++) {
			answersText[i] = answers[i].getText();
		}

		String password = InternalExchangeUtils.recoverPassword(answersText, SecurePreferencesFactory.getDefault(), moduleID);
		if (password == null) {
			MessageBox prompt = new MessageBox(getShell(), SWT.ICON_ERROR | SWT.YES | SWT.NO);
			prompt.setText(SecUIMessages.pswdRecoveryTitle);
			prompt.setMessage(SecUIMessages.pswNotRecoveredMsg);
			if (prompt.open() == SWT.YES)
				return;
		} else { // even in UI case we use digested and encoded password - makes no sense to show it
			MessageBox prompt = new MessageBox(getShell(), SWT.ICON_INFORMATION | SWT.OK);
			prompt.setText(SecUIMessages.pswdRecoveryTitle);
			prompt.setMessage(SecUIMessages.pswRecoveredMsg);
			prompt.open();
		}

		super.okPressed();
	}

	public String[] getResult() {
		return answersText;
	}

}

Back to the top