def update_findlock(device_uuid, update_type, update_data) -> m_LogicReturn: if v.value_empty(device_uuid) \ or v.value_empty(update_data) \ or v.value_empty(update_type): return m_LogicReturn.f_error_msg("post data is missing!") db_resp = db_Findlock().get_findlock(m_FindlockEntries.device_uuid, device_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) Findlock = db_resp.addon_data Findlock: m_Findlock valid_update_types = t.get_types(m_FindlockUpdateTypes()) if update_type not in valid_update_types: return m_LogicReturn.f_error_msg("Unsupported update type!") if update_type == m_FindlockUpdateTypes.gps_location: try: update_data = json.loads(update_data) update_data = m_GpsLocation.fromDict(update_data) except: return m_LogicReturn.f_error_msg("Update data is malformed!") if update_type == m_FindlockUpdateTypes.gps_location: Findlock.GpsLocationHistory.add_location(update_data) else: return m_LogicReturn.f_error_msg( "this update type is not implemented...") db_resp = db_Findlock().update_findlock(Findlock) return m_LogicReturn.f_success_msg("Findlock updated!")
def pair_with_findlock(_device_uuid, _device_pincode, _user_uuid) -> m_LogicReturn: #using if not v.verify_uuid(_device_uuid).success: return m_LogicReturn.f_error_msg(f"{_device_uuid} is not a valid uuid!") db_resp = DbFindlock().get_findlock( m_FindlockEntries.device_uuid, _device_uuid ) if not db_resp.success: return m_LogicReturn.f_error_msg( "This uuid does not exist!") Findlock = db_resp.addon_data Findlock: m_Findlock if Findlock.FindlockPermission.master_uuid is not None: return m_LogicReturn.f_error_msg( "This findlock is already paired!") if Findlock.master_pincode != _device_pincode: return m_LogicReturn.f_error_msg( "Pincode mismatch!") Findlock.FindlockPermission.master_uuid = _user_uuid db_resp = DbFindlock().update_findlock(Findlock) if not db_resp.success: return m_LogicReturn.f_error_msg( db_resp.content) return m_LogicReturn.f_success_msg( "Paired!")
def facebook_login(access_token, name, userid) -> m_LogicReturn: if v.value_empty(access_token) \ or v.value_empty(name) \ or v.value_empty(userid): return m_LogicReturn.f_error_msg("missing data!") logic_resp = SocialsLogic.fb_verify_access_token(access_token) if not logic_resp: return m_LogicReturn.f_error_msg("invalid access token") db_resp = db_User().get_user( m_UserEntries.facebook_user_id, userid ) if not db_resp.success: logic_resp = AuthLogic.facebook_register(name, userid) if not logic_resp.success: return m_LogicReturn.f_error_msg(logic_resp.content) db_resp = db_User().get_user( m_UserEntries.facebook_user_id, userid ) _User = db_resp.addon_data _User: m_User logic_resp = SessionLogic.login(_User) if not logic_resp.success: return m_LogicReturn.f_error_msg(logic_resp.content) return m_LogicReturn.f_success_msg("Logged in!")
def get_allowed_users_for_findlock(_findlock_uuid) -> m_LogicReturn: if not v.verify_uuid(_findlock_uuid).success: return m_LogicReturn.f_error_msg(f"{_findlock_uuid} is not a valid uuid!") db_resp = DbFindlock().get_findlock( m_FindlockEntries.device_uuid, _findlock_uuid ) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) findlock = db_resp.addon_data findlock: m_Findlock allowed_names = list() for u in findlock.FindlockPermission.allowed_uuids: db_resp = DbUser().get_user( m_UserEntries.user_uuid, u ) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) tmp_user = db_resp.addon_data tmp_user: m_User allowed_names.append(f"{tmp_user.first_name} {tmp_user.surname}") return m_LogicReturn.f_success("Ok!", allowed_names)
def google_register(google_user_id, user_email, avatar_link) -> m_LogicReturn: if v.value_empty(google_user_id) \ or v.value_empty(user_email): return m_LogicReturn.f_error_msg("post data is missing!") if not v.verify_email(user_email): return m_LogicReturn.f_error_msg("E-mail is not valid!") _User = m_User( first_name=None, surname=None, email=user_email, password_hash=None, phone_number=None, nfc_id=None, finger_id=None, friend_code=Tools.generate_friend_code(), google_user_id=google_user_id, avatar_link=avatar_link, verified=True ) db_resp = db_User().create_user(_User) if db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) return m_LogicReturn.f_success_msg(db_resp.content)
def verify_friendcode(friend_code_input) -> m_LogicReturn: allowed_code_chars = "0123456789" allowed_splitter_char = "#" allowed_code_len = 4 friend_code_input = str(friend_code_input) if not allowed_splitter_char in friend_code_input: return m_LogicReturn.f_error_msg( f"Invalid friendcode (missing '{allowed_splitter_char}')") friend_code_splitted = friend_code_input.split(allowed_splitter_char) if len(friend_code_splitted) != 2: return m_LogicReturn.f_error_msg( "Invalid friendcode (splitted != 2)") code = friend_code_splitted[1] if len(code) != allowed_code_len: return m_LogicReturn.f_error_msg( f"Invalid friendcode (code len != {allowed_code_len})") for c in code: if c not in allowed_code_chars: return m_LogicReturn.f_error_msg( f"Invalid friendcode (code contains illegal char: '{c}')") return m_LogicReturn.f_success_msg("Valid friendcode!")
def user_data_is_missing(_session: m_Session) -> m_LogicReturn: if v.value_empty(_session): return m_LogicReturn.f_error_msg("Data is missing!") if type(_session) != m_Session: return m_LogicReturn.f_error_msg("Session is not a valid type!") _user_dict = _session.User.toDict() return UserLogic.user_data_missing(_user_dict)
def get_user_findlocks(_session: m_Session) -> m_LogicReturn: if v.value_empty(_session): return m_LogicReturn.f_error_msg("Data is missing!") if type(_session) != m_Session: return m_LogicReturn.f_error_msg("Session is not a valid type!") _user_uuid = _session.User.user_uuid return UserLogic.get_user_findlocks(_user_uuid, master_only=False)
def verify_password(password_input) -> m_LogicReturn: password_input = str(password_input) if len(password_input) < 8 or len(password_input) > 16: return m_LogicReturn.f_error_msg( "Password is too long or too short (min: 8, max: 16)") if not any(char.isdigit() for char in password_input): return m_LogicReturn.f_error_msg( "Password should contain atleast one digit") return m_LogicReturn.f_success_msg("Password is valid!")
def update_user_data(_session: m_Session,form_data) -> m_LogicReturn: if v.value_empty(_session): return m_LogicReturn.f_error_msg("Data is missing") if type(_session) != m_Session: return m_LogicReturn.f_error_msg("Session is not a valid type!") _user_dict = _session.User.toDict() return UserLogic.update_user_data( _user_dict, form_data )
def fb_verify_access_token(access_token) -> m_LogicReturn: resp = py_requests.get( f"https://graph.facebook.com/app/?access_token={access_token}") j_resp = resp.json() if "error" in j_resp: return m_LogicReturn.f_error_msg(j_resp['error']) if j_resp['name'] != "Findlock": return m_LogicReturn.f_error_msg( "Access token is not from findlock!") return m_LogicReturn.f_success_msg("Valid access token!")
def get_name_by_uuid(i_uuid) -> m_LogicReturn: #using if not v.verify_uuid(i_uuid).success: return m_LogicReturn.f_error_msg("Invalid UUID") db_resp = db_User().get_user(m_UserEntries.user_uuid, i_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) User = db_resp.addon_data User: m_User return m_LogicReturn.f_success_msg(f"{User.first_name} {User.surname}")
def add_friend(_user_uuid ,_findlock_uuid, _friendcode) -> m_LogicReturn: #using if not v.verify_uuid(_findlock_uuid).success: return m_LogicReturn.f_error_msg(f"{_findlock_uuid} is not a valid uuid!") if not v.verify_uuid(_user_uuid).success: return m_LogicReturn.f_error_msg(f"{_user_uuid} is not a valid uuid!") logic_resp = UserLogic.check_if_user_allowed_to_use_findlock(_user_uuid,_findlock_uuid) if not logic_resp.success: return m_LogicReturn.f_error_msg( logic_resp.content) if not v.verify_friendcode(_friendcode).success: return m_LogicReturn.f_error_msg(f"{_friendcode} is not a valid friendcode") friendcode_splitter = str(_friendcode).split("#") voornaam = friendcode_splitter[0] friendcode = friendcode_splitter[1] users_of_friendcode = DbUser().get_all_users() if not users_of_friendcode.success: return m_LogicReturn.f_error_msg( users_of_friendcode.content) users_of_friendcode = users_of_friendcode.addon_data user_of_friendcode = None for u in users_of_friendcode: _voornaam = u.first_name _friendcode = u.friend_code if _voornaam == voornaam and _friendcode == friendcode: user_of_friendcode = u if v.value_empty(user_of_friendcode): return m_LogicReturn.f_error_msg( "Could not find user by this friendcode!") user_of_friendcode: m_User db_resp = DbFindlock().get_findlock( m_FindlockEntries.device_uuid, _findlock_uuid ) if not db_resp.success: return m_LogicReturn.f_error_msg( "Could not get findlock with this uuid!") Findlock = db_resp.addon_data Findlock: m_Findlock if user_of_friendcode.user_uuid in Findlock.FindlockPermission.allowed_uuids: return m_LogicReturn.f_error_msg( "This user already have permissions") Findlock.FindlockPermission.addAllowedUuid(user_of_friendcode.user_uuid) db_resp = DbFindlock().update_findlock(Findlock) if not db_resp.success: return m_LogicReturn.f_error_msg( db_resp.content) return m_LogicReturn.f_success_msg(f"{friendcode} is added!")
def get_user_info(user_uuid) -> m_LogicReturn: if not v.verify_uuid(user_uuid).success: return m_LogicReturn.f_error_msg( f"{user_uuid} is not a valid uuid") db_resp = db_User().get_user(m_UserEntries.user_uuid, user_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) _user = db_resp.addon_data _user: m_User return m_LogicReturn.f_success("User found!", _user)
def verify_token_id(google_token_id, client_id): idinfo = id_token.verify_oauth2_token(google_token_id, requests.Request()) if idinfo['aud'] != config.get_gcid(): return m_LogicReturn.f_error_msg("Could not verify audience.") if idinfo['iss'] not in [ 'accounts.google.com', 'https://accounts.google.com' ]: return m_LogicReturn.f_error_msg("Wrong issuer!") # returns user id return m_LogicReturn.f_success("Verified!", addon_data=idinfo['sub'])
def remove_user(u_uuid) -> m_LogicReturn: db_resp = db_User().get_user(m_UserEntries.user_uuid, u_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) _User = db_resp.addon_data _User: m_User db_resp = db_User().delete_user(_User) if db_resp.success: return m_LogicReturn.f_success_msg(db_resp.content) return m_LogicReturn.f_error_msg(db_resp.content)
def get_findlock_info(findlock_uuid) -> m_LogicReturn: if not v.verify_uuid(findlock_uuid).success: return m_LogicReturn.f_error_msg( f"{findlock_uuid} is not a valid uuid") db_resp = db_Findlock().get_findlock(m_FindlockEntries.device_uuid, findlock_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) _findlock = db_resp.addon_data _findlock: m_Findlock return m_LogicReturn.f_success("Findlock found!", _findlock)
def get_info_about_findlock(device_uuid) -> m_LogicReturn: if not v.verify_uuid(device_uuid).success: return m_LogicReturn.f_error_msg(f"{device_uuid} is not a valid uuid!") db_resp = DbFindlock().get_findlock( m_FindlockEntries.device_uuid, device_uuid ) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.addon_data) Findlock = db_resp.addon_data Findlock: m_Findlock return m_LogicReturn.f_success("Found info!", Findlock)
def get_findlock(device_uuid) -> m_LogicReturn: if v.value_empty(device_uuid): return m_LogicReturn.f_error_msg("post data is missing") db_resp = db_Findlock().get_findlock(m_FindlockEntries.device_uuid, device_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) Findlock = db_resp.addon_data Findlock: m_Findlock return m_LogicReturn.f_success_msg(Findlock.toDict())
def get_findlock_event(device_uuid) -> m_LogicReturn: #using if not v.verify_uuid(device_uuid).success: return m_LogicReturn.f_error_msg( f"{device_uuid} is not a valid uuid") db_resp = db_Findlock().get_findlock(m_FindlockEntries.device_uuid, device_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) Findlock = db_resp.addon_data Findlock: m_Findlock # TODO: check if client waits for data in data and not in content return m_LogicReturn.f_success("Found event.", Findlock.Event.toDict())
def remove_findlock(findlock_uuid) -> m_LogicReturn: if not v.verify_uuid(findlock_uuid).success: return m_LogicReturn.f_error_msg("UUID is not valid!") db_resp = db_Findlock().get_findlock(m_FindlockEntries.device_uuid, findlock_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) _Findlock = db_resp.addon_data _Findlock: m_Findlock db_resp = db_Findlock().delete_findlock(_Findlock) if db_resp.success: return m_LogicReturn.f_success_msg(db_resp.content) return m_LogicReturn.f_error_msg(db_resp.content)
def update_findlock_event(form_data) -> m_LogicReturn: device_uuid = form_data.get('device_uuid', None) executed = form_data.get('executed', None) if v.value_empty(device_uuid) \ or v.value_empty(executed): return m_LogicReturn.f_error_msg("Data is missing!") if type(executed) == int: executed = True if executed == 1 else False elif type(executed) == str: executed = True if str(executed).lower() == "true" else False else: return m_LogicReturn.f_error_msg("Unknown type of executed!") return RestLogic.update_findlock_event(device_uuid, executed)
def add_friend(_session: m_Session, form_data) -> m_LogicReturn: print(form_data) friend_code = form_data.get("friendCode", None) device_uuid = form_data.get("device_uuid", default_device_uuid) #doet pijn maar moet wel if v.value_empty(friend_code) \ or v.value_empty(device_uuid): return m_LogicReturn.f_error_msg("Data is missing!") if type(_session) != m_Session: return m_LogicReturn.f_error_msg("Session is not a valid type!") _user_uuid = _session.User.user_uuid return UserLogic.add_friend( _user_uuid, device_uuid, friend_code )
def get_findlock(self, search_type: m_FindlockEntries, query) -> m_LogicReturn: res = self.find_one({search_type: query}) return m_LogicReturn.f_error_msg( "Findlock not found") if res is None else m_LogicReturn.f_success( "Found findlock!", m_Findlock.fromDict(res))
def get_list_of_users() -> m_LogicReturn: db_resp = db_User().get_all_users() if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) _Users = db_resp.addon_data _Users: [m_User] return m_LogicReturn.f_success(f"List of {len(_Users)} users", _Users)
def login(user_email, user_password) -> m_LogicReturn: if v.value_empty(user_email) \ or v.value_empty(user_password): return m_LogicReturn.f_error_msg("Post data is missing!") if not v.verify_email(user_email): return m_LogicReturn.f_error_msg("E-mail is invalid!") v_pass = v.verify_password(user_password) if not v_pass.success: return m_LogicReturn.f_error_msg(v_pass.content) db_resp = db_User().get_user( m_UserEntries.email, user_email ) if not db_resp.success: return m_LogicReturn.f_error_msg("Wrong email/password combination") _User = db_resp.addon_data _User: m_User if not Encryption.verify_password( user_password, _User.password_hash): return m_LogicReturn.f_error_msg("Wrong email/password combination") if not _User.verified: return m_LogicReturn.f_error_msg("User did not verify via email.") logic_resp = SessionLogic.login(_User) if not logic_resp.success: return m_LogicReturn.f_error_msg(logic_resp.content) return m_LogicReturn.f_success_msg("Logged in!")
def get_findlock_location(device_uuid) -> m_LogicReturn: if not v.verify_uuid(device_uuid).success: return m_LogicReturn.f_error_msg("Invalid UUID") db_resp = db_Findlock().get_findlock(m_FindlockEntries.device_uuid, device_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) findlock = db_resp.addon_data findlock: m_Findlock n_locations = findlock.GpsLocationHistory.get_location_count() - 1 if n_locations < 0: return m_LogicReturn.f_error_msg("No locations") return m_LogicReturn.f_success( "Returned.", findlock.GpsLocationHistory.get_location(n_locations).toDict())
def get_list_of_findlocks() -> m_LogicReturn: db_resp = db_Findlock().get_all_findlocks() if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) _Findlocks = db_resp.addon_data _Findlocks: [m_Findlock] return m_LogicReturn.f_success(f"List of {len(_Findlocks)} findlocks", _Findlocks)
def user_allowed_to_use_findlock(_session: m_Session, device_uuid) -> m_LogicReturn: if v.value_empty(_session) \ or v.value_empty(device_uuid): return m_LogicReturn.f_error_msg("Data is missing!") _user_uuid = _session.User.user_uuid return UserLogic.check_if_user_allowed_to_use_findlock( _user_uuid, device_uuid, _master_only=False )
def add_gps_loc_to_findlock(findlock_uuid, form_data) -> m_LogicReturn: lat = form_data.get("latInput", None) lng = form_data.get("lngInput", None) if v.value_empty(findlock_uuid) or \ v.value_empty(lat) or \ v.value_empty(lng): return m_LogicReturn.f_error_msg("Data is missing!") return AdminLogic.add_gps_location_to_findlock(findlock_uuid, lat, lng)