Skip to content

Commit e828528

Browse files
authored
Added StreamLit as example project (#44)
* StreamLit as second example project
1 parent e14dbe0 commit e828528

20 files changed

+3463
-225
lines changed

examples/flask/poetry.lock

Lines changed: 401 additions & 221 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/flask/pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
2-
name = "passwordless-example"
3-
version = "0.1.1"
2+
name = "passwordless-example-flask"
3+
version = "1.0.0"
44
description = "Passwordless.dev Python SDK usage Example"
55
authors = [
66
"Bitwarden <[email protected]>"
@@ -11,8 +11,8 @@ maintainers = [
1111
readme = "README.md"
1212

1313
[tool.poetry.dependencies]
14-
python = "^3.8"
15-
passwordless = "^0"
14+
python = ">=3.8"
15+
passwordless = "^1"
1616
flask = "^2"
1717
flasgger = "^0"
1818
flask-marshmallow = "^0"

examples/streamlit/.gitignore

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# pdm
105+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106+
#pdm.lock
107+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108+
# in version control.
109+
# https://pdm.fming.dev/#use-with-ide
110+
.pdm.toml
111+
112+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
113+
__pypackages__/
114+
115+
# Celery stuff
116+
celerybeat-schedule
117+
celerybeat.pid
118+
119+
# SageMath parsed files
120+
*.sage.py
121+
122+
# Environments
123+
#.env
124+
.venv
125+
env/
126+
venv/
127+
ENV/
128+
env.bak/
129+
venv.bak/
130+
131+
# Spyder project settings
132+
.spyderproject
133+
.spyproject
134+
135+
# Rope project settings
136+
.ropeproject
137+
138+
# mkdocs documentation
139+
/site
140+
141+
# mypy
142+
.mypy_cache/
143+
.dmypy.json
144+
dmypy.json
145+
146+
# Pyre type checker
147+
.pyre/
148+
149+
# pytype static type analyzer
150+
.pytype/
151+
152+
# Cython debug symbols
153+
cython_debug/
154+
155+
# PyCharm
156+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
157+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
158+
# and can be added to the global gitignore or merged into this file. For a more nuclear
159+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
160+
.idea/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[browser]
2+
gatherUsageStats = false
3+
serverAddress="localhost"
4+
serverPort=8501
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
passwordless_api_url="https://v4.passwordless.dev"
2+
passwordless_api_key=""
3+
passwordless_api_secret=""

examples/streamlit/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Passwordless Python SDK Example
2+
3+
Please read the documentation here: https://docs.passwordless.dev
4+
5+
## Getting Started
6+
7+
This example uses *StreamLit* and provides UI to interact with
8+
the [Passwordless Python SDK][passwordless-python-sdk] to the *Passwordless API*.
9+
10+
Python 3.8 or newer is required to run the application.
11+
For frontend, install Node.
12+
13+
Install [Poetry][poetry] if not already installed.
14+
15+
Activate shell: `poetry shell`
16+
17+
Install dependencies: `poetry install --with dev`
18+
19+
1. Get your own API keys here: https://admin.passwordless.dev/signup
20+
2. Change the value of the Key and Secret in [.streamlit/secrets.toml file](.streamlit/secrets.toml)
21+
- (optional) In case of self-hosting, change the value of the Url with the base url where your *Passwordless API*
22+
instance is running.
23+
3. Build the application:
24+
```
25+
cd passwordless_auth/frontend
26+
npm install
27+
npm run build
28+
```
29+
4. Start the application `poetry run streamlit run app.py`
30+
5. The application will now listen on port `8501` e.g. http://localhost:8501, where you can *Sign In* and *Register*
31+
users within your Application.
32+
33+
### Preparing for production
34+
35+
1. Edit the `[browser]` section in the [config.toml](.streamlit%2Fconfig.toml) file:
36+
- Change `serverAddress` to your host name
37+
- Change `serverPort` to your port. For `https://` use 443.
38+
2. To run the application with `https://` scheme:
39+
1. Put reverse proxy with SSL in front of the application.
40+
2. Provide the path to the certificate and key files, by modifying `sslCertFile` and `sslKeyFile` in the
41+
`[server]` section of the [config.toml](.streamlit%2Fconfig.toml) file
42+
43+
Note: WebAuthn will only work with `https://` scheme with a valid, trusted certificate.
44+
45+
[passwordless-python-sdk]:https://github.com/bitwarden/passwordless-python
46+
47+
[poetry]:https://python-poetry.org/docs/#installation

examples/streamlit/__init__.py

Whitespace-only changes.

examples/streamlit/app.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import streamlit as st
2+
3+
st.set_page_config(page_title="Passwordless StreamLit example")
4+
st.title("Bitwarden Passwordless.dev - Python")
5+
6+
st.write(
7+
"[Documentation](https://docs.passwordless.dev) - [Get your API keys]("
8+
"https://admin.passwordless.dev/signup)"
9+
)
10+
11+
if (
12+
"passwordless_api_key" not in st.secrets
13+
or "passwordless_api_secret" not in st.secrets
14+
or "passwordless_api_url" not in st.secrets
15+
or st.secrets["passwordless_api_key"] == ""
16+
or st.secrets["passwordless_api_secret"] == ""
17+
or st.secrets["passwordless_api_url"] == ""
18+
):
19+
st.write(
20+
"To run this example you must first specify yor API Key "
21+
"and API Secret in _.streamlit/secrets.toml_ file."
22+
)
23+
24+
if "login_user" in st.session_state:
25+
st.write("## Logged in User: ", st.session_state.login_user)
26+
else:
27+
st.write("## Sign in to your account or register new account")

examples/streamlit/pages/1_sign_in.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import passwordless_api
2+
import streamlit as st
3+
from passwordless_auth import passwordless_auth_component
4+
5+
st.set_page_config(page_title="Sign In")
6+
st.title("Sign In")
7+
8+
st.text_input("Alias", key="alias")
9+
10+
if st.button("Login"):
11+
if "auth_error" in st.session_state:
12+
del st.session_state.auth_error
13+
st.session_state.login_init = True
14+
15+
if "login_init" in st.session_state:
16+
auth_result = passwordless_auth_component(
17+
"passwordless_auth_login",
18+
{
19+
"api_url": st.secrets["passwordless_api_url"],
20+
"api_key": st.secrets["passwordless_api_key"],
21+
"type": "login",
22+
"alias": st.session_state.alias,
23+
},
24+
)
25+
print(auth_result)
26+
27+
if auth_result is not None:
28+
29+
if "token" in auth_result:
30+
st.session_state.login_user = passwordless_api.login(
31+
auth_result["token"]
32+
)
33+
else:
34+
st.session_state.auth_error = auth_result
35+
36+
del st.session_state.login_init
37+
38+
if "login_user" in st.session_state:
39+
st.write("## User: ", st.session_state.login_user)
40+
41+
if "auth_error" in st.session_state:
42+
st.write("## Error: ", st.session_state.auth_error)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import passwordless_api
2+
import streamlit as st
3+
from passwordless_auth import passwordless_auth_component
4+
5+
st.set_page_config(page_title="Login")
6+
st.title("Register")
7+
8+
st.text_input("Username", key="username")
9+
st.text_input("Alias", key="alias")
10+
11+
if st.button("Register"):
12+
if "auth_error" in st.session_state:
13+
del st.session_state.auth_error
14+
15+
registered_token = passwordless_api.register(
16+
st.session_state.username, st.session_state.alias
17+
)
18+
19+
st.session_state.register_token = registered_token.token
20+
21+
if "register_token" in st.session_state:
22+
auth_result = passwordless_auth_component(
23+
"passwordless_auth_register",
24+
data={
25+
"api_url": st.secrets["passwordless_api_url"],
26+
"api_key": st.secrets["passwordless_api_key"],
27+
"type": "register",
28+
"token": st.session_state.register_token,
29+
},
30+
)
31+
print(auth_result)
32+
33+
if auth_result is not None:
34+
35+
if "token" in auth_result:
36+
st.session_state.register_auth_result = auth_result
37+
else:
38+
st.session_state.auth_error = auth_result
39+
40+
del st.session_state.register_token
41+
42+
if "register_auth_result" in st.session_state:
43+
st.write("## Result: ", st.session_state.register_auth_result)
44+
45+
if "auth_error" in st.session_state:
46+
st.write("## Error: ", st.session_state.auth_error)

0 commit comments

Comments
 (0)