Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8be852dc84a9fd90e0e502ec8e08a85f4fab7f44 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2010 Tasktop Technologies 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.commons.repositories.core;

import java.io.IOException;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.security.storage.EncodingUtils;
import org.eclipse.equinox.security.storage.ISecurePreferences;
import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
import org.eclipse.equinox.security.storage.StorageException;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.commons.repositories.core.auth.ICredentialsStore;

/**
 * @author Steffen Pingel
 */
public class SecureCredentialsStore implements ICredentialsStore {

	private static final String ID_NODE = "org.eclipse.mylyn.commons.repository"; //$NON-NLS-1$

	private final String id;

	private InMemoryCredentialsStore inMemoryStore;

	private boolean loggedStorageException;

	public SecureCredentialsStore(String id) {
		this.id = id;
	}

	public void clear() {
		//getSecurePreferences().clear();
		getSecurePreferences().removeNode();
		getInMemoryStore().clear();
	}

	public synchronized void copyTo(ICredentialsStore target) {
		Assert.isNotNull(target);
		if (!(target instanceof SecureCredentialsStore)) {
			throw new IllegalArgumentException(
					"SecureCredentialsStore may only by copied to stores of the same type: " + target.getClass()); //$NON-NLS-1$
		}

		ISecurePreferences preferences = getSecurePreferences();
		for (String key : preferences.keys()) {
			try {
				String value = preferences.get(key, null);
				boolean encrypted = preferences.isEncrypted(key);
				target.put(key, value, encrypted);
			} catch (StorageException e) {
				handle(e);
			}
		}
		if (inMemoryStore != null) {
			inMemoryStore.copyTo(target);
		}
	}

	public void flush() throws IOException {
		getSecurePreferences().flush();
	}

	public String get(final String key, final String def) {
		InMemoryCredentialsStore memoryStore = getInMemoryStore();
		synchronized (memoryStore) {
			if (memoryStore.hasKey(key)) {
				return memoryStore.get(key, def);
			}
		}
		try {
			return getSecurePreferences().get(key, def);
		} catch (StorageException e) {
			handle(e);
			return memoryStore.get(key, def);
		}
	}

	public boolean getBoolean(String key, boolean def) {
		InMemoryCredentialsStore memoryStore = getInMemoryStore();
		synchronized (memoryStore) {
			if (memoryStore.hasKey(key)) {
				return memoryStore.getBoolean(key, def);
			}
		}
		try {
			return getSecurePreferences().getBoolean(key, def);
		} catch (StorageException e) {
			handle(e);
			return memoryStore.getBoolean(key, def);
		}
	}

	public byte[] getByteArray(String key, byte[] def) {
		InMemoryCredentialsStore memoryStore = getInMemoryStore();
		synchronized (memoryStore) {
			if (memoryStore.hasKey(key)) {
				return memoryStore.getByteArray(key, def);
			}
		}
		try {
			return getSecurePreferences().getByteArray(key, def);
		} catch (StorageException e) {
			handle(e);
			return memoryStore.getByteArray(key, def);
		}
	}

	public String getId() {
		return id;
	}

	public String[] keys() {
		return getSecurePreferences().keys();
	}

	public void put(String key, String value, boolean encrypt) {
		put(key, value, encrypt, true);
	}

	public void put(String key, String value, boolean encrypt, boolean persist) {
		if (persist) {
			try {
				getSecurePreferences().put(key, value, encrypt);
				getInMemoryStore().remove(key);
			} catch (StorageException e) {
				handle(e);
				getInMemoryStore().put(key, value, encrypt, true);
			}
		} else {
			getInMemoryStore().put(key, value, encrypt, false);
			getSecurePreferences().remove(key);
		}
	}

	public void putBoolean(String key, boolean value, boolean encrypt) {
		try {
			getSecurePreferences().putBoolean(key, value, encrypt);
			getInMemoryStore().remove(key);
		} catch (StorageException e) {
			handle(e);
			getInMemoryStore().putBoolean(key, value, encrypt);
		}
	}

	public void putByteArray(String key, byte[] value, boolean encrypt) {
		try {
			getSecurePreferences().putByteArray(key, value, encrypt);
			getInMemoryStore().remove(key);
		} catch (StorageException e) {
			handle(e);
			getInMemoryStore().putByteArray(key, value, encrypt);
		}
	}

	public void remove(String key) {
		getSecurePreferences().remove(key);
	}

	protected synchronized InMemoryCredentialsStore getInMemoryStore() {
		if (inMemoryStore == null) {
			inMemoryStore = InMemoryCredentialsStore.getStore(id);
		}
		return inMemoryStore;
	}

	protected ISecurePreferences getSecurePreferences() {
		ISecurePreferences securePreferences = SecurePreferencesFactory.getDefault().node(ID_NODE);
		securePreferences = securePreferences.node(EncodingUtils.encodeSlashes(getId()));
		return securePreferences;
	}

	private void handle(StorageException e) {
		if (!loggedStorageException) {
			loggedStorageException = true;
			StatusHandler.log(new Status(
					IStatus.ERROR,
					RepositoriesCoreInternal.ID_PLUGIN,
					"Unexpected error accessing secure storage, falling back to in memory store for credentials. Some credentials may not be saved.", //$NON-NLS-1$
					e));
		}
	}

}

Back to the top