Skip to content

Commit 43457af

Browse files
committed
Refactor auth logic
1 parent 74fcc84 commit 43457af

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

zstash/globus.py

Lines changed: 30 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,29 @@ 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+
# Initialize the NativeAppAuthClient with the client ID
138+
CLIENT_ID = "6c1629cf-446c-49e7-af95-323c6412397f"
139+
client = NativeAppAuthClient(CLIENT_ID, app_name="Zstash")
140+
141+
# Start the OAuth2 flow
142+
client.oauth2_start_flow(refresh_tokens=True)
143+
144+
# Get the authorization URL and prompt the user to visit it
145+
authorize_url = client.oauth2_get_authorize_url()
146+
print("Please go to this URL and login: {0}".format(authorize_url))
147+
148+
# Get the authorization code from the user
149+
auth_code = input("Please enter the code you get after login here: ").strip()
150+
151+
# Exchange the authorization code for tokens
152+
token_response = client.oauth2_exchange_code_for_tokens(auth_code)
153+
154+
# Extract the access token for the Globus Transfer service
155+
globus_transfer_data = token_response.by_resource_server["transfer.api.globus.org"]
156+
globus_transfer_token = globus_transfer_data["access_token"]
157+
158+
# Initialize the TransferClient with the obtained access token
159+
transfer_authorizer = AccessTokenAuthorizer(globus_transfer_token)
138160
transfer_client = TransferClient(authorizer=transfer_authorizer)
139161

140162
for ep_id in [local_endpoint, remote_endpoint]:

0 commit comments

Comments
 (0)