logi-auth 1.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.
- logi_auth-1.0.1/LICENSE +80 -0
- logi_auth-1.0.1/NOTICE +4 -0
- logi_auth-1.0.1/PKG-INFO +210 -0
- logi_auth-1.0.1/README.md +194 -0
- logi_auth-1.0.1/pyproject.toml +24 -0
- logi_auth-1.0.1/setup.cfg +4 -0
- logi_auth-1.0.1/src/logi_auth/__init__.py +22 -0
- logi_auth-1.0.1/src/logi_auth/errors.py +39 -0
- logi_auth-1.0.1/src/logi_auth/id_token_verifier.py +194 -0
- logi_auth-1.0.1/src/logi_auth/server.py +226 -0
- logi_auth-1.0.1/src/logi_auth.egg-info/PKG-INFO +210 -0
- logi_auth-1.0.1/src/logi_auth.egg-info/SOURCES.txt +15 -0
- logi_auth-1.0.1/src/logi_auth.egg-info/dependency_links.txt +1 -0
- logi_auth-1.0.1/src/logi_auth.egg-info/requires.txt +1 -0
- logi_auth-1.0.1/src/logi_auth.egg-info/top_level.txt +1 -0
- logi_auth-1.0.1/tests/test_id_token_verifier.py +109 -0
- logi_auth-1.0.1/tests/test_server.py +93 -0
logi_auth-1.0.1/LICENSE
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
|
|
5
|
+
http://www.apache.org/licenses/
|
|
6
|
+
|
|
7
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
8
|
+
|
|
9
|
+
1. Definitions.
|
|
10
|
+
|
|
11
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
|
16
|
+
|
|
17
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
|
18
|
+
|
|
19
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
|
20
|
+
|
|
21
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
|
22
|
+
|
|
23
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
|
24
|
+
|
|
25
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
|
26
|
+
|
|
27
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
|
28
|
+
|
|
29
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
|
30
|
+
|
|
31
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
|
32
|
+
|
|
33
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
|
34
|
+
|
|
35
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
|
36
|
+
|
|
37
|
+
You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
|
38
|
+
|
|
39
|
+
You must cause any modified files to carry prominent notices stating that You changed the files; and
|
|
40
|
+
|
|
41
|
+
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
|
42
|
+
|
|
43
|
+
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
|
44
|
+
|
|
45
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
|
46
|
+
|
|
47
|
+
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
|
48
|
+
|
|
49
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
|
50
|
+
|
|
51
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
|
52
|
+
|
|
53
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
|
54
|
+
|
|
55
|
+
END OF TERMS AND CONDITIONS
|
|
56
|
+
|
|
57
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
58
|
+
|
|
59
|
+
To apply the Apache License to your work, attach the following
|
|
60
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
61
|
+
replaced with your own identifying information. (Don't include
|
|
62
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
63
|
+
comment syntax for the file format. We also recommend that a
|
|
64
|
+
file or class name and description of purpose be included on the
|
|
65
|
+
same "printed page" as the copyright notice for easier
|
|
66
|
+
identification within third-party archives.
|
|
67
|
+
|
|
68
|
+
Copyright 2026 Dcode
|
|
69
|
+
|
|
70
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
71
|
+
you may not use this file except in compliance with the License.
|
|
72
|
+
You may obtain a copy of the License at
|
|
73
|
+
|
|
74
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
75
|
+
|
|
76
|
+
Unless required by applicable law or agreed to in writing, software
|
|
77
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
78
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
79
|
+
See the License for the specific language governing permissions and
|
|
80
|
+
limitations under the License.
|
logi_auth-1.0.1/NOTICE
ADDED
logi_auth-1.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: logi-auth
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: Server-side "Sign in with logi" for Python/Django — confidential OAuth 2.0 + id_token (RS256) verification.
|
|
5
|
+
Author: Dcode
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/dcode-co/logi-auth-python
|
|
8
|
+
Project-URL: Issues, https://github.com/dcode-co/logi-auth-python/issues
|
|
9
|
+
Keywords: logi,1pass,oauth,oidc,django,auth,openid,id-token
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
License-File: NOTICE
|
|
14
|
+
Requires-Dist: cryptography>=41.0
|
|
15
|
+
Dynamic: license-file
|
|
16
|
+
|
|
17
|
+
# logi-auth (Python)
|
|
18
|
+
|
|
19
|
+
Server-side **"Sign in with logi"** for Python / Django backends — confidential
|
|
20
|
+
OAuth 2.0 Authorization Code exchange + **id_token (RS256) verification**. Only
|
|
21
|
+
dependency: `cryptography` (no HTTP client dependency — stdlib `urllib`).
|
|
22
|
+
|
|
23
|
+
This is the **confidential / backend** counterpart to the public-client SDKs
|
|
24
|
+
(browser, iOS, Android, Flutter). If your RP has a backend, verify on the
|
|
25
|
+
server with this package — do **not** rely on a client-side check.
|
|
26
|
+
|
|
27
|
+
> **Why it matters:** a backend that skips the id_token `aud` check can be
|
|
28
|
+
> tricked into accepting a token minted for a *different* client (cross-client
|
|
29
|
+
> account takeover — the launchcrew/krx incident). `exchange_code_and_verify`
|
|
30
|
+
> always verifies signature + iss + aud + exp + nonce before returning `sub`.
|
|
31
|
+
|
|
32
|
+
## Supported versions
|
|
33
|
+
|
|
34
|
+
| Requirement | Version |
|
|
35
|
+
|-------------|---------|
|
|
36
|
+
| **Python** | **>= 3.9** |
|
|
37
|
+
| **Django** | any — the package is framework-agnostic; use it from any view |
|
|
38
|
+
| Flask / FastAPI / etc. | any |
|
|
39
|
+
| Dependencies | `cryptography >= 41.0` |
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install logi-auth
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Django view example
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
# settings.py (or a dedicated logi_auth.py config module)
|
|
51
|
+
import os
|
|
52
|
+
from logi_auth import LogiAuthServer
|
|
53
|
+
|
|
54
|
+
LOGI = LogiAuthServer(
|
|
55
|
+
client_id=os.environ["LOGI_CLIENT_ID"],
|
|
56
|
+
client_secret=os.environ["LOGI_CLIENT_SECRET"], # confidential client
|
|
57
|
+
redirect_uri="https://app.example.com/auth/logi/callback",
|
|
58
|
+
)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
# views.py
|
|
63
|
+
import secrets
|
|
64
|
+
|
|
65
|
+
from django.http import HttpResponseBadRequest
|
|
66
|
+
from django.shortcuts import redirect
|
|
67
|
+
|
|
68
|
+
from logi_auth import ServerError
|
|
69
|
+
|
|
70
|
+
from .settings import LOGI
|
|
71
|
+
from .models import User
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def logi_start(request):
|
|
75
|
+
state = secrets.token_hex(16)
|
|
76
|
+
nonce = secrets.token_hex(16)
|
|
77
|
+
request.session["logi_state"] = state
|
|
78
|
+
request.session["logi_nonce"] = nonce
|
|
79
|
+
return redirect(LOGI.authorization_url(state=state, nonce=nonce))
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def logi_callback(request):
|
|
83
|
+
if request.GET.get("state") != request.session.pop("logi_state", None):
|
|
84
|
+
return HttpResponseBadRequest("state mismatch")
|
|
85
|
+
|
|
86
|
+
# The provider may redirect back with ?error=access_denied (user cancelled)
|
|
87
|
+
# and no `code` — handle that before the exchange instead of 500-ing.
|
|
88
|
+
if request.GET.get("error"):
|
|
89
|
+
return redirect(f"/login?error={request.GET['error']}")
|
|
90
|
+
code = request.GET.get("code")
|
|
91
|
+
if not code:
|
|
92
|
+
return HttpResponseBadRequest("missing authorization code")
|
|
93
|
+
|
|
94
|
+
try:
|
|
95
|
+
result = LOGI.exchange_code_and_verify(
|
|
96
|
+
code=code,
|
|
97
|
+
nonce=request.session.pop("logi_nonce", None),
|
|
98
|
+
)
|
|
99
|
+
except ServerError as e:
|
|
100
|
+
# result.sub is only ever set after signature+iss+aud+exp+nonce all pass.
|
|
101
|
+
return redirect(f"/login?error={e.code}")
|
|
102
|
+
|
|
103
|
+
# result.sub is the verified pairwise subject — key your User record on it.
|
|
104
|
+
user, _ = User.objects.get_or_create(logi_sub=result.sub, defaults={"email": result.email})
|
|
105
|
+
request.session["user_id"] = user.id
|
|
106
|
+
return redirect("/")
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Identity claims (email/name) are **not** guaranteed on the id_token — fetch
|
|
110
|
+
them from `GET {issuer}/oauth/userinfo` with the returned `access_token` as a
|
|
111
|
+
Bearer token if you need more than `sub`/`email`.
|
|
112
|
+
|
|
113
|
+
## Public client (PKCE, no secret)
|
|
114
|
+
|
|
115
|
+
Omit `client_secret` and pass a `code_challenge` / `code_verifier`:
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
logi = LogiAuthServer(client_id=client_id, redirect_uri=redirect_uri) # no secret
|
|
119
|
+
url = logi.authorization_url(state=state, nonce=nonce, code_challenge=code_challenge)
|
|
120
|
+
result = logi.exchange_code_and_verify(code=code, nonce=nonce, code_verifier=code_verifier)
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## API
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
from logi_auth import LogiAuthServer, LogiSession, verify_id_token, LogiAuthError, IdTokenError, ServerError
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### `LogiAuthServer(...)`
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
LogiAuthServer(
|
|
133
|
+
*,
|
|
134
|
+
client_id: str,
|
|
135
|
+
redirect_uri: str,
|
|
136
|
+
client_secret: str | None = None,
|
|
137
|
+
issuer: str = "https://api.1pass.dev",
|
|
138
|
+
token_issuer: str = "https://api.1pass.dev",
|
|
139
|
+
scopes: list[str] | None = None, # default: ["openid", "profile:basic", "email"]
|
|
140
|
+
jwks_cache_ttl: int = 3600,
|
|
141
|
+
)
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
- **`.authorization_url(*, state, nonce, scopes=None, code_challenge=None, prompt=None) -> str`**
|
|
145
|
+
Builds the `/oauth/authorize` redirect URL.
|
|
146
|
+
- **`.exchange_code_and_verify(*, code, nonce, code_verifier=None) -> LogiSession`**
|
|
147
|
+
Exchanges the authorization `code` for tokens, then verifies the returned
|
|
148
|
+
`id_token` (signature via JWKS + `iss` + `aud` + `exp` + `nonce`, with a
|
|
149
|
+
transparent single JWKS refetch on key rotation) before returning a
|
|
150
|
+
`LogiSession`.
|
|
151
|
+
|
|
152
|
+
### `LogiSession`
|
|
153
|
+
|
|
154
|
+
Returned only once id_token verification has fully passed:
|
|
155
|
+
|
|
156
|
+
| Field | Type |
|
|
157
|
+
|-------|------|
|
|
158
|
+
| `sub` | `str` — verified pairwise subject |
|
|
159
|
+
| `email` | `str \| None` |
|
|
160
|
+
| `id_token` | `str` |
|
|
161
|
+
| `access_token` | `str` |
|
|
162
|
+
| `refresh_token` | `str \| None` |
|
|
163
|
+
| `expires_at` | `int \| None` — unix timestamp |
|
|
164
|
+
| `scope` | `str \| None` |
|
|
165
|
+
| `claims` | `dict` — full verified id_token claim set |
|
|
166
|
+
|
|
167
|
+
## Error handling
|
|
168
|
+
|
|
169
|
+
Both error types carry a `.code` string for programmatic branching (same
|
|
170
|
+
codes as the Ruby/Node/Web SDKs and the shared golden vectors).
|
|
171
|
+
|
|
172
|
+
`ServerError.code`:
|
|
173
|
+
|
|
174
|
+
| Code | Meaning |
|
|
175
|
+
|------|---------|
|
|
176
|
+
| `invalid_nonce` | Missing nonce — the sign-in session likely expired |
|
|
177
|
+
| `token_exchange_failed` | `/oauth/token` returned a non-2xx status or malformed body |
|
|
178
|
+
| `missing_id_token` | Token response had no `id_token` (was `openid` in scopes?) |
|
|
179
|
+
| `id_token_invalid` | id_token failed verification — `.detail` carries the underlying `IdTokenError.code` |
|
|
180
|
+
| `jwks_fetch_failed` | JWKS endpoint unreachable or returned a malformed document |
|
|
181
|
+
| `network_error` | Transport-level failure talking to the issuer |
|
|
182
|
+
|
|
183
|
+
`IdTokenError.code` (raised by `verify_id_token` directly, or wrapped into
|
|
184
|
+
`ServerError("id_token_invalid", ...)` by `exchange_code_and_verify`):
|
|
185
|
+
|
|
186
|
+
`malformed`, `missing_kid`, `unknown_kid`, `bad_signature`, `iss_mismatch`,
|
|
187
|
+
`aud_mismatch`, `expired`, `nonce_mismatch`, `missing_claim`, `at_hash_mismatch`.
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
from logi_auth import ServerError
|
|
191
|
+
|
|
192
|
+
try:
|
|
193
|
+
result = LOGI.exchange_code_and_verify(code=code, nonce=nonce)
|
|
194
|
+
except ServerError as e:
|
|
195
|
+
logger.warning("logi sign-in failed: %s (%s)", e.code, e.detail)
|
|
196
|
+
return redirect(f"/login?error={e.code}")
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Security
|
|
200
|
+
|
|
201
|
+
`exchange_code_and_verify` performs the full id_token verification —
|
|
202
|
+
signature against the issuer's JWKS, `iss`, `aud` (against your `client_id`),
|
|
203
|
+
`exp`, and `nonce` — **before** it ever returns a `sub`. Verifying on the
|
|
204
|
+
server, not just trusting a client-supplied token, is what closes the
|
|
205
|
+
cross-client account-takeover class of bug: a frontend alone cannot prove that
|
|
206
|
+
an id_token it received was actually minted for *your* `client_id`.
|
|
207
|
+
|
|
208
|
+
## License
|
|
209
|
+
|
|
210
|
+
Apache-2.0
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# logi-auth (Python)
|
|
2
|
+
|
|
3
|
+
Server-side **"Sign in with logi"** for Python / Django backends — confidential
|
|
4
|
+
OAuth 2.0 Authorization Code exchange + **id_token (RS256) verification**. Only
|
|
5
|
+
dependency: `cryptography` (no HTTP client dependency — stdlib `urllib`).
|
|
6
|
+
|
|
7
|
+
This is the **confidential / backend** counterpart to the public-client SDKs
|
|
8
|
+
(browser, iOS, Android, Flutter). If your RP has a backend, verify on the
|
|
9
|
+
server with this package — do **not** rely on a client-side check.
|
|
10
|
+
|
|
11
|
+
> **Why it matters:** a backend that skips the id_token `aud` check can be
|
|
12
|
+
> tricked into accepting a token minted for a *different* client (cross-client
|
|
13
|
+
> account takeover — the launchcrew/krx incident). `exchange_code_and_verify`
|
|
14
|
+
> always verifies signature + iss + aud + exp + nonce before returning `sub`.
|
|
15
|
+
|
|
16
|
+
## Supported versions
|
|
17
|
+
|
|
18
|
+
| Requirement | Version |
|
|
19
|
+
|-------------|---------|
|
|
20
|
+
| **Python** | **>= 3.9** |
|
|
21
|
+
| **Django** | any — the package is framework-agnostic; use it from any view |
|
|
22
|
+
| Flask / FastAPI / etc. | any |
|
|
23
|
+
| Dependencies | `cryptography >= 41.0` |
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install logi-auth
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Django view example
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
# settings.py (or a dedicated logi_auth.py config module)
|
|
35
|
+
import os
|
|
36
|
+
from logi_auth import LogiAuthServer
|
|
37
|
+
|
|
38
|
+
LOGI = LogiAuthServer(
|
|
39
|
+
client_id=os.environ["LOGI_CLIENT_ID"],
|
|
40
|
+
client_secret=os.environ["LOGI_CLIENT_SECRET"], # confidential client
|
|
41
|
+
redirect_uri="https://app.example.com/auth/logi/callback",
|
|
42
|
+
)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
# views.py
|
|
47
|
+
import secrets
|
|
48
|
+
|
|
49
|
+
from django.http import HttpResponseBadRequest
|
|
50
|
+
from django.shortcuts import redirect
|
|
51
|
+
|
|
52
|
+
from logi_auth import ServerError
|
|
53
|
+
|
|
54
|
+
from .settings import LOGI
|
|
55
|
+
from .models import User
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def logi_start(request):
|
|
59
|
+
state = secrets.token_hex(16)
|
|
60
|
+
nonce = secrets.token_hex(16)
|
|
61
|
+
request.session["logi_state"] = state
|
|
62
|
+
request.session["logi_nonce"] = nonce
|
|
63
|
+
return redirect(LOGI.authorization_url(state=state, nonce=nonce))
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def logi_callback(request):
|
|
67
|
+
if request.GET.get("state") != request.session.pop("logi_state", None):
|
|
68
|
+
return HttpResponseBadRequest("state mismatch")
|
|
69
|
+
|
|
70
|
+
# The provider may redirect back with ?error=access_denied (user cancelled)
|
|
71
|
+
# and no `code` — handle that before the exchange instead of 500-ing.
|
|
72
|
+
if request.GET.get("error"):
|
|
73
|
+
return redirect(f"/login?error={request.GET['error']}")
|
|
74
|
+
code = request.GET.get("code")
|
|
75
|
+
if not code:
|
|
76
|
+
return HttpResponseBadRequest("missing authorization code")
|
|
77
|
+
|
|
78
|
+
try:
|
|
79
|
+
result = LOGI.exchange_code_and_verify(
|
|
80
|
+
code=code,
|
|
81
|
+
nonce=request.session.pop("logi_nonce", None),
|
|
82
|
+
)
|
|
83
|
+
except ServerError as e:
|
|
84
|
+
# result.sub is only ever set after signature+iss+aud+exp+nonce all pass.
|
|
85
|
+
return redirect(f"/login?error={e.code}")
|
|
86
|
+
|
|
87
|
+
# result.sub is the verified pairwise subject — key your User record on it.
|
|
88
|
+
user, _ = User.objects.get_or_create(logi_sub=result.sub, defaults={"email": result.email})
|
|
89
|
+
request.session["user_id"] = user.id
|
|
90
|
+
return redirect("/")
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Identity claims (email/name) are **not** guaranteed on the id_token — fetch
|
|
94
|
+
them from `GET {issuer}/oauth/userinfo` with the returned `access_token` as a
|
|
95
|
+
Bearer token if you need more than `sub`/`email`.
|
|
96
|
+
|
|
97
|
+
## Public client (PKCE, no secret)
|
|
98
|
+
|
|
99
|
+
Omit `client_secret` and pass a `code_challenge` / `code_verifier`:
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
logi = LogiAuthServer(client_id=client_id, redirect_uri=redirect_uri) # no secret
|
|
103
|
+
url = logi.authorization_url(state=state, nonce=nonce, code_challenge=code_challenge)
|
|
104
|
+
result = logi.exchange_code_and_verify(code=code, nonce=nonce, code_verifier=code_verifier)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## API
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from logi_auth import LogiAuthServer, LogiSession, verify_id_token, LogiAuthError, IdTokenError, ServerError
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### `LogiAuthServer(...)`
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
LogiAuthServer(
|
|
117
|
+
*,
|
|
118
|
+
client_id: str,
|
|
119
|
+
redirect_uri: str,
|
|
120
|
+
client_secret: str | None = None,
|
|
121
|
+
issuer: str = "https://api.1pass.dev",
|
|
122
|
+
token_issuer: str = "https://api.1pass.dev",
|
|
123
|
+
scopes: list[str] | None = None, # default: ["openid", "profile:basic", "email"]
|
|
124
|
+
jwks_cache_ttl: int = 3600,
|
|
125
|
+
)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
- **`.authorization_url(*, state, nonce, scopes=None, code_challenge=None, prompt=None) -> str`**
|
|
129
|
+
Builds the `/oauth/authorize` redirect URL.
|
|
130
|
+
- **`.exchange_code_and_verify(*, code, nonce, code_verifier=None) -> LogiSession`**
|
|
131
|
+
Exchanges the authorization `code` for tokens, then verifies the returned
|
|
132
|
+
`id_token` (signature via JWKS + `iss` + `aud` + `exp` + `nonce`, with a
|
|
133
|
+
transparent single JWKS refetch on key rotation) before returning a
|
|
134
|
+
`LogiSession`.
|
|
135
|
+
|
|
136
|
+
### `LogiSession`
|
|
137
|
+
|
|
138
|
+
Returned only once id_token verification has fully passed:
|
|
139
|
+
|
|
140
|
+
| Field | Type |
|
|
141
|
+
|-------|------|
|
|
142
|
+
| `sub` | `str` — verified pairwise subject |
|
|
143
|
+
| `email` | `str \| None` |
|
|
144
|
+
| `id_token` | `str` |
|
|
145
|
+
| `access_token` | `str` |
|
|
146
|
+
| `refresh_token` | `str \| None` |
|
|
147
|
+
| `expires_at` | `int \| None` — unix timestamp |
|
|
148
|
+
| `scope` | `str \| None` |
|
|
149
|
+
| `claims` | `dict` — full verified id_token claim set |
|
|
150
|
+
|
|
151
|
+
## Error handling
|
|
152
|
+
|
|
153
|
+
Both error types carry a `.code` string for programmatic branching (same
|
|
154
|
+
codes as the Ruby/Node/Web SDKs and the shared golden vectors).
|
|
155
|
+
|
|
156
|
+
`ServerError.code`:
|
|
157
|
+
|
|
158
|
+
| Code | Meaning |
|
|
159
|
+
|------|---------|
|
|
160
|
+
| `invalid_nonce` | Missing nonce — the sign-in session likely expired |
|
|
161
|
+
| `token_exchange_failed` | `/oauth/token` returned a non-2xx status or malformed body |
|
|
162
|
+
| `missing_id_token` | Token response had no `id_token` (was `openid` in scopes?) |
|
|
163
|
+
| `id_token_invalid` | id_token failed verification — `.detail` carries the underlying `IdTokenError.code` |
|
|
164
|
+
| `jwks_fetch_failed` | JWKS endpoint unreachable or returned a malformed document |
|
|
165
|
+
| `network_error` | Transport-level failure talking to the issuer |
|
|
166
|
+
|
|
167
|
+
`IdTokenError.code` (raised by `verify_id_token` directly, or wrapped into
|
|
168
|
+
`ServerError("id_token_invalid", ...)` by `exchange_code_and_verify`):
|
|
169
|
+
|
|
170
|
+
`malformed`, `missing_kid`, `unknown_kid`, `bad_signature`, `iss_mismatch`,
|
|
171
|
+
`aud_mismatch`, `expired`, `nonce_mismatch`, `missing_claim`, `at_hash_mismatch`.
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
from logi_auth import ServerError
|
|
175
|
+
|
|
176
|
+
try:
|
|
177
|
+
result = LOGI.exchange_code_and_verify(code=code, nonce=nonce)
|
|
178
|
+
except ServerError as e:
|
|
179
|
+
logger.warning("logi sign-in failed: %s (%s)", e.code, e.detail)
|
|
180
|
+
return redirect(f"/login?error={e.code}")
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Security
|
|
184
|
+
|
|
185
|
+
`exchange_code_and_verify` performs the full id_token verification —
|
|
186
|
+
signature against the issuer's JWKS, `iss`, `aud` (against your `client_id`),
|
|
187
|
+
`exp`, and `nonce` — **before** it ever returns a `sub`. Verifying on the
|
|
188
|
+
server, not just trusting a client-supplied token, is what closes the
|
|
189
|
+
cross-client account-takeover class of bug: a frontend alone cannot prove that
|
|
190
|
+
an id_token it received was actually minted for *your* `client_id`.
|
|
191
|
+
|
|
192
|
+
## License
|
|
193
|
+
|
|
194
|
+
Apache-2.0
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "logi-auth"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Server-side \"Sign in with logi\" for Python/Django — confidential OAuth 2.0 + id_token (RS256) verification."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
authors = [{ name = "Dcode" }]
|
|
13
|
+
keywords = ["logi", "1pass", "oauth", "oidc", "django", "auth", "openid", "id-token"]
|
|
14
|
+
dependencies = ["cryptography>=41.0"]
|
|
15
|
+
|
|
16
|
+
[project.urls]
|
|
17
|
+
Homepage = "https://github.com/dcode-co/logi-auth-python"
|
|
18
|
+
Issues = "https://github.com/dcode-co/logi-auth-python/issues"
|
|
19
|
+
|
|
20
|
+
[tool.setuptools.packages.find]
|
|
21
|
+
where = ["src"]
|
|
22
|
+
|
|
23
|
+
[tool.setuptools.dynamic]
|
|
24
|
+
version = { attr = "logi_auth.__version__" }
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""logi_auth — server-side "Sign in with logi" for Python / Django backends.
|
|
2
|
+
|
|
3
|
+
Confidential OAuth 2.0 code exchange + id_token (RS256) verification. Same
|
|
4
|
+
safety contract as the iOS/Android/Web/Flutter/Node/Ruby SDKs (shared golden
|
|
5
|
+
vectors).
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from .errors import IdTokenError, LogiAuthError, ServerError
|
|
9
|
+
from .id_token_verifier import verify_id_token
|
|
10
|
+
from .server import LogiAuthServer, LogiSession
|
|
11
|
+
|
|
12
|
+
__version__ = "1.0.1"
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"LogiAuthServer",
|
|
16
|
+
"LogiSession",
|
|
17
|
+
"verify_id_token",
|
|
18
|
+
"LogiAuthError",
|
|
19
|
+
"IdTokenError",
|
|
20
|
+
"ServerError",
|
|
21
|
+
"__version__",
|
|
22
|
+
]
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"""Error types for logi_auth."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class LogiAuthError(Exception):
|
|
5
|
+
"""Base for all errors raised by this package."""
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class IdTokenError(LogiAuthError):
|
|
9
|
+
"""id_token verification failure.
|
|
10
|
+
|
|
11
|
+
``code`` mirrors the Web verifier and the golden-vector strings exactly
|
|
12
|
+
(e.g. ``"bad_signature"``, ``"aud_mismatch"``).
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
CODES = (
|
|
16
|
+
"malformed",
|
|
17
|
+
"missing_kid",
|
|
18
|
+
"unknown_kid",
|
|
19
|
+
"bad_signature",
|
|
20
|
+
"iss_mismatch",
|
|
21
|
+
"aud_mismatch",
|
|
22
|
+
"expired",
|
|
23
|
+
"nonce_mismatch",
|
|
24
|
+
"missing_claim",
|
|
25
|
+
"at_hash_mismatch",
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
def __init__(self, code: str):
|
|
29
|
+
self.code = code
|
|
30
|
+
super().__init__(f"id_token verification failed: {code}")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class ServerError(LogiAuthError):
|
|
34
|
+
"""OAuth / transport failure raised by :class:`LogiAuthServer`."""
|
|
35
|
+
|
|
36
|
+
def __init__(self, code: str, message: str, detail=None):
|
|
37
|
+
self.code = code
|
|
38
|
+
self.detail = detail
|
|
39
|
+
super().__init__(message)
|