Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 460a311e6168d9fb42a2536ec41faea65101cc09 (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
/*******************************************************************************
 * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com>
 * Copyright (C) 2010, Philipp Thun <philipp.thun@sap.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.ui.internal;

import java.io.IOException;
import java.text.MessageFormat;

import org.eclipse.egit.core.securestorage.UserPasswordCredentials;
import org.eclipse.egit.ui.Activator;
import org.eclipse.equinox.security.storage.StorageException;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.transport.URIish;

/**
 * Utilities for EGit secure store
 */
public class SecureStoreUtils {
	/**
	 * Store credentials for the given uri
	 *
	 * @param credentials
	 * @param uri
	 * @return true if successful
	 */
	public static boolean storeCredentials(UserPasswordCredentials credentials,
			URIish uri) {
		if (credentials != null && uri != null) {
			try {
				org.eclipse.egit.core.Activator.getDefault().getSecureStore()
						.putCredentials(uri, credentials);
			} catch (StorageException e) {
				Activator.handleError(MessageFormat.format(
						UIText.SecureStoreUtils_writingCredentialsFailed, uri),
						e, true);
				return false;
			} catch (IOException e) {
				Activator.handleError(MessageFormat.format(
						UIText.SecureStoreUtils_writingCredentialsFailed, uri),
						e, true);
				return false;
			}
		}
		return true;
	}

	/**
	 * Gets credentials stored for the given uri. Logs {@code StorageException}
	 * if thrown by the secure store implementation and removes credentials
	 * which can't be read from secure store
	 *
	 * @param uri
	 * @return credentials stored in secure store for given uri
	 */
	public static @Nullable UserPasswordCredentials getCredentials(
			final URIish uri) {
		try {
			return org.eclipse.egit.core.Activator.getDefault()
					.getSecureStore().getCredentials(uri);
		} catch (StorageException e) {
			Activator.logError(MessageFormat.format(
					UIText.SecureStoreUtils_errorReadingCredentials,
					uri), e);
			clearCredentials(uri);
			return null;
		}
	}

	/**
	 * Clear credentials stored for the given uri if any exist
	 *
	 * @param uri
	 */
	public static void clearCredentials(final URIish uri) {
		try {
			org.eclipse.egit.core.Activator.getDefault().getSecureStore()
					.clearCredentials(uri);
		} catch (IOException e) {
			Activator.logError(MessageFormat.format(
					UIText.SecureStoreUtils_errorClearingCredentials, uri), e);
		}
	}

}

Back to the top