Spaces:
Sleeping
Sleeping
Update pipelines/auth_utils.py
Browse files- pipelines/auth_utils.py +63 -0
pipelines/auth_utils.py
CHANGED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import streamlit_authenticator as stauth
|
3 |
+
|
4 |
+
# Example credential dictionary for demonstration.
|
5 |
+
# In practice, store hashed passwords in a safe location (e.g., environment variables, secrets manager).
|
6 |
+
# You can generate hashed passwords using stauth.Hasher.
|
7 |
+
CREDENTIALS = {
|
8 |
+
"usernames": {
|
9 |
+
"alice": {
|
10 |
+
"name": "Alice Doe",
|
11 |
+
"password": "$2b$12$abc123exampleHashALICEXXXXXXXXXXXXXXX",
|
12 |
+
"role": "premium"
|
13 |
+
},
|
14 |
+
"bob": {
|
15 |
+
"name": "Bob Smith",
|
16 |
+
"password": "$2b$12$abc123exampleHashBOBXXXXXXXXXXXXXXXXXX",
|
17 |
+
"role": "free"
|
18 |
+
}
|
19 |
+
}
|
20 |
+
}
|
21 |
+
|
22 |
+
|
23 |
+
def login():
|
24 |
+
"""
|
25 |
+
Creates a login widget for the Streamlit app using streamlit_authenticator.
|
26 |
+
|
27 |
+
Returns:
|
28 |
+
(username, authentication_status, authenticator_obj)
|
29 |
+
- username: str or None
|
30 |
+
- authentication_status: bool (True if authenticated)
|
31 |
+
- authenticator_obj: an instance of the Authenticate class, so you can call logout, etc.
|
32 |
+
"""
|
33 |
+
authenticator = stauth.Authenticate(
|
34 |
+
credentials=CREDENTIALS,
|
35 |
+
cookie_name="smart_edit_cookie",
|
36 |
+
key="abcdef",
|
37 |
+
cookie_expiry_days=1
|
38 |
+
)
|
39 |
+
|
40 |
+
# This call draws the login form in the Streamlit UI.
|
41 |
+
name, authentication_status, username = authenticator.login("Login", "main")
|
42 |
+
|
43 |
+
return username, authentication_status, authenticator
|
44 |
+
|
45 |
+
|
46 |
+
def show_logout_button(authenticator):
|
47 |
+
"""
|
48 |
+
Renders a logout button in the Streamlit UI for authenticated users.
|
49 |
+
"""
|
50 |
+
authenticator.logout("Logout", "main")
|
51 |
+
|
52 |
+
|
53 |
+
def is_premium_user(username):
|
54 |
+
"""
|
55 |
+
Checks if the logged-in user has the "premium" role.
|
56 |
+
|
57 |
+
Returns:
|
58 |
+
True if the user's role is premium, False otherwise.
|
59 |
+
"""
|
60 |
+
# Retrieve the stored role from the credentials dict
|
61 |
+
if username in CREDENTIALS["usernames"]:
|
62 |
+
return CREDENTIALS["usernames"][username].get("role") == "premium"
|
63 |
+
return False
|