|
| 1 | +import logging |
| 2 | +import time |
| 3 | +from typing import Any, Optional |
| 4 | + |
| 5 | +import httpx |
| 6 | +import typer |
| 7 | +from pydantic import BaseModel |
| 8 | + |
| 9 | +from fastapi_cli.config import settings |
| 10 | +from fastapi_cli.utils.api import APIClient |
| 11 | +from fastapi_cli.utils.auth import AuthConfig, write_auth_config |
| 12 | +from fastapi_cli.utils.cli import get_rich_toolkit |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class AuthorizationData(BaseModel): |
| 18 | + user_code: str |
| 19 | + device_code: str |
| 20 | + verification_uri: str |
| 21 | + verification_uri_complete: str |
| 22 | + interval: int = 5 |
| 23 | + |
| 24 | + |
| 25 | +class TokenResponse(BaseModel): |
| 26 | + access_token: str |
| 27 | + |
| 28 | + |
| 29 | +def _start_device_authorization( |
| 30 | + client: httpx.Client, |
| 31 | +) -> Optional[AuthorizationData]: |
| 32 | + try: |
| 33 | + response = client.post( |
| 34 | + "/login/device/authorization", data={"client_id": settings.client_id} |
| 35 | + ) |
| 36 | + |
| 37 | + response.raise_for_status() |
| 38 | + |
| 39 | + except httpx.HTTPError as e: |
| 40 | + logger.debug("Error: %s", e) |
| 41 | + |
| 42 | + return None |
| 43 | + |
| 44 | + return AuthorizationData.model_validate(response.json()) |
| 45 | + |
| 46 | + |
| 47 | +def _fetch_access_token( |
| 48 | + client: httpx.Client, device_code: str, interval: int |
| 49 | +) -> Optional[str]: |
| 50 | + while True: |
| 51 | + response = client.post( |
| 52 | + "/login/device/token", |
| 53 | + data={ |
| 54 | + "device_code": device_code, |
| 55 | + "client_id": settings.client_id, |
| 56 | + "grant_type": "urn:ietf:params:oauth:grant-type:device_code", |
| 57 | + }, |
| 58 | + ) |
| 59 | + |
| 60 | + if response.status_code not in (200, 400): |
| 61 | + logger.debug("Error: %s", response.json()) |
| 62 | + return None |
| 63 | + |
| 64 | + if response.status_code == 400: |
| 65 | + data = response.json() |
| 66 | + |
| 67 | + if data.get("error") != "authorization_pending": |
| 68 | + logger.debug("Error: %s", data) |
| 69 | + return None |
| 70 | + |
| 71 | + if response.status_code == 200: |
| 72 | + break |
| 73 | + |
| 74 | + time.sleep(interval) |
| 75 | + |
| 76 | + response_data = TokenResponse.model_validate(response.json()) |
| 77 | + |
| 78 | + return response_data.access_token |
| 79 | + |
| 80 | + |
| 81 | +def login() -> Any: |
| 82 | + """ |
| 83 | + Login to FastAPI Cloud. 🚀 |
| 84 | + """ |
| 85 | + with get_rich_toolkit() as toolkit, APIClient() as client: |
| 86 | + toolkit.print_title("Login to FastAPI Cloud", tag="FastAPI") |
| 87 | + |
| 88 | + toolkit.print_line() |
| 89 | + |
| 90 | + with toolkit.progress("Starting authorization") as progress: |
| 91 | + authorization_data = _start_device_authorization(client) |
| 92 | + |
| 93 | + if authorization_data is None: |
| 94 | + progress.set_error( |
| 95 | + "Something went wrong while contacting the FastAPI Cloud server. Please try again later." |
| 96 | + ) |
| 97 | + |
| 98 | + raise typer.Exit(1) |
| 99 | + |
| 100 | + url = authorization_data.verification_uri_complete |
| 101 | + |
| 102 | + progress.log(f"Opening {url}") |
| 103 | + |
| 104 | + toolkit.print_line() |
| 105 | + |
| 106 | + with toolkit.progress("Waiting for user to authorize...") as progress: |
| 107 | + typer.launch(url) |
| 108 | + |
| 109 | + access_token = _fetch_access_token( |
| 110 | + client, authorization_data.device_code, authorization_data.interval |
| 111 | + ) |
| 112 | + |
| 113 | + if access_token is None: |
| 114 | + progress.set_error( |
| 115 | + "Something went wrong while contacting the FastAPI Cloud server. Please try again later." |
| 116 | + ) |
| 117 | + |
| 118 | + raise typer.Exit(1) |
| 119 | + |
| 120 | + write_auth_config(AuthConfig(access_token=access_token)) |
| 121 | + |
| 122 | + progress.log("Now you are logged in! 🚀") |
0 commit comments