Skip to content

Commit

Permalink
chore: small refactoring of login command
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsocket committed Oct 16, 2024
1 parent 3f21bcd commit 856ce70
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions dreadnode_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,6 @@
cli.add_typer(agent_cli, name="agent", help="Manage agents")


async def authentication_flow(server: str) -> t.Any:
client = api.Client(base_url=server)

print(":laptop_computer: requesting device code ...")

codes = await client.get_device_codes()
user_code = codes.get("user_code")

print(
f":link: visit {client.url_for_user_code(user_code)} in your browser to verify this device with the code [bold]{user_code}[/]"
)

return await client.poll_for_token(codes.get("device_code"))


@cli.command(help="Authenticate to the platform.")
def login(
server: t.Annotated[str, typer.Option("--server", "-s", help="URL of the server")] = PLATFORM_BASE_URL,
Expand All @@ -47,8 +32,27 @@ def login(
if env_server:
server = env_server

auth = asyncio.run(authentication_flow(server))
# create client with no auth data
client = api.Client(base_url=server)

print(":laptop_computer: requesting device code ...")

# request user and device codes
codes = asyncio.run(client.get_device_codes())

# present verification URL to user
user_code = codes.get("user_code")
device_code = codes.get("device_code")
print(
f":link: visit {client.url_for_user_code(user_code)} in your browser to verify this device with the code [bold]{user_code}[/]"
)

# poll for the access token after user verification
auth = asyncio.run(client.poll_for_token(device_code))
if auth is None:
raise Exception("authentication failed")

# store the authentication data for the profile
UserConfig.read().set_profile_config(
profile,
ServerConfig(url=server, access_token=auth.get("access_token"), refresh_token=auth.get("refresh_token")),
Expand Down

0 comments on commit 856ce70

Please sign in to comment.