This repository has been archived by the owner on Oct 27, 2019. It is now read-only.
/
SecureStorageUserAdmin.java
271 lines (243 loc) · 9.58 KB
/
SecureStorageUserAdmin.java
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*******************************************************************************
* Copyright (c) 2011 IBM Corporation and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.orion.internal.server.user.securestorage;
import org.eclipse.orion.server.useradmin.servlets.UserServlet;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.orion.internal.server.servlets.workspace.authorization.AuthorizationService;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.spec.PBEKeySpec;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.security.storage.ISecurePreferences;
import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
import org.eclipse.equinox.security.storage.StorageException;
import org.eclipse.equinox.security.storage.provider.IProviderHints;
import org.eclipse.orion.server.core.LogHelper;
import org.eclipse.orion.server.useradmin.EmptyAuthorization;
import org.eclipse.orion.server.useradmin.OrionUserAdmin;
import org.eclipse.orion.server.useradmin.Role;
import org.eclipse.orion.server.useradmin.User;
import org.eclipse.orion.server.useradmin.WebIdeAuthorization;
import org.eclipse.osgi.service.datalocation.Location;
import org.osgi.framework.BundleContext;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.service.useradmin.Authorization;
/**
* The implementation of User Service on Equinox Secure Storage
*/
public class SecureStorageUserAdmin extends OrionUserAdmin {
static final String ORION_SERVER_NODE = "org.eclipse.orion.server"; //$NON-NLS-1$
static final String USERS = "users"; //$NON-NLS-1$
static final String USER_LOGIN = "login"; //$NON-NLS-1$
static final String USER_NAME = "name"; //$NON-NLS-1$
static final String USER_PASSWORD = "password"; //$NON-NLS-1$
static final String USER_ROLES = "roles"; //$NON-NLS-1$
static final String USER_ROLE_NAME = "name"; //$NON-NLS-1$
static final String ADMIN_LOGIN_VALUE = "admin"; //$NON-NLS-1$
static final String ADMIN_NAME_VALUE = "Administrator"; //$NON-NLS-1$
private ISecurePreferences storage;
private Map<String, Role> roles = new HashMap<String, Role>();
public SecureStorageUserAdmin() {
initSecurePreferences();
initStorage();
}
private void initStorage() {
// initialize the admin account
String adminDefaultPassword = System.getProperty(Activator.ORION_STORAGE_ADMIN_DEFAULT_PASSWORD, null);
if (adminDefaultPassword != null && getUser(USER_LOGIN, ADMIN_LOGIN_VALUE) == null) {
createUser(new User(ADMIN_LOGIN_VALUE, ADMIN_NAME_VALUE, adminDefaultPassword));
try {
AuthorizationService.addUserRight(ADMIN_LOGIN_VALUE, UserServlet.USERS_URI);
AuthorizationService.addUserRight(ADMIN_LOGIN_VALUE, UserServlet.USERS_URI +"/*"); //$NON-NLS-1$
} catch (CoreException e) {
LogHelper.log(e);
}
}
//add default roles
for (String role : new String[] {"admin", "user", "quest"}) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
roles.put(role, new Role(role, org.osgi.service.useradmin.Role.ROLE));
}
private void initSecurePreferences() {
//try to create our own secure storage under the platform instance location
URL location = getStorageLocation();
if (location != null) {
Map<String, Object> options = new HashMap<String, Object>();
options.put(IProviderHints.PROMPT_USER, Boolean.FALSE);
String password = System.getProperty(Activator.ORION_STORAGE_PASSWORD, ""); //$NON-NLS-1$
options.put(IProviderHints.DEFAULT_PASSWORD, new PBEKeySpec(password.toCharArray()));
try {
storage = SecurePreferencesFactory.open(location, options);
} catch (IOException e) {
LogHelper.log(new Status(IStatus.ERROR, Activator.PI_USER_SECURESTORAGE, "Error initializing user storage location", e)); //$NON-NLS-1$
}
} else {
LogHelper.log(new Status(IStatus.WARNING, Activator.PI_USER_SECURESTORAGE, "No instance location set. Storing user data in user home directory")); //$NON-NLS-1$
}
//fall back to default secure storage location if we failed to create our own
if (storage == null)
storage = SecurePreferencesFactory.getDefault().node(ORION_SERVER_NODE);
}
/**
* Returns the location for user data to be stored.
*/
private URL getStorageLocation() {
BundleContext context = Activator.getContext();
Collection<ServiceReference<Location>> refs;
try {
refs = context.getServiceReferences(Location.class, Location.INSTANCE_FILTER);
} catch (InvalidSyntaxException e) {
// we know the instance location filter syntax is valid
throw new RuntimeException(e);
}
if (refs.isEmpty())
return null;
ServiceReference<Location> ref = refs.iterator().next();
Location location = context.getService(ref);
try {
try {
if (location != null)
return location.getDataArea(Activator.PI_USER_SECURESTORAGE + "/user_store"); //$NON-NLS-1$
} catch (IOException e) {
LogHelper.log(e);
}
} finally {
context.ungetService(ref);
}
//return null if we are unable to determine instance location.
return null;
}
public Role createRole(String name, int type) {
throw new UnsupportedOperationException();
}
public boolean removeRole(String name) {
return false;
}
public Role getRole(String name) {
return roles.get(name);
}
public Role[] getRoles(String filter) throws InvalidSyntaxException {
return (Role[]) roles.values().toArray();
}
public Collection<User> getUsers() {
if (!storage.nodeExists(USERS)) {
return null;
}
ISecurePreferences usersPrefs = storage.node(USERS);
Collection<User> users = null;
for (String childName : usersPrefs.childrenNames()) {
if (users == null)
users = new ArrayList<User>();
ISecurePreferences userPrefs = usersPrefs.node(childName);
try {
User user = new User(childName, userPrefs.get(USER_NAME, ""), "" /* don't expose the password */); //$NON-NLS-1$ //$NON-NLS-2$
for (String roleName : userPrefs.node(USER_ROLES).childrenNames()) {
user.addRole(getRole(roleName));
}
users.add(user);
} catch (StorageException e) {
LogHelper.log(new Status(IStatus.ERROR, Activator.PI_USER_SECURESTORAGE, IStatus.ERROR, "Error loading user: " + childName, e)); //$NON-NLS-1$
}
}
return users;
}
public User getUser(String key, String value) {
// TODO currently searching only by login, all other searches return nothing
if (key.equals(USER_LOGIN)) {
String nodeName = USERS + '/' + value;
if (!storage.nodeExists(nodeName)) {
return null;
}
ISecurePreferences userPrefs = storage.node(nodeName);
try {
User user = new User(value, userPrefs.get(USER_NAME, ""), userPrefs.get(USER_PASSWORD, "")); //$NON-NLS-1$ //$NON-NLS-2$
for (String roleName : userPrefs.node(USER_ROLES).childrenNames()) {
user.addRole(getRole(roleName));
}
return user;
} catch (StorageException e) {
LogHelper.log(new Status(IStatus.ERROR, Activator.PI_USER_SECURESTORAGE, IStatus.ERROR, "Can not get user: " + value, e)); //$NON-NLS-1$
}
}
return null;
}
public User createUser(User user) {
String nodeName = USERS + '/' + user.getLogin();
if (storage.nodeExists(nodeName)) {
return null;
}
try {
internalCreateOrUpdateUser(storage.node(nodeName), user);
return user;
} catch (Exception e) {
LogHelper.log(new Status(IStatus.ERROR, Activator.PI_USER_SECURESTORAGE, IStatus.ERROR, "Can not create user: " + user.getLogin(), e)); //$NON-NLS-1$
}
return null;
}
public boolean updateUser(String oldLogin, User user) {
String nodeName = USERS + '/' + user.getLogin();
if (!storage.nodeExists(nodeName)) {
return false;
}
try {
internalCreateOrUpdateUser(storage.node(nodeName), user);
return true;
} catch (Exception e) {
LogHelper.log(new Status(IStatus.ERROR, Activator.PI_USER_SECURESTORAGE, IStatus.ERROR, "Can not update user: " + user.getLogin(), e)); //$NON-NLS-1$
}
return false;
}
private User internalCreateOrUpdateUser(ISecurePreferences userPrefs, User user) throws StorageException, IOException {
userPrefs.put(USER_NAME, user.getName(), false);
userPrefs.put(USER_PASSWORD, user.getPassword(), true);
ISecurePreferences rolesPrefs = userPrefs.node(USER_ROLES);
for (String roleName : rolesPrefs.childrenNames())
rolesPrefs.node(roleName).removeNode();
for (org.osgi.service.useradmin.Role role : user.getRoles())
rolesPrefs.node(((Role) role).getName());
userPrefs.flush();
return user;
}
public boolean deleteUser(User user) {
String nodeName = USERS + '/' + user.getLogin();
if (!storage.nodeExists(nodeName)) {
return false;
}
ISecurePreferences userPrefs = storage.node(nodeName);
userPrefs.removeNode();
try {
userPrefs.flush();
return true;
} catch (IOException e) {
LogHelper.log(new Status(IStatus.ERROR, Activator.PI_USER_SECURESTORAGE, IStatus.ERROR, "Cannot delete user: " + user.getLogin(), e)); //$NON-NLS-1$
}
return false;
}
public Authorization getAuthorization(org.osgi.service.useradmin.User user) {
if (user instanceof User) {
return new WebIdeAuthorization((User) user);
}
return new EmptyAuthorization();
}
@Override
public boolean canCreateUsers() {
return true;
}
@Override
public String getStoreName() {
return "Orion";
}
}