oauth
oauth
¶
Shared OAuth 2.0 helpers for all connectors.
Provides:
- OAuthProvider registry with configs for Google, Strava, Spotify
- Generic run_connector_oauth() that opens browser + catches callback
- URL builder, token persistence, and token cleanup utilities
Classes¶
OAuthProvider
dataclass
¶
OAuthProvider(name: str, display_name: str, auth_endpoint: str, token_endpoint: str, scopes: List[str], setup_url: str, setup_hint: str, callback_port: int = 8789, callback_host: str = '127.0.0.1', callback_path: str = '/callback', token_auth: str = 'body', extra_auth_params: Dict[str, str] = dict(), connector_ids: Tuple[str, ...] = (), credential_files: Tuple[str, ...] = ())
Configuration for an OAuth 2.0 provider.
Functions¶
get_provider_for_connector
¶
get_provider_for_connector(connector_id: str) -> Optional[OAuthProvider]
Return the OAuthProvider that covers connector_id, or None.
Source code in src/openjarvis/connectors/oauth.py
get_client_credentials
¶
get_client_credentials(provider: OAuthProvider) -> Optional[Tuple[str, str]]
Load stored client_id and client_secret for provider.
Checks credential files in ~/.openjarvis/connectors/ and falls
back to environment variables OPENJARVIS_{NAME}_CLIENT_ID and
OPENJARVIS_{NAME}_CLIENT_SECRET.
Source code in src/openjarvis/connectors/oauth.py
save_client_credentials
¶
save_client_credentials(provider: OAuthProvider, client_id: str, client_secret: str) -> None
Persist client credentials so the user never has to enter them again.
Source code in src/openjarvis/connectors/oauth.py
build_google_auth_url
¶
build_google_auth_url(client_id: str, redirect_uri: str = _DEFAULT_REDIRECT_URI, scopes: Optional[List[str]] = None) -> str
Build a Google OAuth2 consent URL.
| PARAMETER | DESCRIPTION |
|---|---|
client_id
|
The OAuth 2.0 client ID from the Google Cloud Console.
TYPE:
|
redirect_uri
|
Where Google should redirect after consent. Defaults to the local
callback server at
TYPE:
|
scopes
|
List of OAuth scopes to request. Defaults to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Full consent URL including query string. |
Source code in src/openjarvis/connectors/oauth.py
resolve_google_credentials
¶
Return the best available Google credentials file path.
Checks the connector-specific file first, then falls back to the
shared google.json. Returns connector_path if neither exists
(so is_connected() correctly returns False).
Source code in src/openjarvis/connectors/oauth.py
load_tokens
¶
Load OAuth tokens from a JSON file.
Returns None if the file is missing, unreadable, or contains
invalid JSON.
Source code in src/openjarvis/connectors/oauth.py
save_tokens
¶
Persist tokens to path as JSON with owner-only (0o600) permissions.
Creates parent directories as needed.
Source code in src/openjarvis/connectors/oauth.py
delete_tokens
¶
exchange_google_token
¶
exchange_google_token(code: str, client_id: str, client_secret: str, redirect_uri: str = _DEFAULT_REDIRECT_URI) -> Dict[str, Any]
Exchange an authorization code for access + refresh tokens.
| PARAMETER | DESCRIPTION |
|---|---|
code
|
The authorization code received from Google's consent redirect.
TYPE:
|
client_id
|
OAuth 2.0 client ID.
TYPE:
|
client_secret
|
OAuth 2.0 client secret.
TYPE:
|
redirect_uri
|
Must match the redirect URI used when obtaining the auth code.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
Token response containing |
Source code in src/openjarvis/connectors/oauth.py
run_oauth_flow
¶
run_oauth_flow(client_id: str, client_secret: str, scopes: List[str], credentials_path: str, redirect_uri: str = _DEFAULT_REDIRECT_URI) -> Dict[str, Any]
Run the full OAuth flow: browser consent, callback, token exchange.
Steps:
- Build consent URL
- Start localhost callback server
- Open browser to consent URL
- Wait for Google to redirect with
?code=... - Exchange code for
access_token+refresh_token - Save tokens to credentials_path
- Return the tokens dict
| PARAMETER | DESCRIPTION |
|---|---|
client_id
|
OAuth 2.0 client ID.
TYPE:
|
client_secret
|
OAuth 2.0 client secret.
TYPE:
|
scopes
|
List of OAuth scopes to request.
TYPE:
|
credentials_path
|
Where to persist the resulting tokens.
TYPE:
|
redirect_uri
|
Local callback URI. Defaults to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
Token response from Google ( |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If the user denies authorization or the callback times out. |
Source code in src/openjarvis/connectors/oauth.py
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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 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 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | |
run_connector_oauth
¶
run_connector_oauth(connector_id: str, client_id: str = '', client_secret: str = '') -> Dict[str, Any]
Run a complete OAuth flow for connector_id.
- Look up the
OAuthProvider - Resolve client credentials (arg → stored → env)
- Build auth URL and open the user's browser
- Start localhost callback server and wait for the code
- Exchange the code for tokens
- Save tokens to all relevant credential files
Returns the raw token response dict.