def get_o_auth_hs(request):
    """
    Gets HydroShare Open Authorization.
    
    Arguments:      [request]
    Returns:        [hs]
    Referenced By:  [controllers_ajax.chart_data, controllers_ajax.create_layer]
    References:     []
    Libraries:      [HydroShareAuthOAuth2, HydroShare]
    """

    if use_hs_client_helper:
        hs = get_oauth_hs(request)
    else:
        hs_instance_name = "www"
        client_id = getattr(settings, "SOCIAL_AUTH_HYDROSHARE_KEY", None)
        client_secret = getattr(settings, "SOCIAL_AUTH_HYDROSHARE_SECRET", None)
        # this line will throw out from django.core.exceptions.ObjectDoesNotExist\
        # if current user is not signed in via HydroShare OAuth
        token = request.user.social_auth.get(provider='hydroshare').extra_data['token_dict']
        hs_hostname = "{0}.hydroshare.org".format(hs_instance_name)
        auth = HydroShareAuthOAuth2(client_id, client_secret, token=token)
        hs = HydroShare(auth=auth, hostname=hs_hostname)

    return hs
Example #2
0
 def get_hs_oauth2_token(self):
     hs = None
     # Get token ourselves (i.e. without using oauthlib, etc.)
     try:
         if self.use_https:
             scheme = 'https'
         else:
             scheme = 'http'
         data = {'grant_type': 'password',
                 'username': self.creator, 'password': self.creatorPassword}
         if self.port:
             token_url = self._TOKEN_URL_PROTO_WITH_PORT.format(scheme=scheme,
                                                                hostname=self.url,
                                                                port=self.port)
         else:
             token_url = self._TOKEN_URL_PROTO_WITHOUT_PORT.format(scheme=scheme,
                                                                   hostname=self.url)
         r = requests.post(token_url, data=data, auth=(self.client_id, self.client_secret),
                           verify=self.verify)
         token = json.loads(r.text)
         oauth_token = HydroShareAuthOAuth2(self.client_id, self.client_secret,
                                            self.url, use_https=self.use_https, port=self.port,
                                            token=token)
         hs = HydroShare(hostname=self.url, auth=oauth_token, use_https=self.use_https, verify=self.verify,
                         port=self.port)
     except:
         self.fail("OAuth2 Connection Failed" + str(sys.exc_info()[0]))
     return hs
Example #3
0
    def authenticate(self, username, password, client_id=None, client_secret=None):
        """
        Authenticates access to allow read/write access to privileged resource_cache
        :param username: username for HydroShare.org
        :param password: password associated with username
        :param client_id: Client ID obtained from HydroShare
        :param client_secret: Client Secret provided by HydroShare
        :return: Returns true if authentication was successful, false otherwise
        """

        if not all([username, password]):
            self.auth = None
            return False

        if client_id is not None and client_secret is not None:
            self.auth = HydroShareAuthOAuth2(client_id, client_secret, username=username, password=password)
        else:
            self.auth = HydroShareAuthBasic(username, password)
        try:
            self.client = HydroShare(auth=self.auth)  # , verify=False)
            self.user_info = self.client.getUserInfo()
            return True
        except HydroShareException as e:  # for incorrect username/password combinations
            print('Authentication failed: {}'.format(e))
        except InvalidGrantError as e:  # for failures when attempting to use OAuth2
            print('Credentials could not be validated: {}'.format(e))
        except InvalidClientError as e:
            print('Invalid client ID and/or client secret: {}'.format(e))
        except Exception as e:
            print(e)
        
        self.auth = None

        return False
Example #4
0
    def get_client(self, user_id):
        # TODO Add check for when user not found
        token = HydroShareToken.objects.get(user_id=user_id)
        if token.is_expired:
            token = self.renew_access_token(user_id)

        auth = HydroShareAuthOAuth2(CLIENT_ID, CLIENT_SECRET,
                                    token=token.get_oauth_dict())

        return HydroShareClient(hostname=HOSTNAME, auth=auth)
def getOAuthHS(request):
    hs_instance_name = "www"
    client_id = getattr(settings, "SOCIAL_AUTH_HYDROSHARE_KEY", None)
    client_secret = getattr(settings, "SOCIAL_AUTH_HYDROSHARE_SECRET", None)
    # this line will throw out from django.core.exceptions.ObjectDoesNotExist if current user is not signed in via HydroShare OAuth
    token = request.user.social_auth.get(provider='hydroshare').extra_data['token_dict']
    hs_hostname = "{0}.hydroshare.org".format(hs_instance_name)
    auth = HydroShareAuthOAuth2(client_id, client_secret, token=token)
    hs = HydroShare(auth=auth, hostname=hs_hostname)
    return hs
Example #6
0
    def get_client(self):  # type: () -> HydroShareAdapter
        """
        Passes authentication details to underlying HydroShare object for authorization via OAuth 2.0.
        """
        if self.auth_type == OAUTH_AC:
            token = self.get_token()
            auth = HydroShareAuthOAuth2(self.__client_id,
                                        self.__client_secret,
                                        token=token)
        elif self.auth_type == OAUTH_ROPC:
            auth = HydroShareAuthOAuth2(self.__client_id,
                                        self.__client_secret,
                                        username=self.username,
                                        password=self.password)
        else:
            raise InvalidGrantError("Invalid authorization grant type.")

        authorization_header = self.get_authorization_header()
        return HydroShareAdapter(auth=auth,
                                 default_headers=authorization_header)
def get_oauth_hs(request):
    global hs_hostname

    client_id = getattr(settings, 'SOCIAL_AUTH_HYDROSHARE_KEY', 'None')
    client_secret = getattr(settings, 'SOCIAL_AUTH_HYDROSHARE_SECRET', 'None')

    # Throws django.core.exceptions.ObjectDoesNotExist if current user is not signed in via HydroShare OAuth
    token = request.user.social_auth.get(
        provider='hydroshare').extra_data['token_dict']
    auth = HydroShareAuthOAuth2(client_id, client_secret, token=token)

    return HydroShare(auth=auth, hostname=hs_hostname, use_https=True)
Example #8
0
    def __init__(self, username=None, password=None, cache=True):
        self.hs = None
        self.content = {}

        # connect to hydroshare using OAUTH2
        authfile = os.path.expanduser("~/.hs_auth")
        if os.path.exists(authfile):
            with open(authfile, 'rb') as f:
                token, cid = pickle.load(f)
            auth = HydroShareAuthOAuth2(cid, '', token=token)
        else:
            # connect to hydroshare using Basic Authentication
            self.cache = cache
            if cache:
                utilities.load_environment(
                    os.path.join(os.environ['NOTEBOOK_HOME'], '.env'))

            self.auth_path = os.environ.get('NOTEBOOK_HOME',
                                            '/home/jovyan/.auth')

            uname = username
            if uname is None:
                if 'HS_USR_NAME' in os.environ.keys():
                    uname = os.environ['HS_USR_NAME']

            if password is None:
                # get a secure connection to hydroshare
                auth = self.getSecureConnection(uname)
            else:
                print('WARNING: THIS IS NOT A SECURE METHOD OF CONNECTING TO '
                      'HYDROSHARE...AVOID TYPING CREDENTIALS AS PLAIN TEXT')
                auth = HydroShareAuthBasic(username=uname, password=password)

        try:
            self.hs = HydroShare(auth=auth)
            self.hs.getUserInfo()
            print('Successfully established a connection with HydroShare')

        except HydroShareHTTPException as e:
            print('Failed to establish a connection with HydroShare.\n  '
                  'Please check that you provided the correct credentials.\n'
                  '%s' % e)
            # remove the cached authentication
            if os.path.exists(self.auth_path):
                os.remove(self.auth_path)
            return None

        # set the HS resource download directory
        download_dir = os.environ.get('JUPYTER_DOWNLOADS', 'Downloads')
        if not os.path.isdir(download_dir):
            os.makedirs(download_dir)
        self.download_dir = download_dir
Example #9
0
    def setUp(self):
        self.resources_to_delete = []
        self.url = os.environ['HYDROSHARE'] if os.environ.get('HYDROSHARE') is not None else "dev.hydroshare.org"
        self.use_https = os.getenv('USE_HTTPS', 'True')
        if self.use_https == 'True':
            self.use_https = True
        else:
            self.use_https = False

        self.verify = os.getenv('VERIFY_CERTS', 'True')
        if self.verify == 'True':
            self.verify = True
        else:
            self.verify = False

        self.port = os.environ.get('PORT', None)

        # OAuth2
        self.client_id = os.environ.get('CLIENT_ID', None)
        self.client_secret = os.environ.get('CLIENT_SECRET', None)

        # need to have some generic titles. Not sure how we can pass them
        self.creator = os.environ['Creator'] if os.environ.get('Creator') is not None else 'admin'
        self.creatorPassword = os.environ.get('CreatorPassword') # if it's empty, fail the auth tests
        self.auth = None
        self.oauth = None
        if self.creatorPassword:
            self.auth = HydroShareAuthBasic(username=self.creator,password=self.creatorPassword)
            if self.client_id and self.client_secret:
                self.oauth = HydroShareAuthOAuth2(self.client_id, self.client_secret,
                                                  self.url, use_https=self.use_https, port=self.port,
                                                  username=self.creator, password=self.creatorPassword)
        # items to look up. Present info from dev.hydroshare.org
        self.a_Title = os.environ['ResourceTitle'] if  os.environ.get('ResourceTitle') is not None else 'Logan DEM'
        self.a_resource_id = os.environ['ResourceId'] if os.environ.get('ResourceId') is not None else 'e327f29ff92b4871a4a94556db7b7635'
        self.a_resource_type = os.environ['ResourceType'] if os.environ.get('ResourceType') is not None else 'RasterResource'
        self.a_resource_filename = os.environ['ResourceName'] if os.environ.get('ResourceName') is not None else 'logan.tif'
        # create
        self.test_genericResource_path = '../../raw/document/pdf/HIC2014-1566.pdf'
        self.test_netcdfResource_path = '../../raw/netcdf_rapid_compliant/-nfiehydroZone-home-public-usgs-national-2015-08-19-national_2015-08-19.nc'

        if self.url.startswith('www'):
            raise unittest.SkipTest("No Live Tests on www")
        expected_testpath = os.getcwd().endswith('api')
        if not expected_testpath:
            self.fail( "tests need to run from 'tests/api' current path is:" + os.getcwd())

        self.tmp_dir = tempfile.mkdtemp()
Example #10
0
    def login(self):
        hs = None

        # check for oauth
        authfile = os.path.expanduser("~/.hs_auth")
        try:
            with open(authfile, 'rb') as f:
                token, cid = pickle.load(f)
            auth = HydroShareAuthOAuth2(cid, '', token=token)
            self.log.info('auth=%s' % str(auth))

            hs = self.check_auth(auth)
            self.log.info('hs=%s' % str(hs))

            if hs is None:
                message = url_escape(
                    "Oauth Login Failed.  Login with username and password or logout from JupyterHub and reauthenticate with Hydroshare."
                )
        except:
            message = ''

        if hs is None:
            # If oauth fails, we can log in using
            # user name and password.  These are saved in
            # files in the home directory.
            pwfile = os.path.expanduser("~/.hs_pass")
            userfile = os.path.expanduser("~/.hs_user")

            try:
                with open(userfile) as f:
                    username = f.read().strip()
                with open(pwfile) as f:
                    password = f.read().strip()
                auth = HydroShareAuthBasic(username=username,
                                           password=password)
                hs = self.check_auth(auth)
                if hs is None:
                    message = url_escape("Login Failed. Please Try again")
            except:
                message = url_escape(
                    "You need to provide login credentials to access HydroShare Resources."
                )

        if hs is None:
            _next = url_escape(url_escape(self.request.uri))
            upath = urljoin(self.request.uri, 'hslogin')
            self.redirect('%s?error=%s&next=%s' % (upath, message, _next))
Example #11
0
    def create_resource(self, uid, title, keywords, abstract):

        # load user credentials
        self.logger.info('Connecting to the HydroShare API')
        token = json.loads(self.current_user)

        # connect with HydroShare
        auth = HydroShareAuthOAuth2(env.oauth_client_id, '', token=token)
        hs = HydroShare(auth=auth)

        # compress the subset output
        self.logger.info('Compressing subset output as zip')
        datapath = os.path.join(env.output_dir, uid)
        shutil.make_archive(datapath, 'zip', datapath)

        # create the resource using the hsapi
        self.logger.info('Creating HydroShare resource')
        extra_metadata = '{"appkey": "MyBinder"}'
        resource_id = hs.createResource(
            'CompositeResource',
            title,
            resource_file=f'{datapath}.zip',
            keywords=keywords,
            abstract=abstract,
            extra_metadata=extra_metadata,
        )

        #        options = {
        #                    "zip_with_rel_path": f"{uid}.zip",
        #                    "remove_original_zip": True,
        #                    "overwrite": False
        #                  }
        #        app_log.info('unzipping the HS content')
        #        app_log.info(options)
        #        result = hs.resource(resource_id).functions.unzip(options)
        #        app_log.info(result)

        # redirect to the HS page when finished
        self.redirect(
            f'https://hydroshare.org/resource/{resource_id}?resource-mode=edit'
        )
    def getSecureConnection(self, username=None):
        """Establishes a secure connection with hydroshare.

        args:
        -- username: Optional

        returns:
        -- hydroshare authentication information
        """

        # Use oauth2 if possible
        authfile = os.path.expanduser("~/.hs_auth")
        if os.path.exists(authfile):
            with open(authfile, 'rb') as f:
                token, cid = pickle.load(f)
            return HydroShareAuthOAuth2(cid, '', token=token)

        print('\nThe hs_utils library requires a secure connection to '
              'your HydroShare account.')

        if not os.path.exists(self.auth_path):
            print('\nThe hs_utils library requires a secure connection to '
                  'your HydroShare account.')
            if username is None:
                if 'HS_USR_NAME' in os.environ.keys():
                    username = os.environ['HS_USR_NAME']
                else:
                    username = input('Please enter your HydroShare username: '******'Enter the HydroShare password for user '
                                '\'%s\': ' % username)
            auth = HydroShareAuthBasic(username=username, password=p)

            if self.cache:
                with open(self.auth_path, 'wb') as f:
                    pickle.dump(auth, f, protocol=2)
        else:
            with open(self.auth_path, 'rb') as f:
                auth = pickle.load(f)
        return auth
    def handle(self, *args,
               **options):  # (f,fileid, databeginson,columnheaderson, cmd):
        # cmdline = bool(options['cmdline'][0])
        username = str(options['username'][0])
        password = str(options['password'][0])
        hs_client_id = settings.SOCIAL_AUTH_HYDROSHARE_UP_KEY
        hs_client_secret = settings.SOCIAL_AUTH_HYDROSHARE_UP_SECRET
        auth = HydroShareAuthOAuth2(hs_client_id,
                                    hs_client_secret,
                                    username=username,
                                    password=password)
        # print(username)
        # print(password)
        export_complete = True
        resource_link = ''
        user = str(options['django_user_name'][0])
        datasettitle = str(options['datasettitle'][0])
        startdate = str(options['startdate'][0])
        enddate = str(options['enddate'][0])
        datafile = str(options['datafile'][0])
        dbfilename = str(options['dbfilename'][0])
        # print(request.POST['hydroshareusername'])

        # hs = get_oauth_hs(request)
        # userInfo = hs.getUserInfo()
        #
        hs = HydroShare(auth=auth)
        # username = hs.getUserInfo()
        # print(username)
        abstracttext = ''
        title = ''

        abstracttext += 'CZ Manager dataset: ' + str(datasettitle)
        title += 'CZ Manager dataset ' + str(datasettitle)
        abstract = abstracttext
        keywords = ['ODM2']
        rtype = 'GenericResource'
        fpath = datafile  # str(exportdb.DATABASES['default']['NAME'])

        # # print(fpath)
        # #metadata = '[{"coverage":{"type":"period", "value":{"start":"'+entered_start_date +'", "end":"'+ entered_end_date +'"}}}, {"creator":{"name":"Miguel Leon"}}]'
        metadata = '[{"coverage":{"type":"period", "value":{"start":"' + str(startdate) + '", "end":"' + str(
            enddate) + '"}}}, ' \
                       '{"creator":{"name":"' + user + '"}}]'
        extra_metadata = '{"key-1": "value-1", "key-2": "value-2"}'
        # #abstract = 'My abstract'
        # #title = 'My resource'
        # #keywords = ('my keyword 1', 'my keyword 2')
        # #rtype = 'GenericResource'
        # #fpath = 'C:/Users/leonmi/Google Drive/ODM2AdminLT2/ODM2SQliteBlank.db'
        # #metadata = '[{"coverage":{"type":"period", "value":{"start":"01/01/2000", "end":"12/12/2010"}}}, {"creator":{"name":"John Smith"}}, {"creator":{"name":"Lisa Miller"}}]'
        # #extra_metadata = '{"key-1": "value-1", "key-2": "value-2"}'
        # messages.success(request, 'Profile details updated.')
        resource_id = hs.createResource(rtype,
                                        title,
                                        resource_file=datafile,
                                        resource_filename=dbfilename,
                                        keywords=keywords,
                                        abstract=abstract,
                                        metadata=metadata,
                                        extra_metadata=extra_metadata)
        print('resource created')
        print(resource_id)