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