Skip to content

Commit 0a99562

Browse files
committed
Authentication
1 parent eabbf77 commit 0a99562

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

astroquery/mast/missions.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ class MastMissionsClass(MastQueryWithLogin):
4747
'spectral_type', 'bmv0_mag', 'u_mag', 'b_mag', 'v_mag', 'gaia_g_mean_mag', 'star_mass',
4848
'instrument', 'grating', 'filter', 'observation_id']
4949

50-
def __init__(self, *, mission='hst'):
51-
super().__init__()
50+
def __init__(self, *, mission='hst', mast_token=None):
51+
super().__init__(mast_token=mast_token)
5252

5353
self.dataset_kwds = { # column keywords corresponding to dataset ID
5454
'hst': 'sci_data_set_name',
@@ -497,8 +497,19 @@ def download_file(self, uri, *, local_path=None, cache=True, verbose=True):
497497
url = data_url
498498

499499
except HTTPError as err:
500+
if err.response.status_code == 401:
501+
no_auth_msg = f'You are not authorized to download from {data_url}.'
502+
if self._authenticated:
503+
no_auth_msg += ('\nPlease check your authentication token. You can generate a new '
504+
'token at https://auth.mast.stsci.edu/token?suggested_name=Astroquery&'
505+
'suggested_scope=mast:exclusive_access')
506+
else:
507+
no_auth_msg += ('\nPlease authenticate yourself using the `~astroquery.mast.MastMissions.login` '
508+
'function or initialize `~astroquery.mast.MastMissions` with an authentication '
509+
'token.')
510+
log.warning(no_auth_msg)
500511
status = 'ERROR'
501-
msg = 'HTTPError: {0}'.format(err)
512+
msg = f'HTTPError: {err}'
502513
url = data_url
503514

504515
return status, msg, url

astroquery/mast/tests/test_mast.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from astropy.io import fits
1313

1414
import astropy.units as u
15+
from requests import HTTPError, Response
1516

1617
from astroquery.mast.services import _json_to_table
1718
from astroquery.utils.mocks import MockResponse
@@ -147,6 +148,11 @@ def request_mockreturn(url, params={}):
147148

148149

149150
def download_mockreturn(*args, **kwargs):
151+
if 'unauthorized' in args[0]:
152+
response = Response()
153+
response.reason = 'Unauthorized'
154+
response.status_code = 401
155+
raise HTTPError(response=response)
150156
return ('COMPLETE', None, None)
151157

152158

@@ -376,6 +382,30 @@ def test_missions_download_products(patch_post, tmp_path):
376382
extension='jpg',
377383
download_dir=tmp_path)
378384

385+
386+
def test_missions_download_no_auth(patch_post, caplog):
387+
# Exclusive access products should not be downloaded if user is not authenticated
388+
# User is not authenticated
389+
uri = 'unauthorized.fits'
390+
result = mast.MastMissions.download_file(uri)
391+
assert result[0] == 'ERROR'
392+
assert 'HTTPError' in result[1]
393+
with caplog.at_level('WARNING', logger='astroquery'):
394+
assert 'You are not authorized to download' in caplog.text
395+
assert 'Please authenticate yourself' in caplog.text
396+
caplog.clear()
397+
398+
# User is authenticated, but doesn't have proper permissions
399+
test_token = "56a9cf3df4c04052atest43feb87f282"
400+
mast.MastMissions.login(token=test_token)
401+
result = mast.MastMissions.download_file(uri)
402+
assert result[0] == 'ERROR'
403+
assert 'HTTPError' in result[1]
404+
with caplog.at_level('WARNING', logger='astroquery'):
405+
assert 'You are not authorized to download' in caplog.text
406+
assert 'Please check your authentication token' in caplog.text
407+
408+
379409
###################
380410
# MastClass tests #
381411
###################

0 commit comments

Comments
 (0)