|
2 | 2 | # https://github.com/globusonline/globus-registered-api |
3 | 3 | # Copyright 2025 Globus <support@globus.org> |
4 | 4 | # SPDX-License-Identifier: MIT |
| 5 | +import json |
| 6 | +import os |
| 7 | + |
| 8 | +from globus_sdk import AuthClient, ClientApp, UserApp |
5 | 9 |
|
6 | 10 | import click |
7 | 11 |
|
8 | | -group = click.Group() |
| 12 | +RAPI_NATIVE_CLIENT_ID = "9dc7dfff-cfe8-4339-927b-28d29e1b2f42" |
| 13 | + |
| 14 | + |
| 15 | +def _create_globus_app() -> UserApp | ClientApp: |
| 16 | + """ |
| 17 | + Create and return a Globus app based on environment variables. |
| 18 | +
|
| 19 | + Checks for GLOBUS_CLIENT_ID and GLOBUS_CLIENT_SECRET environment variables. |
| 20 | + If both are present, creates a ClientApp for client credentials authentication. |
| 21 | + Otherwise, creates a UserApp with a registered native client. |
| 22 | +
|
| 23 | + :return: A ClientApp if both environment variables are set, otherwise a UserApp |
| 24 | + :raises ValueError: If only one of the required environment variables is set |
| 25 | + """ |
| 26 | + client_id = os.getenv("GLOBUS_REGISTERED_API_CLIENT_ID") |
| 27 | + client_secret = os.getenv("GLOBUS_REGISTERED_API_CLIENT_SECRET") |
| 28 | + app_name = "globus-registered-api-cli" |
| 29 | + |
| 30 | + # Validate: both or neither |
| 31 | + if bool(client_id) ^ bool(client_secret): |
| 32 | + raise ValueError( |
| 33 | + "Both GLOBUS_CLIENT_ID and GLOBUS_CLIENT_SECRET must be set, or neither." |
| 34 | + ) |
| 35 | + |
| 36 | + if client_id and client_secret: |
| 37 | + return ClientApp(app_name=app_name, client_id=client_id, client_secret=client_secret) |
| 38 | + else: |
| 39 | + return UserApp(app_name=app_name, client_id=RAPI_NATIVE_CLIENT_ID) |
| 40 | + |
| 41 | + |
| 42 | +def _create_auth_client(app: UserApp | ClientApp) -> AuthClient: |
| 43 | + """ |
| 44 | + Create an AuthClient for the given app. |
| 45 | +
|
| 46 | + :param app: A Globus app instance to use for authentication |
| 47 | + :return: An AuthClient configured with the provided app |
| 48 | + """ |
| 49 | + return AuthClient(app=app) |
| 50 | + |
| 51 | + |
| 52 | +@click.group() |
| 53 | +@click.pass_context |
| 54 | +def cli(ctx: click.Context) -> None: |
| 55 | + """Globus Registered API Command Line Interface.""" |
| 56 | + ctx.obj = _create_globus_app() |
| 57 | + |
| 58 | + |
| 59 | +@cli.command() |
| 60 | +@click.option("--format", type=click.Choice(["json", "text"]), default="text") |
| 61 | +@click.pass_context |
| 62 | +def whoami(ctx: click.Context, format: str) -> None: |
| 63 | + """ |
| 64 | + Display information about the authenticated user. |
| 65 | + """ |
| 66 | + app: UserApp | ClientApp = ctx.obj |
| 67 | + auth_client = _create_auth_client(app) |
| 68 | + res = auth_client.userinfo() |
9 | 69 |
|
| 70 | + if format == "text": |
| 71 | + click.echo(res["preferred_username"]) |
| 72 | + else: |
| 73 | + click.echo(json.dumps(res.data, indent=2)) |
10 | 74 |
|
11 | | -@group.command() |
12 | | -def bogus() -> None: |
13 | | - """Print 'bogus'.""" |
14 | 75 |
|
15 | | - print("bogus") |
| 76 | +@cli.command() |
| 77 | +@click.pass_context |
| 78 | +def logout(ctx: click.Context) -> None: |
| 79 | + """ |
| 80 | + Log out the current user by revoking all tokens. |
| 81 | + """ |
| 82 | + app: UserApp | ClientApp = ctx.obj |
| 83 | + app.logout() |
| 84 | + click.echo("Logged out successfully.") |
0 commit comments