authentication: pass whitelist and credential as arguments to the server.
authorLuc Yriarte <luc.yriarte@linux.intel.com>
Tue, 11 Sep 2012 17:36:52 +0000 (19:36 +0200)
committerLuc Yriarte <luc.yriarte@linux.intel.com>
Tue, 11 Sep 2012 17:36:52 +0000 (19:36 +0200)
Clients can connect without manifest if server has opendoor option.

cloudeebus/cloudeebus.js
cloudeebus/cloudeebus.py
doc/dbus-tools/dbus-register.html
doc/dbus-tools/dbus-send.html
doc/sample/CREDENTIALS [new file with mode: 0644]
doc/sample/WHITELIST [new file with mode: 0644]

index cf8f8b0..ff24013 100644 (file)
@@ -37,7 +37,7 @@ cloudeebus.connect = function(uri, manifest, successCB, errorCB) {
        cloudeebus.uri = uri;
        
        function onWAMPSessionAuthenticatedCB(permissions) {
-               cloudeebus.log("Connected as " + manifest.name + " to " + cloudeebus.uri);
+               cloudeebus.log("Connected to " + cloudeebus.uri);
                cloudeebus.sessionBus = new cloudeebus.BusConnection("session", cloudeebus.wampSession);
                cloudeebus.systemBus = new cloudeebus.BusConnection("system", cloudeebus.wampSession);
                if (successCB)
@@ -51,10 +51,15 @@ cloudeebus.connect = function(uri, manifest, successCB, errorCB) {
        
        function onWAMPSessionConnectedCB(session) {
                cloudeebus.wampSession = session;
-               cloudeebus.wampSession.authreq(
-                               manifest.name, 
-                               {permissions: JSON.stringify(manifest.permissions)}
-                       ).then(onWAMPSessionChallengedCB, errorCB);
+               if (manifest)
+                       cloudeebus.wampSession.authreq(
+                                       manifest.name, 
+                                       {permissions: JSON.stringify(manifest.permissions)}
+                               ).then(onWAMPSessionChallengedCB, errorCB);
+               else
+                       cloudeebus.wampSession.authreq().then(function() {
+                               cloudeebus.wampSession.auth().then(onWAMPSessionAuthenticatedCB, errorCB);
+                               }, errorCB);
        }
 
        function onWAMPSessionErrorCB(code, reason) {
index bef9716..6b613c7 100755 (executable)
@@ -21,7 +21,7 @@
 #
 
 
-import sys, dbus, json
+import argparse, dbus, io, json, sys
 
 from twisted.internet import glib2reactor
 # Configure the twisted mainloop to be run inside the glib mainloop.
@@ -42,18 +42,23 @@ glib.init_threads()
 
 # enable debug log
 from twisted.python import log
-log.startLogging(sys.stdout)
 
 
 
 ###############################################################################
+
+OPENDOOR = False
+CREDENTIALS = {}
+WHITELIST = []
+
+###############################################################################
+
 def hashId(list):
        str = list[0]
        for item in list[1:len(list)]:
                str += "#" + item
        return str
 
-
 ###############################################################################
 class DbusCache:
        def __init__(self):
@@ -140,8 +145,9 @@ class CloudeebusService:
        def proxyObject(self, busName, serviceName, objectName):
                id = hashId([serviceName, objectName])
                if not self.proxyObjects.has_key(id):
-                       # check permissions, array.index throws exception
-                       self.permissions.index(serviceName)
+                       if not OPENDOOR:
+                               # check permissions, array.index throws exception
+                               self.permissions.index(serviceName)
                        bus = cache.dbusConnexion(busName)
                        self.proxyObjects[id] = bus.get_object(serviceName, objectName)
                return self.proxyObjects[id]
@@ -205,32 +211,10 @@ class CloudeebusService:
 ###############################################################################
 class CloudeebusServerProtocol(WampCraServerProtocol):
        
-       PASSWD = {
-               "cloudeebus": "secret"
-               }
-       
-       WHITELIST = [
-               "com.intel.media-service-upnp",
-               "com.intel.renderer-service-upnp",
-               "org.freedesktop.DBus",
-               "org.freedesktop.DisplayManager",
-               "org.freedesktop.FileManager1",
-               "org.freedesktop.ModemManager",
-               "org.freedesktop.NetworkManager",
-               "org.freedesktop.Notifications",
-               "org.freedesktop.Tracker1",
-               "org.gnome.Nautilus",
-               "org.gnome.Rygel1",
-               "org.gnome.ScreenSaver",
-               "org.neard",
-               "org.ofono"
-               ]
-       
-
        def onSessionOpen(self):
                # CRA authentication options
                self.clientAuthTimeout = 0
-               self.clientAuthAllowAnonymous = True
+               self.clientAuthAllowAnonymous = OPENDOOR
                # CRA authentication init
                WampCraServerProtocol.onSessionOpen(self)
        
@@ -240,16 +224,21 @@ class CloudeebusServerProtocol(WampCraServerProtocol):
        
        
        def getAuthSecret(self, key):
-               return self.PASSWD.get(key, None)
+               secret = CREDENTIALS.get(key, None)
+               if secret is None:
+                       return None
+               # secret must be of str type to be hashed
+               return secret.encode('utf-8')
        
 
        def onAuthenticated(self, key, permissions):
-               # check authentication key
-               if key is None:
-                       raise Exception("Authentication failed")
-               # check permissions, array.index throws exception
-               for req in permissions:
-                       self.WHITELIST.index(req)
+               if not OPENDOOR:
+                       # check authentication key
+                       if key is None:
+                               raise Exception("Authentication failed")
+                       # check permissions, array.index throws exception
+                       for req in permissions:
+                               WHITELIST.index(req)
                # create cloudeebus service instance
                self.cloudeebusService = CloudeebusService(permissions)
                # register it for RPC
@@ -266,16 +255,38 @@ class CloudeebusServerProtocol(WampCraServerProtocol):
 
 
 ###############################################################################
+
 if __name__ == '__main__':
+       
        cache = DbusCache()
+
+       parser = argparse.ArgumentParser(description='Javascript DBus bridge.')
+       parser.add_argument('-d', '--debug', action='store_true')
+       parser.add_argument('-o', '--opendoor', action='store_true')
+       parser.add_argument('-p', '--port', default='9000')
+       parser.add_argument('-c', '--credentials')
+       parser.add_argument('-w', '--whitelist')
+       
+       args = parser.parse_args(sys.argv[1:])
+
+       if args.debug:
+               log.startLogging(sys.stdout)
+       
+       OPENDOOR = args.opendoor
+       
+       if args.credentials:
+               jfile = open(args.credentials)
+               CREDENTIALS = json.load(jfile)
+               jfile.close()
        
-       port = "9000"
-       if len(sys.argv) == 2:
-               port = sys.argv[1]
+       if args.whitelist:
+               jfile = open(args.whitelist)
+               WHITELIST = json.load(jfile)
+               jfile.close()
        
-       uri = "ws://localhost:" + port
+       uri = "ws://localhost:" + args.port
        
-       factory = WampServerFactory(uri, debugWamp = True)
+       factory = WampServerFactory(uri, debugWamp = args.debug)
        factory.protocol = CloudeebusServerProtocol
        factory.setProtocolOptions(allowHixie76 = True)
        
index 7963a49..3c29817 100644 (file)
@@ -4,29 +4,6 @@
                <!-- include AutobahnJS .. that's all you need -->
                <script src="../../lib/autobahn.min.js"></script>
                <script language="javascript" type="text/javascript">
-               
-               var manifest = {
-                       name: "cloudeebus",
-                       version: "v0.0 / development",
-                       key: "secret",
-                       permissions: [
-                               "com.intel.media-service-upnp",
-                               "com.intel.renderer-service-upnp",
-                               "org.freedesktop.DBus",
-                               "org.freedesktop.DisplayManager",
-                               "org.freedesktop.FileManager1",
-                               "org.freedesktop.ModemManager",
-                               "org.freedesktop.NetworkManager",
-                               "org.freedesktop.Notifications",
-                               "org.freedesktop.Tracker1",
-                               "org.gnome.Nautilus",
-                               "org.gnome.Rygel1",
-                               "org.gnome.ScreenSaver",
-                               "org.neard",
-                               "org.ofono"
-                       ]
-               };
-               
                // WAMP session object
                var mSession = null;
 
 
                window.onload = function() {
 
-                       function onSessionAuthenticatedCB(permissions) {
-                               log_append("Session successfully authenticated.");
+                       function onSessionAuthenticatedCB() { // WAMP session was authenticated
+                               log_append("Session successfully connected.");
                        }
 
-                       function onSessionChallengedCB(challenge) {
-                               var signature = mSession.authsign(challenge, manifest.key);
-                               mSession.auth(signature).then(onSessionAuthenticatedCB, log_append);
-                       }
-       
                        function onSessionConnectedCB(session) { // WAMP session was established
                                mSession = session;
-                               log_append("Session connected, authenticating.");
-                               mSession.authreq(
-                                               manifest.name,
-                                               {permissions: JSON.stringify(manifest.permissions)}
-                                       ).then(onSessionChallengedCB, log_append);
+                               mSession.authreq().then(function() {
+                                       mSession.auth().then(onSessionAuthenticatedCB, log_append);
+                               }, log_append);
                        }
 
                        function onSessionErrorCB(code, reason) { // WAMP session is gone
index de1007d..a442cdd 100644 (file)
@@ -4,29 +4,6 @@
                <!-- include AutobahnJS .. that's all you need -->
                <script src="../../lib/autobahn.min.js"></script>
                <script language="javascript" type="text/javascript">
-               
-               var manifest = {
-                       name: "cloudeebus",
-                       version: "v0.0 / development",
-                       key: "secret",
-                       permissions: [
-                               "com.intel.media-service-upnp",
-                               "com.intel.renderer-service-upnp",
-                               "org.freedesktop.DBus",
-                               "org.freedesktop.DisplayManager",
-                               "org.freedesktop.FileManager1",
-                               "org.freedesktop.ModemManager",
-                               "org.freedesktop.NetworkManager",
-                               "org.freedesktop.Notifications",
-                               "org.freedesktop.Tracker1",
-                               "org.gnome.Nautilus",
-                               "org.gnome.Rygel1",
-                               "org.gnome.ScreenSaver",
-                               "org.neard",
-                               "org.ofono"
-                       ]
-               };
-               
                // WAMP session object
                var mSession = null;
 
 
                window.onload = function() {
 
-                       function onSessionAuthenticatedCB(permissions) {
-                               log_append("Session successfully authenticated.");
+                       function onSessionAuthenticatedCB() { // WAMP session was authenticated
+                               log_append("Session successfully connected.");
                        }
 
-                       function onSessionChallengedCB(challenge) {
-                               var signature = mSession.authsign(challenge, manifest.key);
-                               mSession.auth(signature).then(onSessionAuthenticatedCB, log_append);
-                       }
-       
                        function onSessionConnectedCB(session) { // WAMP session was established
                                mSession = session;
-                               log_append("Session connected, authenticating.");
-                               mSession.authreq(
-                                               manifest.name,
-                                               {permissions: JSON.stringify(manifest.permissions)}
-                                       ).then(onSessionChallengedCB, log_append);
+                               mSession.authreq().then(function() {
+                                       mSession.auth().then(onSessionAuthenticatedCB, log_append);
+                               }, log_append);
                        }
 
                        function onSessionErrorCB(code, reason) { // WAMP session is gone
diff --git a/doc/sample/CREDENTIALS b/doc/sample/CREDENTIALS
new file mode 100644 (file)
index 0000000..61e3cfa
--- /dev/null
@@ -0,0 +1,3 @@
+{
+       "cloudeebus": "secret"
+}
diff --git a/doc/sample/WHITELIST b/doc/sample/WHITELIST
new file mode 100644 (file)
index 0000000..ecf7290
--- /dev/null
@@ -0,0 +1,16 @@
+[
+       "com.intel.media-service-upnp",
+       "com.intel.renderer-service-upnp",
+       "org.freedesktop.DBus",
+       "org.freedesktop.DisplayManager",
+       "org.freedesktop.FileManager1",
+       "org.freedesktop.ModemManager",
+       "org.freedesktop.NetworkManager",
+       "org.freedesktop.Notifications",
+       "org.freedesktop.Tracker1",
+       "org.gnome.Nautilus",
+       "org.gnome.Rygel1",
+       "org.gnome.ScreenSaver",
+       "org.neard",
+       "org.ofono"
+]