Authentication
Authentication utilities for Prusa Connect.
This module mimics the pattern found in google-auth.
It provides a Credentials object that can automatically refresh tokens
and attach headers to requests.
How to use the most important parts:
- PrusaConnectCredentials: The main credentials object for interacting with the SDK. Pass this to
PrusaConnectClient(credentials=...).
- interactive_login: A helper function to kick off an interactive OAuth2 flow, returning a JSON serializable dict
with access and refresh tokens.
- get_default_token_path: Helper to determine the standard configuration path to persist tokens locally.
| FUNCTION | DESCRIPTION |
|---|---|
get_default_token_path |
Returns the platform-specific path for the token file using platformdirs. |
interactive_login |
Performs the full PKCE login flow, including screen scraping and TOTP. |
PrusaAccessToken
Bases: PrusaJwtModel
Structure of the Access Token.
Source code in src/prusa/connect/client/auth.py
102 103 104 105 106 107 108 109 110 111 | |
PrusaConnectCredentials
Authentication credentials that allow making authorized API calls.
This class manages the lifecycle of the access token, including automatic refreshing when expired.
| METHOD | DESCRIPTION |
|---|---|
__init__ |
Initialize credentials. |
before_request |
Injects the Authorization header into the request headers. |
from_env |
Factory: Load credentials from environment variables. |
from_file |
Factory: Load credentials from a JSON file. |
load_default |
Factory: Attempt to load credentials from default locations. |
refresh |
Forces a token refresh using the refresh token. |
Source code in src/prusa/connect/client/auth.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
valid
property
Checks if the current access token is valid (not expired).
__init__(token_info, token_saver=None)
Initialize credentials.
| PARAMETER | DESCRIPTION |
|---|---|
token_info
|
Dictionary or PrusaJWTTokenSet containing access_token, etc.
TYPE:
|
token_saver
|
Optional callback executed when tokens are refreshed (to save to disk).
TYPE:
|
Source code in src/prusa/connect/client/auth.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
before_request(headers)
Injects the Authorization header into the request headers.
Refreshes the token automatically if needed.
Source code in src/prusa/connect/client/auth.py
269 270 271 272 273 274 275 276 277 | |
from_env()
classmethod
Factory: Load credentials from environment variables.
Checks: 1. PRUSA_TOKENS_JSON: A JSON string containing the full token set. 2. PRUSA_TOKEN: A raw Access Token (JWT).
Source code in src/prusa/connect/client/auth.py
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | |
from_file(path)
classmethod
Factory: Load credentials from a JSON file.
Source code in src/prusa/connect/client/auth.py
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |
load_default()
classmethod
Factory: Attempt to load credentials from default locations.
Priority: 1. Environment Variables (PRUSA_TOKENS_JSON, PRUSA_TOKEN) 2. Platform default config file
Source code in src/prusa/connect/client/auth.py
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
refresh()
Forces a token refresh using the refresh token.
Source code in src/prusa/connect/client/auth.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
PrusaIdentityToken
Bases: PrusaJwtModel
Structure of the Identity Token.
Source code in src/prusa/connect/client/auth.py
125 126 127 128 129 130 131 132 133 | |
PrusaJWTTokenSet
Bases: BaseModel
JWT token data structure.
| METHOD | DESCRIPTION |
|---|---|
dump_tokens |
Returns the raw tokens as a dictionary, suitable for saving to disk. |
Source code in src/prusa/connect/client/auth.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
dump_tokens()
Returns the raw tokens as a dictionary, suitable for saving to disk.
Source code in src/prusa/connect/client/auth.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
PrusaJwtModel
Bases: BaseModel
Base model for JWT-based tokens that can parse raw strings.
| METHOD | DESCRIPTION |
|---|---|
__init__ |
Allows initializing with a raw JWT string. |
parse_jwt_string |
Parses a raw JWT string into a dictionary of claims. |
Source code in src/prusa/connect/client/auth.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
__init__(token=None, /, **data)
Allows initializing with a raw JWT string.
Source code in src/prusa/connect/client/auth.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
parse_jwt_string(data)
classmethod
Parses a raw JWT string into a dictionary of claims.
Source code in src/prusa/connect/client/auth.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
PrusaRefreshToken
Bases: PrusaJwtModel
Structure of the Refresh Token.
Source code in src/prusa/connect/client/auth.py
114 115 116 117 118 119 120 121 122 | |
get_default_token_path()
Returns the platform-specific path for the token file using platformdirs.
Source code in src/prusa/connect/client/auth.py
185 186 187 188 189 | |
interactive_login(email, password, otp_callback)
Performs the full PKCE login flow, including screen scraping and TOTP.
| PARAMETER | DESCRIPTION |
|---|---|
email
|
Prusa account email.
TYPE:
|
password
|
Prusa account password.
TYPE:
|
otp_callback
|
A function that returns the 6-digit 2FA code if requested.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PrusaJWTTokenSet
|
A dict containing the token response (access_token, refresh_token, etc). |
| RAISES | DESCRIPTION |
|---|---|
PrusaAuthError
|
If login fails. |
Source code in src/prusa/connect/client/auth.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | |