nextlayer-sdk-python 1.2.1__tar.gz → 1.3.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.
- {nextlayer_sdk_python-1.2.1 → nextlayer_sdk_python-1.3.1}/PKG-INFO +29 -1
- {nextlayer_sdk_python-1.2.1 → nextlayer_sdk_python-1.3.1}/README.md +27 -0
- {nextlayer_sdk_python-1.2.1 → nextlayer_sdk_python-1.3.1}/pyproject.toml +1 -1
- {nextlayer_sdk_python-1.2.1 → nextlayer_sdk_python-1.3.1}/src/nextlayer/sdk/__init__.py +1 -1
- {nextlayer_sdk_python-1.2.1 → nextlayer_sdk_python-1.3.1}/src/nextlayer/sdk/auth.py +65 -8
- {nextlayer_sdk_python-1.2.1 → nextlayer_sdk_python-1.3.1}/src/nextlayer/sdk/__init__.pye +0 -0
- {nextlayer_sdk_python-1.2.1 → nextlayer_sdk_python-1.3.1}/src/nextlayer/sdk/auth_code_browser_flow.py +0 -0
- {nextlayer_sdk_python-1.2.1 → nextlayer_sdk_python-1.3.1}/src/nextlayer/sdk/auth_httpx.py +0 -0
- {nextlayer_sdk_python-1.2.1 → nextlayer_sdk_python-1.3.1}/src/nextlayer/sdk/errors.py +0 -0
- {nextlayer_sdk_python-1.2.1 → nextlayer_sdk_python-1.3.1}/src/nextlayer/sdk/utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: nextlayer-sdk-python
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.3.1
|
|
4
4
|
Summary: Client utilities to interact with next layer public APIs
|
|
5
5
|
Author: Wolfgang Powisch
|
|
6
6
|
Author-email: wolfgang.powisch@nextlayer.at
|
|
@@ -12,6 +12,7 @@ Classifier: Programming Language :: Python :: 3.11
|
|
|
12
12
|
Classifier: Programming Language :: Python :: 3.12
|
|
13
13
|
Classifier: Programming Language :: Python :: 3.13
|
|
14
14
|
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
15
16
|
Requires-Dist: PyYAML (>=6.0,<7.0)
|
|
16
17
|
Requires-Dist: httpx (>=0.28.1,<0.29.0)
|
|
17
18
|
Requires-Dist: pydantic (>=2.12.5,<3.0.0)
|
|
@@ -51,6 +52,10 @@ variable or directly passed to the `NlAuth` constructor.
|
|
|
51
52
|
Alternatively the password can be suppliend in the `NEXTLAYERSDK_PASSWORD` environment
|
|
52
53
|
variable.
|
|
53
54
|
|
|
55
|
+
For machine-to-machine authentication (no user account), a `client_secret` can be
|
|
56
|
+
supplied instead of `username`/`password` - see
|
|
57
|
+
[Client Credentials Grant](#client-credentials-grant-service-accounts) below.
|
|
58
|
+
|
|
54
59
|
## Usage
|
|
55
60
|
|
|
56
61
|
```python
|
|
@@ -86,6 +91,29 @@ client = httpx.Client(
|
|
|
86
91
|
print(client.get("/users/self").json())
|
|
87
92
|
```
|
|
88
93
|
|
|
94
|
+
### Client Credentials Grant (service accounts)
|
|
95
|
+
|
|
96
|
+
For machine-to-machine authentication without a user account, supply a
|
|
97
|
+
`client_secret` instead of `username`/`password`:
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
nlauth = NlAuth(client_id="your_id", client_secret="topsecret")
|
|
101
|
+
access_token = nlauth.get_access_token()
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The `client_id`/`client_secret` can also be set via the `NEXTLAYERSDK_CLIENT_ID`/
|
|
105
|
+
`NEXTLAYERSDK_CLIENT_SECRET` environment variables.
|
|
106
|
+
Whenever a `client_secret` is present (non-empty), `NlAuth` auto-detects it and uses
|
|
107
|
+
the OAuth2 `client_credentials` grant instead of the password grant - no `username`
|
|
108
|
+
is needed or used. It is mutually exclusive with `browser_login=True` (passing both
|
|
109
|
+
raises `ValueError`).
|
|
110
|
+
|
|
111
|
+
Note: `client_secret` is deliberately **not** exposed as a `nextlayer-auth` CLI flag -
|
|
112
|
+
command-line arguments are visible to other local users via `ps`/process listings and
|
|
113
|
+
get persisted in shell history. Use the environment variable, the config file, or the
|
|
114
|
+
constructor param instead.
|
|
115
|
+
|
|
116
|
+
|
|
89
117
|
## Usage fron commandline
|
|
90
118
|
|
|
91
119
|
The package also installs a commandline utility `nextlayer-auth` which takes care
|
|
@@ -30,6 +30,10 @@ variable or directly passed to the `NlAuth` constructor.
|
|
|
30
30
|
Alternatively the password can be suppliend in the `NEXTLAYERSDK_PASSWORD` environment
|
|
31
31
|
variable.
|
|
32
32
|
|
|
33
|
+
For machine-to-machine authentication (no user account), a `client_secret` can be
|
|
34
|
+
supplied instead of `username`/`password` - see
|
|
35
|
+
[Client Credentials Grant](#client-credentials-grant-service-accounts) below.
|
|
36
|
+
|
|
33
37
|
## Usage
|
|
34
38
|
|
|
35
39
|
```python
|
|
@@ -65,6 +69,29 @@ client = httpx.Client(
|
|
|
65
69
|
print(client.get("/users/self").json())
|
|
66
70
|
```
|
|
67
71
|
|
|
72
|
+
### Client Credentials Grant (service accounts)
|
|
73
|
+
|
|
74
|
+
For machine-to-machine authentication without a user account, supply a
|
|
75
|
+
`client_secret` instead of `username`/`password`:
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
nlauth = NlAuth(client_id="your_id", client_secret="topsecret")
|
|
79
|
+
access_token = nlauth.get_access_token()
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
The `client_id`/`client_secret` can also be set via the `NEXTLAYERSDK_CLIENT_ID`/
|
|
83
|
+
`NEXTLAYERSDK_CLIENT_SECRET` environment variables.
|
|
84
|
+
Whenever a `client_secret` is present (non-empty), `NlAuth` auto-detects it and uses
|
|
85
|
+
the OAuth2 `client_credentials` grant instead of the password grant - no `username`
|
|
86
|
+
is needed or used. It is mutually exclusive with `browser_login=True` (passing both
|
|
87
|
+
raises `ValueError`).
|
|
88
|
+
|
|
89
|
+
Note: `client_secret` is deliberately **not** exposed as a `nextlayer-auth` CLI flag -
|
|
90
|
+
command-line arguments are visible to other local users via `ps`/process listings and
|
|
91
|
+
get persisted in shell history. Use the environment variable, the config file, or the
|
|
92
|
+
constructor param instead.
|
|
93
|
+
|
|
94
|
+
|
|
68
95
|
## Usage fron commandline
|
|
69
96
|
|
|
70
97
|
The package also installs a commandline utility `nextlayer-auth` which takes care
|
|
@@ -3,7 +3,7 @@ name = "nextlayer-sdk-python"
|
|
|
3
3
|
packages = [
|
|
4
4
|
{ include = "nextlayer", from="src" }
|
|
5
5
|
]
|
|
6
|
-
version = "1.
|
|
6
|
+
version = "1.3.1"
|
|
7
7
|
description = "Client utilities to interact with next layer public APIs"
|
|
8
8
|
readme = "README.md"
|
|
9
9
|
authors = ["Wolfgang Powisch <wolfgang.powisch@nextlayer.at>"]
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
# do not edit this version line, it will be updated in CI-Pipeline!
|
|
2
|
-
__version__ = "v1.
|
|
2
|
+
__version__ = "v1.3.1"
|
|
@@ -12,7 +12,7 @@ from typing import Any
|
|
|
12
12
|
import jwt
|
|
13
13
|
import yaml
|
|
14
14
|
from keycloak import KeycloakOpenID
|
|
15
|
-
from keycloak.exceptions import
|
|
15
|
+
from keycloak.exceptions import KeycloakOperationError
|
|
16
16
|
from pydantic import BaseModel, Field
|
|
17
17
|
|
|
18
18
|
from . import auth_code_browser_flow, errors, utils
|
|
@@ -44,6 +44,7 @@ class PersistentConfig(BaseModel):
|
|
|
44
44
|
client_id: str = DEFAULT_CLIENT_ID
|
|
45
45
|
username: str | None = None
|
|
46
46
|
password: str | None = None
|
|
47
|
+
client_secret: str | None = None
|
|
47
48
|
extra_params: dict[str, Any] = Field(default_factory=dict)
|
|
48
49
|
|
|
49
50
|
# state updated by NlAuth itself:
|
|
@@ -114,19 +115,24 @@ class NlAuth(object):
|
|
|
114
115
|
realm_name: str | None = None,
|
|
115
116
|
client_id: str | None = None,
|
|
116
117
|
username: str | None = None,
|
|
118
|
+
client_secret: str | None = None,
|
|
117
119
|
ask_totp=False,
|
|
118
120
|
browser_login=False,
|
|
119
121
|
# optional customizations of pre-defined defaults:
|
|
120
122
|
config_defaults: PersistentConfig | None = None,
|
|
121
123
|
browser_login_redirect_uri: str | None = None,
|
|
124
|
+
client_id_envvar="NEXTLAYERSDK_CLIENT_ID",
|
|
122
125
|
username_envvar="NEXTLAYERSDK_USERNAME",
|
|
123
126
|
password_envvar="NEXTLAYERSDK_PASSWORD",
|
|
127
|
+
client_secret_envvar="NEXTLAYERSDK_CLIENT_SECRET",
|
|
124
128
|
):
|
|
125
129
|
if config_defaults is None:
|
|
126
130
|
config_defaults = PersistentConfig()
|
|
127
131
|
|
|
132
|
+
self.client_id_envvar = client_id_envvar
|
|
128
133
|
self.username_envvar = username_envvar
|
|
129
134
|
self.password_envvar = password_envvar
|
|
135
|
+
self.client_secret_envvar = client_secret_envvar
|
|
130
136
|
|
|
131
137
|
self.ask_totp = ask_totp
|
|
132
138
|
self.browser_login = browser_login
|
|
@@ -139,12 +145,24 @@ class NlAuth(object):
|
|
|
139
145
|
|
|
140
146
|
new_server_url = (server_url or self.store.config.server_url).rstrip("/") + "/"
|
|
141
147
|
new_realm_name = realm_name or self.store.config.realm_name
|
|
142
|
-
new_client_id =
|
|
148
|
+
new_client_id = (
|
|
149
|
+
client_id
|
|
150
|
+
or os.environ.get(self.client_id_envvar)
|
|
151
|
+
or self.store.config.client_id
|
|
152
|
+
)
|
|
143
153
|
self.username = (
|
|
144
154
|
username
|
|
145
155
|
or os.environ.get(self.username_envvar)
|
|
146
156
|
or self.store.config.username
|
|
147
157
|
)
|
|
158
|
+
self.client_secret = (
|
|
159
|
+
client_secret
|
|
160
|
+
or os.environ.get(self.client_secret_envvar)
|
|
161
|
+
or self.store.config.client_secret
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
if self.browser_login and self.client_secret:
|
|
165
|
+
raise ValueError("browser_login and client_secret are mutually exclusive")
|
|
148
166
|
|
|
149
167
|
self.store.config.server_url = new_server_url
|
|
150
168
|
self.store.config.realm_name = new_realm_name
|
|
@@ -152,12 +170,16 @@ class NlAuth(object):
|
|
|
152
170
|
|
|
153
171
|
if self.username:
|
|
154
172
|
self.store.config.username = self.username
|
|
173
|
+
# unlike username/client_id, client_secret is intentionally never written
|
|
174
|
+
# back into self.store.config here - like password, it must not be
|
|
175
|
+
# persisted to the YAML config file in plaintext.
|
|
155
176
|
|
|
156
177
|
# Configure client
|
|
157
178
|
self.keycloak_openid = KeycloakOpenID(
|
|
158
179
|
server_url=new_server_url,
|
|
159
180
|
client_id=new_client_id,
|
|
160
181
|
realm_name=new_realm_name,
|
|
182
|
+
client_secret_key=self.client_secret,
|
|
161
183
|
verify=True,
|
|
162
184
|
)
|
|
163
185
|
|
|
@@ -179,6 +201,10 @@ class NlAuth(object):
|
|
|
179
201
|
def refresh_expired(self) -> bool:
|
|
180
202
|
now = int(time.time())
|
|
181
203
|
exp_at = self.store.config.refresh_expires_at
|
|
204
|
+
if exp_at == 0:
|
|
205
|
+
# sentinel set by update_tokens(): refresh token has no expiry
|
|
206
|
+
# (no `exp` claim) - which is valid per the JWT/OAuth2 spec
|
|
207
|
+
return False
|
|
182
208
|
is_expired = not exp_at or exp_at <= now
|
|
183
209
|
if is_expired and exp_at:
|
|
184
210
|
log.debug(f"refresh_token is expired {exp_at!r}")
|
|
@@ -198,14 +224,27 @@ class NlAuth(object):
|
|
|
198
224
|
|
|
199
225
|
def update_tokens(self, tokens: dict[str, Any]) -> str:
|
|
200
226
|
log.debug(
|
|
201
|
-
f"update_tokens: expires_in={tokens['expires_in']!r} refresh_expires_in={tokens
|
|
227
|
+
f"update_tokens: expires_in={tokens['expires_in']!r} refresh_expires_in={tokens.get('refresh_expires_in')!r}"
|
|
202
228
|
)
|
|
203
229
|
self.store.config.expires_at = int(time.time()) + int(
|
|
204
230
|
tokens["expires_in"] * 0.75
|
|
205
231
|
)
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
232
|
+
refresh_expires_in = tokens.get("refresh_expires_in")
|
|
233
|
+
if refresh_expires_in is None:
|
|
234
|
+
# client_credentials grant responses typically have no refresh_token /
|
|
235
|
+
# refresh_expires_in at all - there is nothing to refresh in that case,
|
|
236
|
+
# so None (rather than some made-up default) is the correct value here
|
|
237
|
+
self.store.config.refresh_expires_at = None
|
|
238
|
+
elif refresh_expires_in == 0:
|
|
239
|
+
# refresh_expires_in == 0 means the refresh token itself has no
|
|
240
|
+
# `exp` claim / never expires - perfectly valid per the JWT/OAuth2
|
|
241
|
+
# spec and must not be treated as "already expired" (0 is used as
|
|
242
|
+
# a sentinel for "never expires", see refresh_expired())
|
|
243
|
+
self.store.config.refresh_expires_at = 0
|
|
244
|
+
else:
|
|
245
|
+
self.store.config.refresh_expires_at = int(time.time()) + int(
|
|
246
|
+
refresh_expires_in * 0.75
|
|
247
|
+
)
|
|
209
248
|
self.store.config.tokens = tokens
|
|
210
249
|
return tokens["access_token"]
|
|
211
250
|
|
|
@@ -218,6 +257,12 @@ class NlAuth(object):
|
|
|
218
257
|
self.keycloak_openid,
|
|
219
258
|
redirect_uri=self.browser_login_redirect_uri,
|
|
220
259
|
)
|
|
260
|
+
elif self.client_secret:
|
|
261
|
+
extra = self.store.config.extra_params
|
|
262
|
+
log.debug("make keycloak token-request via client_credentials grant")
|
|
263
|
+
tokens = self.keycloak_openid.token(
|
|
264
|
+
grant_type="client_credentials", **extra
|
|
265
|
+
)
|
|
221
266
|
else:
|
|
222
267
|
password = (
|
|
223
268
|
password
|
|
@@ -270,9 +315,12 @@ class NlAuth(object):
|
|
|
270
315
|
tokens = self.keycloak_openid.refresh_token(
|
|
271
316
|
(self.store.config.tokens or {}).get("refresh_token") or ""
|
|
272
317
|
)
|
|
273
|
-
except
|
|
318
|
+
except KeycloakOperationError as e:
|
|
274
319
|
# e.g., 400: b'{"error":"invalid_grant", "error_description":"Session not active"}'
|
|
275
320
|
# ... when session has been deleted in Keycloak and refresh_token cannot be used anymore
|
|
321
|
+
# note: the keycloak-python client raises KeycloakPostError (not
|
|
322
|
+
# KeycloakGetError) here - KeycloakOperationError is their common
|
|
323
|
+
# base class, so this also covers that case
|
|
276
324
|
sys.stderr.write("token refresh failed: %s\n" % (e,))
|
|
277
325
|
return self.do_login()
|
|
278
326
|
|
|
@@ -287,7 +335,12 @@ class NlAuth(object):
|
|
|
287
335
|
self.store.reload_if_modified()
|
|
288
336
|
except FileNotFoundError:
|
|
289
337
|
pass
|
|
290
|
-
if self.
|
|
338
|
+
if self.client_secret and self.token_expired():
|
|
339
|
+
# client_credentials grant tokens come without a refresh_token
|
|
340
|
+
# (see do_login()), so request a fresh one directly instead of
|
|
341
|
+
# attempting a refresh that would only fail
|
|
342
|
+
access_token = self.do_login()
|
|
343
|
+
elif not self.client_secret and self.refresh_expired():
|
|
291
344
|
access_token = self.do_login()
|
|
292
345
|
elif self.token_expired():
|
|
293
346
|
access_token = self.do_refresh()
|
|
@@ -340,6 +393,10 @@ def parse_commandline_args():
|
|
|
340
393
|
"-i", "--client-id", help=f"keycloak client_id - default: {DEFAULT_CLIENT_ID}"
|
|
341
394
|
)
|
|
342
395
|
parser.add_argument("-u", "--username", help="username")
|
|
396
|
+
# Deliberately no --client-secret CLI flag: command-line arguments are visible
|
|
397
|
+
# to other local users via `ps`/process listings and get persisted in shell
|
|
398
|
+
# history. Use the NEXTLAYERSDK_CLIENT_SECRET environment variable, the config
|
|
399
|
+
# file, or the NlAuth(client_secret=...) constructor param instead.
|
|
343
400
|
parser.add_argument("-a", "--aud", help="set audience - default: not set")
|
|
344
401
|
parser.add_argument(
|
|
345
402
|
"-t",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|