public class CredentialStore extends Object
DefaultCredentialStorePluginImpl. Although both classes are similar,
the low-level logic for managing the credentials has been factored out in this
class for increased flexibility. Note however that contrary to
DefaultCredentialStorePluginImpl, this class does not interact with the user.
Credentials are managed in a Properties file whose containing 2 types of entries:
<REALM>.<user>.Password=<encrypted-password>
<REALM>.DefaultUser=<default-user>
The passwords are encrypted using a key that is constructed from three parts of key material:
This is not the highest security, but is considered sufficient in the current context. Properly securing passwords that must be stored locally is hard. No matter the logic used, one always ends up having to store some decryption key somewhere. The fact that the master password file can be stored in the user home directory, not accessible for reading to others makes the solution sufficiently secure.
This class uses a sequence of mappings between resource Pattern's and corresponding realms and users. These mapping are specified when the class is initialized.
See java.util.regex for information about regular expressions in Java.
A resource is typically the URL of a service, such as https://john.smith@acme.com/my-git-repository.git.
When a method takes a resource, it maps this resource to a realm using these mappings. The first mapping whose resource Pattern matches the resource is used. A mapping must correspond to a resource. A catch-all mapping can be used if necessary.
Passwords are associated with realms obtained from the mappings, not resources directory.
A resource can also include a user. Mappings can specify a captured group to extract it.
Passwords are associated with realms, not resources directly. Therefore no method takes a realm as an argument. However, in some cases, such as for a tool that allows the user to manage the credentials, it can be convenient to let the user explicitly specify realms. This can be achieved generically by introducing a mapping that, for example, maps "REALM:(.*)" to the realm $1 (the first captured group).
| Modifier and Type | Class and Description |
|---|---|
static class |
CredentialStore.RealmUser
Realm and user tupple.
|
static class |
CredentialStore.ResourceInfo
Information about a resource.
|
static class |
CredentialStore.ResourcePatternRealmUser
Mapping from a resource Pattern to a realm and user.
|
| Modifier and Type | Field and Description |
|---|---|
static String |
DEFAULT_CREDENTIAL_FILE
Default credential file.
|
static String |
DEFAULT_MASTER_KEY_FILE
Default master password file.
|
static String |
PROPERTY_SUFFIX_DEFAULT_USER
Property suffix for the default user.
|
static String |
PROPERTY_SUFFIX_PASSWORD
Property suffix for a password.
|
| Constructor and Description |
|---|
CredentialStore(Path pathCredentialFile,
Path pathMasterKeyFile,
List<CredentialStore.ResourcePatternRealmUser> listResourcePatternRealmUser)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
deleteDefaultUser(String resource)
Removes the default user for the realm corresponding to a resource.
|
boolean |
deletePassword(String resource,
String user)
Deletes a password from the credential store.
|
String |
getDefaultUser(String resource)
Returns the default user for a resource.
|
List<CredentialStore.RealmUser> |
getListRealmUser() |
List<CredentialStore.RealmUser> |
getListRealmUserDefault() |
List<CredentialStore.ResourcePatternRealmUser> |
getListResourcePatternRealmUser() |
String |
getPassword(String resource,
String user)
Returns a password from the credential store.
|
CredentialStore.ResourceInfo |
getResourceInfo(String resource)
Returns information about a resource.
|
void |
resetCredentialFile()
Resets the credential file so that it is reloaded when next required.
|
boolean |
setDefaultUser(String resource,
String user)
Sets the default user for the realm corresponding to a resource.
|
boolean |
setPassword(String resource,
String user,
String password)
Sets a password in the credential store.
|
public static final String DEFAULT_CREDENTIAL_FILE
The caller can use this constant to construct a credential file Path which uses that same file name, but a different directory.
public static final String DEFAULT_MASTER_KEY_FILE
The caller can use this constant to construct a master password file Path which uses that same file name, but a different directory.
public static final String PROPERTY_SUFFIX_DEFAULT_USER
public static final String PROPERTY_SUFFIX_PASSWORD
public CredentialStore(Path pathCredentialFile, Path pathMasterKeyFile, List<CredentialStore.ResourcePatternRealmUser> listResourcePatternRealmUser)
If pathMasterPassworedFile is null, the master password file is "dragom-master-password" in the user home directory.
If pathCredentialFile is null, the credential file is "dragom-credentials.properties" in the user home directory.
After initialization, listResourcePatternRealmUser is considered as belonging to this class and should not be modified by the caller. This ciass does not make a copy for efficiency reasons.
pathMasterKeyFile - Path of the master password file. Can be null.pathCredentialFile - Path of the credential file. Can be null.listResourcePatternRealmUser - List of CredentialStore.ResourcePatternRealmUser.public void resetCredentialFile()
public CredentialStore.ResourceInfo getResourceInfo(String resource)
resource - Resource.public String getPassword(String resource, String user)
If no resourcePattern mapping to a realm and user is found, null is returned.
If user is null, a user must be inferred. If it is specified within the resource, this user is used. Otherwise, if a default user is defined in the credentials for the realm corresponding to the resource, this user is used. Otherwise, null is returned.
If user is not null and a user is specified within the resource, they must match, otherwise null is returned.
If null is returned, getResourceInfo(java.lang.String) can be called to know more about
the resource.
resource - Resource.user - User. Can be null.public boolean setPassword(String resource, String user, String password)
If no resourcePattern mapping to a realm and user is found, false is returned.
If user is null, a user must be inferred. If it is specified within the resource, this user is used. Otherwise, if a default user is defined in the credentials for the realm corresponding to the resource, this user is used. Otherwise, false is returned.
If user is not null and a user is specified within the resource, they must match, otherwise false is returned.
If false is returned, getResourceInfo(java.lang.String) can be called to know more about
the resource.
If a password is already set for the realm and the user, it is overwritten.
resource - Resource.user - User. Can be null.password - Password.public boolean deletePassword(String resource, String user)
If no resourcePattern mapping to a realm and user is found, false is returned.
If user is null, a user must be inferred. If it is specified within the resource, this user is used. Otherwise, if a default user is defined in the credentials for the realm corresponding to the resource, this user is used. Otherwise, false is returned.
If user is not null and a user is specified within the resource, they must match, otherwise false is returned.
If false is returned, getResourceInfo(java.lang.String) can be called to know more about
the resource.
resource - Resource.user - User. Can be null.public List<CredentialStore.ResourcePatternRealmUser> getListResourcePatternRealmUser()
CredentialStore.ResourcePatternRealmUser.public List<CredentialStore.RealmUser> getListRealmUser()
public List<CredentialStore.RealmUser> getListRealmUserDefault()
public String getDefaultUser(String resource)
If no resourcePattern mapping to a realm and user is found, null is returned.
If the resource specifies a user, null is returned.
Otherwise, the default user specified for the realm corresponding to the resource is returned.
If no default user is defined for the realm, null is returned.
resource - Resource.public boolean setDefaultUser(String resource, String user)
If no resourcePattern mapping to a realm and user is found, false is returned.
If the resource specifies a user, false is returned.
If a default user is already defined for the realm corresponding to the resource, it is overwritten.
resource - Resource.user - User.public boolean deleteDefaultUser(String resource)
If no resourcePattern mapping to a realm and user is found, false is returned.
If the resource specifies a user, false is returned.
resource - Resource.Copyright © 2015–2016 AZYVA INC.. All rights reserved.