// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.io.auth;
import java.net.PasswordAuthentication;
import java.net.Authenticator.RequestorType;
import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.data.oauth.OAuthToken;
import org.openstreetmap.josm.gui.io.CredentialDialog;
import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel;
/**
* This is the default credential manager in JOSM. It keeps username and password for both
* the OSM API and an optional HTTP proxy in the JOSM preferences file.
*
*/
public class JosmPreferencesCredentialManager implements CredentialsManager {
/**
* @see CredentialsManager#lookup(RequestorType)
*/
public PasswordAuthentication lookup(RequestorType requestorType) throws CredentialsManagerException{
if (requestorType == null)
return null;
String user;
String password;
switch(requestorType) {
case SERVER:
user = Main.pref.get("osm-server.username", null);
password = Main.pref.get("osm-server.password", null);
if (user == null)
return null;
return new PasswordAuthentication(user, password == null ? new char[0] : password.toCharArray());
case PROXY:
user = Main.pref.get(ProxyPreferencesPanel.PROXY_USER, null);
password = Main.pref.get(ProxyPreferencesPanel.PROXY_PASS, null);
if (user == null)
return null;
return new PasswordAuthentication(user, password == null ? null : password.toCharArray());
}
return null;
}
/**
* @see CredentialsManager#store(RequestorType, PasswordAuthentication)
*/
public void store(RequestorType requestorType, PasswordAuthentication credentials) throws CredentialsManagerException {
if (requestorType == null)
return;
switch(requestorType) {
case SERVER:
Main.pref.put("osm-server.username", credentials.getUserName());
if (credentials.getPassword() == null) {
Main.pref.put("osm-server.password", null);
} else {
Main.pref.put("osm-server.password", String.valueOf(credentials.getPassword()));
}
break;
case PROXY:
Main.pref.put(ProxyPreferencesPanel.PROXY_USER, credentials.getUserName());
if (credentials.getPassword() == null) {
Main.pref.put(ProxyPreferencesPanel.PROXY_PASS, null);
} else {
Main.pref.put(ProxyPreferencesPanel.PROXY_PASS, String.valueOf(credentials.getPassword()));
}
break;
}
}
/**
* @see CredentialsManager#getCredentials(RequestorType, boolean)
*/
public CredentialsManagerResponse getCredentials(RequestorType requestorType, boolean noSuccessWithLastResponse) throws CredentialsManagerException{
if (requestorType == null)
return null;
PasswordAuthentication credentials = lookup(requestorType);
String username = (credentials == null || credentials.getUserName() == null) ? "" : credentials.getUserName();
String password = (credentials == null || credentials.getPassword() == null) ? "" : String.valueOf(credentials.getPassword());
CredentialsManagerResponse response = new CredentialsManagerResponse();
if (noSuccessWithLastResponse|| username.equals("") || password.equals("")) {
CredentialDialog dialog = null;
switch(requestorType) {
case SERVER: dialog = CredentialDialog.getOsmApiCredentialDialog(username, password); break;
case PROXY: dialog = CredentialDialog.getHttpProxyCredentialDialog(username, password); break;
}
dialog.setVisible(true);
response.setCanceled(dialog.isCanceled());
if (dialog.isCanceled())
return response;
response.setUsername(dialog.getUsername());
response.setPassword(dialog.getPassword());
if (dialog.isSaveCredentials()) {
store(requestorType, new PasswordAuthentication(
response.getUsername(),
response.getPassword()
));
}
} else {
response.setUsername(username);
response.setPassword(password.toCharArray());
response.setCanceled(false);
}
return response;
}
/**
* Lookup the current OAuth Access Token to access the OSM server. Replies null, if no
* Access Token is currently managed by this CredentialManager.
*
* @return the current OAuth Access Token to access the OSM server.
* @throws CredentialsManagerException thrown if something goes wrong
*/
public OAuthToken lookupOAuthAccessToken() throws CredentialsManagerException {
String accessTokenKey = Main.pref.get("oauth.access-token.key", null);
String accessTokenSecret = Main.pref.get("oauth.access-token.secret", null);
if (accessTokenKey == null && accessTokenSecret == null)
return null;
return new OAuthToken(accessTokenKey, accessTokenSecret);
}
/**
* Stores the OAuth Access Token <code>accessToken</code>.
*
* @param accessToken the access Token. null, to remove the Access Token.
* @throws CredentialsManagerException thrown if something goes wrong
*/
public void storeOAuthAccessToken(OAuthToken accessToken) throws CredentialsManagerException {
if (accessToken == null) {
Main.pref.put("oauth.access-token.key", null);
Main.pref.put("oauth.access-token.secret", null);
} else {
Main.pref.put("oauth.access-token.key", accessToken.getKey());
Main.pref.put("oauth.access-token.secret", accessToken.getSecret());
}
}
}