<img src="https://yellow-cdn.veclightyear.com/835a84d5/a82c4f94-bc59-48fd-b02c-2188bdc315d7.png" alt="Streamlit Authenticator logo" style="margin-top:50px;width:450px"></img>
一个用于管理Streamlit应用程序用户访问的安全认证模块
Streamlit-Authenticator通过PyPI分发:
pip install streamlit-authenticator
欢迎测试演示应用。
使用Streamlit-Authenticator非常简单,只需导入模块并调用它来验证用户的凭据。
import streamlit as st import streamlit_authenticator as stauth
credentials: usernames: jsmith: email: jsmith@gmail.com failed_login_attempts: 0 # 将自动管理 logged_in: False # 将自动管理 name: John Smith password: abc # 将自动哈希 rbriggs: email: rbriggs@gmail.com failed_login_attempts: 0 # 将自动管理 logged_in: False # 将自动管理 name: Rebecca Briggs password: def # 将自动哈希 cookie: expiry_days: 30 key: some_signature_key # 必须为字符串 name: some_cookie_name pre-authorized: emails: - melsby@gmail.com
import yaml from yaml.loader import SafeLoader with open('../config.yaml') as file: config = yaml.load(file, Loader=SafeLoader) # 一次性预哈希所有明文密码 # Hasher.hash_passwords(config['credentials']) authenticator = stauth.Authenticate( config['credentials'], config['cookie']['name'], config['cookie']['key'], config['cookie']['expiry_days'], config['pre-authorized'] )
Hasher.hash_passwords
参数:
- credentials: dict
- 包含明文密码的凭据字典。
返回:
- dict
- 包含哈希密码的凭据字典。
Authenticate
参数:
- credentials: dict
- 提供用户名、姓名、密码、电子邮件和其他用户数据。
- cookie_name: str
- 指定存储在客户端浏览器上用于无密码重新认证的cookie的名称。
- cookie_key: str
- 指定用于哈希重新认证cookie签名的密钥。
- cookie_expiry_days: float, 默认30.0
- 指定重新认证cookie在客户端浏览器上自动过期的天数。
- pre_authorized: list, 可选, 默认None
- 提供未注册但被授权注册的用户的电子邮件列表。
- validator: Validator, 可选, 默认None
- 提供一个验证器对象,用于检查用户名、姓名和电子邮件字段的有效性。
- auto_hash: bool, 默认True
- 密码自动哈希要求,True:明文密码将自动哈希,False:明文密码不会自动哈希。
authenticator.login()
Authenticate.login
参数:
- location: 字符串,{'main', 'sidebar', 'unrendered'},默认值为'main'
- 指定登录小部件的位置。
- max_concurrent_users: 整数,可选,默认值为None
- 限制并发用户数量。如果未指定,则不限制同时登录的用户数量。
- max_login_attempts: 整数,可选,默认值为None
- 限制失败登录尝试次数。如果未指定,则不限制失败登录尝试次数。
- fields: 字典,可选,默认值为{'Form name':'Login', 'Username':'Username', 'Password':'Password', 'Login':'Login'}
- 自定义标题、按钮和其他字段的文本。
- captcha: 布尔值,默认值为False
- 指定登录小部件的验证码要求,True:需要验证码,False:移除验证码。
- clear_on_submit: 布尔值,默认值为False
- 指定提交时清除设置,True:提交时清除输入,False:提交时保留输入。
- key: 字符串 ,默认值为'Login'
- 提供给小部件的唯一键,以避免重复的WidgetID错误。
- callback: 可调用对象,可选,默认值为None
- 可选的回调函数,在表单提交时调用,参数为一个字典。
- sleep_time: 浮点数,可选,默认值为None
- 登录小部件的可选睡眠时间。
返回值:
- 字符串
- 已认证用户的姓名。
- 布尔值
- 认证状态,None:未输入凭据,False:凭据不正确,True:凭据正确。
- 字符串
- 已认证用户的用户名。
if st.session_state['authentication_status']: authenticator.logout() st.write(f'欢迎 *{st.session_state["name"]}*') st.title('一些内容') elif st.session_state['authentication_status'] is False: st.error('用户名/密码不正确') elif st.session_state['authentication_status'] is None: st.warning('请输入您的用户名和密码')
Authenticate.logout
参数:
- button_name: 字符串,默认值为'Logout'
- 自定义按钮名称。
- location: 字符串,{'main', 'sidebar', 'unrendered'},默认值为'main'
- 指定注销按钮的位置。如果传入'unrendered',将执行注销逻辑而不渲染按钮。
- key: 字符串,默认值为None
- 应在多页应用程序中使用的唯一键。
- callback: 可调用对象,可选,默认值为None
- 可选的回调函数,在表单提交时调用,参数为一个字典。