|
| 1 | +# /// script |
| 2 | +# dependencies = [ |
| 3 | +# "httpx", |
| 4 | +# ] |
| 5 | +# /// |
| 6 | + |
| 7 | +import argparse |
| 8 | +import uuid |
| 9 | + |
| 10 | +import httpx |
| 11 | + |
| 12 | + |
| 13 | +def fetch_token(args): |
| 14 | + # Get auth cookie via password login. |
| 15 | + client = httpx.Client() |
| 16 | + client.post( |
| 17 | + f"{args.host}/v1/login/{args.silo}/local", |
| 18 | + json={"username": args.username, "password": args.password}, |
| 19 | + ).raise_for_status() |
| 20 | + |
| 21 | + # Start the device auth flow. |
| 22 | + u = uuid.uuid4() |
| 23 | + device_resp = httpx.post(f"{args.host}/device/auth", data={"client_id": str(u)}) |
| 24 | + device_resp.raise_for_status() |
| 25 | + device_details = device_resp.json() |
| 26 | + |
| 27 | + # Confirm the device via the authenticated session. |
| 28 | + client.post( |
| 29 | + f"{args.host}/device/confirm", json={"user_code": device_details["user_code"]} |
| 30 | + ).raise_for_status() |
| 31 | + |
| 32 | + # Fetch the token. |
| 33 | + token_resp = httpx.post( |
| 34 | + f"{args.host}/device/token", |
| 35 | + data={ |
| 36 | + "grant_type": "urn:ietf:params:oauth:grant-type:device_code", |
| 37 | + "device_code": device_details["device_code"], |
| 38 | + "client_id": str(u), |
| 39 | + }, |
| 40 | + ) |
| 41 | + token_resp.raise_for_status() |
| 42 | + return token_resp.json()["access_token"] |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + parser = argparse.ArgumentParser() |
| 47 | + parser.add_argument("--host", default="http://localhost:12220") |
| 48 | + parser.add_argument("--silo", default="test-suite-silo") |
| 49 | + parser.add_argument("--username", default="test-privileged") |
| 50 | + parser.add_argument("--password", default="oxide") |
| 51 | + args = parser.parse_args() |
| 52 | + |
| 53 | + print(fetch_token(args)) |
0 commit comments