obi-auth 0.0.1__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.
- obi_auth-0.0.1/.github/workflows/sdist.yml +48 -0
- obi_auth-0.0.1/.github/workflows/tox.yml +49 -0
- obi_auth-0.0.1/.gitignore +13 -0
- obi_auth-0.0.1/CHANGELOG.rst +21 -0
- obi_auth-0.0.1/LICENSE.txt +201 -0
- obi_auth-0.0.1/PKG-INFO +75 -0
- obi_auth-0.0.1/README.md +52 -0
- obi_auth-0.0.1/pyproject.toml +132 -0
- obi_auth-0.0.1/setup.cfg +4 -0
- obi_auth-0.0.1/src/obi_auth/__init__.py +5 -0
- obi_auth-0.0.1/src/obi_auth/client.py +23 -0
- obi_auth-0.0.1/src/obi_auth/config.py +45 -0
- obi_auth-0.0.1/src/obi_auth/exception.py +21 -0
- obi_auth-0.0.1/src/obi_auth/flow.py +89 -0
- obi_auth-0.0.1/src/obi_auth/server.py +86 -0
- obi_auth-0.0.1/src/obi_auth/typedef.py +16 -0
- obi_auth-0.0.1/src/obi_auth.egg-info/PKG-INFO +75 -0
- obi_auth-0.0.1/src/obi_auth.egg-info/SOURCES.txt +27 -0
- obi_auth-0.0.1/src/obi_auth.egg-info/dependency_links.txt +1 -0
- obi_auth-0.0.1/src/obi_auth.egg-info/requires.txt +5 -0
- obi_auth-0.0.1/src/obi_auth.egg-info/top_level.txt +1 -0
- obi_auth-0.0.1/tests/__init__.py +1 -0
- obi_auth-0.0.1/tests/unit/__init__.py +1 -0
- obi_auth-0.0.1/tests/unit/data/.gitignore +0 -0
- obi_auth-0.0.1/tests/unit/test_client.py +34 -0
- obi_auth-0.0.1/tests/unit/test_config.py +57 -0
- obi_auth-0.0.1/tests/unit/test_flow.py +45 -0
- obi_auth-0.0.1/tests/unit/test_server.py +46 -0
- obi_auth-0.0.1/tox.ini +49 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
name: PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_dispatch:
|
|
5
|
+
pull_request:
|
|
6
|
+
push:
|
|
7
|
+
branches:
|
|
8
|
+
- main
|
|
9
|
+
release:
|
|
10
|
+
types:
|
|
11
|
+
- published
|
|
12
|
+
|
|
13
|
+
jobs:
|
|
14
|
+
|
|
15
|
+
build_sdist:
|
|
16
|
+
name: Build sdist
|
|
17
|
+
runs-on: ubuntu-latest
|
|
18
|
+
steps:
|
|
19
|
+
- uses: actions/checkout@v4
|
|
20
|
+
|
|
21
|
+
- name: Build SDist
|
|
22
|
+
run: pipx run build --sdist
|
|
23
|
+
|
|
24
|
+
- name: Check metadata
|
|
25
|
+
run: pipx run twine check dist/*
|
|
26
|
+
|
|
27
|
+
- uses: actions/upload-artifact@v4
|
|
28
|
+
with:
|
|
29
|
+
name: build
|
|
30
|
+
path: dist
|
|
31
|
+
|
|
32
|
+
upload_sdist:
|
|
33
|
+
name: Upload sdist
|
|
34
|
+
needs: [build_sdist]
|
|
35
|
+
runs-on: ubuntu-latest
|
|
36
|
+
permissions:
|
|
37
|
+
# IMPORTANT: this permission is mandatory for trusted publishing
|
|
38
|
+
id-token: write
|
|
39
|
+
if: github.event_name == 'release' && github.event.action == 'published'
|
|
40
|
+
|
|
41
|
+
steps:
|
|
42
|
+
- uses: actions/download-artifact@v4
|
|
43
|
+
with:
|
|
44
|
+
name: build
|
|
45
|
+
path: dist
|
|
46
|
+
|
|
47
|
+
- name: Publish package distributions to PyPI
|
|
48
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
name: Run tox
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
pull_request:
|
|
5
|
+
push:
|
|
6
|
+
branches:
|
|
7
|
+
- main
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
tests:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
strategy:
|
|
13
|
+
matrix:
|
|
14
|
+
include:
|
|
15
|
+
- python-version: '3.11'
|
|
16
|
+
tox-env: py311
|
|
17
|
+
- python-version: '3.11'
|
|
18
|
+
tox-env: lint
|
|
19
|
+
- python-version: '3.12'
|
|
20
|
+
tox-env: py312
|
|
21
|
+
- python-version: '3.13'
|
|
22
|
+
tox-env: py313
|
|
23
|
+
codecov: codecov
|
|
24
|
+
- python-version: '3.13'
|
|
25
|
+
tox-env: check-packaging
|
|
26
|
+
steps:
|
|
27
|
+
- name: Checkout code
|
|
28
|
+
uses: actions/checkout@v4
|
|
29
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
30
|
+
uses: actions/setup-python@v5
|
|
31
|
+
with:
|
|
32
|
+
python-version: ${{ matrix.python-version }}
|
|
33
|
+
- name: Install dependencies
|
|
34
|
+
run: |
|
|
35
|
+
python -m pip install --upgrade pip setuptools wheel
|
|
36
|
+
python -m pip install --upgrade tox tox-uv
|
|
37
|
+
- name: Run tox
|
|
38
|
+
run: |
|
|
39
|
+
tox run -e ${{ matrix.tox-env }}
|
|
40
|
+
- name: Upload to codecov
|
|
41
|
+
if: ${{ matrix.codecov == 'codecov' }}
|
|
42
|
+
uses: codecov/codecov-action@v4
|
|
43
|
+
with:
|
|
44
|
+
token: ${{ secrets.CODECOV_TOKEN }}
|
|
45
|
+
fail_ci_if_error: false
|
|
46
|
+
files: ./coverage.xml
|
|
47
|
+
flags: pytest
|
|
48
|
+
verbose: true
|
|
49
|
+
name: "${{ github.repository }}-${{ matrix.tox-env }}"
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Changelog
|
|
2
|
+
=========
|
|
3
|
+
|
|
4
|
+
Version 0.1
|
|
5
|
+
-----------
|
|
6
|
+
|
|
7
|
+
New Features
|
|
8
|
+
~~~~~~~~~~~~
|
|
9
|
+
- Example code in Python
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
Bug Fixes
|
|
13
|
+
~~~~~~~~~
|
|
14
|
+
- Describe Bug Fixes here
|
|
15
|
+
- Remove this subsection if there are no Bug Fixes
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
Improvements
|
|
19
|
+
~~~~~~~~~~~~
|
|
20
|
+
- Describe Improvements here
|
|
21
|
+
- Remove this subsection if there are no Improvements
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
obi_auth-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: obi_auth
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Python library for retrieving Keycloak access tokens interactively
|
|
5
|
+
Author-email: Open Brain Institute <info@openbraininstitute.org>
|
|
6
|
+
Maintainer-email: Open Brain Institute <info@openbraininstitute.org>
|
|
7
|
+
License: Apache-2.0
|
|
8
|
+
Project-URL: documentation, https://github.com/openbraininstitute/obi-auth
|
|
9
|
+
Project-URL: repository, https://github.com/openbraininstitute/obi-auth
|
|
10
|
+
Project-URL: changelog, https://github.com/openbraininstitute/obi-auth/CHANGELOG.rst
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Requires-Python: >=3.11
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE.txt
|
|
17
|
+
Requires-Dist: httpx
|
|
18
|
+
Requires-Dist: pydantic
|
|
19
|
+
Requires-Dist: pydantic-settings
|
|
20
|
+
Requires-Dist: uvicorn
|
|
21
|
+
Requires-Dist: fastapi
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
24
|
+
[![Build status][build_status_badge]][build_status_target]
|
|
25
|
+
[![License][license_badge]][license_target]
|
|
26
|
+
[![Code coverage][coverage_badge]][coverage_target]
|
|
27
|
+
[![CodeQL][codeql_badge]][codeql_target]
|
|
28
|
+
[![PyPI][pypi_badge]][pypi_target]
|
|
29
|
+
|
|
30
|
+
# obi-auth
|
|
31
|
+
|
|
32
|
+
obi-auth is a library for retrieving Keycloak access tokens interactively. It helps developers and testers quickly authenticate against Keycloak without writing scripts or configuring complex clients.
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
pip install obi-auth
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Examples
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from obi_auth import get_token
|
|
45
|
+
|
|
46
|
+
access_token = get_token(environment="staging")
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
|
|
51
|
+
Copyright (c) 2025 Open Brain Institute
|
|
52
|
+
|
|
53
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
54
|
+
you may not use this file except in compliance with the License.
|
|
55
|
+
You may obtain a copy of the License at
|
|
56
|
+
|
|
57
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
58
|
+
|
|
59
|
+
Unless required by applicable law or agreed to in writing, software
|
|
60
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
61
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
62
|
+
See the License for the specific language governing permissions and
|
|
63
|
+
limitations under the License.
|
|
64
|
+
|
|
65
|
+
[build_status_badge]: https://github.com/openbraininstitute/obi-auth/actions/workflows/tox.yml/badge.svg
|
|
66
|
+
[build_status_target]: https://github.com/openbraininstitute/obi-auth/actions
|
|
67
|
+
[license_badge]: https://img.shields.io/pypi/l/obi-auth
|
|
68
|
+
[license_target]: https://github.com/openbraininstitute/obi-auth/blob/main/LICENSE.txt
|
|
69
|
+
[coverage_badge]: https://codecov.io/github/openbraininstitute/obi-auth/coverage.svg?branch=main
|
|
70
|
+
[coverage_target]: https://codecov.io/github/openbraininstitute/obi-auth?branch=main
|
|
71
|
+
[codeql_badge]: https://github.com/openbraininstitute/obi-auth/actions/workflows/github-code-scanning/codeql/badge.svg
|
|
72
|
+
[codeql_target]: https://github.com/openbraininstitute/obi-auth/actions/workflows/github-code-scanning/codeql
|
|
73
|
+
[pypi_badge]: https://github.com/openbraininstitute/obi-auth/actions/workflows/sdist.yml/badge.svg
|
|
74
|
+
[pypi_target]: https://pypi.org/project/obi-auth/
|
|
75
|
+
|
obi_auth-0.0.1/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[![Build status][build_status_badge]][build_status_target]
|
|
2
|
+
[![License][license_badge]][license_target]
|
|
3
|
+
[![Code coverage][coverage_badge]][coverage_target]
|
|
4
|
+
[![CodeQL][codeql_badge]][codeql_target]
|
|
5
|
+
[![PyPI][pypi_badge]][pypi_target]
|
|
6
|
+
|
|
7
|
+
# obi-auth
|
|
8
|
+
|
|
9
|
+
obi-auth is a library for retrieving Keycloak access tokens interactively. It helps developers and testers quickly authenticate against Keycloak without writing scripts or configuring complex clients.
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
pip install obi-auth
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Examples
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
from obi_auth import get_token
|
|
22
|
+
|
|
23
|
+
access_token = get_token(environment="staging")
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## License
|
|
27
|
+
|
|
28
|
+
Copyright (c) 2025 Open Brain Institute
|
|
29
|
+
|
|
30
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
31
|
+
you may not use this file except in compliance with the License.
|
|
32
|
+
You may obtain a copy of the License at
|
|
33
|
+
|
|
34
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
35
|
+
|
|
36
|
+
Unless required by applicable law or agreed to in writing, software
|
|
37
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
38
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
39
|
+
See the License for the specific language governing permissions and
|
|
40
|
+
limitations under the License.
|
|
41
|
+
|
|
42
|
+
[build_status_badge]: https://github.com/openbraininstitute/obi-auth/actions/workflows/tox.yml/badge.svg
|
|
43
|
+
[build_status_target]: https://github.com/openbraininstitute/obi-auth/actions
|
|
44
|
+
[license_badge]: https://img.shields.io/pypi/l/obi-auth
|
|
45
|
+
[license_target]: https://github.com/openbraininstitute/obi-auth/blob/main/LICENSE.txt
|
|
46
|
+
[coverage_badge]: https://codecov.io/github/openbraininstitute/obi-auth/coverage.svg?branch=main
|
|
47
|
+
[coverage_target]: https://codecov.io/github/openbraininstitute/obi-auth?branch=main
|
|
48
|
+
[codeql_badge]: https://github.com/openbraininstitute/obi-auth/actions/workflows/github-code-scanning/codeql/badge.svg
|
|
49
|
+
[codeql_target]: https://github.com/openbraininstitute/obi-auth/actions/workflows/github-code-scanning/codeql
|
|
50
|
+
[pypi_badge]: https://github.com/openbraininstitute/obi-auth/actions/workflows/sdist.yml/badge.svg
|
|
51
|
+
[pypi_target]: https://pypi.org/project/obi-auth/
|
|
52
|
+
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=42", "setuptools-scm[toml]>=6.2"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "obi_auth"
|
|
7
|
+
dependencies = [
|
|
8
|
+
"httpx",
|
|
9
|
+
"pydantic",
|
|
10
|
+
"pydantic-settings",
|
|
11
|
+
"uvicorn",
|
|
12
|
+
"fastapi"
|
|
13
|
+
]
|
|
14
|
+
authors = [
|
|
15
|
+
{name = "Open Brain Institute", email = "info@openbraininstitute.org"}
|
|
16
|
+
]
|
|
17
|
+
maintainers = [
|
|
18
|
+
{name = "Open Brain Institute", email = "info@openbraininstitute.org"}
|
|
19
|
+
]
|
|
20
|
+
description = "Python library for retrieving Keycloak access tokens interactively"
|
|
21
|
+
readme = "README.md"
|
|
22
|
+
requires-python = ">=3.11"
|
|
23
|
+
license = {text = "Apache-2.0"}
|
|
24
|
+
classifiers = [
|
|
25
|
+
"Programming Language :: Python :: 3.11",
|
|
26
|
+
"Programming Language :: Python :: 3.12",
|
|
27
|
+
"Programming Language :: Python :: 3.13",
|
|
28
|
+
]
|
|
29
|
+
dynamic = ["version"]
|
|
30
|
+
|
|
31
|
+
[project.urls]
|
|
32
|
+
documentation = "https://github.com/openbraininstitute/obi-auth"
|
|
33
|
+
repository = "https://github.com/openbraininstitute/obi-auth"
|
|
34
|
+
changelog = "https://github.com/openbraininstitute/obi-auth/CHANGELOG.rst"
|
|
35
|
+
|
|
36
|
+
[tool.setuptools_scm]
|
|
37
|
+
local_scheme = "no-local-version"
|
|
38
|
+
|
|
39
|
+
[tool.pytest.ini_options]
|
|
40
|
+
addopts = [
|
|
41
|
+
"-vv",
|
|
42
|
+
"-ra",
|
|
43
|
+
"--import-mode=importlib",
|
|
44
|
+
"--cov=obi_auth",
|
|
45
|
+
"--durations=10",
|
|
46
|
+
"--durations-min=1.0",
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
[tool.ruff]
|
|
50
|
+
line-length = 100
|
|
51
|
+
target-version = "py311"
|
|
52
|
+
|
|
53
|
+
[tool.ruff.lint]
|
|
54
|
+
select = [
|
|
55
|
+
"A", # flake8-builtins
|
|
56
|
+
"B", # bugbear
|
|
57
|
+
"D", # pydocstyle
|
|
58
|
+
"E", # pycodestyle
|
|
59
|
+
"F", # pyflakes
|
|
60
|
+
"I", # isort
|
|
61
|
+
"S", # bandit
|
|
62
|
+
"UP", # pyupgrade
|
|
63
|
+
]
|
|
64
|
+
ignore = [
|
|
65
|
+
"S107", "S602", "S603", "S608"
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
[tool.ruff.lint.pydocstyle]
|
|
69
|
+
convention = "google"
|
|
70
|
+
|
|
71
|
+
[tool.ruff.lint.per-file-ignores]
|
|
72
|
+
"tests/*.py" = [
|
|
73
|
+
"ANN", # Missing type annotation
|
|
74
|
+
"D", # pydocstyle
|
|
75
|
+
"ERA001", # Found commented-out code
|
|
76
|
+
"INP001", # Missing `__init__.py`
|
|
77
|
+
"PLC2701", # Private name import from external module
|
|
78
|
+
"PLR2004", # magic value (constant) used in comparison (i.e. expected == 3)
|
|
79
|
+
"S101", # Use of `assert` detected
|
|
80
|
+
"SIM117", # Use a single `with` statement with multiple contexts
|
|
81
|
+
"SLF001", # private member access
|
|
82
|
+
]
|
|
83
|
+
|
|
84
|
+
[tool.ruff.lint.pylint]
|
|
85
|
+
# Maximum number of arguments for function / method
|
|
86
|
+
max-args = 12
|
|
87
|
+
# Maximum number of locals for function / method body
|
|
88
|
+
max-locals = 15
|
|
89
|
+
# Maximum number of return / yield for function / method body
|
|
90
|
+
max-returns = 6
|
|
91
|
+
# Maximum number of branch for function / method body
|
|
92
|
+
max-branches = 12
|
|
93
|
+
# Maximum number of statements in function / method body
|
|
94
|
+
max-statements = 50
|
|
95
|
+
# Maximum number of public methods for a class (see R0904).
|
|
96
|
+
max-public-methods = 60
|
|
97
|
+
|
|
98
|
+
[tool.ruff.format]
|
|
99
|
+
# Like Black, use double quotes for strings.
|
|
100
|
+
quote-style = "double"
|
|
101
|
+
# Like Black, indent with spaces, rather than tabs.
|
|
102
|
+
indent-style = "space"
|
|
103
|
+
# Like Black, automatically detect the appropriate line ending.
|
|
104
|
+
line-ending = "auto"
|
|
105
|
+
# Enable auto-formatting of code examples in docstrings. Markdown,
|
|
106
|
+
# reStructuredText code/literal blocks and doctests are all supported.
|
|
107
|
+
docstring-code-format = true
|
|
108
|
+
# Set the line length limit used when formatting code snippets in
|
|
109
|
+
# docstrings.
|
|
110
|
+
docstring-code-line-length = 100
|
|
111
|
+
# Like Black, respect magic trailing commas.
|
|
112
|
+
skip-magic-trailing-comma = false
|
|
113
|
+
|
|
114
|
+
[tool.coverage.paths]
|
|
115
|
+
source = [
|
|
116
|
+
"src",
|
|
117
|
+
"*/site-packages",
|
|
118
|
+
]
|
|
119
|
+
|
|
120
|
+
[tool.coverage.run]
|
|
121
|
+
branch = true
|
|
122
|
+
parallel = false
|
|
123
|
+
|
|
124
|
+
[tool.coverage.report]
|
|
125
|
+
show_missing = true
|
|
126
|
+
precision = 0
|
|
127
|
+
fail_under = 50
|
|
128
|
+
|
|
129
|
+
[tool.mypy]
|
|
130
|
+
show_error_codes = true
|
|
131
|
+
ignore_missing_imports = true
|
|
132
|
+
allow_redefinition = true
|
obi_auth-0.0.1/setup.cfg
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""This module provides a client for the obi_auth service."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
from obi_auth.exception import AuthFlowError, ClientError, ConfigError, LocalServerError
|
|
6
|
+
from obi_auth.flow import pkce_authenticate
|
|
7
|
+
from obi_auth.server import AuthServer
|
|
8
|
+
from obi_auth.typedef import DeploymentEnvironment
|
|
9
|
+
|
|
10
|
+
L = logging.getLogger(__name__)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def get_token(*, environment: DeploymentEnvironment | None = None) -> str | None:
|
|
14
|
+
"""Get token."""
|
|
15
|
+
try:
|
|
16
|
+
with AuthServer().run() as local_server:
|
|
17
|
+
return pkce_authenticate(server=local_server, override_env=environment)
|
|
18
|
+
except AuthFlowError as e:
|
|
19
|
+
raise ClientError("Authentication process failed.") from e
|
|
20
|
+
except LocalServerError as e:
|
|
21
|
+
raise ClientError("Local server failed to authenticate.") from e
|
|
22
|
+
except ConfigError as e:
|
|
23
|
+
raise ClientError("There is a mistake with configuration settings.") from e
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""This module provides a config for the obi_auth service."""
|
|
2
|
+
|
|
3
|
+
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
4
|
+
|
|
5
|
+
from obi_auth.exception import ConfigError
|
|
6
|
+
from obi_auth.typedef import DeploymentEnvironment, KeycloakRealm
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Settings(BaseSettings):
|
|
10
|
+
"""Environment settings for this library."""
|
|
11
|
+
|
|
12
|
+
model_config = SettingsConfigDict(
|
|
13
|
+
env_file=".env",
|
|
14
|
+
env_prefix="OBI_AUTH_",
|
|
15
|
+
env_file_encoding="utf-8",
|
|
16
|
+
case_sensitive=True,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
KEYCLOAK_ENV: DeploymentEnvironment = DeploymentEnvironment.staging
|
|
20
|
+
KEYCLOAK_REALM: KeycloakRealm = KeycloakRealm.sbo
|
|
21
|
+
KEYCLOAK_CLIENT_ID: str = "obi-entitysdk-auth"
|
|
22
|
+
|
|
23
|
+
LOCAL_SERVER_TIMEOUT: int = 60
|
|
24
|
+
|
|
25
|
+
def get_keycloak_url(self, override_env: DeploymentEnvironment | None = None):
|
|
26
|
+
"""Return keycloak url."""
|
|
27
|
+
match env := override_env or self.KEYCLOAK_ENV:
|
|
28
|
+
case DeploymentEnvironment.staging:
|
|
29
|
+
return f"https://staging.openbraininstitute.org/auth/realms/{self.KEYCLOAK_REALM}"
|
|
30
|
+
case DeploymentEnvironment.production:
|
|
31
|
+
return f"https://openbraininstitute.org/auth/realms/{self.KEYCLOAK_REALM}"
|
|
32
|
+
raise ConfigError(f"Unknown deployment environment {env}")
|
|
33
|
+
|
|
34
|
+
def get_keycloak_token_endpoint(self, override_env: DeploymentEnvironment | None = None) -> str:
|
|
35
|
+
"""Return keycloak token endpoint."""
|
|
36
|
+
base_url = self.get_keycloak_url(override_env=override_env)
|
|
37
|
+
return f"{base_url}/protocol/openid-connect/token"
|
|
38
|
+
|
|
39
|
+
def get_keycloak_auth_endpoint(self, override_env: DeploymentEnvironment | None = None) -> str:
|
|
40
|
+
"""Return keycloak auth endpoint."""
|
|
41
|
+
base_url = self.get_keycloak_url(override_env=override_env)
|
|
42
|
+
return f"{base_url}/protocol/openid-connect/auth"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
settings = Settings()
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""Exceptions module."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ObiAuthError(Exception):
|
|
5
|
+
"""Generic obi-auth error."""
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class AuthFlowError(ObiAuthError):
|
|
9
|
+
"""Authenticatin flow error."""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ClientError(ObiAuthError):
|
|
13
|
+
"""Client related error."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class LocalServerError(ObiAuthError):
|
|
17
|
+
"""Local server related error."""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ConfigError(ObiAuthError):
|
|
21
|
+
"""Configuration settings error."""
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""Authorization flow module."""
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import hashlib
|
|
5
|
+
import logging
|
|
6
|
+
import os
|
|
7
|
+
import re
|
|
8
|
+
import urllib.parse
|
|
9
|
+
import webbrowser
|
|
10
|
+
|
|
11
|
+
import httpx
|
|
12
|
+
|
|
13
|
+
from obi_auth.config import settings
|
|
14
|
+
from obi_auth.server import AuthServer
|
|
15
|
+
from obi_auth.typedef import DeploymentEnvironment
|
|
16
|
+
|
|
17
|
+
L = logging.getLogger(__name__)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _generate_pkce_pair() -> tuple[str, str]:
|
|
21
|
+
"""Generate PKCE pair."""
|
|
22
|
+
code_verifier = base64.urlsafe_b64encode(os.urandom(40)).decode("utf-8")
|
|
23
|
+
code_verifier = re.sub("[^a-zA-Z0-9]+", "", code_verifier)
|
|
24
|
+
|
|
25
|
+
code_challenge = hashlib.sha256(code_verifier.encode("utf-8")).digest()
|
|
26
|
+
code_challenge = base64.urlsafe_b64encode(code_challenge).decode("utf-8")
|
|
27
|
+
code_challenge = code_challenge.replace("=", "")
|
|
28
|
+
return code_verifier, code_challenge
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _build_auth_url(
|
|
32
|
+
code_challenge: str, redirect_uri: str, override_env: DeploymentEnvironment | None
|
|
33
|
+
) -> str:
|
|
34
|
+
"""Construct authentication url to open with a browser."""
|
|
35
|
+
params = {
|
|
36
|
+
"response_type": "code",
|
|
37
|
+
"client_id": settings.KEYCLOAK_CLIENT_ID,
|
|
38
|
+
"redirect_uri": redirect_uri,
|
|
39
|
+
"scope": "openid",
|
|
40
|
+
"code_challenge": code_challenge,
|
|
41
|
+
"code_challenge_method": "S256",
|
|
42
|
+
"kc_idp_hint": "github",
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
base_auth_url = settings.get_keycloak_auth_endpoint(override_env=override_env)
|
|
46
|
+
|
|
47
|
+
# Build the full URL
|
|
48
|
+
encoded_params = urllib.parse.urlencode(params)
|
|
49
|
+
return f"{base_auth_url}?{encoded_params}"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _authorize(
|
|
53
|
+
server: AuthServer, code_challenge: str, override_env: DeploymentEnvironment | None
|
|
54
|
+
) -> str:
|
|
55
|
+
"""Ask user to login in order to retrieve a code to exchange for a token."""
|
|
56
|
+
auth_url = _build_auth_url(code_challenge, server.redirect_uri, override_env)
|
|
57
|
+
L.info("Authentication url: %s", auth_url)
|
|
58
|
+
webbrowser.open(auth_url)
|
|
59
|
+
return server.wait_for_code()
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _exchange_code_for_token(
|
|
63
|
+
code: str, redirect_uri: str, code_verifier: str, override_env: DeploymentEnvironment | None
|
|
64
|
+
) -> str:
|
|
65
|
+
response = httpx.post(
|
|
66
|
+
url=settings.get_keycloak_token_endpoint(override_env=override_env),
|
|
67
|
+
data={
|
|
68
|
+
"grant_type": "authorization_code",
|
|
69
|
+
"code": code,
|
|
70
|
+
"client_id": settings.KEYCLOAK_CLIENT_ID,
|
|
71
|
+
"redirect_uri": redirect_uri,
|
|
72
|
+
"code_verifier": code_verifier,
|
|
73
|
+
},
|
|
74
|
+
)
|
|
75
|
+
response.raise_for_status()
|
|
76
|
+
|
|
77
|
+
access_token = response.json()["access_token"]
|
|
78
|
+
|
|
79
|
+
return access_token
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def pkce_authenticate(
|
|
83
|
+
*, server: AuthServer, override_env: DeploymentEnvironment | None = None
|
|
84
|
+
) -> str:
|
|
85
|
+
"""Get access token using the PCKE authentication flow."""
|
|
86
|
+
code_verifier, code_challenge = _generate_pkce_pair()
|
|
87
|
+
code = _authorize(server, code_challenge, override_env)
|
|
88
|
+
access_token = _exchange_code_for_token(code, server.redirect_uri, code_verifier, override_env)
|
|
89
|
+
return access_token
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""This module provides a simple HTTP server that listens for a Keycloak authorization code."""
|
|
2
|
+
|
|
3
|
+
import contextlib
|
|
4
|
+
import logging
|
|
5
|
+
import socket
|
|
6
|
+
import threading
|
|
7
|
+
from collections.abc import Iterator
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from typing import Self
|
|
10
|
+
|
|
11
|
+
import uvicorn
|
|
12
|
+
from fastapi import FastAPI, HTTPException, Request
|
|
13
|
+
|
|
14
|
+
from obi_auth.config import settings
|
|
15
|
+
from obi_auth.exception import LocalServerError
|
|
16
|
+
|
|
17
|
+
L = logging.getLogger(__name__)
|
|
18
|
+
HOST = "localhost"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class AuthState:
|
|
23
|
+
"""Class to manage authentication state."""
|
|
24
|
+
|
|
25
|
+
code: str | None = None
|
|
26
|
+
event: threading.Event = threading.Event()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class AuthServer:
|
|
30
|
+
"""Class to manage authentication state."""
|
|
31
|
+
|
|
32
|
+
def __init__(self):
|
|
33
|
+
"""Initialize authentication server."""
|
|
34
|
+
self.app = FastAPI()
|
|
35
|
+
self.auth_state = AuthState()
|
|
36
|
+
self.port = None
|
|
37
|
+
|
|
38
|
+
@self.app.get("/callback")
|
|
39
|
+
def callback(request: Request):
|
|
40
|
+
"""Handle the Keycloak redirect and extracts the authorization code."""
|
|
41
|
+
code = request.query_params.get("code")
|
|
42
|
+
|
|
43
|
+
if not code:
|
|
44
|
+
raise HTTPException(status_code=400, detail="Authorization code not found")
|
|
45
|
+
|
|
46
|
+
self.auth_state.code = code
|
|
47
|
+
self.auth_state.event.set() # Signal that we received the code
|
|
48
|
+
|
|
49
|
+
return {"message": "Authentication successful. You can close this window."}
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def redirect_uri(self) -> str:
|
|
53
|
+
"""Return redirect uril for server callback."""
|
|
54
|
+
if not self.port:
|
|
55
|
+
raise LocalServerError("Server has no port assigned.")
|
|
56
|
+
return f"http://{HOST}:{self.port}/callback"
|
|
57
|
+
|
|
58
|
+
@staticmethod
|
|
59
|
+
def _find_free_port() -> int:
|
|
60
|
+
"""Bind to port 0 to let the OS select a free port, then return that port."""
|
|
61
|
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
62
|
+
s.bind(("", 0))
|
|
63
|
+
return s.getsockname()[1]
|
|
64
|
+
|
|
65
|
+
@contextlib.contextmanager
|
|
66
|
+
def run(self) -> Iterator[Self]:
|
|
67
|
+
"""Start server in a background thread."""
|
|
68
|
+
self.port = self._find_free_port()
|
|
69
|
+
config = uvicorn.Config(app=self.app, port=self.port, host=HOST, log_level="error")
|
|
70
|
+
server = uvicorn.Server(config=config)
|
|
71
|
+
thread = threading.Thread(target=server.run, daemon=True)
|
|
72
|
+
try:
|
|
73
|
+
thread.start()
|
|
74
|
+
L.info("Local server listening on http://%s:%s", HOST, self.port)
|
|
75
|
+
yield self
|
|
76
|
+
finally:
|
|
77
|
+
L.debug("Stopping the local server")
|
|
78
|
+
server.should_exit = True
|
|
79
|
+
thread.join(timeout=1)
|
|
80
|
+
|
|
81
|
+
def wait_for_code(self, timeout: int = settings.LOCAL_SERVER_TIMEOUT) -> str:
|
|
82
|
+
"""Wait for code."""
|
|
83
|
+
if self.auth_state.event.wait(timeout):
|
|
84
|
+
self.auth_state.event.clear()
|
|
85
|
+
return self.auth_state.code
|
|
86
|
+
raise LocalServerError("Timeout waiting for authorization code")
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""This module provides typedefs for the obi_auth service."""
|
|
2
|
+
|
|
3
|
+
from enum import StrEnum
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class DeploymentEnvironment(StrEnum):
|
|
7
|
+
"""Deployment environment."""
|
|
8
|
+
|
|
9
|
+
staging = "staging"
|
|
10
|
+
production = "production"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class KeycloakRealm(StrEnum):
|
|
14
|
+
"""Keycloak realms."""
|
|
15
|
+
|
|
16
|
+
sbo = "SBO"
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: obi_auth
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Python library for retrieving Keycloak access tokens interactively
|
|
5
|
+
Author-email: Open Brain Institute <info@openbraininstitute.org>
|
|
6
|
+
Maintainer-email: Open Brain Institute <info@openbraininstitute.org>
|
|
7
|
+
License: Apache-2.0
|
|
8
|
+
Project-URL: documentation, https://github.com/openbraininstitute/obi-auth
|
|
9
|
+
Project-URL: repository, https://github.com/openbraininstitute/obi-auth
|
|
10
|
+
Project-URL: changelog, https://github.com/openbraininstitute/obi-auth/CHANGELOG.rst
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Requires-Python: >=3.11
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE.txt
|
|
17
|
+
Requires-Dist: httpx
|
|
18
|
+
Requires-Dist: pydantic
|
|
19
|
+
Requires-Dist: pydantic-settings
|
|
20
|
+
Requires-Dist: uvicorn
|
|
21
|
+
Requires-Dist: fastapi
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
24
|
+
[![Build status][build_status_badge]][build_status_target]
|
|
25
|
+
[![License][license_badge]][license_target]
|
|
26
|
+
[![Code coverage][coverage_badge]][coverage_target]
|
|
27
|
+
[![CodeQL][codeql_badge]][codeql_target]
|
|
28
|
+
[![PyPI][pypi_badge]][pypi_target]
|
|
29
|
+
|
|
30
|
+
# obi-auth
|
|
31
|
+
|
|
32
|
+
obi-auth is a library for retrieving Keycloak access tokens interactively. It helps developers and testers quickly authenticate against Keycloak without writing scripts or configuring complex clients.
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
pip install obi-auth
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Examples
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from obi_auth import get_token
|
|
45
|
+
|
|
46
|
+
access_token = get_token(environment="staging")
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
|
|
51
|
+
Copyright (c) 2025 Open Brain Institute
|
|
52
|
+
|
|
53
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
54
|
+
you may not use this file except in compliance with the License.
|
|
55
|
+
You may obtain a copy of the License at
|
|
56
|
+
|
|
57
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
58
|
+
|
|
59
|
+
Unless required by applicable law or agreed to in writing, software
|
|
60
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
61
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
62
|
+
See the License for the specific language governing permissions and
|
|
63
|
+
limitations under the License.
|
|
64
|
+
|
|
65
|
+
[build_status_badge]: https://github.com/openbraininstitute/obi-auth/actions/workflows/tox.yml/badge.svg
|
|
66
|
+
[build_status_target]: https://github.com/openbraininstitute/obi-auth/actions
|
|
67
|
+
[license_badge]: https://img.shields.io/pypi/l/obi-auth
|
|
68
|
+
[license_target]: https://github.com/openbraininstitute/obi-auth/blob/main/LICENSE.txt
|
|
69
|
+
[coverage_badge]: https://codecov.io/github/openbraininstitute/obi-auth/coverage.svg?branch=main
|
|
70
|
+
[coverage_target]: https://codecov.io/github/openbraininstitute/obi-auth?branch=main
|
|
71
|
+
[codeql_badge]: https://github.com/openbraininstitute/obi-auth/actions/workflows/github-code-scanning/codeql/badge.svg
|
|
72
|
+
[codeql_target]: https://github.com/openbraininstitute/obi-auth/actions/workflows/github-code-scanning/codeql
|
|
73
|
+
[pypi_badge]: https://github.com/openbraininstitute/obi-auth/actions/workflows/sdist.yml/badge.svg
|
|
74
|
+
[pypi_target]: https://pypi.org/project/obi-auth/
|
|
75
|
+
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
.gitignore
|
|
2
|
+
CHANGELOG.rst
|
|
3
|
+
LICENSE.txt
|
|
4
|
+
README.md
|
|
5
|
+
pyproject.toml
|
|
6
|
+
tox.ini
|
|
7
|
+
.github/workflows/sdist.yml
|
|
8
|
+
.github/workflows/tox.yml
|
|
9
|
+
src/obi_auth/__init__.py
|
|
10
|
+
src/obi_auth/client.py
|
|
11
|
+
src/obi_auth/config.py
|
|
12
|
+
src/obi_auth/exception.py
|
|
13
|
+
src/obi_auth/flow.py
|
|
14
|
+
src/obi_auth/server.py
|
|
15
|
+
src/obi_auth/typedef.py
|
|
16
|
+
src/obi_auth.egg-info/PKG-INFO
|
|
17
|
+
src/obi_auth.egg-info/SOURCES.txt
|
|
18
|
+
src/obi_auth.egg-info/dependency_links.txt
|
|
19
|
+
src/obi_auth.egg-info/requires.txt
|
|
20
|
+
src/obi_auth.egg-info/top_level.txt
|
|
21
|
+
tests/__init__.py
|
|
22
|
+
tests/unit/__init__.py
|
|
23
|
+
tests/unit/test_client.py
|
|
24
|
+
tests/unit/test_config.py
|
|
25
|
+
tests/unit/test_flow.py
|
|
26
|
+
tests/unit/test_server.py
|
|
27
|
+
tests/unit/data/.gitignore
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
obi_auth
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""obi_auth tests."""
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""obi_auth unit tests."""
|
|
File without changes
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from unittest.mock import Mock, patch
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from obi_auth import client as test_module
|
|
6
|
+
from obi_auth import exception
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@patch("obi_auth.flow.webbrowser")
|
|
10
|
+
@patch("obi_auth.client.AuthServer")
|
|
11
|
+
def test_get_token(mock_server, mock_web, httpx_mock):
|
|
12
|
+
httpx_mock.add_response(method="POST", json={"access_token": "mock-token"})
|
|
13
|
+
|
|
14
|
+
mock_local = Mock()
|
|
15
|
+
mock_local.redirect_uri = "mock-redirect-uri"
|
|
16
|
+
mock_local.wait_for_code.return_value = "mock-code"
|
|
17
|
+
mock_server.run.return_value.__enter__.return_value = mock_local
|
|
18
|
+
|
|
19
|
+
res = test_module.get_token()
|
|
20
|
+
assert res == "mock-token"
|
|
21
|
+
|
|
22
|
+
mock_server.side_effect = exception.AuthFlowError()
|
|
23
|
+
with pytest.raises(exception.ClientError, match="Authentication process failed."):
|
|
24
|
+
test_module.get_token()
|
|
25
|
+
|
|
26
|
+
mock_server.side_effect = exception.ConfigError()
|
|
27
|
+
with pytest.raises(
|
|
28
|
+
exception.ClientError, match="There is a mistake with configuration settings."
|
|
29
|
+
):
|
|
30
|
+
test_module.get_token()
|
|
31
|
+
|
|
32
|
+
mock_server.side_effect = exception.LocalServerError()
|
|
33
|
+
with pytest.raises(exception.ClientError, match="Local server failed to authenticate."):
|
|
34
|
+
test_module.get_token()
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from obi_auth import config as test_module
|
|
4
|
+
from obi_auth import exception
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@pytest.fixture
|
|
8
|
+
def settings(monkeypatch):
|
|
9
|
+
monkeypatch.setenv("KEYCLOAK_ENV", "staging")
|
|
10
|
+
monkeypatch.setenv("KEYCLOAK_REALM", "SBO")
|
|
11
|
+
return test_module.Settings()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def test_get_keycloak_url(settings):
|
|
15
|
+
res = settings.get_keycloak_url()
|
|
16
|
+
assert res == "https://staging.openbraininstitute.org/auth/realms/SBO"
|
|
17
|
+
|
|
18
|
+
res = settings.get_keycloak_url(override_env="staging")
|
|
19
|
+
assert res == "https://staging.openbraininstitute.org/auth/realms/SBO"
|
|
20
|
+
|
|
21
|
+
res = settings.get_keycloak_url(override_env="production")
|
|
22
|
+
assert res == "https://openbraininstitute.org/auth/realms/SBO"
|
|
23
|
+
|
|
24
|
+
with pytest.raises(exception.ConfigError, match="Unknown deployment environment foo"):
|
|
25
|
+
settings.get_keycloak_url(override_env="foo")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def test_get_keycloak_token_endpoint(settings):
|
|
29
|
+
res = settings.get_keycloak_token_endpoint()
|
|
30
|
+
assert (
|
|
31
|
+
res
|
|
32
|
+
== "https://staging.openbraininstitute.org/auth/realms/SBO/protocol/openid-connect/token"
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
res = settings.get_keycloak_token_endpoint(override_env="staging")
|
|
36
|
+
assert (
|
|
37
|
+
res
|
|
38
|
+
== "https://staging.openbraininstitute.org/auth/realms/SBO/protocol/openid-connect/token"
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
res = settings.get_keycloak_token_endpoint(override_env="production")
|
|
42
|
+
assert res == "https://openbraininstitute.org/auth/realms/SBO/protocol/openid-connect/token"
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def test_get_keycloak_auth_endpoint(settings):
|
|
46
|
+
res = settings.get_keycloak_auth_endpoint()
|
|
47
|
+
assert (
|
|
48
|
+
res == "https://staging.openbraininstitute.org/auth/realms/SBO/protocol/openid-connect/auth"
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
res = settings.get_keycloak_auth_endpoint(override_env="staging")
|
|
52
|
+
assert (
|
|
53
|
+
res == "https://staging.openbraininstitute.org/auth/realms/SBO/protocol/openid-connect/auth"
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
res = settings.get_keycloak_auth_endpoint(override_env="production")
|
|
57
|
+
assert res == "https://openbraininstitute.org/auth/realms/SBO/protocol/openid-connect/auth"
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from unittest.mock import Mock, patch
|
|
2
|
+
|
|
3
|
+
from obi_auth import flow as test_module
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def test_build_auth_url():
|
|
7
|
+
res = test_module._build_auth_url(
|
|
8
|
+
code_challenge="foo",
|
|
9
|
+
redirect_uri="bar",
|
|
10
|
+
override_env=None,
|
|
11
|
+
)
|
|
12
|
+
assert res == (
|
|
13
|
+
"https://staging.openbraininstitute.org/auth/realms/SBO/protocol/openid-connect/auth"
|
|
14
|
+
"?response_type=code"
|
|
15
|
+
"&client_id=obi-entitysdk-auth"
|
|
16
|
+
"&redirect_uri=bar"
|
|
17
|
+
"&scope=openid"
|
|
18
|
+
"&code_challenge=foo"
|
|
19
|
+
"&code_challenge_method=S256"
|
|
20
|
+
"&kc_idp_hint=github"
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_exchange_code_for_token(httpx_mock):
|
|
25
|
+
httpx_mock.add_response(method="POST", json={"access_token": "mock-token"})
|
|
26
|
+
res = test_module._exchange_code_for_token(
|
|
27
|
+
code="mock-code",
|
|
28
|
+
redirect_uri="mock-uri",
|
|
29
|
+
code_verifier="mock-verifier",
|
|
30
|
+
override_env=None,
|
|
31
|
+
)
|
|
32
|
+
assert res == "mock-token"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@patch("obi_auth.flow.webbrowser")
|
|
36
|
+
def test_authorize(mocked_webbrowser):
|
|
37
|
+
mock_server = Mock()
|
|
38
|
+
mock_server.wait_for_code.return_value = "mock-code"
|
|
39
|
+
|
|
40
|
+
res = test_module._authorize(
|
|
41
|
+
server=mock_server,
|
|
42
|
+
code_challenge="mock-challenge",
|
|
43
|
+
override_env=None,
|
|
44
|
+
)
|
|
45
|
+
assert res == "mock-code"
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import httpx
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
from obi_auth import server as test_module
|
|
5
|
+
from obi_auth.exception import LocalServerError
|
|
6
|
+
from obi_auth.server import AuthServer
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@pytest.fixture
|
|
10
|
+
def server():
|
|
11
|
+
return test_module.AuthServer()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@pytest.fixture
|
|
15
|
+
def running_server(server):
|
|
16
|
+
with server.run() as local_server:
|
|
17
|
+
yield local_server
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_server(server):
|
|
21
|
+
assert isinstance(server, AuthServer)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_find_free_port():
|
|
25
|
+
assert AuthServer._find_free_port() > 0
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def test_redirect_uri(server):
|
|
29
|
+
with pytest.raises(LocalServerError, match="Server has no port assigned."):
|
|
30
|
+
_ = server.redirect_uri
|
|
31
|
+
server.port = "8000"
|
|
32
|
+
assert server.redirect_uri == f"http://localhost:{server.port}/callback"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def test_wait_for_code(running_server):
|
|
36
|
+
with pytest.raises(LocalServerError, match="Timeout waiting for authorization code"):
|
|
37
|
+
running_server.wait_for_code(timeout=0.1)
|
|
38
|
+
|
|
39
|
+
response = httpx.get(f"{running_server.redirect_uri}")
|
|
40
|
+
assert response.status_code == 400
|
|
41
|
+
|
|
42
|
+
response = httpx.get(f"{running_server.redirect_uri}?code=mock-code")
|
|
43
|
+
response.raise_for_status()
|
|
44
|
+
|
|
45
|
+
res = running_server.wait_for_code()
|
|
46
|
+
assert res == "mock-code"
|
obi_auth-0.0.1/tox.ini
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
[base]
|
|
2
|
+
name = obi_auth
|
|
3
|
+
path = src/{[base]name}
|
|
4
|
+
testdeps =
|
|
5
|
+
pytest
|
|
6
|
+
pytest-cov
|
|
7
|
+
pytest-httpx
|
|
8
|
+
coverage[toml]
|
|
9
|
+
|
|
10
|
+
[tox]
|
|
11
|
+
envlist =
|
|
12
|
+
lint
|
|
13
|
+
check-packaging
|
|
14
|
+
py{311,312,313}
|
|
15
|
+
|
|
16
|
+
minversion = 3.18
|
|
17
|
+
|
|
18
|
+
[testenv]
|
|
19
|
+
deps =
|
|
20
|
+
{[base]testdeps}
|
|
21
|
+
commands =
|
|
22
|
+
python -m pytest {posargs}
|
|
23
|
+
python -m coverage xml
|
|
24
|
+
python -m coverage html
|
|
25
|
+
|
|
26
|
+
[testenv:lint]
|
|
27
|
+
deps =
|
|
28
|
+
ruff
|
|
29
|
+
mypy
|
|
30
|
+
commands =
|
|
31
|
+
python -m ruff format --check
|
|
32
|
+
python -m ruff check
|
|
33
|
+
python -m mypy src
|
|
34
|
+
|
|
35
|
+
[testenv:format]
|
|
36
|
+
deps =
|
|
37
|
+
ruff
|
|
38
|
+
commands =
|
|
39
|
+
python -m ruff format
|
|
40
|
+
python -m ruff check --fix
|
|
41
|
+
|
|
42
|
+
[testenv:check-packaging]
|
|
43
|
+
skip_install = true
|
|
44
|
+
deps =
|
|
45
|
+
build
|
|
46
|
+
twine
|
|
47
|
+
commands =
|
|
48
|
+
python -m build -o {envtmpdir}/dist
|
|
49
|
+
twine check {envtmpdir}/dist/*
|