Add authorization support in jabber imple and tests for authorization

cusax-fix
Damian Minkov 20 years ago
parent ad7eec06d2
commit a1cc66aef6

@ -16,6 +16,7 @@
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.service.protocol.jabberconstants.*;
import net.java.sip.communicator.util.*;
import org.jivesoftware.smack.filter.*;
/**
* The Jabber implementation of a Persistent Presence Operation set. This class
@ -108,6 +109,8 @@ public class OperationSetPersistentPresenceJabberImpl
*/
private ServerStoredContactListJabberImpl ssContactList = null;
private JabberSubscriptionListener subscribtionPacketListener = null;
public OperationSetPersistentPresenceJabberImpl(
ProtocolProviderServiceJabberImpl provider)
{
@ -554,12 +557,19 @@ public void renameServerStoredContactGroup(ContactGroup group,
* @param handler an instance of an AuthorizationHandler for
* authorization requests coming from other users requesting
* permission add us to their contact list.
* @todo Implement this
* net.java.sip.communicator.service.protocol.OperationSetPresence
* method
*/
public void setAuthorizationHandler(AuthorizationHandler handler)
{
if(subscribtionPacketListener == null)
{
subscribtionPacketListener = new JabberSubscriptionListener();
PacketFilter packetFilter = new PacketTypeFilter(Presence.class);
jabberProvider.getConnection().
addPacketListener(subscribtionPacketListener, packetFilter);
}
subscribtionPacketListener.handler = handler;
}
/**
@ -1050,4 +1060,68 @@ public void presenceChanged(String XMPPAddress)
}
}
}
private class JabberSubscriptionListener
implements PacketListener
{
AuthorizationHandler handler = null;
public void processPacket(Packet packet)
{
Presence presence = (Presence)packet;
if (presence != null && presence.getType() == Presence.Type.SUBSCRIBE)
{
logger.trace(presence.getFrom() + " wants to add you to its contact list");
// buddy want to add you to its roster
String fromID = presence.getFrom();
ContactJabberImpl srcContact = ssContactList.findContactById(fromID);
if(srcContact == null)
srcContact = createVolatileContact(fromID);
AuthorizationRequest req = new AuthorizationRequest();
AuthorizationResponse response = handler.processAuthorisationRequest(req, srcContact);
if(response != null && response.getResponseCode().equals(AuthorizationResponse.ACCEPT))
{
Presence responsePacket = new Presence(Presence.Type.SUBSCRIBED);
responsePacket.setTo(fromID);
logger.info("Sending Accepted Subscription");
jabberProvider.getConnection().sendPacket(responsePacket);
}
else
{
Presence responsePacket = new Presence(Presence.Type.UNSUBSCRIBED);
responsePacket.setTo(fromID);
logger.info("Sending Rejected Subscription");
jabberProvider.getConnection().sendPacket(responsePacket);
}
}
else if (presence != null && presence.getType() == Presence.Type.UNSUBSCRIBED)
{
logger.trace(presence.getFrom() + " does not allow your subscription");
ContactJabberImpl contact =
ssContactList.findContactById(presence.getFrom());
if(contact != null)
{
AuthorizationResponse response =
new AuthorizationResponse(AuthorizationResponse.REJECT, "");
handler.processAuthorizationResponse(response, contact);
ssContactList.removeContact(contact);
}
}
else if (presence != null && presence.getType() == Presence.Type.SUBSCRIBED)
{
ContactJabberImpl contact =
ssContactList.findContactById(presence.getFrom());
AuthorizationResponse response = new AuthorizationResponse(
AuthorizationResponse.ACCEPT, "");
handler.processAuthorizationResponse(response, contact);
}
}
}
}

@ -242,6 +242,8 @@ private void connectAndLogin(SecurityAuthority authority)
logger.error("Domain not resolved " + ex1.getMessage());
}
Roster.setDefaultSubscriptionMode(Roster.SUBSCRIPTION_MANUAL);
connection = new XMPPConnection(
serverAddress,
Integer.parseInt(serverPort),
@ -255,8 +257,7 @@ private void connectAndLogin(SecurityAuthority authority)
if(connection.isAuthenticated())
{
connection.getRoster().
setSubscriptionMode(Roster.SUBSCRIPTION_ACCEPT_ALL);
setSubscriptionMode(Roster.SUBSCRIPTION_MANUAL);
fireRegistrationStateChanged(
getRegistrationState(),

@ -39,6 +39,9 @@ public class TestOperationSetPresence
private OperationSetPresence operationSetPresence2 = null;
private String statusMessageRoot = new String("Our status is now: ");
private static AuthEventCollector authEventCollector1 = new AuthEventCollector();
private static AuthEventCollector authEventCollector2 = new AuthEventCollector();
public TestOperationSetPresence(String name)
{
super(name);
@ -419,17 +422,84 @@ public void postTestSubscribe()
{
logger.debug("Testing Subscription and Subscription Event Dispatch.");
// First create a subscription and verify that it really gets created.
logger.trace("set Auth Handlers");
operationSetPresence1.setAuthorizationHandler(authEventCollector1);
operationSetPresence2.setAuthorizationHandler(authEventCollector2);
/**
* Testing Scenario
*
* user1 add user2
* - check user2 receive auth request
* - user2 deny
* - check user1 received deny
* user1 add user2
* - check user2 receive auth request
* - user2 accept
* - check user1 received accept
*/
// first we will reject
authEventCollector2.responseToRequest =
new AuthorizationResponse(AuthorizationResponse.REJECT, null);
operationSetPresence1.subscribe(fixture.userID2);
authEventCollector2.waitForAuthRequest(10000);
assertTrue("Error authorization request not received from " +
fixture.userID2,
authEventCollector2.isAuthorizationRequestReceived);
authEventCollector1.waitForAuthResponse(10000);
assertTrue("Error authorization reply not received from " +
fixture.userID1,
authEventCollector1.isAuthorizationResponseReceived);
assertEquals("Error received authorization reply not as expected",
authEventCollector2.responseToRequest.getResponseCode(),
authEventCollector1.response.getResponseCode());
pauseAfterStateChanges();
SubscriptionEventCollector subEvtCollector
= new SubscriptionEventCollector();
operationSetPresence1.addSubsciptionListener(subEvtCollector);
synchronized(subEvtCollector){
operationSetPresence1.subscribe(fixture.userID2);
subEvtCollector.waitForEvent(10000);
//don't want any more events
operationSetPresence1.removeSubscriptionListener(subEvtCollector);
}
// second we will accept
authEventCollector2.responseToRequest =
new AuthorizationResponse(AuthorizationResponse.ACCEPT, null);
authEventCollector2.isAuthorizationRequestReceived = false;
authEventCollector1.isAuthorizationResponseReceived = false;
operationSetPresence1.subscribe(fixture.userID2);
authEventCollector2.waitForAuthRequest(10000);
assertTrue("Error authorization request not received from " +
fixture.userID2,
authEventCollector2.isAuthorizationRequestReceived);
authEventCollector1.waitForAuthResponse(10000);
assertTrue("Error authorization reply not received from " +
fixture.userID1,
authEventCollector1.isAuthorizationResponseReceived);
assertEquals("Error received authorization reply not as expected",
authEventCollector2.responseToRequest.getResponseCode(),
authEventCollector1.response.getResponseCode());
// fix . from no on accept all subscription request for the two tested accounts
authEventCollector1.responseToRequest =
new AuthorizationResponse(AuthorizationResponse.ACCEPT, null);
authEventCollector2.responseToRequest =
new AuthorizationResponse(AuthorizationResponse.ACCEPT, null);
operationSetPresence1.removeSubscriptionListener(subEvtCollector);
assertEquals("Subscription event dispatching failed."
, 1, subEvtCollector.collectedEvents.size());
@ -913,11 +983,9 @@ private static class AuthEventCollector
implements AuthorizationHandler
{
boolean isAuthorizationRequestSent = false;
String authorizationRequestReason = null;
boolean isAuthorizationResponseReceived = false;
AuthorizationResponse response = null;
String authorizationResponseString = null;
// receiving auth request
AuthorizationResponse responseToRequest = null;
@ -933,7 +1001,6 @@ public AuthorizationResponse processAuthorisationRequest(
sourceContact);
isAuthorizationRequestReceived = true;
authorizationRequestReason = req.getReason();
notifyAll();
@ -958,7 +1025,6 @@ public AuthorizationRequest createAuthorizationRequest(Contact contact)
logger.trace("createAuthorizationRequest " + contact);
AuthorizationRequest authReq = new AuthorizationRequest();
authReq.setReason(authorizationRequestReason);
isAuthorizationRequestSent = true;
@ -972,10 +1038,8 @@ public void processAuthorizationResponse(AuthorizationResponse
{
isAuthorizationResponseReceived = true;
this.response = response;
authorizationResponseString = response.getReason();
logger.trace("processAuthorizationResponse '" +
authorizationResponseString + "' " +
response.getResponseCode() + " " +
sourceContact);

Loading…
Cancel
Save