1 /*******************************************************************************
2 * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com>
3 * Copyright (C) 2010, Philipp Thun <philipp.thun@sap.com>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License 2.0
7 * which accompanies this distribution, and is available at
8 * https://www.eclipse.org/legal/epl-2.0/
10 * SPDX-License-Identifier: EPL-2.0
11 *******************************************************************************/
12 package org
.eclipse
.egit
.ui
.internal
;
14 import java
.io
.IOException
;
15 import java
.text
.MessageFormat
;
17 import org
.eclipse
.egit
.core
.credentials
.UserPasswordCredentials
;
18 import org
.eclipse
.egit
.ui
.Activator
;
19 import org
.eclipse
.equinox
.security
.storage
.StorageException
;
20 import org
.eclipse
.jgit
.annotations
.Nullable
;
21 import org
.eclipse
.jgit
.transport
.URIish
;
24 * Utilities for EGit secure store
26 public class SecureStoreUtils
{
28 * Store credentials for the given uri
32 * @return true if successful
34 public static boolean storeCredentials(UserPasswordCredentials credentials
,
36 if (credentials
!= null && uri
!= null) {
38 org
.eclipse
.egit
.core
.Activator
.getDefault()
39 .getCredentialsStore().putCredentials(uri
, credentials
);
40 } catch (StorageException
| IOException e
) {
41 Activator
.handleError(MessageFormat
.format(
42 UIText
.SecureStoreUtils_writingCredentialsFailed
, uri
),
51 * Gets credentials stored for the given uri. Logs {@code StorageException}
52 * if thrown by the secure store implementation and removes credentials
53 * which can't be read from secure store
56 * @return credentials stored in secure store for given uri
58 public static @Nullable UserPasswordCredentials
getCredentials(
64 return org
.eclipse
.egit
.core
.Activator
.getDefault()
65 .getCredentialsStore().getCredentials(uri
);
66 } catch (StorageException e
) {
67 Activator
.logError(MessageFormat
.format(
68 UIText
.SecureStoreUtils_errorReadingCredentials
,
70 clearCredentials(uri
);
76 * Clear credentials stored for the given uri if any exist
80 public static void clearCredentials(final URIish uri
) {
85 org
.eclipse
.egit
.core
.Activator
.getDefault().getCredentialsStore()
86 .clearCredentials(uri
);
87 } catch (IOException e
) {
88 Activator
.logError(MessageFormat
.format(
89 UIText
.SecureStoreUtils_errorClearingCredentials
, uri
), e
);