def __init__(self, model=None, modelserver=None, user=None, password=None, app=None, description="", debug=0, ca_certs_file="ca_certs/ca.pem"): if modelserver == None: raise ValueError, "modelserver is None" if ':' in modelserver: self.modelserver, self.modelport = modelserver.split(":") else: self.modelserver = modelserver self.modelport = 2005 # default port for personis server self.modelname = model self.user = user self.password = password self.app = app self.description = description self.debug = debug self.key = None self.ca_certs_file = ca_certs_file if self.app == None: self.auth = user + ":" + password self.authType = "user" else: self.authType = "app" try: self.key = Personis_base.import_app_key(app) except Personis_exceptions.KeyFileNotFoundError: self.key = Personis_base.generate_app_key(self.app) fingerprint = Personis_base.generate_app_fingerprint(self.key) AppRequestAuth(model=self.modelname, modelserver=self.modelserver, app=self.app, key=self.key.publickey().exportKey(), description=self.description) message = "Authorisation has been requested for app " + self.app + " to access model " + self.model + " on server " + self.modelserver + ".\n" message += "Key fingerprint: %s\n" % (fingerprint) raise Personis_exceptions.AuthRequestedError(message) ok = False try: command = "access" args = {'modelname': self.modelname} if self.app != None: self.auth = self.app + ":" + Personis_base.generate_app_signature( self.app, self.key) args['authType'] = 'app' else: args['authType'] = 'user' args['auth'] = self.auth if self.debug != 0: print "jsondocall:", self.modelserver, self.modelport, self.ca_certs_file, self.modelname, self.authType, self.auth ok = jsoncall.do_call(self.modelserver, self.modelport, self.ca_certs_file, command, args) if self.debug != 0: print "---------------------- result returned", ok except: if debug > 0: traceback.print_exc() raise raise ValueError, "cannot access model '%s', server '%s'" % ( self.modelname, self.modelserver) if not ok: raise ValueError, "server '%s' cannot access model '%s'" % ( self.modelserver, self.modelname)
def printAskContext( info ): (cobjlist, contexts, theviews, thesubs) = info printcomplist(cobjlist, printev = "yes") print "Contexts: %s" % str(contexts) print "Views: %s" % str(theviews) print "Subscriptions: %s" % str(thesubs) print "===================================================================" print "Simple example showing how permissions work" print "===================================================================" um = Personis_base.Access(model="Alice", modeldir='Tests/Models', authType='user', auth='alice:secret') print "Register an app" try: key = Personis_base.generate_app_key("MyHealth") fingerprint = Personis_base.generate_app_fingerprint(key) Personis_base.AppRequestAuth(model='Alice', modeldir='Tests/Models', app='MyHealth', key=key.publickey().exportKey(), description="My Health Manager") except Exception as e: print "App auth request failed with exception : %s\n" % (e) requests = um.listrequests() print requests fingerprint2 = requests['MyHealth']['fingerprint'] if fingerprint2 != fingerprint: print "Fingerprints don't match!" else: print "Fingerprints match" appdetails = um.registerapp(app="MyHealth", desc="My Health Manager", fingerprint=fingerprint) print "Registered ok: ", appdetails
um = Personis_base.Access(model="Alice", modeldir='Tests/Models', authType='user', auth='alice:secret') print "List the registered apps (should be none):" apps = um.listapps() print apps print "Try and set permissions on a context for an unregistered app:" try: um.setpermission(context=["Personal"], app="MyHealth", permissions={'ask':True, 'tell':False}) except Exception as e: print "setpermission failed with exception : %s\n" % (e) print "Register an app" try: key = Personis_base.generate_app_key("MyHealth") fingerprint = Personis_base.generate_app_fingerprint(key) Personis_base.AppRequestAuth(model='Alice', modeldir='Tests/Models', app='MyHealth', key=key.publickey().exportKey(), description="My Health Manager") except Exception as e: print "App auth request failed with exception : %s\n" % (e) requests = um.listrequests() print requests fingerprint2 = requests['MyHealth']['fingerprint'] if fingerprint2 != fingerprint: print "Fingerprints don't match!" else: print "Fingerprints match" appdetails = um.registerapp(app="MyHealth", desc="My Health Manager", fingerprint=fingerprint) print "Registered ok: ", appdetails print "List the registered apps (should be one):"
print "Views: %s" % str(theviews) print "Subscriptions: %s" % str(thesubs) print "===================================================================" print "Simple example showing how permissions work" print "===================================================================" um = Personis_base.Access(model="Alice", modeldir='Tests/Models', authType='user', auth='alice:secret') print "Register an app" try: key = Personis_base.generate_app_key("MyHealth") fingerprint = Personis_base.generate_app_fingerprint(key) Personis_base.AppRequestAuth(model='Alice', modeldir='Tests/Models', app='MyHealth', key=key.publickey().exportKey(), description="My Health Manager") except Exception as e: print "App auth request failed with exception : %s\n" % (e) requests = um.listrequests() print requests fingerprint2 = requests['MyHealth']['fingerprint'] if fingerprint2 != fingerprint: print "Fingerprints don't match!" else:
print apps print "Try and set permissions on a context for an unregistered app:" try: um.setpermission(context=["Personal"], app="MyHealth", permissions={ 'ask': True, 'tell': False }) except Exception as e: print "setpermission failed with exception : %s\n" % (e) print "Register an app" try: key = Personis_base.generate_app_key("MyHealth") fingerprint = Personis_base.generate_app_fingerprint(key) Personis_base.AppRequestAuth(model='Alice', modeldir='Tests/Models', app='MyHealth', key=key.publickey().exportKey(), description="My Health Manager") except Exception as e: print "App auth request failed with exception : %s\n" % (e) requests = um.listrequests() print requests fingerprint2 = requests['MyHealth']['fingerprint'] if fingerprint2 != fingerprint: print "Fingerprints don't match!" else: