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 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 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 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 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 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 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 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 check_if_user_allowed_to_use_findlock(_user_uuid,_findlock_uuid, _master_only=True) -> m_LogicReturn: #using if not v.verify_uuid(_user_uuid).success: return m_LogicReturn.f_error_msg(f"{_user_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 if _user_uuid == Findlock.FindlockPermission.master_uuid: return m_LogicReturn.f_success_msg( "User is master") if not _master_only: if _user_uuid in Findlock.FindlockPermission.allowed_uuids: return m_LogicReturn.f_success_msg( "User is allowed!") return m_LogicReturn.f_error_msg( "User is not allowed!")
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 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 send_email(to_addr, message): context = ssl.create_default_context() email_config = config.get_email_config() try: with smtplib.SMTP_SSL(email_config.server, email_config.port, context=context) as server: server.login(email_config.sender_mail, email_config.password) server.sendmail(email_config.sender_mail, to_addr, message.as_string()) return m_LogicReturn.f_success_msg("Email sent!") except: return m_LogicReturn.f_error_msg("Can't send email")
def update_session_using_db() -> m_LogicReturn: user_session = m_Session.fromSession(session) db_resp = db_User().get_user(m_UserEntries.user_uuid, user_session.User.user_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) User = db_resp.addon_data User: m_User user_session.User = User session['filo'] = user_session.toDict() return m_LogicReturn.f_success_msg("Session updated!")
def update_findlock_event(device_uuid, executed: bool) -> m_LogicReturn: 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 Findlock.Event.executed = executed db_Findlock().update_findlock(Findlock) return m_LogicReturn.f_success_msg("updated!")
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 add_gps_location_to_findlock(fl_uuid, lat, lng) -> m_LogicReturn: db_resp = db_Findlock().get_findlock(m_FindlockEntries.device_uuid, fl_uuid) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) _Findlock = db_resp.addon_data _Findlock: m_Findlock _new_gps_location = m_GpsLocation(lat, lng, str(Tools.get_unix_time())) _Findlock.GpsLocationHistory.add_location(_new_gps_location) db_Findlock().update_findlock(_Findlock) return m_LogicReturn.f_success_msg("ok!")
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 facebook_register(name, userid) -> m_LogicReturn: if v.value_empty(name) or v.value_empty(userid): return m_LogicReturn.f_error_msg("data is missing!") _User = m_User( first_name=str(name).split(" ")[0], surname=str(name).split(" ")[1], email=None, password_hash=None, phone_number=None, nfc_id=None, finger_id=None, friend_code=Tools.generate_friend_code(), facebook_user_id=userid, verified=True ) db_resp = db_User().create_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 process_confirmation_code(confirm_token) -> m_LogicReturn: email = Token.confirm_token(confirm_token) if email is False: return m_LogicReturn.f_error_msg("This link is invalid or has expired.") User = db_User().get_user( m_UserEntries.email, email ) if not User.success: return m_LogicReturn.f_error_msg("Can't find user with this email") User = User.addon_data User: m_User User.verified = True db_resp = db_User().update_user(User) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) return m_LogicReturn.f_success_msg("User is verified now!")
def findlock_update(findlock_uuid, data_keys, data_values) -> m_LogicReturn: if not v.verify_uuid(findlock_uuid).success: return m_LogicReturn.f_error_msg( f"{findlock_uuid} is not a valid uuid") if len(data_keys) != len(data_values): return m_LogicReturn.f_error_msg("Mismatch in data!") for i in range(0, len(data_values)): data_val = data_values[i] if data_val == "None": data_values[i] = None elif data_val == "True": data_values[i] = True elif data_val == "False": data_values[i] = False valid_data_keys = Tools.get_types(m_FindlockEntries()) 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 _findlock_dict = _findlock.toDict() for i in range(0, len(data_keys)): key = data_keys[i] if key not in valid_data_keys: return m_LogicReturn.f_error_msg(f"{key} is not a valid key!") val = data_values[i] _findlock_dict[key] = val _findlock = m_Findlock.fromDict(_findlock_dict) db_resp = db_Findlock().update_findlock(_findlock) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) return m_LogicReturn.f_success_msg(db_resp.content)
def register(user_email, user_password, user_password2) -> m_LogicReturn: if v.value_empty(user_email) \ or v.value_empty(user_password) \ or v.value_empty(user_password2): return m_LogicReturn.f_error_msg("post data is missing") v_pass = v.verify_password(user_password) if not v_pass.success: return m_LogicReturn.f_error_msg(v_pass.content) if not v.verify_email(user_email): return m_LogicReturn.f_error_msg("E-mail is invalid") if user_password != user_password2: return m_LogicReturn.f_error_msg("Passwords are not the same!") password_hash = Encryption.encrypt_password( user_password) _User = m_User( first_name=None, surname=None, email=user_email, password_hash=password_hash, phone_number=None, nfc_id=None, finger_id=None, friend_code=Tools.generate_friend_code(), avatar_link="https://www.clevelanddentalhc.com/wp-content/uploads/2018/03/sample-avatar-300x300.jpg" ) db_resp = db_User().create_user(_User) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) token_code = Token.generate_confirm_token(user_email) print(f"Token code: {token_code}") email_message = Email.get_register_email(user_email, token_code) email_resp = Email.send_email(user_email, email_message) if not email_resp.success: return m_LogicReturn.f_error_msg("Failed to send email!") return m_LogicReturn.f_success_msg(db_resp.content)
def wipe_collection(col_name) -> m_LogicReturn: valid_col_names = ["users", "findlocks"] if col_name not in valid_col_names: return m_LogicReturn.f_error_msg( f"{col_name} is not a valid col name!") if col_name == "users": db_User().drop_col() db_User().create_user(debug_data.get_dummy_user()) db_User().create_user(debug_data.get_dummy_user_with_permissions()) elif col_name == "findlocks": db_Findlock().drop_col() _findlock = debug_data.get_dummy_findlock() _findlock.FindlockPermission.master_uuid = debug_data.get_dummy_user( ).user_uuid _findlock.FindlockPermission.addAllowedUuid( debug_data.get_dummy_user_with_permissions().user_uuid) db_Findlock().create_findlock(_findlock) return m_LogicReturn.f_success_msg("Executed.")
def user_send_mail(user_uuid, subject, content) -> 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 _user_email = _user.email _user_voornaam = _user.first_name admin_mail = Email.get_admin_email(_user_email, _user_voornaam, subject, content) logic_resp = Email.send_email(_user_email, admin_mail) if not logic_resp.success: return m_LogicReturn.f_error_msg(logic_resp.content) return m_LogicReturn.f_success_msg("Mail sent!")
def user_update(user_uuid, data_keys, data_values) -> m_LogicReturn: if not v.verify_uuid(user_uuid).success: return m_LogicReturn.f_error_msg( f"{user_uuid} is not a valid uuid") if len(data_keys) != len(data_values): return m_LogicReturn.f_error_msg("Mismatch in data!") for i in range(0, len(data_values)): data_val = data_values[i] if data_val == "None": data_values[i] = None elif data_val == "True": data_values[i] = True elif data_val == "False": data_values[i] = False valid_data_keys = Tools.get_types(m_UserEntries()) 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 _user_dict = _user.toDict() for i in range(0, len(data_keys)): key = data_keys[i] if key not in valid_data_keys: return m_LogicReturn.f_error_msg(f"{key} is not a valid key!") val = data_values[i] _user_dict[key] = val _user = m_User.fromDict(_user_dict) db_resp = db_User().update_user(_user) if not db_resp.success: return m_LogicReturn.f_error_msg(db_resp.content) return m_LogicReturn.f_success_msg(db_resp.content)
def set_findlock_event(device_uuid, event_type) -> m_LogicReturn: #using valid_event_types = t.get_types(m_EventTypes()) print(valid_event_types) if event_type not in valid_event_types: return m_LogicReturn.f_error_msg( f"{event_type} is not a valid event type!") 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 _findlock.Event = m_Event(event_type, t.get_unix_time()) db_resp = db_Findlock().update_findlock(_findlock) return m_LogicReturn.f_success_msg("Event updated!")
def update_user_data(_user_dict: dict,form_data) -> m_LogicReturn: #using if type(_user_dict) != dict: return m_LogicReturn.f_error_msg("Session type is invalid") logic_resp = UserLogic.user_data_missing(_user_dict) if not logic_resp.success: return m_LogicReturn.f_error_msg(logic_resp.content) missing_list = logic_resp.addon_data for i in missing_list: form_entry = form_data.get(f"{i}Input", None) if form_entry is not None and len(form_entry) > 0: _user_dict[i] = form_entry new_user = m_User.fromDict(_user_dict) db_resp = DbUser().update_user(new_user) if not db_resp.success: return m_LogicReturn.f_error_msg( db_resp.content) if not SessionLogic.update_session_using_db().success: return m_LogicReturn.f_error_msg( "Updating session gaat fout!") return m_LogicReturn.f_success_msg("Updated user data!")
def google_login(google_token_id, user_email, client_id, avatar_link) -> m_LogicReturn: if v.value_empty(google_token_id) \ or v.value_empty(user_email) \ or v.value_empty(client_id) \ or v.value_empty(avatar_link): return m_LogicReturn.f_error_msg("Data is missing!") if not v.verify_email(user_email): return m_LogicReturn.f_error_msg("E-mail is invalid!") logic_resp = SocialsLogic.verify_token_id(google_token_id, client_id) if not logic_resp.success: return m_LogicReturn.f_error_msg("invalid id token") google_user_id = logic_resp.addon_data db_resp = db_User().get_user( m_UserEntries.google_user_id, google_user_id ) if not db_resp.success: logic_resp = AuthLogic.google_register( google_user_id, user_email, avatar_link) if not logic_resp.success: return m_LogicReturn.f_error_msg(logic_resp.content) db_resp = db_User().get_user( m_UserEntries.google_user_id, google_user_id ) _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 update_findlock(self, Findlock: m_Findlock) -> m_LogicReturn: self.update_one({m_FindlockEntries.device_uuid: Findlock.device_uuid}, {"$set": Findlock.toDict()}) return m_LogicReturn.f_success_msg("Findlock updated!")
def update_user(self, User: m_User) -> m_LogicReturn: self.update_one({m_UserEntries.user_uuid: User.user_uuid}, {"$set": User.toDict()}) return m_LogicReturn.f_success_msg("Updated!")