Skip to content

Commit 81197c3

Browse files
jd-aubsipocz
authored andcommitted
Switch CASDA to QueryWithLogin
1 parent f20c85b commit 81197c3

File tree

6 files changed

+215
-66
lines changed

6 files changed

+215
-66
lines changed

astroquery/casda/__init__.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Conf(_config.ConfigNamespace):
1111

1212
server = _config.ConfigItem(
1313
['https://casda.csiro.au/casda_vo_tools/sia2/query'],
14-
'Name of the CASDA SIA server to use.'
14+
'Address of the CASDA SIA server to use.'
1515
)
1616
timeout = _config.ConfigItem(
1717
30,
@@ -25,7 +25,14 @@ class Conf(_config.ConfigNamespace):
2525
['https://casda.csiro.au/casda_data_access/'],
2626
'Address of the CASDA SODA server'
2727
)
28-
28+
login_url = _config.ConfigItem(
29+
['https://data.csiro.au/casda_vo_proxy/vo/tap/availability'],
30+
'Address for test logins'
31+
)
32+
username = _config.ConfigItem(
33+
'',
34+
'Optional default username for CASDA archive.'
35+
)
2936

3037
conf = Conf()
3138

astroquery/casda/core.py

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import time
88
from xml.etree import ElementTree
99
from datetime import datetime, timezone
10+
import keyring
1011

1112
# 2. third party imports
1213
import astropy.units as u
@@ -17,24 +18,22 @@
1718
import numpy as np
1819

1920
# 3. local imports - use relative imports
20-
# commonly required local imports shown below as example
21-
# all Query classes should inherit from BaseQuery.
22-
from ..query import BaseQuery
23-
# has common functions required by most modules
21+
from ..query import QueryWithLogin
2422
from ..utils import commons
2523
# prepend_docstr is a way to copy docstrings between methods
2624
from ..utils import prepend_docstr_nosections
2725
# async_to_sync generates the relevant query tools from _async methods
2826
from ..utils import async_to_sync
2927
# import configurable items declared in __init__.py
3028
from . import conf
29+
from ..exceptions import LoginError
3130

3231
# export all the public classes and methods
3332
__all__ = ['Casda', 'CasdaClass']
3433

3534

3635
@async_to_sync
37-
class CasdaClass(BaseQuery):
36+
class CasdaClass(QueryWithLogin):
3837

3938
"""
4039
Class for accessing ASKAP data through the CSIRO ASKAP Science Data Archive (CASDA). Typical usage:
@@ -46,18 +45,69 @@ class CasdaClass(BaseQuery):
4645
URL = conf.server
4746
TIMEOUT = conf.timeout
4847
POLL_INTERVAL = conf.poll_interval
48+
USERNAME = conf.username
4949
_soda_base_url = conf.soda_base_url
50+
_login_url =conf.login_url
5051
_uws_ns = {'uws': 'http://www.ivoa.net/xml/UWS/v1.0'}
5152

5253
def __init__(self, user=None, password=None):
5354
super().__init__()
54-
if user is None:
55-
self._authenticated = False
55+
56+
def _login(self, *, username=None, store_password=False,
57+
reenter_password=False, password=None):
58+
"""
59+
login to non-public data as a known user
60+
61+
Parameters
62+
----------
63+
username : str, optional
64+
Username to the CASDA archive, uses ATNF OPAL credentials. If not given, it should be
65+
specified in the config file.
66+
store_password : bool, optional
67+
Stores the password securely in your keyring. Default is False.
68+
reenter_password : bool, optional
69+
Asks for the password even if it is already stored in the
70+
keyring. This is the way to overwrite an already stored passwork
71+
on the keyring. Default is False.
72+
password : str, optional
73+
Password for the CASDA archive. If not given, it will obtained either from the keyring or interactively.
74+
"""
75+
76+
if username is None:
77+
if not self.USERNAME:
78+
raise LoginError("If you do not pass a username to login(), "
79+
"you should configure a default one!")
80+
else:
81+
username = self.USERNAME
82+
83+
if password is None:
84+
# Get password from keyring or prompt
85+
password, password_from_keyring = self._get_password(
86+
"astroquery:casda.csiro.au", username, reenter=reenter_password)
5687
else:
57-
self._authenticated = True
58-
# self._user = user
59-
# self._password = password
60-
self._auth = (user, password)
88+
# Bypass the keyring if a password is supplied
89+
password_from_keyring = None
90+
store_password = False
91+
92+
# Login to CASDA to test credentals
93+
log.info("Authenticating {0} on CASDA ...".format(username))
94+
auth = (username, password)
95+
login_response = self._request("GET", self._login_url, auth=auth,
96+
timeout=self.TIMEOUT, cache=False)
97+
authenticated = login_response.status_code == 200
98+
99+
if authenticated:
100+
log.info("Authentication successful!")
101+
self.USERNAME = username
102+
self._auth = (username, password)
103+
104+
# When authenticated, save password in keyring if needed
105+
if password_from_keyring is None and store_password:
106+
keyring.set_password("astroquery:casda.csiro.au", username, password)
107+
else:
108+
log.exception("Authentication failed")
109+
110+
return authenticated
61111

62112
def query_region_async(self, coordinates, radius=1*u.arcmin, height=None, width=None,
63113
get_query_payload=False, cache=True):
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<availability xmlns="http://www.ivoa.net/xml/VOSIAvailability/v1.0">
3+
<available>true</available>
4+
<upSince>2022-04-07T11:11:40.006Z</upSince>
5+
<note></note>
6+
</availability>

0 commit comments

Comments
 (0)