Exemplo n.º 1
0
Arquivo: oauth.py Projeto: ttemim/jwql
def register_oauth():
    """Register the ``jwql`` application with the ``auth.mast``
    authentication service.

    Returns
    -------
    oauth : Object
        An object containing methods to authenticate a user, provided
        by the ``auth.mast`` service.
    """

    # Get configuration parameters
    for key in ['client_id', 'client_secret', 'auth_mast']:
        check_config_for_key(key)
    client_id = get_config()['client_id']
    client_secret = get_config()['client_secret']
    auth_mast = get_config()['auth_mast']

    # Register with auth.mast
    oauth = OAuth()
    client_kwargs = {'scope': 'mast:user:info'}
    oauth.register(
        'mast_auth',
        client_id='{}'.format(client_id),
        client_secret='{}'.format(client_secret),
        access_token_url='https://{}/oauth/access_token?client_secret={}'.
        format(auth_mast, client_secret),
        access_token_params=None,
        refresh_token_url=None,
        authorize_url='https://{}/oauth/authorize'.format(auth_mast),
        api_base_url='https://{}/1.1/'.format(auth_mast),
        client_kwargs=client_kwargs)

    return oauth
Exemplo n.º 2
0
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
Exemplo n.º 3
0
def get_mast_base_url(request=None):
    """Return base url for mnemonic query.

    Parameters
    ----------
    request : HttpRequest object
        Incoming request from the webpage

    Returns
    -------
    token : str or None
        Base url for MAST JWST EDB API.
    """

    try:
        # check if token is available via config file
        check_config_for_key('mast_base_url')
        base_url = get_config()['mast_base_url']
        print('Base URL returned from config file.')
        return base_url
    except (KeyError, ValueError):
        # check if token is available via environment variable
        # see https://auth.mast.stsci.edu/info
        try:
            base_url = os.environ['ENG_BASE_URL']
            print('Base URL returned from environment variable.')
            return base_url
        except KeyError:
            return None
Exemplo n.º 4
0
Arquivo: oauth.py Projeto: ttemim/jwql
    def user_info(request, **kwargs):
        """Store authenticated user credentials in a cookie and return
        it.  If the user is not authenticated, store no credentials in
        the cookie.

        Parameters
        ----------
        request : HttpRequest object
            Incoming request from the webpage

        Returns
        -------
        fn : function
            The decorated function
        """

        cookie = request.COOKIES.get("ASB-AUTH")

        # If user is authenticated, return user credentials
        if cookie is not None:
            check_config_for_key('auth_mast')
            # Note: for now, this must be the development version
            auth_mast = get_config()['auth_mast']

            response = requests.get('https://{}/info'.format(auth_mast),
                                    headers={
                                        'Accept': 'application/json',
                                        'Authorization':
                                        'token {}'.format(cookie)
                                    })
            response = response.json()
            response['access_token'] = cookie

        # If user is not authenticated, return no credentials
        else:
            response = {'ezid': None, "anon": True, 'access_token': None}

        return fn(request, response, **kwargs)
Exemplo n.º 5
0
from jwql.instrument_monitors.miri_monitors.data_trending import dashboard as miri_dash
from jwql.instrument_monitors.nirspec_monitors.data_trending import dashboard as nirspec_dash
from jwql.jwql_monitors import monitor_cron_jobs
from jwql.utils.utils import ensure_dir_exists
from jwql.utils.constants import MONITORS
from jwql.utils.constants import JWST_INSTRUMENT_NAMES_MIXEDCASE
from jwql.utils.constants import JWST_INSTRUMENT_NAMES_SHORTHAND
from jwql.utils.preview_image import PreviewImage
from jwql.utils.credentials import get_mast_token
from .forms import MnemonicSearchForm, MnemonicQueryForm, MnemonicExplorationForm

# astroquery.mast import that depends on value of auth_mast
# this import has to be made before any other import of astroquery.mast
from jwql.utils.utils import get_config, filename_parser, check_config_for_key

check_config_for_key('auth_mast')
auth_mast = get_config()['auth_mast']
mast_flavour = '.'.join(auth_mast.split('.')[1:])
from astropy import config

conf = config.get_config('astroquery')
conf['mast'] = {'server': 'https://{}'.format(mast_flavour)}
from astroquery.mast import Mast
from jwedb.edb_interface import mnemonic_inventory

__location__ = os.path.realpath(
    os.path.join(os.getcwd(), os.path.dirname(__file__)))
FILESYSTEM_DIR = os.path.join(get_config()['jwql_dir'], 'filesystem')
PREVIEW_IMAGE_FILESYSTEM = os.path.join(get_config()['jwql_dir'],
                                        'preview_images')
THUMBNAIL_FILESYSTEM = os.path.join(get_config()['jwql_dir'], 'thumbnails')