def profilerequest(self, event): """Handles client profile actions :param event: """ hfoslog("[AUTH] Profile update %s" % event) if event.action != "update": hfoslog("[AUTH] Unsupported profile action: ", event, lvl=warn) return try: newprofile = event.data hfoslog("[AUTH] Updating with %s " % newprofile, lvl=debug) userprofile = profileobject.find_one({'uuid': event.user.uuid}) hfoslog("[AUTH] Updating %s" % userprofile, lvl=debug) userprofile.update(newprofile) userprofile.save() hfoslog("[AUTH] Profile stored.") # TODO: Give client feedback except Exception as e: hfoslog("[AUTH] General profile request error %s %s" % (type(e), e), lvl=error)
def authenticationrequest(self, event): """Handles authentication requests from clients :param event: AuthenticationRequest with user's credentials """ hfoslog("[AUTH] Auth request for ", event.username, event.clientuuid) if (len(event.username) < 3) or (len(event.passhash) < 3): hfoslog("[AUTH] Illegal username or password received, login cancelled", lvl=warn) return useraccount = None clientconfig = None userprofile = None try: useraccount = userobject.find_one({'username': event.username}) hfoslog("[AUTH] Account: %s" % useraccount._fields, lvl=debug) except Exception as e: hfoslog("[AUTH] No userobject due to error: ", e, type(e), lvl=error) if useraccount: hfoslog("[AUTH] User found.") if useraccount.passhash == event.passhash: hfoslog("[AUTH] Passhash matches, checking client and profile.", lvl=debug) requestedclientuuid = event.requestedclientuuid # Client requests to get an existing client configuration or has none clientconfig = clientconfigobject.find_one({'uuid': requestedclientuuid}) if clientconfig: hfoslog("[AUTH] Checking client configuration permissions", lvl=debug) if clientconfig.useruuid != useraccount.uuid: clientconfig = None hfoslog("[AUTH] Unauthorized client configuration requested", lvl=warn) else: hfoslog("[AUTH] Unknown client configuration requested: ", requestedclientuuid, event.__dict__, lvl=warn) if not clientconfig: hfoslog("[AUTH] Creating new default client configuration") # Either no configuration was found or requested # -> Create a new client configuration clientconfig = clientconfigobject() clientconfig.uuid = event.clientuuid clientconfig.name = "New client" clientconfig.description = "New client configuration from " + useraccount.username clientconfig.useruuid = useraccount.uuid # TODO: Make sure the profile is only saved if the client could store it, too clientconfig.save() try: userprofile = profileobject.find_one({'uuid': str(useraccount.uuid)}) hfoslog("[AUTH] Profile: ", userprofile, useraccount.uuid, lvl=debug) useraccount.passhash = "" self.fireEvent( authentication(useraccount.username, (useraccount, userprofile, clientconfig), event.clientuuid, useraccount.uuid, event.sock), "auth") except Exception as e: hfoslog("[AUTH] No profile due to error: ", e, type(e), lvl=error) else: hfoslog("[AUTH] Password was wrong!", lvl=warn) hfoslog("[AUTH] Done with Login request", lvl=debug) else: hfoslog("[AUTH] Creating user") try: newuser = userobject({'username': event.username, 'passhash': event.passhash, 'uuid': str(uuid4())}) newuser.save() except Exception as e: hfoslog("[AUTH] Problem creating new user: "******"[AUTH] New profile uuid: ", newprofile.uuid, lvl=verbose) newprofile.components = {'enabled': ["dashboard", "map", "weather", "settings"]} newprofile.save() except Exception as e: hfoslog("[AUTH] Problem creating new profile: ", type(e), e, lvl=error) return try: # TODO: Clone or reference systemwide default configuration newclientconfig = clientconfigobject() newclientconfig.uuid = event.clientuuid newclientconfig.name = "New client" newclientconfig.description = "New client configuration from " + newuser.username newclientconfig.useruuid = newuser.uuid newclientconfig.save() except Exception as e: hfoslog("[AUTH] Problem creating new clientconfig: ", type(e), e, lvl=error) return try: self.fireEvent( authentication(newuser.username, (newuser, newprofile, newclientconfig), event.clientuuid, newuser.uuid, event.sock), "auth") self.fireEvent(send(event.clientuuid, {'component': 'auth', 'action': 'new', 'data': 'registration successful' }, sendtype="client"), "hfosweb") except Exception as e: hfoslog("[AUTH] Error during new account confirmation transmission", e, lvl=error)