Skip to content

Commit

Permalink
it works
Browse files Browse the repository at this point in the history
  • Loading branch information
James Michael DuPont committed Apr 24, 2010
1 parent 037f72b commit 2432202
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 49 deletions.
Expand Up @@ -13,7 +13,7 @@
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.PasswordAuthentication;
import java.net.Authenticator.RequestorType;
//import java.net.Authenticator.RequestorType;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
Expand Down Expand Up @@ -185,7 +185,7 @@ public void initFromPreferences(Preferences pref) {
super.initFromPreferences(pref);
CredentialsManager cm = CredentialsManagerFactory.getCredentialManager();
try {
PasswordAuthentication pa = cm.lookup(RequestorType.SERVER);
PasswordAuthentication pa = cm.lookup(/*RequestorType.SERVER*/);
if (pa == null) {
tfUserName.setText("");
tfPassword.setText("");
Expand Down
Expand Up @@ -7,7 +7,7 @@
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.net.PasswordAuthentication;
import java.net.Authenticator.RequestorType;
//import java.net.Authenticator.RequestorType;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
Expand Down Expand Up @@ -104,7 +104,7 @@ public BasicAuthenticationPreferencesPanel() {
public void initFromPreferences() {
CredentialsManager cm = CredentialsManagerFactory.getCredentialManager();
try {
PasswordAuthentication pa = cm.lookup(RequestorType.SERVER);
PasswordAuthentication pa = cm.lookup(/*RequestorType.SERVER*/);
if (pa == null) {
tfOsmUserName.setText("");
tfOsmPassword.setText("");
Expand All @@ -128,7 +128,7 @@ public void saveToPreferences() {
tfOsmUserName.getText().trim(),
tfOsmPassword.getPassword()
);
cm.store(RequestorType.SERVER, pa);
cm.store(/*RequestorType.SERVER,*/ pa);
// always save the username to the preferences if it isn't already saved there
// by the credential manager
if (! (cm instanceof JosmPreferencesCredentialManager)) {
Expand Down
Expand Up @@ -13,7 +13,7 @@
import java.awt.event.ItemListener;
import java.net.PasswordAuthentication;
import java.net.ProxySelector;
import java.net.Authenticator.RequestorType;
//import java.net.Authenticator.RequestorType;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -322,7 +322,7 @@ public void initFromPreferences() {
// the credentials manager
CredentialsManager cm = CredentialsManagerFactory.getCredentialManager();
try {
PasswordAuthentication pa = cm.lookup(RequestorType.PROXY);
PasswordAuthentication pa = cm.lookup(/*RequestorType.PROXY*/);
if (pa == null) {
tfProxyHttpUser.setText("");
tfProxyHttpPassword.setText("");
Expand Down Expand Up @@ -400,7 +400,7 @@ public void saveToPreferences() {
tfProxyHttpUser.getText().trim(),
tfProxyHttpPassword.getPassword()
);
cm.store(RequestorType.PROXY, pa);
cm.store(/*RequestorType.PROXY,*/ pa);
} catch(CredentialsManagerException e) {
e.printStackTrace();
}
Expand Down
4 changes: 2 additions & 2 deletions src/org/openstreetmap/josm/io/OsmConnection.java
Expand Up @@ -4,7 +4,7 @@
import static org.openstreetmap.josm.tools.I18n.tr;

import java.net.HttpURLConnection;
import java.net.Authenticator.RequestorType;
//import java.net.Authenticator.RequestorType;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
Expand Down Expand Up @@ -80,7 +80,7 @@ protected void addBasicAuthorizationHeader(HttpURLConnection con) throws OsmTran
String token;
try {
synchronized (CredentialsManagerFactory.getCredentialManager()) {
response = CredentialsManagerFactory.getCredentialManager().getCredentials(RequestorType.SERVER, false /* don't know yet whether the credentials will succeed */);
response = CredentialsManagerFactory.getCredentialManager().getCredentials(/*RequestorType.SERVER,*/ false /* don't know yet whether the credentials will succeed */);
}
} catch (CredentialsManagerException e) {
throw new OsmTransferException(e);
Expand Down
8 changes: 4 additions & 4 deletions src/org/openstreetmap/josm/io/auth/CredentialsManager.java
Expand Up @@ -2,7 +2,7 @@
package org.openstreetmap.josm.io.auth;

import java.net.PasswordAuthentication;
import java.net.Authenticator.RequestorType;
//import java.net.Authenticator.RequestorType;

import org.openstreetmap.josm.data.oauth.OAuthToken;

Expand All @@ -27,7 +27,7 @@ public interface CredentialsManager {
* @return the credentials
* @throws CredentialsManagerException thrown if a problem occurs in a implementation of this interface
*/
public PasswordAuthentication lookup(RequestorType requestorType) throws CredentialsManagerException;
public PasswordAuthentication lookup(/*RequestorType requestorType*/) throws CredentialsManagerException;

/**
* Saves the credentials in <code>credentials</code> for the given service type.
Expand All @@ -37,7 +37,7 @@ public interface CredentialsManager {
* @param credentials the credentials
* @throws CredentialsManagerException thrown if a problem occurs in a implementation of this interface
*/
public void store(RequestorType requestorType, PasswordAuthentication credentials) throws CredentialsManagerException;
public void store(/*RequestorType requestorType,*/ PasswordAuthentication credentials) throws CredentialsManagerException;

/**
*
Expand All @@ -48,7 +48,7 @@ public interface CredentialsManager {
* @throws CredentialsManagerException thrown if a problem occurs in a implementation of this interface
*/
public CredentialsManagerResponse getCredentials(RequestorType requestorType, boolean noSuccessWithLastResponse) throws CredentialsManagerException;
public CredentialsManagerResponse getCredentials(/*RequestorType requestorType, */ boolean noSuccessWithLastResponse) throws CredentialsManagerException;

/**
* Lookup the current OAuth Access Token to access the OSM server. Replies null, if no
Expand Down
14 changes: 8 additions & 6 deletions src/org/openstreetmap/josm/io/auth/DefaultAuthenticator.java
Expand Up @@ -29,7 +29,9 @@ public static void createInstance(CredentialsManager credentialManager) {
}

private CredentialsManager credentialManager;
private final Map<RequestorType, Boolean> credentialsTried = new HashMap<RequestorType, Boolean>();
// private final Map<RequestorType, Boolean> credentialsTried = new HashMap<RequestorType, Boolean>();
private boolean credentialsTried = false;

private boolean enabled = true;

/**
Expand All @@ -49,18 +51,18 @@ private DefaultAuthenticator(CredentialsManager credentialManager) {
if (!enabled)
return null;
try {
if (getRequestorType().equals(Authenticator.RequestorType.SERVER)) {
//if (getRequestorType().equals(Authenticator.RequestorType.SERVER)) {
// if we are working with OAuth we don't prompt for a password
//
String authMethod = Main.pref.get("osm-server.auth-method", "basic");
if (authMethod.equals("oauth"))
return null;
}
boolean tried = credentialsTried.get(getRequestorType()) != null;
CredentialsManagerResponse response = credentialManager.getCredentials(getRequestorType(), tried);
//}
Boolean tried = ! credentialsTried;
CredentialsManagerResponse response = credentialManager.getCredentials(/*getRequestorType(), */tried);
if (response == null || response.isCanceled())
return null;
credentialsTried.put(getRequestorType(), true);
credentialsTried =true;
return new PasswordAuthentication(response.getUsername(), response.getPassword());
} catch(CredentialsManagerException e) {
e.printStackTrace();
Expand Down
Expand Up @@ -2,7 +2,7 @@
package org.openstreetmap.josm.io.auth;

import java.net.PasswordAuthentication;
import java.net.Authenticator.RequestorType;
//import java.net.Authenticator.RequestorType;

import org.openstreetmap.josm.Main;
import org.openstreetmap.josm.data.oauth.OAuthToken;
Expand All @@ -14,85 +14,89 @@
* the OSM API and an optional HTTP proxy in the JOSM preferences file.
*
*/
public class JosmPreferencesCredentialManager implements CredentialsManager {
public class JosmPreferencesCredentialManager implements
org.openstreetmap.josm.io.auth.CredentialsManager

{

/**
* @see CredentialsManager#lookup(RequestorType)
*/
public PasswordAuthentication lookup(RequestorType requestorType) throws CredentialsManagerException{
if (requestorType == null)
return null;
public PasswordAuthentication lookup(/*RequestorType requestorType*/) throws CredentialsManagerException{
//if (requestorType == null)
// return null;
String user;
String password;
switch(requestorType) {
case SERVER:
// 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:
/*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;
*/
//}
//return null;
}

/**
* @see CredentialsManager#store(RequestorType, PasswordAuthentication)
*/
public void store(RequestorType requestorType, PasswordAuthentication credentials) throws CredentialsManagerException {
if (requestorType == null)
return;
switch(requestorType) {
case SERVER:
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:
// 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);
public CredentialsManagerResponse getCredentials(/*RequestorType requestorType, */boolean noSuccessWithLastResponse) throws CredentialsManagerException{
// if (requestorType == null)
/// return null;
PasswordAuthentication credentials = lookup();
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;
}
// switch(requestorType) {
//case SERVER:
dialog = CredentialDialog.getOsmApiCredentialDialog(username, password);
//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(
store( new PasswordAuthentication(
response.getUsername(),
response.getPassword()
));
Expand Down

0 comments on commit 2432202

Please sign in to comment.