Skip to content

Commit 44acb9c

Browse files
committed
Authentication fix
1 parent 3fdd7b9 commit 44acb9c

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

astroquery/mast/missions.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import astropy.units as u
1919
import numpy as np
20+
import requests
2021
from astropy.coordinates import Angle, BaseCoordinateFrame, SkyCoord
2122
from astropy.io import fits
2223
from astropy.table import Column, Row, Table, vstack
@@ -1023,9 +1024,19 @@ def read_product(self, uri, *, mission=None, **kwargs):
10231024
'Roman Space Telescope mission. Please install it with `pip install {package}` '
10241025
'or install astroquery with optional dependencies using '
10251026
'`pip install astroquery[all]`.', ImportWarning)
1026-
# Use fsspec to open the file directly from the
1027+
1028+
# Make an authenticated request to get the presigned S3 URL for the ASDF file
1029+
headers = {"Authorization": f"token {self._auth_obj.session.cookies['mast_token']}"}
1030+
resp = requests.get(
1031+
download_url,
1032+
headers=headers,
1033+
allow_redirects=False,
1034+
)
1035+
resp.raise_for_status()
1036+
1037+
# Use fsspec to open the file directly from the S3 URL, and then read it with asdf
10271038
fs = fsspec.filesystem("https")
1028-
f = fs.open(download_url, "rb")
1039+
f = fs.open(resp.headers["Location"], "rb")
10291040
return asdf.open(f, **kwargs)
10301041
else:
10311042
raise InvalidQueryError(f"Unsupported file type for reading: {uri}. "

astroquery/mast/tests/test_mast.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,16 @@ def mock_asdf_open(mocker):
765765
pytest.importorskip("asdf")
766766
pytest.importorskip("fsspec")
767767

768+
# Mock response from requests.get for the initial URL fetch
769+
mock_response = MagicMock()
770+
mock_response.status_code = 307
771+
mock_response.headers = {
772+
"Location": "https://example-bucket.s3.amazonaws.com/test.asdf?signature=abc123"
773+
}
774+
mock_requests = MagicMock()
775+
mock_requests.get.return_value = mock_response
776+
mocker.patch("requests.get", mock_requests)
777+
768778
# Mock fsspec.filesystem and the file handle
769779
mock_fs = MagicMock()
770780
mock_file = MagicMock()

0 commit comments

Comments
 (0)