Skip to content

Commit 28f5ba2

Browse files
committed
v0.4.2
1 parent 5d912cb commit 28f5ba2

26 files changed

+1689
-1090
lines changed

README.md

Lines changed: 65 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@
2222
- [Creating a config file](#3-creating-a-config-file)
2323
- [Setup](#4-setup)
2424
- [Creating a login widget](#5-creating-a-login-widget)
25-
- [Creating a guest login button](#6-creating-a-guest-login-button) 🚀 **NEW**
25+
- [Creating a guest login widget](#6-creating-a-guest-login-widget) 🚀 **NEW**
2626
- [Authenticating users](#7-authenticating-users)
27-
- [Creating a reset password widget](#8-creating-a-reset-password-widget)
28-
- [Creating a new user registration widget](#9-creating-a-new-user-registration-widget)
29-
- [Creating a forgot password widget](#10-creating-a-forgot-password-widget)
30-
- [Creating a forgot username widget](#11-creating-a-forgot-username-widget)
31-
- [Creating an update user details widget](#12-creating-an-update-user-details-widget)
32-
- [Updating the config file](#13-updating-the-config-file)
27+
- [Enabling two factor authentication](#8-enabling-two-factor-authentication) 🚀 **NEW**
28+
- [Creating a reset password widget](#9-creating-a-reset-password-widget)
29+
- [Creating a new user registration widget](#10-creating-a-new-user-registration-widget)
30+
- [Creating a forgot password widget](#11-creating-a-forgot-password-widget)
31+
- [Creating a forgot username widget](#12-creating-a-forgot-username-widget)
32+
- [Creating an update user details widget](#13-creating-an-update-user-details-widget)
33+
- [Updating the config file](#14-updating-the-config-file)
3334
- [License](#license)
3435

3536
### 1. Quickstart
3637

38+
* Subscribe to receive a free [API key](https://streamlitauthenticator.com)
3739
* Check out the [demo app](https://demo-app-v0-3-3.streamlit.app/).
3840
* Feel free to visit the [API reference](https://streamlit-authenticator.readthedocs.io/en/stable/).
3941
* And finally follow the tutorial below.
@@ -64,8 +66,8 @@ import streamlit_authenticator as stauth
6466
```python
6567
cookie:
6668
expiry_days: 30
67-
key: some_signature_key # Must be a string
68-
name: some_cookie_name
69+
key: # To be filled with any string
70+
name: # To be filled with any string
6971
credentials:
7072
usernames:
7173
jsmith:
@@ -101,6 +103,7 @@ oauth2: # Optional
101103
pre-authorized: # Optional
102104
emails:
103105
- melsby@gmail.com
106+
api_key: # Optional - register to receive a free API key: https://streamlitauthenticator.com
104107
```
105108

106109
* _Please note that the 'failed_login_attempts' and 'logged_in' fields corresponding to each user's number of failed login attempts and log-in status in the credentials will be added and managed automatically._
@@ -152,6 +155,8 @@ authenticator = stauth.Authenticate(
152155
> - Provides a validator object that will check the validity of the username, name, and email fields.
153156
> - **auto_hash:** _bool, default True_
154157
> - Automatic hashing requirement for passwords, True: plain text passwords will be hashed automatically, False: plain text passwords will not be hashed automatically.
158+
> - **api_key:** _str, optional, default None_
159+
> - API key used to connect to the cloud server to send reset passwords and two factor authorization codes to the user by email.
155160
> - ****kwargs:** _dict, optional_
156161
> - Arguments to pass to the Authenticate class.
157162
@@ -194,7 +199,7 @@ except Exception as e:
194199
* **_Please remember to re-invoke an 'unrendered' login widget on each and every page in a multi-page application._**
195200
* **_Please remember to update the config file (as shown in step 13) after you use this widget._**
196201

197-
### 6. Creating a guest login button
202+
### 6. Creating a guest login widget
198203

199204
* You may use the **experimental_guest_login** button to log in non-registered users with their Google or Microsoft accounts using OAuth2.
200205
* To create the client ID and client secret parameters for Google OAuth2 please refer to [Google's documentation](https://developers.google.com/identity/protocols/oauth2).
@@ -229,6 +234,8 @@ except Exception as e:
229234
> - Disables the ability for the same user to log in multiple sessions, True: single session allowed, False: multiple sessions allowed.
230235
> - **roles:** _list, optional, default None_
231236
> - User roles for guest users.
237+
> - **use_container_width:** _bool, default False_
238+
> - Button width setting, True: width will match container, False: width will fit to button contents.
232239
> - **callback:** _callable, optional, default None_
233240
> - Callback function that will be invoked on button press with a dict as a parameter.
234241
@@ -239,22 +246,21 @@ except Exception as e:
239246
![](https://github.com/mkhorasani/Streamlit-Authenticator/blob/main/graphics/guest_login_microsoft.JPG)
240247

241248
* Please note that upon successful login, the guest user's name, email, and other information will be registered in the credentials dictionary and their re-authentication cookie will be saved automatically.
242-
* **_Please remember to update the config file (as shown in step 13) after you use this button._**
243249

244250
### 7. Authenticating users
245251

246-
* You can then retrieve the name, authentication status, and username from Streamlit's session state using **st.session_state['name']**, **st.session_state['authentication_status']**, **st.session_state['username']**, and **st.session_state['roles']** to allow a verified user to access restricted content.
252+
* You can then retrieve the name, authentication status, username, and roles from Streamlit's session state using the keys **'name'**, **'authentication_status'**, **'username'**, and **'roles'** to allow a verified user to access restricted content.
247253
* You may also render a logout button, or may choose not to render the button if you only need to implement the logout logic programmatically.
248254
* The optional **key** parameter for the logout button should be used with multi-page applications to prevent Streamlit from throwing duplicate key errors.
249255

250256
```python
251-
if st.session_state['authentication_status']:
257+
if st.session_state.get('authentication_status'):
252258
authenticator.logout()
253-
st.write(f'Welcome *{st.session_state["name"]}*')
259+
st.write(f'Welcome *{st.session_state.get("name")}*')
254260
st.title('Some content')
255-
elif st.session_state['authentication_status'] is False:
261+
elif st.session_state.get('authentication_status') is False:
256262
st.error('Username/password is incorrect')
257-
elif st.session_state['authentication_status'] is None:
263+
elif st.session_state.get('authentication_status') is None:
258264
st.warning('Please enter your username and password')
259265
```
260266

@@ -266,6 +272,8 @@ elif st.session_state['authentication_status'] is None:
266272
> - Specifies the location of the logout button. If 'unrendered' is passed, the logout logic will be executed without rendering the button.
267273
> - **key:** _str, default None_
268274
> - Unique key that should be used in multi-page applications.
275+
> - **use_container_width:** _bool, default False_
276+
> - Button width setting, True: width will match container, False: width will fit to button contents.
269277
> - **callback:** _callable, optional, default None_
270278
> - Callback function that will be invoked on form submission with a dict as a parameter.
271279
@@ -275,16 +283,33 @@ elif st.session_state['authentication_status'] is None:
275283

276284
![](https://github.com/mkhorasani/Streamlit-Authenticator/blob/main/graphics/incorrect_login.JPG)
277285

278-
* You may also retrieve the number of failed login attempts a user has made by accessing **st.session_state['failed_login_attempts']** which returns a dictionary with the username as key and the number of failed attempts as the value.
286+
* You may also retrieve the number of failed login attempts a user has made by accessing **st.session_state.get('failed_login_attempts')** which returns a dictionary with the username as key and the number of failed attempts as the value.
279287

280-
### 8. Creating a reset password widget
288+
### 8. Enabling two factor authentication
289+
290+
* You may enable two factor authentication for the **register_user**, **forgot_password**, and **forgot_username** widgets for enhanced security.
291+
* First register to receive a free API key [here](https://streamlitauthenticator.com).
292+
* Then add your API key to the the authenticator object as **api_key** or alternatively add it to the config file as shown in step 3.
293+
* Finally set the **two_factor_auth** parameter for the widget to True, this will prompt the user to enter a four digit code sent to their email.
294+
295+
![](https://github.com/mkhorasani/Streamlit-Authenticator/blob/main/graphics/two_factor_authentication.JPG)
296+
297+
![](https://github.com/mkhorasani/Streamlit-Authenticator/blob/main/graphics/two_factor_authentication_email.JPG)
298+
299+
* For the **forgot_password** and **forgot_username** widgets if you require the returned password and username to be sent to the user's email then you may set the **send_email** parameter to True.
300+
301+
![](https://github.com/mkhorasani/Streamlit-Authenticator/blob/main/graphics/two_factor_authentication_email_password.JPG)
302+
303+
![](https://github.com/mkhorasani/Streamlit-Authenticator/blob/main/graphics/two_factor_authentication_email_username.JPG)
304+
305+
### 9. Creating a reset password widget
281306

282307
* You may use the **reset_password** widget to allow a logged in user to modify their password as shown below.
283308

284309
```python
285-
if st.session_state['authentication_status']:
310+
if st.session_state.get('authentication_status'):
286311
try:
287-
if authenticator.reset_password(st.session_state['username']):
312+
if authenticator.reset_password(st.session_state.get('username')):
288313
st.success('Password modified successfully')
289314
except Exception as e:
290315
st.error(e)
@@ -312,7 +337,7 @@ if st.session_state['authentication_status']:
312337

313338
* **_Please remember to update the config file (as shown in step 13) after you use this widget._**
314339

315-
### 9. Creating a new user registration widget
340+
### 10. Creating a new user registration widget
316341

317342
* You may use the **register_user** widget to allow a user to sign up to your application as shown below.
318343
* If you require the user to be pre-authorized, define a **pre_authorized** list of emails that are allowed to register, and add it to the config file or provide it as a parameter to the **register_user** widget.
@@ -346,6 +371,10 @@ except Exception as e:
346371
> - User roles for registered users.
347372
> - **merge_username_email:** _bool, default False_
348373
> - Merges username into email field, True: username will be the same as the email, False: username and email will be independent.
374+
> - **password_hint:** _bool, default True_
375+
> - Requirement for entering a password hint, True: password hint field added, False: password hint field removed.
376+
> - **two_factor_auth:** _bool, default False_
377+
> - Specifies whether to enable two factor authentication for the forgot password widget, True: two factor authentication enabled, False: two factor authentication disabled.
349378
> - **clear_on_submit:** _bool, default False_
350379
> - Specifies the clear on submit setting, True: clears inputs on submit, False: keeps inputs on submit.
351380
> - **key:** _str, default 'Register user'_
@@ -364,7 +393,7 @@ except Exception as e:
364393

365394
* **_Please remember to update the config file (as shown in step 13) after you use this widget._**
366395

367-
### 10. Creating a forgot password widget
396+
### 11. Creating a forgot password widget
368397

369398
* You may use the **forgot_password** widget to allow a user to generate a new random password.
370399
* The new password will be automatically hashed and saved in the credentials dictionary.
@@ -392,6 +421,10 @@ except Exception as e:
392421
> - Customizes the text of headers, buttons and other fields.
393422
> - **captcha:** _bool, default False_
394423
> - Specifies the captcha requirement for the forgot password widget, True: captcha required, False: captcha removed.
424+
> - **send_email:** _bool, default False_
425+
> - Specifies whether to send the generated password to the user's email, True: password will be sent to user's email, False: password will not be sent to user's email.
426+
> - **two_factor_auth:** _bool, default False_
427+
> - Specifies whether to enable two factor authentication for the forgot password widget, True: two factor authentication enabled, False: two factor authentication disabled.
395428
> - **clear_on_submit:** _bool, default False_
396429
> - Specifies the clear on submit setting, True: clears inputs on submit, False: keeps inputs on submit.
397430
> - **key:** _str, default 'Forgot password'_
@@ -410,7 +443,7 @@ except Exception as e:
410443

411444
* **_Please remember to update the config file (as shown in step 13) after you use this widget._**
412445

413-
### 11. Creating a forgot username widget
446+
### 12. Creating a forgot username widget
414447

415448
* You may use the **forgot_username** widget to allow a user to retrieve their forgotten username.
416449
* The widget will return the username and email which the developer should then transfer to the user securely.
@@ -436,6 +469,10 @@ except Exception as e:
436469
> - Customizes the text of headers, buttons and other fields.
437470
> - **captcha:** _bool, default False_
438471
> - Specifies the captcha requirement for the forgot username widget, True: captcha required, False: captcha removed.
472+
> - **send_email:** _bool, default False_
473+
> - Specifies whether to send the retrieved username to the user's email, True: username will be sent to user's email, False: username will not be sent to user's email.
474+
> - **two_factor_auth:** _bool, default False_
475+
> - Specifies whether to enable two factor authentication for the forgot username widget, True: two factor authentication enabled, False: two factor authentication disabled.
439476
> - **clear_on_submit:** _bool, default False_
440477
> - Specifies the clear on submit setting, True: clears inputs on submit, False: keeps inputs on submit.
441478
> - **key:** _str, default 'Forgot username'_
@@ -450,15 +487,15 @@ except Exception as e:
450487
451488
![](https://github.com/mkhorasani/Streamlit-Authenticator/blob/main/graphics/forgot_username.JPG)
452489

453-
### 12. Creating an update user details widget
490+
### 13. Creating an update user details widget
454491

455492
* You may use the **update_user_details** widget to allow a logged in user to update their name and/or email.
456493
* The widget will automatically save the updated details in both the credentials dictionary and re-authentication cookie.
457494

458495
```python
459-
if st.session_state['authentication_status']:
496+
if st.session_state.get('authentication_status'):
460497
try:
461-
if authenticator.update_user_details(st.session_state['username']):
498+
if authenticator.update_user_details(st.session_state.get('username')):
462499
st.success('Entries updated successfully')
463500
except Exception as e:
464501
st.error(e)
@@ -486,13 +523,13 @@ if st.session_state['authentication_status']:
486523

487524
* **_Please remember to update the config file (as shown in step 13) after you use this widget._**
488525

489-
### 13. Updating the config file
526+
### 14. Updating the config file
490527

491528
* Please ensure that the config file is re-saved whenever the contents are modified or after using any of the widgets or buttons.
492529

493530
```python
494531
with open('../config.yaml', 'w') as file:
495-
yaml.dump(config, file, default_flow_style=False)
532+
yaml.dump(config, file, default_flow_style=False, allow_unicode=True)
496533
```
497534
* Please note that this step is not required if you are providing the config file as a path to the **Authenticate** class.
498535

config.yaml

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
1+
api_key: null
12
cookie:
23
expiry_days: 30
34
key: some_key
45
name: some_cookie_name
56
credentials:
67
usernames:
7-
dbaldwin:
8-
9-
failed_login_attempts: 0
10-
logged_in: false
11-
name: David Baldwin
12-
password: $2b$12$E9/bCaN/r8sN/FV2l8NpgOgspUBAp7UAVU6BgsXJzp/pW8gQWCprC
138
jsmith:
149
1510
failed_login_attempts: 0
11+
first_name: John
12+
last_name: Smith
1613
logged_in: false
17-
name: John Smith
18-
password: $2b$12$iWlVOac3uujRvTrXDi6wructXftKmo/GyQd6SMu5FmyX306kH.yFO
14+
password: $2b$12$lhOfbg3eMo9/TIcgFOvlbuYTGJQBdTLU/ek3SiDOooJNrIktqfKr6
15+
roles:
16+
- admin
17+
- editor
18+
- viewer
1919
rbriggs:
2020
2121
failed_login_attempts: 0
22+
first_name: Rebecca
23+
last_name: Briggs
2224
logged_in: false
23-
name: Rebecca Briggs
24-
password: $2b$12$uNaTgvGPG9rMbzOJHYaPQePw0DUfp1qHBrSq6l4O304qani6pKFpm
25-
rcouper:
26-
27-
failed_login_attempts: 0
28-
logged_in: false
29-
name: Ross Couper
30-
password: $2b$12$Tir/PbHVmmnt5kgNxgOwMuxNIb2fv2pJ.q71TW8ekvbugCqkye4yu
25+
password: $2b$12$SxD/z1xJN6C7G7ygeiCuDOR88ZFAMoky9nCm9JgACEuJiVbVV6/4a
26+
roles:
27+
- viewer
3128
oauth2:
3229
google:
3330
client_id: null
26.3 KB
Loading
92.6 KB
Loading
91.2 KB
Loading
92.2 KB
Loading

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="streamlit-authenticator",
8-
version="0.4.1",
8+
version="0.4.2",
99
author="Mohammad Khorasani",
1010
author_email="[email protected]",
1111
description="A secure authentication module to manage user access in a Streamlit application.",
@@ -24,6 +24,7 @@
2424
install_requires=[
2525
"bcrypt >= 3.1.7",
2626
"captcha >= 0.5.0",
27+
"cryptography >= 42.0.5",
2728
"extra-streamlit-components >= 0.1.70",
2829
"PyJWT >=2.3.0",
2930
"PyYAML >= 5.3.1",

0 commit comments

Comments
 (0)