awacke1 commited on
Commit
3b5fc66
Β·
verified Β·
1 Parent(s): 2171d3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -68
app.py CHANGED
@@ -1,90 +1,80 @@
1
  import streamlit as st
2
  import json
3
  import os
4
- from cryptography.fernet import Fernet, InvalidToken
 
5
 
6
- # Initialize session state
7
  if 'username' not in st.session_state:
8
- st.session_state.username = ''
9
  if 'scratch_pad' not in st.session_state:
10
- st.session_state.scratch_pad = ''
11
 
12
- # Load or create user history file
13
  history_file = 'user_history.json'
14
  if not os.path.exists(history_file):
15
- with open(history_file, 'w') as file:
16
- json.dump({}, file)
17
 
18
- # Load user history
19
  with open(history_file, 'r') as file:
20
- user_history = json.load(file)
21
-
22
- # Generate encryption key
23
- key = Fernet.generate_key()
24
- fernet = Fernet(key)
25
 
26
  def save_session(username, password):
27
- st.session_state.username = username
28
- st.session_state.scratch_pad = st.session_state.scratch_pad
29
- encrypted_password = fernet.encrypt(password.encode()).decode()
30
- user_history[username] = {
31
- 'password': encrypted_password,
32
- 'scratch_pad': st.session_state.scratch_pad
33
- }
34
- with open(history_file, 'w') as file:
35
- json.dump(user_history, file)
36
 
37
  def register_user(username, password):
38
- if username not in user_history:
39
- save_session(username, password)
40
- st.success("User registered successfully!")
41
- else:
42
- st.warning("Username already exists. Please choose a different username.")
43
-
44
 
45
  def login_user(username, password):
46
- if username in user_history:
47
- encrypted_password = user_history[username]['password']
48
- try:
49
- decrypted_password = fernet.decrypt(encrypted_password.encode()).decode()
50
- if password == decrypted_password:
51
- st.session_state.username = username
52
- st.session_state.scratch_pad = user_history[username]['scratch_pad']
53
- st.success("Logged in successfully!")
54
- return True
55
- else:
56
- st.error("Incorrect password. Please try again.")
57
- except InvalidToken:
58
- st.error("Invalid password. Please try again.")
59
- else:
60
- st.warning("Username not found. Please register first.")
61
- return False
62
-
63
  def reset_password(username):
64
- if username in user_history:
65
- new_password = Fernet.generate_password(8).decode()
66
- save_session(username, new_password)
67
- st.success(f"Password reset successful. Your new password is: {new_password}")
68
- else:
69
- st.warning("Username not found.")
 
 
70
 
71
- st.title("Memory of Me - State Aware Authentication and User Data Memory")
72
 
73
  if st.session_state.username:
74
- st.subheader(f"Welcome back, {st.session_state.username}!")
75
- scratch_pad = st.text_area("Scratch Pad", value=st.session_state.scratch_pad, height=200)
76
- if st.button("πŸ’Ύ Save"):
77
- save_session(st.session_state.username, '')
78
- if st.button("πŸ”‘ Reset Password"):
79
- reset_password(st.session_state.username)
80
  else:
81
- username = st.text_input("Username:")
82
- password = st.text_input("Password:", type="password")
83
- col1, col2 = st.columns(2)
84
- with col1:
85
- if st.button("πŸ”‘ Login"):
86
- if login_user(username, password):
87
- st.experimental_rerun()
88
- with col2:
89
- if st.button("πŸ“ Register"):
90
- register_user(username, password)
 
1
  import streamlit as st
2
  import json
3
  import os
4
+ import random
5
+ import string
6
 
 
7
  if 'username' not in st.session_state:
8
+ st.session_state.username = ''
9
  if 'scratch_pad' not in st.session_state:
10
+ st.session_state.scratch_pad = ''
11
 
 
12
  history_file = 'user_history.json'
13
  if not os.path.exists(history_file):
14
+ with open(history_file, 'w') as file:
15
+ json.dump({}, file)
16
 
 
17
  with open(history_file, 'r') as file:
18
+ user_history = json.load(file)
 
 
 
 
19
 
20
  def save_session(username, password):
21
+ st.session_state.username = username
22
+ st.session_state.scratch_pad = st.session_state.scratch_pad
23
+ user_history[username] = {
24
+ 'password': password,
25
+ 'scratch_pad': st.session_state.scratch_pad
26
+ }
27
+ with open(history_file, 'w') as file:
28
+ json.dump(user_history, file)
 
29
 
30
  def register_user(username, password):
31
+ if username not in user_history:
32
+ save_session(username, password)
33
+ st.success("User registered successfully!")
34
+ else:
35
+ st.warning("Username already exists. Please choose a different username.")
 
36
 
37
  def login_user(username, password):
38
+ if username in user_history:
39
+ stored_password = user_history[username]['password']
40
+ if password == stored_password:
41
+ st.session_state.username = username
42
+ st.session_state.scratch_pad = user_history[username]['scratch_pad']
43
+ st.success("Logged in successfully!")
44
+ return True
45
+ else:
46
+ st.error("Incorrect password. Please try again.")
47
+ else:
48
+ st.warning("Username not found. Please register first.")
49
+ return False
50
+
 
 
 
 
51
  def reset_password(username):
52
+ if username in user_history:
53
+ new_password = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
54
+ user_history[username]['password'] = new_password
55
+ with open(history_file, 'w') as file:
56
+ json.dump(user_history, file)
57
+ st.success(f"Password reset successful. Your new password is: {new_password}")
58
+ else:
59
+ st.warning("Username not found.")
60
 
61
+ st.title("Welcome to My App")
62
 
63
  if st.session_state.username:
64
+ st.subheader(f"Welcome back, {st.session_state.username}!")
65
+ scratch_pad = st.text_area("Scratch Pad", value=st.session_state.scratch_pad, height=200)
66
+ if st.button("πŸ’Ύ Save"):
67
+ save_session(st.session_state.username, user_history[st.session_state.username]['password'])
68
+ if st.button("πŸ”‘ Reset Password"):
69
+ reset_password(st.session_state.username)
70
  else:
71
+ username = st.text_input("Username:")
72
+ password = st.text_input("Password:", type="password")
73
+ col1, col2 = st.columns(2)
74
+ with col1:
75
+ if st.button("πŸ”‘ Login"):
76
+ if login_user(username, password):
77
+ st.experimental_rerun()
78
+ with col2:
79
+ if st.button("πŸ“ Register"):
80
+ register_user(username, password)