LinhVuu/Banking-Advanced-Authentication-System
0
1# Import libraries2import os3import streamlit as st4from PIL import Image5from streamlit_extras.switch_page_button import switch_page6from baam_functions import *7from datetime import datetime8from bokeh.models.widgets import Button9from bokeh.models import CustomJS10from streamlit_bokeh_events import streamlit_bokeh_events11 12# Set direction as current folder13sourceFileDir = os.path.dirname(os.path.abspath(__file__))14os.chdir(sourceFileDir)15 16logo = Image.open('img/logo.png')17st.set_page_config(page_title = "BAAM", page_icon = logo)18 19# Initialise variables to pass between pages20st.session_state['user_dict'] = {}21st.session_state['verification'] = False22st.session_state['location'] = ""23st.session_state['raw_location'] = ""24st.session_state['username'] = ""25# st.session_state['typing_speed'] = 026 27def main():28 """Banking Advanced Authentication System"""29 30 # # Note that API key's running out of budget31 # contact_url = "https://www.linkedin.com/in/linhvuu"32 # st.write("I am running out of energy. Please contact [my assistant](%s) to wake me up." % contact_url)33 34 menu = ["Home", "SignUp", "Login"]35 choice = st.sidebar.selectbox("Menu", menu)36 37 if choice == "Home":38 st.subheader("Banking Advanced Authentication Module") 39 st.image("img/home.png") 40 st.write("Please select one action from the Menu on the side bar. Thank you.")41 st.image("img/menu.png")42 43 elif choice == "SignUp":44 st.subheader("Create New Account")45 new_user = st.text_input("Username")46 new_password = st.text_input("Password",type='password')47 48 if st.button("Signup"):49 create_user_table()50 add_user_data(new_user,make_hashes(new_password))51 st.success("You have successfully created a valid Account")52 st.info("Go to Login Menu to login")53 54 elif choice == "Login":55 56 # Header of the page57 col1, col2, col3 = st.columns(3)58 with col1:59 st.write(' ')60 61 # Logo62 with col2:63 st.image("img/Standard_Chartered.png", width=175)64 with col3:65 st.write(' ')66 67 # Login form68 st.subheader("Login")69 username = st.text_input("Username")70 password = st.text_input("Password",type='password')71 72 is_tester = st.checkbox('Login to test')73 login_button = Button(label="Login")74 75 # Collect location when login button is clicked 76 login_button.js_on_event("button_click", CustomJS(code="""77 navigator.geolocation.getCurrentPosition(78 (loc) => {79 document.dispatchEvent(new CustomEvent("GET_LOCATION", {detail: {lat: loc.coords.latitude, lon: loc.coords.longitude}}))80 }81 )82 """))83 84 # Get location85 location = streamlit_bokeh_events(86 login_button,87 events="GET_LOCATION",88 key="get_location",89 refresh_on_update=False,90 override_height=75,91 debounce_time=0)92 93 # If can get location94 if location: 95 96 # Hash password & verify password97 hashed_pswd = make_hashes(password) 98 result = login_user(username,check_hashes(password, hashed_pswd))99 100 # When the username and password are correct101 if result:102 103 # time_start = st.session_state['time_start']104 105 # Stop counting time after the user finished entering username, password and clicked login button106 login_time = datetime.now()107 108 # # *** Calculate the login duration in seconds (the time it takes user to enter username, password and click login button)109 # login_duration = login_time - time_start110 # login_duration_seconds = login_duration.total_seconds()111 112 # # Calculate typing speed (characters per minute)113 # typing_speed = (len(username) + len(password)) * 60 / login_duration_seconds114 115 # Launch app for end user116 if not(is_tester):117 118 # Collect user's information when logging in119 #user_dict, st.session_state['location'] = collect_data(username, location, login_time, typing_speed)120 user_dict, st.session_state['location'] = collect_data(username, location, login_time)121 122 # Store user information to pass to other pages123 st.session_state['user_dict'] = user_dict124 verification = verify_user(user_dict)125 126 # THAO LE CODE: 127 # weight edit in logic_tl128 weight = {'device_uuid': 40, 'mac_address': 40, 'device_name': 30.0, 'device_model': 20.0, \129 'device_vendor': 4.0, 'ip_v4': 30, 'isp_name': 15.0, 'ip_country': 3.0, 'suburd': 30, 'district': 22.5, \130 'city': 15.0, 'country': 3.0}131 # ThaoLe note:132 # st.write(f"print the user_dict {user_dict}")133 # st.write(f'type of user_dict {type(user_dict)}')134 # end not135 136 st.session_state['verification'] = verification137 138 # Add to the login database after successful verification139 if verification:140 add_login_data(user_dict)141 142 # Open Transfer Money Page 143 switch_page("Transfer Money 💸")144 145 # Launch Testing Tool146 else:147 st.session_state['raw_location'] = location148 st.session_state['username'] = username149 # st.session_state['typing_speed'] = typing_speed150 switch_page("SelectUseCase") 151 152 else:153 st.warning("Incorrect Username/Password")154 155 # Start counting time156 st.session_state['time_start'] = datetime.now()157 158 # #To export database to csv file (can be commented out if not needed)159 # export_csv()160 161if __name__ == '__main__':162 main()163 