Skip to content

Commit

Permalink
Clean up authentication mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkanis committed Aug 15, 2011
1 parent 63658b0 commit 0dc1164
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 45 deletions.
5 changes: 5 additions & 0 deletions src/net/skweez/sipgate/NoAuthenticationException.java
@@ -0,0 +1,5 @@
package net.skweez.sipgate;

public class NoAuthenticationException extends Exception {

}
18 changes: 12 additions & 6 deletions src/net/skweez/sipgate/PreferencesAuthenticator.java
Expand Up @@ -6,11 +6,10 @@
import android.content.SharedPreferences;

/**
* An implementation of {@link Authenticator} that gets user name and password
* from the {@link SharedPreferences}.
*
* @author mks
* @author $Author: mks $
* @version $Rev: 9 $
* @levd.rating RED Rev:
* @author Michael Kanis
*/
public class PreferencesAuthenticator extends Authenticator {

Expand All @@ -23,7 +22,14 @@ public PreferencesAuthenticator(SharedPreferences preferences) {
/** {@inheritDoc} */
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(prefs.getString("username", ""),
prefs.getString("password", "").toCharArray());

String userName = prefs.getString("username", null);
String password = prefs.getString("password", null);

if (userName == null || password == null) {
return null;
}

return new PasswordAuthentication(userName, password.toCharArray());
}
}
68 changes: 29 additions & 39 deletions src/net/skweez/sipgate/api/xmlrpc/SipgateXmlRpcImpl.java
@@ -1,17 +1,3 @@
/*-----------------------------------------------------------------------+
| sipgate Kontostand
| |
$Id: codetemplates.xml 9 2009-03-06 10:09:52Z mks $
| |
| Copyright (c) 2004-2011 Technische Universitaet Muenchen |
| |
| Technische Universitaet Muenchen ######### ########## |
| Institut fuer Informatik - Lehrstuhl IV ## ## ## ## ## |
| Prof. Dr. Manfred Broy ## ## ## ## ## |
| Boltzmannstr. 3 ## ## ## ## ## |
| 85748 Garching bei Muenchen ## ## ## ## ## |
| Germany ## ###### ## ## |
+-----------------------------------------------------------------------*/
package net.skweez.sipgate.api.xmlrpc;

import java.net.Authenticator;
Expand All @@ -23,6 +9,7 @@
import java.util.List;
import java.util.Map;

import net.skweez.sipgate.NoAuthenticationException;
import net.skweez.sipgate.api.Call;
import net.skweez.sipgate.api.ECallStatus;
import net.skweez.sipgate.api.Gender;
Expand All @@ -33,14 +20,9 @@
import net.skweez.sipgate.api.UserUri;

import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCException;

/**
*
* @author mks
* @author $Author: mks $
* @version $Rev: 9 $
* @levd.rating RED Rev:
* @author Michael Kanis
*/
public class SipgateXmlRpcImpl implements ISipgateAPI {

Expand All @@ -50,20 +32,7 @@ public class SipgateXmlRpcImpl implements ISipgateAPI {
API_URI = URI.create("https://samurai.sipgate.net/RPC2");
}

private final XMLRPCClient client;

public SipgateXmlRpcImpl() {
PasswordAuthentication authentication = Authenticator
.requestPasswordAuthentication(null, 80, "http", null, null);

String username = authentication.getUserName();
String password = String.valueOf(authentication.getPassword());

client = new XMLRPCClient(API_URI, username, password);
}

/** {@inheritDoc} */
@SuppressWarnings({ "rawtypes", "unchecked" })
public Price getBalance() {
Map<String, Map> result = (Map<String, Map>) executeMethod("samurai.BalanceGet");
Map currentBalance = result.get("CurrentBalance");
Expand Down Expand Up @@ -97,8 +66,10 @@ public int compare(Call call1, Call call2) {
private Call createCallFromMap(Map map) {
Call call = new Call();

call.setLocalURI(SipgateUriHelper.createUriFromString((String) map.get("LocalUri")));
call.setRemoteURI(SipgateUriHelper.createUriFromString((String) map.get("RemoteUri")));
call.setLocalURI(SipgateUriHelper.createUriFromString((String) map
.get("LocalUri")));
call.setRemoteURI(SipgateUriHelper.createUriFromString((String) map
.get("RemoteUri")));
call.setStatus(ECallStatus.fromString((String) map.get("Status")));
call.setTimestamp((String) map.get("Timestamp"));

Expand All @@ -115,8 +86,9 @@ public UserUri[] getUserUriList() {
Map entry = (Map) userUriMap[i];

userUriList[i] = new UserUri(entry.get("E164Out").toString(),
SipgateUriHelper.createUriFromString(entry.get("SipUri").toString()),
new Boolean(entry.get("DefaultUri").toString()));
SipgateUriHelper.createUriFromString(entry.get("SipUri")
.toString()), new Boolean(entry.get("DefaultUri")
.toString()));
}
return userUriList;
}
Expand All @@ -130,10 +102,28 @@ public UserName getUserName() {

private Map<String, ? extends Object> executeMethod(String method,
String... params) {

try {
return (Map<String, Object>) client.callEx(method, params);
} catch (final XMLRPCException exception) {
return (Map<String, Object>) getAuthenticatedClient().callEx(
method, params);
} catch (final Exception exception) {
throw new SipgateException(exception);
}
}

private XMLRPCClient getAuthenticatedClient()
throws NoAuthenticationException {

PasswordAuthentication authentication = Authenticator
.requestPasswordAuthentication(null, 80, "http", null, null);

if (authentication != null) {
String username = authentication.getUserName();
String password = String.valueOf(authentication.getPassword());

return new XMLRPCClient(API_URI, username, password);
} else {
throw new NoAuthenticationException();
}
}
}

0 comments on commit 0dc1164

Please sign in to comment.