def get_mast_token(request=None): """Return MAST token from either Astroquery.Mast, webpage cookies, the JWQL configuration file, or an environment variable. Parameters ---------- request : HttpRequest object Incoming request from the webpage Returns ------- token : str or None User-specific MAST token string, if available """ if Mast.authenticated(): print('Authenticated with Astroquery MAST magic') return None else: try: # check if token is available via config file check_config_for_key('mast_token') token = get_config()['mast_token'] print('Authenticated with config.json MAST token.') return token except (KeyError, ValueError): # check if token is available via environment variable # see https://auth.mast.stsci.edu/info try: token = os.environ['MAST_API_TOKEN'] print('Authenticated with MAST token environment variable.') return token except KeyError: return None
def mast_authenticate(token=None): """Verify MAST authentication status, login if needed.""" if Mast.authenticated() is False: if token is None: raise ValueError( 'You are not authenticated in MAST. Please provide a valid token.' ) else: Mast.login(token=token)
def log_into_mast(request): """Login via astroquery.mast if user authenticated in web app. Parameters ---------- request : HttpRequest object Incoming request from the webpage """ if Mast.authenticated(): return True # get the MAST access token if present access_token = str(get_mast_token(request)) # authenticate with astroquery.mast if necessary if access_token != 'None': Mast.login(token=access_token) return Mast.authenticated() else: return False