ayushnoori commited on
Commit
f128fe5
Β·
1 Parent(s): 92afb9c

Add user authentication

Browse files
.gitignore CHANGED
@@ -3,4 +3,7 @@
3
  .DS_Store
4
 
5
  # Ignore python cache files
6
- __pycache__/
 
 
 
 
3
  .DS_Store
4
 
5
  # Ignore python cache files
6
+ __pycache__/
7
+
8
+ # Ignore secrets
9
+ .streamlit/secrets.toml
.streamlit/config.toml ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ [client]
2
+ showSidebarNavigation = false
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import streamlit as st
 
2
 
3
  # Standard imports
4
  import numpy as np
@@ -17,15 +18,88 @@ import seaborn as sns
17
  # Path manipulation
18
  from pathlib import Path
19
  import sys
20
- sys.path.append('./scripts')
21
 
22
  # Custom and other imports
23
  import project_config
24
  from utils import add_logo
 
25
 
26
  # Insert logo
27
- add_logo(project_config.MEDIA_DIR / 'gravity_logo.png')
28
 
29
- # Page title
30
- st.title("Input")
 
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import hmac
3
 
4
  # Standard imports
5
  import numpy as np
 
18
  # Path manipulation
19
  from pathlib import Path
20
  import sys
 
21
 
22
  # Custom and other imports
23
  import project_config
24
  from utils import add_logo
25
+ from menu import menu
26
 
27
  # Insert logo
28
+ # add_logo(project_config.MEDIA_DIR / 'gravity_logo.png')
29
 
30
+ # Initialize st.session_state.role to None
31
+ if "role" not in st.session_state:
32
+ st.session_state.role = None
33
 
34
+ # # Retrieve the role from Session State to initialize the widget
35
+ # st.session_state._role = st.session_state.role
36
+
37
+ # def set_role():
38
+ # # Callback function to save the role selection to Session State
39
+ # st.session_state.role = st.session_state._role
40
+
41
+ def check_password():
42
+ """Returns `True` if the user had a correct password."""
43
+
44
+ def login_form():
45
+ """Form with widgets to collect user information"""
46
+ with st.form("Credentials"):
47
+ st.text_input("Username", key="username")
48
+ st.text_input("Password", type="password", key="password")
49
+ st.form_submit_button("Log In", on_click=password_entered)
50
+
51
+ def password_entered():
52
+ """Checks whether a password entered by the user is correct."""
53
+ if st.session_state["username"] in st.secrets[
54
+ "passwords"
55
+ ] and hmac.compare_digest(
56
+ st.session_state["password"],
57
+ st.secrets.passwords[st.session_state["username"]],
58
+ ):
59
+ st.session_state["password_correct"] = True
60
+
61
+ # Retrieve user name
62
+ if st.session_state["username"] in st.secrets["users"]:
63
+ st.session_state["name"] = st.secrets["users"][st.session_state["username"]]
64
+ else:
65
+ st.session_state["name"] = st.session_state["username"]
66
+
67
+ # Check if the username is an admin
68
+ if st.session_state["username"] in st.secrets["admins"]['admins']:
69
+ st.session_state["role"] = "admin"
70
+ else:
71
+ st.session_state["role"] = "user"
72
+
73
+ # Check if username is in a team
74
+ if st.session_state["username"] in st.secrets["teams"]:
75
+ st.session_state["team"] = st.secrets["teams"][st.session_state["username"]]
76
+ else:
77
+ st.session_state["team"] = None
78
+
79
+ # Don't store the username or password
80
+ del st.session_state["password"]
81
+ # del st.session_state["username"]
82
+ else:
83
+ st.session_state["password_correct"] = False
84
+
85
+ # Return True if the username + password is validated
86
+ if st.session_state.get("password_correct", False):
87
+ return True
88
+
89
+ # Show inputs for username + password
90
+ login_form()
91
+ if "password_correct" in st.session_state:
92
+ st.error("πŸ˜• User not known or password incorrect.")
93
+ return False
94
+
95
+ # # Selectbox to choose role
96
+ # st.selectbox(
97
+ # "Select your role:",
98
+ # [None, "user", "admin", "super-admin"],
99
+ # key="_role",
100
+ # on_change=set_role,
101
+ # )
102
+ menu() # Render the dynamic menu!
103
+
104
+ if not check_password():
105
+ st.stop()
menu.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # From https://docs.streamlit.io/develop/tutorials/multipage/st.page_link-nav
2
+ import streamlit as st
3
+
4
+
5
+ def authenticated_menu():
6
+
7
+ # Show a navigation menu for authenticated users
8
+ # st.sidebar.page_link("app.py", label="Switch Accounts", icon="πŸ”’")
9
+ st.sidebar.page_link("pages/user.py", label="Profile", icon="πŸ‘€")
10
+ st.sidebar.page_link("pages/validate.py", label="Input", icon="βœ…")
11
+ if st.session_state.role in ["admin"]:
12
+ st.sidebar.page_link("pages/admin.py", label="Manage Users", icon="πŸ”§")
13
+
14
+
15
+ def unauthenticated_menu():
16
+
17
+ # Show a navigation menu for unauthenticated users
18
+ st.sidebar.page_link("app.py", label="Log In", icon="πŸ”’")
19
+
20
+
21
+ def menu():
22
+ # Determine if a user is logged in or not, then show the correct navigation menu
23
+ if "role" not in st.session_state or st.session_state.role is None:
24
+ unauthenticated_menu()
25
+ return
26
+ authenticated_menu()
27
+
28
+
29
+ def menu_with_redirect():
30
+ # Redirect users to the main page if not logged in, otherwise continue to
31
+ # render the navigation menu
32
+ if "role" not in st.session_state or st.session_state.role is None:
33
+ st.switch_page("app.py")
34
+ menu()
pages/admin.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from menu import menu_with_redirect
3
+
4
+ # Redirect to app.py if not logged in, otherwise show the navigation menu
5
+ menu_with_redirect()
6
+
7
+ # Verify the user's role
8
+ if st.session_state.role not in ["admin"]:
9
+ st.warning("You do not have permission to view this page.")
10
+ st.stop()
11
+
12
+ st.title("User Management")
13
+ st.markdown(f"You are currently logged with the role of {st.session_state.role}.")
pages/user.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from menu import menu_with_redirect
3
+
4
+ # Redirect to app.py if not logged in, otherwise show the navigation menu
5
+ menu_with_redirect()
6
+
7
+ st.title("Welcome to Gravity")
8
+ st.markdown(f"Hello, {st.session_state.name}!")
pages/{1_Validate.py β†’ validate.py} RENAMED
@@ -1,16 +1,15 @@
1
  import streamlit as st
 
2
 
3
  # Path manipulation
4
  from pathlib import Path
5
  import sys
6
- sys.path.append('..')
7
- sys.path.append('scripts')
8
 
9
  # Custom and other imports
10
  import project_config
11
  from utils import add_logo
12
 
13
- # Insert logo
14
- add_logo(project_config.MEDIA_DIR / 'gravity_logo.png')
15
 
16
  st.title("Validate")
 
1
  import streamlit as st
2
+ from menu import menu_with_redirect
3
 
4
  # Path manipulation
5
  from pathlib import Path
6
  import sys
 
 
7
 
8
  # Custom and other imports
9
  import project_config
10
  from utils import add_logo
11
 
12
+ # Redirect to app.py if not logged in, otherwise show the navigation menu
13
+ menu_with_redirect()
14
 
15
  st.title("Validate")
requirements.txt CHANGED
@@ -5,4 +5,5 @@ matplotlib
5
  seaborn
6
  pathlib
7
  torch
8
- altair<5
 
 
5
  seaborn
6
  pathlib
7
  torch
8
+ altair<5
9
+ hmac
scripts/utils.py β†’ utils.py RENAMED
File without changes