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