fastapi-zitadel-auth 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. fastapi_zitadel_auth-0.1.0/.codecov.yml +16 -0
  2. fastapi_zitadel_auth-0.1.0/.github/dependabot.yml +19 -0
  3. fastapi_zitadel_auth-0.1.0/.github/workflows/codeql.yml +54 -0
  4. fastapi_zitadel_auth-0.1.0/.github/workflows/publish.yml +39 -0
  5. fastapi_zitadel_auth-0.1.0/.github/workflows/test.yml +37 -0
  6. fastapi_zitadel_auth-0.1.0/.gitignore +166 -0
  7. fastapi_zitadel_auth-0.1.0/.pre-commit-config.yaml +25 -0
  8. fastapi_zitadel_auth-0.1.0/.python-version +1 -0
  9. fastapi_zitadel_auth-0.1.0/LICENCE +21 -0
  10. fastapi_zitadel_auth-0.1.0/PKG-INFO +178 -0
  11. fastapi_zitadel_auth-0.1.0/README.md +153 -0
  12. fastapi_zitadel_auth-0.1.0/demo_project/.env.example +8 -0
  13. fastapi_zitadel_auth-0.1.0/demo_project/__init__.py +3 -0
  14. fastapi_zitadel_auth-0.1.0/demo_project/dependencies.py +28 -0
  15. fastapi_zitadel_auth-0.1.0/demo_project/server.py +62 -0
  16. fastapi_zitadel_auth-0.1.0/demo_project/service_user.py +85 -0
  17. fastapi_zitadel_auth-0.1.0/demo_project/settings.py +24 -0
  18. fastapi_zitadel_auth-0.1.0/docs/CONTRIBUTING.md +13 -0
  19. fastapi_zitadel_auth-0.1.0/docs/ZITADEL_SETUP.md +31 -0
  20. fastapi_zitadel_auth-0.1.0/pyproject.toml +67 -0
  21. fastapi_zitadel_auth-0.1.0/src/fastapi_zitadel_auth/__init__.py +10 -0
  22. fastapi_zitadel_auth-0.1.0/src/fastapi_zitadel_auth/auth.py +154 -0
  23. fastapi_zitadel_auth-0.1.0/src/fastapi_zitadel_auth/config.py +39 -0
  24. fastapi_zitadel_auth-0.1.0/src/fastapi_zitadel_auth/exceptions.py +14 -0
  25. fastapi_zitadel_auth-0.1.0/src/fastapi_zitadel_auth/jwks.py +37 -0
  26. fastapi_zitadel_auth-0.1.0/src/fastapi_zitadel_auth/models.py +56 -0
  27. fastapi_zitadel_auth-0.1.0/src/fastapi_zitadel_auth/py.typed +0 -0
  28. fastapi_zitadel_auth-0.1.0/src/fastapi_zitadel_auth/token.py +45 -0
  29. fastapi_zitadel_auth-0.1.0/tests/.env.test +3 -0
  30. fastapi_zitadel_auth-0.1.0/tests/__init__.py +0 -0
  31. fastapi_zitadel_auth-0.1.0/tests/conftest.py +97 -0
  32. fastapi_zitadel_auth-0.1.0/tests/test_app.py +135 -0
  33. fastapi_zitadel_auth-0.1.0/tests/test_auth.py +362 -0
  34. fastapi_zitadel_auth-0.1.0/tests/test_config.py +92 -0
  35. fastapi_zitadel_auth-0.1.0/tests/test_jwks.py +87 -0
  36. fastapi_zitadel_auth-0.1.0/tests/test_models.py +224 -0
  37. fastapi_zitadel_auth-0.1.0/tests/test_token.py +282 -0
  38. fastapi_zitadel_auth-0.1.0/tests/utils.py +45 -0
  39. fastapi_zitadel_auth-0.1.0/uv.lock +861 -0
@@ -0,0 +1,16 @@
1
+ codecov:
2
+ require_ci_to_pass: yes
3
+
4
+ coverage:
5
+ precision: 1
6
+ round: down
7
+ status:
8
+ project:
9
+ default:
10
+ target: auto
11
+ patch: no
12
+ changes: no
13
+
14
+ comment:
15
+ layout: "diff,files"
16
+ require_changes: yes
@@ -0,0 +1,19 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "github-actions"
4
+ directory: "/"
5
+ schedule:
6
+ interval: "weekly"
7
+ time: "09:00"
8
+ timezone: "Europe/Zurich"
9
+ commit-message:
10
+ prefix: "deps(actions):"
11
+
12
+ # uv dependabot for uv.lock not yet available, see https://docs.astral.sh/uv/guides/integration/dependency-bots/#dependabot
13
+ # - package-ecosystem: "pip"
14
+ # schedule:
15
+ # interval: "weekly"
16
+ # time: "09:00"
17
+ # timezone: "Europe/Zurich"
18
+ # commit-message:
19
+ # prefix: "deps(api):"
@@ -0,0 +1,54 @@
1
+ name: "CodeQL"
2
+
3
+ on:
4
+ push:
5
+ branches: [ "main" ]
6
+ schedule:
7
+ - cron: '15 7 * * 1' # 07:15 Monday
8
+
9
+
10
+ jobs:
11
+ analyze:
12
+ name: Analyze (${{ matrix.language }})
13
+ runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
14
+ permissions:
15
+ # required for all workflows
16
+ security-events: write
17
+
18
+ # required to fetch internal or private CodeQL packs
19
+ packages: read
20
+
21
+ # only required for workflows in private repositories
22
+ actions: read
23
+ contents: read
24
+
25
+ strategy:
26
+ fail-fast: false
27
+ matrix:
28
+ include:
29
+ - language: python
30
+ build-mode: none
31
+ steps:
32
+ - name: Checkout repository
33
+ uses: actions/checkout@v4
34
+
35
+ - name: Initialize CodeQL
36
+ uses: github/codeql-action/init@v3
37
+ with:
38
+ languages: ${{ matrix.language }}
39
+ build-mode: ${{ matrix.build-mode }}
40
+
41
+ - if: matrix.build-mode == 'manual'
42
+ shell: bash
43
+ run: |
44
+ echo 'If you are using a "manual" build mode for one or more of the' \
45
+ 'languages you are analyzing, replace this with the commands to build' \
46
+ 'your code, for example:'
47
+ echo ' make bootstrap'
48
+ echo ' make release'
49
+ exit 1
50
+
51
+ - name: Perform CodeQL Analysis
52
+ uses: github/codeql-action/analyze@v3
53
+ with:
54
+ category: "/language:${{matrix.language}}"
@@ -0,0 +1,39 @@
1
+ name: Publish
2
+
3
+ on:
4
+ release:
5
+ types: [ published ]
6
+
7
+ permissions:
8
+ id-token: write # Required for trusted publishing
9
+ contents: read # Required for checkout
10
+
11
+ jobs:
12
+ run:
13
+ name: Build and publish release
14
+ runs-on: ubuntu-latest
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+
18
+ - name: Install uv
19
+ uses: astral-sh/setup-uv@v4
20
+ with:
21
+ enable-cache: true
22
+ cache-dependency-glob: uv.lock
23
+
24
+ - name: Set up Python
25
+ run: uv python install 3.12
26
+
27
+ - name: Build
28
+ run: uv build
29
+
30
+ # Using Trusted Publishing via PyPI and uv
31
+ - name: Debug and Publish
32
+ run: |
33
+ echo "Current directory: $(pwd)"
34
+ echo "Workflow file contents:"
35
+ cat .github/workflows/publish.yml
36
+ echo "Environment variables:"
37
+ env | grep -i action
38
+ echo "Running publish command..."
39
+ uv publish --verbose --trusted-publishing always
@@ -0,0 +1,37 @@
1
+ name: Test
2
+
3
+ on: pull_request
4
+
5
+ jobs:
6
+ test:
7
+ name: Run Tests
8
+ runs-on: ubuntu-latest
9
+ strategy:
10
+ matrix:
11
+ python-version: ["3.10", "3.11", "3.12", "3.13"]
12
+
13
+ steps:
14
+ - uses: actions/checkout@v4
15
+
16
+ - name: Install uv
17
+ uses: astral-sh/setup-uv@v4
18
+ with:
19
+ enable-cache: true
20
+ cache-dependency-glob: "uv.lock"
21
+
22
+ - name: Set up Python ${{ matrix.python-version }}
23
+ run: uv python install ${{ matrix.python-version }}
24
+
25
+ - name: Install dependencies
26
+ run: |
27
+ uv sync --dev
28
+
29
+ - name: Run tests
30
+ run: |
31
+ uv run pytest tests/ -v --cov=src --cov-report=xml
32
+
33
+ - name: Upload coverage reports to Codecov
34
+ uses: codecov/codecov-action@v5
35
+ with:
36
+ fail_ci_if_error: true
37
+ token: ${{ secrets.CODECOV_TOKEN }}
@@ -0,0 +1,166 @@
1
+ .idea/
2
+ # private key file from Zitadel
3
+ *.json
4
+
5
+ # Byte-compiled / optimized / DLL files
6
+ __pycache__/
7
+ *.py[cod]
8
+ *$py.class
9
+
10
+ # C extensions
11
+ *.so
12
+
13
+ # Distribution / packaging
14
+ .Python
15
+ build/
16
+ develop-eggs/
17
+ dist/
18
+ downloads/
19
+ eggs/
20
+ .eggs/
21
+ lib/
22
+ lib64/
23
+ parts/
24
+ sdist/
25
+ var/
26
+ wheels/
27
+ share/python-wheels/
28
+ *.egg-info/
29
+ .installed.cfg
30
+ *.egg
31
+ MANIFEST
32
+
33
+ # PyInstaller
34
+ # Usually these files are written by a python script from a template
35
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
36
+ *.manifest
37
+ *.spec
38
+
39
+ # Installer logs
40
+ pip-log.txt
41
+ pip-delete-this-directory.txt
42
+
43
+ # Unit test / coverage reports
44
+ htmlcov/
45
+ .tox/
46
+ .nox/
47
+ .coverage
48
+ .coverage.*
49
+ .cache
50
+ nosetests.xml
51
+ coverage.xml
52
+ *.cover
53
+ *.py,cover
54
+ .hypothesis/
55
+ .pytest_cache/
56
+ cover/
57
+
58
+ # Translations
59
+ *.mo
60
+ *.pot
61
+
62
+ # Django stuff:
63
+ *.log
64
+ local_settings.py
65
+ db.sqlite3
66
+ db.sqlite3-journal
67
+
68
+ # Flask stuff:
69
+ instance/
70
+ .webassets-cache
71
+
72
+ # Scrapy stuff:
73
+ .scrapy
74
+
75
+ # Sphinx documentation
76
+ docs/_build/
77
+
78
+ # PyBuilder
79
+ .pybuilder/
80
+ target/
81
+
82
+ # Jupyter Notebook
83
+ .ipynb_checkpoints
84
+
85
+ # IPython
86
+ profile_default/
87
+ ipython_config.py
88
+
89
+ # pyenv
90
+ # For a library or package, you might want to ignore these files since the code is
91
+ # intended to run in multiple environments; otherwise, check them in:
92
+ # .python-version
93
+
94
+ # pipenv
95
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
96
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
97
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
98
+ # install all needed dependencies.
99
+ #Pipfile.lock
100
+
101
+ # poetry
102
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
103
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
104
+ # commonly ignored for libraries.
105
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
106
+ #poetry.lock
107
+
108
+ # pdm
109
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
110
+ #pdm.lock
111
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
112
+ # in version control.
113
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
114
+ .pdm.toml
115
+ .pdm-python
116
+ .pdm-build/
117
+
118
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
119
+ __pypackages__/
120
+
121
+ # Celery stuff
122
+ celerybeat-schedule
123
+ celerybeat.pid
124
+
125
+ # SageMath parsed files
126
+ *.sage.py
127
+
128
+ # Environments
129
+ .env
130
+ .venv
131
+ env/
132
+ venv/
133
+ ENV/
134
+ env.bak/
135
+ venv.bak/
136
+
137
+ # Spyder project settings
138
+ .spyderproject
139
+ .spyproject
140
+
141
+ # Rope project settings
142
+ .ropeproject
143
+
144
+ # mkdocs documentation
145
+ /site
146
+
147
+ # mypy
148
+ .mypy_cache/
149
+ .dmypy.json
150
+ dmypy.json
151
+
152
+ # Pyre type checker
153
+ .pyre/
154
+
155
+ # pytype static type analyzer
156
+ .pytype/
157
+
158
+ # Cython debug symbols
159
+ cython_debug/
160
+
161
+ # PyCharm
162
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
163
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
164
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
165
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
166
+ #.idea/
@@ -0,0 +1,25 @@
1
+ exclude: README.md
2
+ repos:
3
+ - repo: https://github.com/astral-sh/ruff-pre-commit
4
+ rev: v0.8.1
5
+ hooks:
6
+ - id: ruff
7
+ entry: uv run ruff check src/
8
+ files: \.py$
9
+ - id: ruff-format
10
+ entry: uv run ruff format src/
11
+ files: \.py$
12
+
13
+ - repo: https://github.com/econchick/interrogate
14
+ rev: 1.7.0
15
+ hooks:
16
+ - id: interrogate
17
+ args: [-vv, tests]
18
+
19
+ - repo: https://github.com/pre-commit/pre-commit-hooks
20
+ rev: v5.0.0
21
+ hooks:
22
+ - id: trailing-whitespace
23
+ - id: end-of-file-fixer
24
+ - id: check-toml
25
+ - id: check-yaml
@@ -0,0 +1 @@
1
+ 3.12
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Clean Energy Exchange GmbH
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,178 @@
1
+ Metadata-Version: 2.3
2
+ Name: fastapi-zitadel-auth
3
+ Version: 0.1.0
4
+ Summary: Zitadel authentication for FastAPI
5
+ Author-email: Clean Energy Exchange <info@ceex.ch>
6
+ Keywords: async,asyncio,authentication,fastapi,oauth,oidc,zitadel
7
+ Classifier: Development Status :: 4 - Beta
8
+ Classifier: Environment :: Web Environment
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: Python
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Topic :: Software Development
15
+ Classifier: Topic :: Software Development :: Libraries
16
+ Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
17
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
18
+ Requires-Python: >=3.10
19
+ Requires-Dist: cachetools>=5.5.0
20
+ Requires-Dist: cryptography>=43.0.3
21
+ Requires-Dist: fastapi>=0.115.4
22
+ Requires-Dist: httpx>=0.27.2
23
+ Requires-Dist: pyjwt>=2.9.0
24
+ Description-Content-Type: text/markdown
25
+
26
+ # FastAPI Zitadel Auth
27
+
28
+ FastAPI Zitadel Auth is a Python package that simplifies OAuth2/OIDC authentication in FastAPI applications
29
+ using [Zitadel](https://zitadel.com/) as the identity provider.
30
+ It handles token validation, role-based access control, and Swagger UI integration with just a few lines of code.
31
+
32
+ <a href="https://github.com/cleanenergyexchange/fastapi-zitadel-auth/actions/workflows/test.yml" target="_blank">
33
+ <img src="https://github.com/cleanenergyexchange/fastapi-zitadel-auth/actions/workflows/test.yml/badge.svg" alt="Test status">
34
+ </a>
35
+ <a href="https://codecov.io/gh/cleanenergyexchange/fastapi-zitadel-auth">
36
+ <img src="https://codecov.io/gh/cleanenergyexchange/fastapi-zitadel-auth/graph/badge.svg?token=A3TSXDVLQT" alt="Code coverage"/>
37
+ </a>
38
+ <a href="https://pypi.org/project/fastapi-zitadel-auth" target="_blank">
39
+ <img src="https://img.shields.io/pypi/pyversions/fastapi-zitadel-auth.svg?color=%2334D058" alt="Supported Python versions">
40
+ </a>
41
+ <a href="https://pypi.org/pypi/fastapi-zitadel-auth">
42
+ <img src="https://img.shields.io/pypi/v/fastapi-zitadel-auth.svg?logo=pypi&logoColor=white&label=pypi" alt="Package version">
43
+ </a>
44
+
45
+ ## Features
46
+
47
+ * Authorization Code Flow with PKCE
48
+ * JWT signature validation using JWKS obtained from Zitadel
49
+ * Service User authentication using JWT Profiles
50
+ * Swagger UI integration
51
+ * Zitadel roles as scopes
52
+
53
+
54
+ > [!NOTE]
55
+ > This library implements JWT, locally validated using JWKS, as it prioritizes performance,
56
+ > see [Zitadel docs on Opaque tokens vs JWT](https://zitadel.com/docs/concepts/knowledge/opaque-tokens#use-cases-and-trade-offs).
57
+ > If you need to validate opaque tokens using Introspection, please open an issue – PRs are welcome!
58
+
59
+
60
+ ## Installation and quick start
61
+
62
+ ```bash
63
+ pip install fastapi-zitadel-auth
64
+ ```
65
+
66
+ ```python
67
+ from fastapi import FastAPI, Security
68
+ from fastapi_zitadel_auth import ZitadelAuth, AuthConfig
69
+
70
+ auth = ZitadelAuth(AuthConfig(
71
+ client_id="your-client-id",
72
+ project_id="your-project-id",
73
+ base_url="https://your-instance.zitadel.cloud"
74
+ ))
75
+
76
+ app = FastAPI()
77
+
78
+ @app.get("/protected", dependencies=[Security(auth)])
79
+ def protected_route():
80
+ return {"message": "Access granted!"}
81
+ ```
82
+
83
+ See the [Usage](#usage) section for more details.
84
+
85
+ ## Usage
86
+
87
+ ### Configuration
88
+
89
+ #### Zitadel
90
+
91
+ Set up a new OAuth2 client in Zitadel according to the [docs/ZITADEL_SETUP.md](docs/ZITADEL_SETUP.md).
92
+
93
+ #### FastAPI
94
+
95
+ ```python
96
+ from fastapi import FastAPI, Request, Security
97
+ from fastapi_zitadel_auth import ZitadelAuth, AuthConfig
98
+
99
+ # Your Zitadel configuration
100
+ CLIENT_ID = 'your-zitadel-client-id'
101
+ PROJECT_ID = 'your-zitadel-project-id'
102
+ BASE_URL = 'https://your-instance-xyz.zitadel.cloud'
103
+
104
+ # Create an AuthConfig object with your Zitadel configuration
105
+ config = AuthConfig(
106
+ client_id=CLIENT_ID,
107
+ project_id=PROJECT_ID,
108
+ base_url=BASE_URL,
109
+ scopes={
110
+ "openid": "OpenID Connect",
111
+ "email": "Email",
112
+ "profile": "Profile",
113
+ "urn:zitadel:iam:org:project:id:zitadel:aud": "Audience",
114
+ "urn:zitadel:iam:org:projects:roles": "Roles",
115
+ },
116
+ )
117
+
118
+ # Create a ZitadelAuth object with the AuthConfig usable as a FastAPI dependency
119
+ auth = ZitadelAuth(config)
120
+
121
+ # Create a FastAPI app and configure Swagger UI
122
+ app = FastAPI(
123
+ title="fastapi-zitadel-auth demo",
124
+ swagger_ui_oauth2_redirect_url="/oauth2-redirect",
125
+ swagger_ui_init_oauth={
126
+ "usePkceWithAuthorizationCodeGrant": True,
127
+ "clientId": CLIENT_ID,
128
+ "scopes": " ".join(
129
+ [
130
+ "openid",
131
+ "email",
132
+ "profile",
133
+ "urn:zitadel:iam:org:project:id:zitadel:aud",
134
+ "urn:zitadel:iam:org:projects:roles",
135
+ ]
136
+ ),
137
+ },
138
+ )
139
+
140
+ # Create an endpoint and protect it with the ZitadelAuth dependency
141
+ @app.get(
142
+ "/api/private",
143
+ summary="Private endpoint, requiring a valid token with `system` scope",
144
+ dependencies=[Security(auth, scopes=["system"])],
145
+ )
146
+ def private(request: Request):
147
+ return {
148
+ "message": f"Hello, protected world! Here is Zitadel user {request.state.user.user_id}"
149
+ }
150
+
151
+ ```
152
+
153
+ ## Demo app
154
+
155
+ See `demo_project` for a complete example, including service user login. To run the demo app:
156
+
157
+ ```bash
158
+ uv run demo_project/server.py
159
+ ```
160
+
161
+ Then navigate to `http://localhost:8001/docs` to see the Swagger UI.
162
+
163
+
164
+ ### Service user
165
+
166
+ Service users are "machine users" in Zitadel.
167
+
168
+ To log in as a service user, change the config in `demo_project/service_user.py`, then
169
+
170
+ ```bash
171
+ uv run demo_project/service_user.py
172
+ ```
173
+
174
+ Make sure you have a running server at `http://localhost:8001`.
175
+
176
+ ## Development
177
+
178
+ See [docs/CONTRIBUTING.md](docs/CONTRIBUTING.md) for development instructions.