1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.ui.auth;
\r
14 import java.io.IOException;
\r
16 import org.eclipse.equinox.security.storage.ISecurePreferences;
\r
17 import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
\r
18 import org.eclipse.equinox.security.storage.StorageException;
\r
19 import org.eclipse.jface.dialogs.Dialog;
\r
20 import org.eclipse.swt.widgets.Shell;
\r
21 import org.simantics.db.management.discovery.ServerInfo;
\r
22 import org.simantics.db.service.ServerInformation;
\r
23 import org.simantics.ui.auth.model.LoginModel;
\r
24 import org.simantics.ui.internal.Activator;
\r
25 import org.simantics.utils.ui.ExceptionUtils;
\r
28 * @author Tuukka Lehtonen
\r
30 public class AuthenticationUtils {
\r
34 * @param forceQuery always present query dialog regardless of the remember
\r
38 public static LoginModel queryAuthenticationInfo(Shell parentShell, ServerInfo forServer, boolean forceQuery) {
\r
39 LoginModel model = loadAuthenticationInfo(forServer);
\r
41 model = new LoginModel(forServer);
\r
43 boolean remember = model.isRemember();
\r
45 // Verify that the model data contains at least something before
\r
46 // skipping the interactive query.
\r
47 if (model.getName().trim().isEmpty())
\r
50 if (!remember || forceQuery) {
\r
51 LoginDialog dialog = new LoginDialog(parentShell, Activator.getDefault().getDialogSettings(), model);
\r
52 if (dialog.open() != Dialog.OK)
\r
59 private static LoginModel loadAuthenticationInfo(ServerInfo forServer) {
\r
60 // Cannot store any info if the server cannot be identified uniquely.
\r
61 ServerInformation info = forServer.getInfo();
\r
64 String id = formId(info);
\r
67 // Preferences preferences = new InstanceScope().getNode(Activator.PLUGIN_ID);
\r
68 // if (!preferences.nodeExists(id))
\r
70 // Preferences server = preferences.node(id);
\r
72 // String name = server.get("name", "");
\r
73 // String pass = server.get("pass", "");
\r
75 // return new LoginModel(forServer, name, pass, true, true);
\r
76 // } catch (BackingStoreException e) {
\r
77 // // Don't pass this forward, just log it.
\r
78 // ExceptionUtils.logAndShowError(e);
\r
83 ISecurePreferences preferences = SecurePreferencesFactory.getDefault().node("procore");
\r
84 if (!preferences.nodeExists(id))
\r
86 ISecurePreferences server = preferences.node(id);
\r
88 String name = server.get("name", "");
\r
89 String pass = server.get("pass", "");
\r
91 return new LoginModel(forServer, name, pass, true, true);
\r
92 } catch (StorageException e) {
\r
93 // Don't pass this forward, just log it.
\r
94 ExceptionUtils.logAndShowError(e);
\r
99 public static void storeAuthenticationInfo(ServerInfo forServer, LoginModel model) throws IOException {
\r
100 ServerInformation info = forServer.getInfo();
\r
102 throw new IllegalArgumentException("server information not available for uniquely identifying the server");
\r
103 String id = formId(info);
\r
105 // Preferences preferences = new InstanceScope().getNode(Activator.PLUGIN_ID);
\r
106 // Preferences server = preferences.node(id);
\r
107 // server.put("name", model.getName());
\r
108 // server.put("hash", hashData);
\r
111 // } catch (BackingStoreException e) {
\r
112 // throw new IOException(e);
\r
115 ISecurePreferences preferences = SecurePreferencesFactory.getDefault().node("procore");
\r
116 ISecurePreferences server = preferences.node(id);
\r
118 server.put("name", model.getName(), false);
\r
119 server.put("pass", model.getPassword(), true);
\r
121 } catch (StorageException e) {
\r
122 throw new IOException(e);
\r
126 private static String formId(ServerInformation info) {
\r
127 return info.getDatabaseId();
\r