My favorites | Sign in
Project Logo
                
Search
for
Updated May 19, 2009 by ptony82
WriteSocialVPNAdapter  
Explains how to implement a SocialVPN adapter to connect to backend.

Introduction

SocialVPN is designed to connect to multiple social networking backends. The goal of this page is to show how easy it is to implement a basic SocialVPN adapter and test with the SocialVPN system.

The Main Idea

SocialVPN create social networking connections through X.509 certificate exchanges between social peers. The social networking backends facilitate this exchange by serving as a trusted source where information about peers can be stored and retreived. Therefore, writing a SocialVPN adapter consists of mainly implementing five functions described in the SocialInterface.cs file in the src directory.

Login

The login method allows users to login through the web interface, implement this function to authenticate users again your social networking backend.

Expample from TestNetwork.cs shown below:

public bool Login(string id, string username, string password) {
      return true;
    }

GetFriends

The GetFriends method should only return a list of unique identifiers representing friends (preferrably email addresses). In the example below, we connect to a REST server and retreive a list of friends for a particular user.

    public List<string> GetFriends() {
      List<string> new_friends = new List<string>();
      Dictionary<string, string> parameters = 
        new Dictionary<string, string>();

      parameters["m"] = "getfriends";
      parameters["uid"] = _local_user.Uid;
      string response = SocialUtils.Request(_url, parameters);

      string[] friends = response.Split('\n');
      foreach(string friend in friends) {
        new_friends.Add(friend);
      }
      return new_friends;
    }

GetFingerprints

The GetFingerprints method returns a list of fingerprints or the DhtKey attribute of the SocialUser object (SocialUser.cs) which is later used to retreive the key from the DHT. In the example below, the list of users is given to the REST server which in turn returns a list of fingerprints for the list of users.

    public List<string> GetFingerprints(string[] uids) {
      string dl = ",";
      StringBuilder friendlist = new StringBuilder();
      if(uids != null) {
        foreach(string uid in uids) {
          friendlist.Append(uid + dl);
        }
      }

      List<string> fingerprints = new List<string>();
      Dictionary<string, string> parameters = 
        new Dictionary<string, string>();

      parameters["m"] = "getfprs";
      parameters["uids"] = friendlist.ToString();
      string response = SocialUtils.Request(_url, parameters);

      string[] fprs = response.Split('\n');
      foreach(string fpr in fprs) {
        fingerprints.Add(fpr);
      }
      return fingerprints;
    }

GetCertificates

This method does not need to be implemented, some users may not want to use the DHT for store the certificates, they may decide to store the certificates within their own trusted datastore, hence it would be the case to implement this method. It works the same way as the GetFingerprints method but returns a list of byte arrays instead of friends. If you should only implement one of these methods, either GetFingerprints or GetCertificates.

ValidateCertificate

This method validates a particular peers certificate and verifies that it can be trusted.

The Whole Thing

Below is the complete implementation of the test network class, it is recommended to use it as a starting template to implement your SocialVPN adapters.

/*
Copyright (C) 2009 Pierre St Juste <ptony82@ufl.edu>, University of Florida

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Brunet;
using Brunet.DistributedServices;

#if SVPN_NUNIT
using NUnit.Framework;
#endif

namespace SocialVPN {

  public class TestNetwork : IProvider, ISocialNetwork {

    protected readonly string _url;

    protected readonly SocialUser _local_user;

    protected readonly List<string> _fingerprints;

    public TestNetwork(SocialUser user, byte[] certData) {
      _local_user = user;
      _fingerprints = new List<string>();
      _url = "http://socialvpntest.appspot.com/api/";
    }

    public bool Login(string id, string username, string password) {
      return true;
    }

    public bool Logout() {
      return true;
    }

    public List<string> GetFriends() {
      List<string> new_friends = new List<string>();
      Dictionary<string, string> parameters = 
        new Dictionary<string, string>();

      parameters["m"] = "getfriends";
      parameters["uid"] = _local_user.Uid;
      string response = SocialUtils.Request(_url, parameters);

      string[] friends = response.Split('\n');
      foreach(string friend in friends) {
        new_friends.Add(friend);
      }
      return new_friends;
    }

    public List<string> GetFingerprints(string[] uids) {
      string dl = ",";
      StringBuilder friendlist = new StringBuilder();
      if(uids != null) {
        foreach(string uid in uids) {
          friendlist.Append(uid + dl);
        }
      }

      List<string> fingerprints = new List<string>();
      Dictionary<string, string> parameters = 
        new Dictionary<string, string>();

      parameters["m"] = "getfprs";
      parameters["uids"] = friendlist.ToString();
      string response = SocialUtils.Request(_url, parameters);

      string[] fprs = response.Split('\n');
      foreach(string fpr in fprs) {
        fingerprints.Add(fpr);
      }
      return fingerprints;
    }

    public List<byte[]> GetCertificates(string[] uids) {
      return null;
    }

    public bool StoreFingerprint() {
      List<string> fingerprints = GetFingerprints(new string[] 
                                                  {_local_user.Uid});
      if(!fingerprints.Contains(_local_user.DhtKey)) {
        Dictionary<string, string> parameters = 
          new Dictionary<string, string>();

        parameters["m"] = "store";
        parameters["uid"] = _local_user.Uid;
        parameters["fpr"] = _local_user.DhtKey;
        SocialUtils.Request(_url, parameters);
      }
      return true;
    }

    public bool ValidateCertificate(byte[] certData) {
      return true;
    }
  }

#if SVPN_NUNIT
  [TestFixture]
  public class TestNetworkTester {
    [Test]
    public void TestNetworkTest() {
      /*
      string uid = "ptony82@ufl.edu";
      string name = "Pierre St Juste";
      string pcid = "pdesktop";
      string version = "SVPN_0.3.0";
      string country = "US";
      string address = 
        Brunet.Applications.Utils.GenerateAHAddress().ToString();
      SocialUtils.CreateCertificate(uid, name, pcid, version, country,
                                    address, "certificates", "private_key");
      */
      string cert_path = System.IO.Path.Combine("certificates", "lc.cert");
      byte[] cert_data = SocialUtils.ReadFileBytes(cert_path);
      SocialUser user = new SocialUser(cert_data);

      Console.WriteLine(user);
      ///*
      TestNetwork backend = new TestNetwork(user, cert_data);
      backend.StoreFingerprint();
      backend.GetFriends();
      backend.GetFingerprints(null);
      //*/
    }
  } 
#endif

}

Sign in to add a comment
Hosted by Google Code