Skip to content

Commit 0f730a8

Browse files
authored
Merge pull request #34 from jupyter-naas/33-feat-automatic-credentials-if-jupyterhub_api_token
33 feat automatic credentials if jupyterhub api token
2 parents 448617d + 4403032 commit 0f730a8

File tree

1 file changed

+39
-45
lines changed

1 file changed

+39
-45
lines changed

naas_python/utils/domains_base/authorization.py

+39-45
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def __init__(
187187
self,
188188
port=38745,
189189
trade_url="https://auth.naas.ai/bearer/workspace/longlived",
190+
trade_jupyterhub_url="https://auth.naas.ai/bearer/jupyterhub/longlived",
190191
timeout=60, # 60,
191192
login_url=os.environ.get('NAAS_LOGIN_URL', "https://naas.ai?cli_token=generate_token"),
192193
):
@@ -197,6 +198,7 @@ def __init__(
197198
# TODO: Fix bug where the async call during dev mode, only finishes after the exact timeout period
198199
self._timeout = timeout
199200
self.trade_url = trade_url
201+
self.trade_jupyterhub_url=trade_jupyterhub_url
200202
self._login_url = login_url
201203
self._redirect_uri = None
202204

@@ -336,6 +338,7 @@ def access_token(self):
336338
def _gather_env_credentials(self):
337339
_SCHEMA_VARS = {
338340
"NAAS_CREDENTIALS_JWT_TOKEN": "jwt_token",
341+
"JUPYTERHUB_API_TOKEN" : "jupyterhub_api_token"
339342
}
340343

341344
credentials = {}
@@ -356,72 +359,63 @@ def _gather_file_credentials(self, credentials_file_path: Path):
356359
self._file_contents = json.loads(self._file_contents)
357360
return self._file_contents
358361

359-
def _generate_credential_file(self, credentials_file_path: Path):
362+
def _generate_credential_file(self, credentials_file_path: Path, jupyterhub_access_token=None, prompt=True):
360363
# Trade access token (and authenticate) and store the long-lived token in the credentials file
364+
if jupyterhub_access_token is not None:
365+
access_token = self.trade_for_long_lived_token(access_token=jupyterhub_access_token, access_token_type="jupyterhub")
366+
self._jwt_token = access_token
367+
else:
368+
access_token = self.trade_for_long_lived_token(self.access_token())
369+
self._jwt_token = access_token
370+
361371
try:
362-
self._jwt_token = self.trade_for_long_lived_token(self.access_token())
363-
364372
# Create target directory in case it does not exists.
365-
os.makedirs(
366-
'/'.join(
367-
credentials_file_path.as_posix().split('/')[:-1]
368-
),
369-
exist_ok=True
370-
)
373+
os.makedirs(os.path.join(os.path.dirname(credentials_file_path)), exist_ok=True)
371374

372375
with open(credentials_file_path, "w") as file:
373-
file.write(json.dumps({"jwt_token": self._jwt_token}))
376+
file.write(json.dumps({"jwt_token": access_token}))
374377

375-
print(f'\n\t✅ CLI Token successfuly generated and stored to {credentials_file_path.as_posix()}\n\n')
378+
if prompt:
379+
print(f'\n\t✅ CLI Token successfuly generated and stored to {credentials_file_path.as_posix()}\n\n')
376380

377-
return self._jwt_token
381+
return access_token
378382
except TimeoutException as e:
379383
print(f'\n\n\t❌ The process was not able to complete in time. Please try again.\n\n')
380384
return None
381385

382-
383-
384386
def check_credentials(self):
385387
# This method is responsible for inspecting the naas credentials files
386388
# locally and retrieving the jwt token if it exists and is valid.
387-
# The order of priority is as follows:
388-
# 1. Check if file exists, if not call appropriate method to start authentication process
389-
# 2.a Check if file is empty, if so call appropriate method to start authentication process
390-
# 2.b If file is not empty, check if token is valid, if not call appropriate method to start authentication process
391-
# 3. If file exists and is not empty and token is valid, set the jwt token to the class property and return
392389

393390
credentials_path = Path(os.path.expanduser("~/.naas/credentials"))
394-
395-
if credentials_path.exists() and credentials_path.is_file():
396-
# First order option for credential gathering is to check the file contents and grab the token
391+
_var_credentials = self._gather_env_credentials()
392+
393+
# look for the existence of overriding environment variable to create the new file
394+
if "jwt_token" in _var_credentials:
395+
# Validate stored token is valid... then assign value and return
396+
self._jwt_token = _var_credentials["jwt_token"]
397+
return self._jwt_token
398+
399+
elif "jupyterhub_api_token" in _var_credentials:
400+
access_token = _var_credentials["jupyterhub_api_token"]
401+
self._generate_credential_file(credentials_file_path=credentials_path, jupyterhub_access_token=access_token, prompt=False)
402+
403+
elif credentials_path.exists() and credentials_path.is_file():
404+
# Check the file contents and grab the token
397405
credentials = self._gather_file_credentials(credentials_path)
398406
logging.debug(f"Credentials file found and not empty.")
399-
credentials.update(self._gather_env_credentials())
400-
401-
# If environment variable is present, override file contents
402-
if "jwt_token" in credentials:
403-
# Validate stored token is valid... then assign value and return
404-
self._jwt_token = credentials["jwt_token"]
405-
return self._jwt_token
406-
407-
else:
408-
# As a second order option, look for the existence of overriding environment variable to create the new file
409-
_var_credentials = self._gather_env_credentials()
410-
411-
if "jwt_token" in _var_credentials:
412-
# Validate stored token is valid... then assign value and return
413-
self._jwt_token = _var_credentials["jwt_token"]
414-
return self._jwt_token
415-
407+
# credentials.update(self._gather_env_credentials())
408+
self._jwt_token = credentials["jwt_token"]
409+
410+
else :
416411
# We could not find any credentials, so we need to start the authentication process
417412
self._generate_credential_file(credentials_file_path=credentials_path)
418413

419-
# if not self._jwt_token:
420-
# raise Exception("Could not find any credentials to authenticate with.")
421-
422-
def trade_for_long_lived_token(self, access_token):
423-
# headers = {"Authorization": f"Bearer {access_token}"}
424-
url = f"{self.trade_url}/?token={access_token}"
414+
def trade_for_long_lived_token(self, access_token, access_token_type="workspace"):
415+
if access_token_type == "workspace":
416+
url = f"{self.trade_url}/?token={access_token}"
417+
if access_token_type == "jupyterhub":
418+
url = f"{self.trade_jupyterhub_url}/?token={access_token}"
425419

426420
response = requests.get(url)
427421

0 commit comments

Comments
 (0)