]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/auth/AuthenticationUtils.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / auth / AuthenticationUtils.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.ui.auth;
13
14 import java.io.IOException;
15
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;
26
27 /**
28  * @author Tuukka Lehtonen
29  */
30 public class AuthenticationUtils {
31
32     /**
33      * @param forServer
34      * @param forceQuery always present query dialog regardless of the remember
35      *        setting attribute
36      * @return
37      */
38     public static LoginModel queryAuthenticationInfo(Shell parentShell, ServerInfo forServer, boolean forceQuery) {
39         LoginModel model = loadAuthenticationInfo(forServer);
40         if (model == null)
41             model = new LoginModel(forServer);
42
43         boolean remember = model.isRemember();
44         if (remember) {
45             // Verify that the model data contains at least something before
46             // skipping the interactive query.
47             if (model.getName().trim().isEmpty())
48                 remember = false;
49         }
50         if (!remember || forceQuery) {
51             LoginDialog dialog = new LoginDialog(parentShell, Activator.getDefault().getDialogSettings(), model);
52             if (dialog.open() != Dialog.OK)
53                 return null;
54         }
55
56         return model;
57     }
58
59     private static LoginModel loadAuthenticationInfo(ServerInfo forServer) {
60         // Cannot store any info if the server cannot be identified uniquely.
61         ServerInformation info = forServer.getInfo();
62         if (info == null)
63             return null;
64         String id = formId(info);
65
66 //        try {
67 //            Preferences preferences = new InstanceScope().getNode(Activator.PLUGIN_ID);
68 //            if (!preferences.nodeExists(id))
69 //                return null;
70 //            Preferences server = preferences.node(id);
71 //            
72 //            String name = server.get("name", "");
73 //            String pass = server.get("pass", "");
74 //
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);
79 //            return null;
80 //        }
81
82         try {
83             ISecurePreferences preferences = SecurePreferencesFactory.getDefault().node("procore");
84             if (!preferences.nodeExists(id))
85                 return null;
86             ISecurePreferences server = preferences.node(id);
87             
88             String name = server.get("name", "");
89             String pass = server.get("pass", "");
90
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);
95             return null;
96         }
97     }
98     
99     public static void storeAuthenticationInfo(ServerInfo forServer, LoginModel model) throws IOException {
100         ServerInformation info = forServer.getInfo();
101         if (info == null)
102             throw new IllegalArgumentException("server information not available for uniquely identifying the server");
103         String id = formId(info);
104
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);
109 //        try {
110 //            server.flush();
111 //        } catch (BackingStoreException e) {
112 //            throw new IOException(e);
113 //        }
114
115         ISecurePreferences preferences = SecurePreferencesFactory.getDefault().node("procore");
116         ISecurePreferences server = preferences.node(id);
117         try {
118             server.put("name", model.getName(), false);
119             server.put("pass", model.getPassword(), true);
120             server.flush();
121         } catch (StorageException e) {
122             throw new IOException(e);
123         }
124     }
125
126     private static String formId(ServerInformation info) {
127         return info.getDatabaseId();
128     }
129     
130 }