Skip to content

Commit 1aa459e

Browse files
committed
Refactor auth logic
1 parent 74fcc84 commit 1aa459e

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

zstash/globus.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
import sys
99

1010
from fair_research_login.client import NativeClient
11-
from globus_sdk import TransferAPIError, TransferClient, TransferData
11+
from globus_sdk import (
12+
AccessTokenAuthorizer,
13+
NativeAppAuthClient,
14+
TransferAPIError,
15+
TransferClient,
16+
TransferData,
17+
)
1218
from globus_sdk.services.transfer.response.iterable import IterableTransferResponse
1319
from six.moves.urllib.parse import urlparse
1420

@@ -128,13 +134,24 @@ def globus_activate(hpss: str):
128134
if remote_endpoint.upper() in hpss_endpoint_map.keys():
129135
remote_endpoint = hpss_endpoint_map.get(remote_endpoint.upper())
130136

131-
native_client = NativeClient(
132-
client_id="6c1629cf-446c-49e7-af95-323c6412397f",
133-
app_name="Zstash",
134-
default_scopes="openid urn:globus:auth:scope:transfer.api.globus.org:all",
135-
)
136-
native_client.login(no_local_server=True, refresh_tokens=True)
137-
transfer_authorizer = native_client.get_authorizers().get("transfer.api.globus.org")
137+
# Authentication -- Globus Consents
138+
# Initialize the NativeAppAuthClient with the client ID
139+
CLIENT_ID = "6c1629cf-446c-49e7-af95-323c6412397f"
140+
client = NativeAppAuthClient(CLIENT_ID, app_name="Zstash")
141+
# Start the OAuth2 flow
142+
client.oauth2_start_flow(refresh_tokens=True)
143+
# Get the authorization URL and prompt the user to visit it
144+
authorize_url = client.oauth2_get_authorize_url()
145+
print(f"Please go to this URL and login: {authorize_url}")
146+
# Get the authorization code from the user
147+
auth_code = input("Please enter the code you get after login here: ").strip()
148+
# Exchange the authorization code for tokens
149+
token_response = client.oauth2_exchange_code_for_tokens(auth_code)
150+
# Extract the access token for the Globus Transfer service
151+
globus_transfer_data = token_response.by_resource_server["transfer.api.globus.org"]
152+
globus_transfer_token = globus_transfer_data["access_token"]
153+
# Initialize the TransferClient with the obtained access token
154+
transfer_authorizer = AccessTokenAuthorizer(globus_transfer_token)
138155
transfer_client = TransferClient(authorizer=transfer_authorizer)
139156

140157
for ep_id in [local_endpoint, remote_endpoint]:

0 commit comments

Comments
 (0)