File size: 5,154 Bytes
c44d66d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d153351
8ef54e5
 
 
c44d66d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a7d867
c44d66d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Import libraries
import os
import streamlit as st
from PIL import Image
from streamlit_extras.switch_page_button import switch_page
from baam_functions import *
from datetime import datetime
from bokeh.models.widgets import Button
from bokeh.models import CustomJS
from streamlit_bokeh_events import streamlit_bokeh_events

# Set direction as current folder
sourceFileDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(sourceFileDir)

logo = Image.open('img/logo.png')
st.set_page_config(page_title = "BAAM", page_icon = logo)

# Initialise variables to pass between pages
st.session_state['user_dict'] = {}
st.session_state['verification'] = False
st.session_state['location'] = ""
st.session_state['raw_location'] = ""
st.session_state['username'] = ""
# st.session_state['typing_speed'] = 0

def main():
	"""Banking Advanced Authentication System"""
	
	# # Note that API key's running out of budget
	# contact_url = "https://www.linkedin.com/in/linhvuu"
	# st.write("I am running out of energy. Please contact [my assistant](%s) to wake me up." % contact_url)

	menu = ["Home", "SignUp", "Login"]
	choice = st.sidebar.selectbox("Menu", menu)

	if choice == "Home":
		st.subheader("Banking Advanced Authentication Module")       
		st.image("img/home.png") 
		st.write("Please select one action from the Menu on the side bar. Thank you.")
		st.image("img/menu.png")

	elif choice == "SignUp":
		st.subheader("Create New Account")
		new_user = st.text_input("Username")
		new_password = st.text_input("Password",type='password')

		if st.button("Signup"):
			create_user_table()
			add_user_data(new_user,make_hashes(new_password))
			st.success("You have successfully created a valid Account")
			st.info("Go to Login Menu to login")

	elif choice == "Login":

		# Header of the page
		col1, col2, col3 = st.columns(3)
		with col1:
			st.write(' ')

		# Logo
		with col2:
			st.image("img/Standard_Chartered.png", width=175)
		with col3:
			st.write(' ')
			
		# Login form
		st.subheader("Login")
		username = st.text_input("Username")
		password = st.text_input("Password",type='password')

		is_tester =  st.checkbox('Login to test')
		login_button = Button(label="Login")

		# Collect location when login button is clicked    
		login_button.js_on_event("button_click", CustomJS(code="""
			navigator.geolocation.getCurrentPosition(
				(loc) => {
					document.dispatchEvent(new CustomEvent("GET_LOCATION", {detail: {lat: loc.coords.latitude, lon: loc.coords.longitude}}))
				}
			)
			"""))

		# Get location
		location = streamlit_bokeh_events(
			login_button,
			events="GET_LOCATION",
			key="get_location",
			refresh_on_update=False,
			override_height=75,
			debounce_time=0)
		
		# If can get location
		if location:		
		
			# Hash password & verify password
			hashed_pswd = make_hashes(password)			
			result = login_user(username,check_hashes(password, hashed_pswd))

			# When the username and password are correct
			if result:
				
				# time_start = st.session_state['time_start']

				# Stop counting time after the user finished entering username, password and clicked login button
				login_time = datetime.now()

				# # *** Calculate the login duration in seconds (the time it takes user to enter username, password and click login button)
				# login_duration = login_time - time_start
				# login_duration_seconds = login_duration.total_seconds()

				# # Calculate typing speed (characters per minute)
				# typing_speed = (len(username) + len(password)) * 60 / login_duration_seconds

				# Launch app for end user
				if not(is_tester):

					# Collect user's information when logging in
					#user_dict, st.session_state['location'] = collect_data(username, location, login_time, typing_speed)
					user_dict, st.session_state['location'] = collect_data(username, location, login_time)
					
					# Store user information to pass to other pages
					st.session_state['user_dict'] = user_dict
					verification = verify_user(user_dict)

					# THAO LE CODE: 
					# weight edit in logic_tl
					weight = {'device_uuid': 40, 'mac_address': 40, 'device_name': 30.0, 'device_model': 20.0, \
						'device_vendor': 4.0, 'ip_v4': 30, 'isp_name': 15.0, 'ip_country': 3.0, 'suburd': 30, 'district': 22.5, \
							'city': 15.0, 'country': 3.0}
					# ThaoLe note:
					# st.write(f"print the user_dict {user_dict}")
					# st.write(f'type of user_dict {type(user_dict)}')
					# end not

					st.session_state['verification'] = verification

					# Add to the login database after successful verification
					if verification:
						add_login_data(user_dict)

					# Open Transfer Money Page		
					switch_page("Transfer Money 💸")

				# Launch Testing Tool
				else:
					st.session_state['raw_location'] = location
					st.session_state['username'] = username
					# st.session_state['typing_speed'] = typing_speed
					switch_page("SelectUseCase")						

			else:
				st.warning("Incorrect Username/Password")

		# Start counting time
		st.session_state['time_start'] = datetime.now()

	# #To export database to csv file (can be commented out if not needed)
	# export_csv()
		
if __name__ == '__main__':
	main()