| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import argparse |
| 4 |
import os |
| 5 |
|
| 6 |
import globus_sdk |
| 7 |
from globus_sdk.token_storage import SimpleJSONFileAdapter |
| 8 |
|
| 9 |
MY_FILE_ADAPTER = SimpleJSONFileAdapter( |
| 10 |
os.path.expanduser("~/.sdk-manage-projects.json") |
| 11 |
) |
| 12 |
|
| 13 |
SCOPES = [globus_sdk.AuthClient.scopes.manage_projects, "openid", "email"] |
| 14 |
RESOURCE_SERVER = globus_sdk.AuthClient.resource_server |
| 15 |
|
| 16 |
# tutorial client ID |
| 17 |
# we recommend replacing this with your own client for any production use-cases |
| 18 |
CLIENT_ID = "61338d24-54d5-408f-a10d-66c06b59f6d2" |
| 19 |
|
| 20 |
NATIVE_CLIENT = globus_sdk.NativeAppAuthClient(CLIENT_ID) |
| 21 |
|
| 22 |
|
| 23 |
def do_login_flow(*, session_params: dict | None = None): |
| 24 |
NATIVE_CLIENT.oauth2_start_flow(requested_scopes=SCOPES) |
| 25 |
# special note! |
| 26 |
# this works because oauth2_get_authorize_url supports session error data |
| 27 |
# as parameters to build the authorization URL |
| 28 |
# you could do this manually with the following supported parameters: |
| 29 |
# - session_required_identities |
| 30 |
# - session_required_single_domain |
| 31 |
# - session_required_policies |
| 32 |
authorize_url = NATIVE_CLIENT.oauth2_get_authorize_url(**session_params) |
| 33 |
print(f"Please go to this URL and login:\n\n{authorize_url}\n") |
| 34 |
auth_code = input("Please enter the code here: ").strip() |
| 35 |
tokens = NATIVE_CLIENT.oauth2_exchange_code_for_tokens(auth_code) |
| 36 |
return tokens |
| 37 |
|
| 38 |
|
| 39 |
def get_tokens(): |
| 40 |
if not MY_FILE_ADAPTER.file_exists(): |
| 41 |
# do a login flow, getting back initial tokens |
| 42 |
response = do_login_flow() |
| 43 |
# now store the tokens and pull out the correct token |
| 44 |
MY_FILE_ADAPTER.store(response) |
| 45 |
tokens = response.by_resource_server[RESOURCE_SERVER] |
| 46 |
else: |
| 47 |
# otherwise, we already did login; load the tokens from that file |
| 48 |
tokens = MY_FILE_ADAPTER.get_token_data(RESOURCE_SERVER) |
| 49 |
|
| 50 |
return tokens |
| 51 |
|
| 52 |
|
| 53 |
def get_auth_client(): |
| 54 |
tokens = get_tokens() |
| 55 |
return globus_sdk.AuthClient( |
| 56 |
authorizer=globus_sdk.AccessTokenAuthorizer(tokens["access_token"]) |
| 57 |
) |
| 58 |
|
| 59 |
|
| 60 |
def create_project(args): |
| 61 |
auth_client = get_auth_client() |
| 62 |
userinfo = auth_client.userinfo() |
| 63 |
print( |
| 64 |
auth_client.create_project( |
| 65 |
args.name, |
| 66 |
contact_email=userinfo["email"], |
| 67 |
admin_ids=userinfo["sub"], |
| 68 |
) |
| 69 |
) |
| 70 |
|
| 71 |
|
| 72 |
def delete_project(args): |
| 73 |
auth_client = get_auth_client() |
| 74 |
print(auth_client.delete_project(args.project_id)) |
| 75 |
|
| 76 |
|
| 77 |
def list_projects(): |
| 78 |
auth_client = get_auth_client() |
| 79 |
for project in auth_client.get_projects(): |
| 80 |
print(f"name: {project['display_name']}") |
| 81 |
print(f"id: {project['id']}") |
| 82 |
print() |
| 83 |
|
| 84 |
|
| 85 |
def main(): |
| 86 |
parser = argparse.ArgumentParser() |
| 87 |
parser.add_argument("action", choices=["create", "delete", "list"]) |
| 88 |
parser.add_argument("-p", "--project-id", help="Project ID for delete") |
| 89 |
parser.add_argument("-n", "--name", help="Project name for create") |
| 90 |
args = parser.parse_args() |
| 91 |
|
| 92 |
try: |
| 93 |
execute(parser, args) |
| 94 |
except globus_sdk.GlobusAPIError as err: |
| 95 |
if not err.info.authorization_parameters: |
| 96 |
raise |
| 97 |
|
| 98 |
err_params = err.info.authorization_parameters |
| 99 |
session_params = {} |
| 100 |
if err_params.session_required_identities: |
| 101 |
print("session required identities detected") |
| 102 |
session_params["session_required_identities"] = ( |
| 103 |
err_params.session_required_identities |
| 104 |
) |
| 105 |
if err_params.session_required_single_domain: |
| 106 |
print("session required single domain detected") |
| 107 |
session_params["session_required_single_domain"] = ( |
| 108 |
err_params.session_required_single_domain |
| 109 |
) |
| 110 |
if err_params.session_required_policies: |
| 111 |
print("session required policies detected") |
| 112 |
session_params["session_required_policies"] = ( |
| 113 |
err_params.session_required_policies |
| 114 |
) |
| 115 |
print(session_params) |
| 116 |
print(err_params) |
| 117 |
response = do_login_flow(session_params=session_params) |
| 118 |
# now store the tokens |
| 119 |
MY_FILE_ADAPTER.store(response) |
| 120 |
print( |
| 121 |
"Reauthenticated successfully to satisfy " |
| 122 |
"session requirements. Will now try again.\n" |
| 123 |
) |
| 124 |
|
| 125 |
# try the action again |
| 126 |
execute(parser, args) |
| 127 |
|
| 128 |
|
| 129 |
def execute(parser, args): |
| 130 |
if args.action == "create": |
| 131 |
if args.name is None: |
| 132 |
parser.error("create requires --name") |
| 133 |
create_project(args) |
| 134 |
elif args.action == "delete": |
| 135 |
if args.project_id is None: |
| 136 |
parser.error("delete requires --project-id") |
| 137 |
delete_project(args) |
| 138 |
elif args.action == "list": |
| 139 |
list_projects() |
| 140 |
else: |
| 141 |
raise NotImplementedError() |
| 142 |
|
| 143 |
|
| 144 |
if __name__ == "__main__": |
| 145 |
main() |