def basic_authorization(authfile='~/.hs_auth_basic'): """ performs basic HS authorization using username and password stored in ~/.hs_auth_basic file in the following format (b64 encoded): { "usr": "******" "pwd": "<password>" } Returns hs_restclient instance or None """ authfile = os.path.expanduser(authfile) # exit if this file doesn't exist if not os.path.exists(authfile): raise Exception(f'Could not find authentication file ' f'[.hs_auth] at {authfile}') try: with open(authfile, 'r') as f: txt = f.read() d = base64.b64decode(txt) creds = json.loads(d.decode()) a = hs_restclient.HydroShareAuthBasic(username=creds['usr'], password=creds['pwd']) hs = hs_restclient.HydroShare(auth=a) hs.getUserInfo() return hs except Exception as e: raise Exception(e) # authorization failed return None
def authenticate(): user_name = raw_input('User name: ') passwd = getpass.getpass() auth = hsrc.HydroShareAuthBasic(username=user_name, password=passwd) hs = hsrc.HydroShare(auth=auth) #del passwd return hs, user_name
def connect(): """ Create a connection to HS using the HS-API """ tries = 0 host = input('Enter host address (default www.hydroshare.org): ' ) or 'www.hydroshare.org' while 1: u = input('Enter HS username: '******'Enter HS password: '******'Authentication failed, attempt %d' % (tries + 1)) tries += 1 if tries >= 3: print('Number of attempts exceeded, exiting') sys.exit(1) print('') return hs
def export_hydroshare(request): project = request.POST.get('project', False) if project is False: messages.error(request, 'Project not found. Please pick a valid project.') return redirect( reverse('geoglows_hydroviewer:geoglows_hydroviewer_creator')) proj_dir = get_project_directory(project) # verify the shapefile zips exist catchment_zip = os.path.join(proj_dir, 'catchment_shapefile.zip') drainageline_zip = os.path.join(proj_dir, 'drainageline_shapefile.zip') if not all( [os.path.exists(catchment_zip), os.path.exists(drainageline_zip)]): if os.path.exists(catchment_zip): os.remove(catchment_zip) if os.path.exists(drainageline_zip): os.remove(drainageline_zip) raise FileNotFoundError('Zipped shapefiles not found') try: # hs = hs_restclient.get_oauth_hs(request) auth = hs_restclient.HydroShareAuthBasic( username=request.POST.get('username'), password=request.POST.get('password')) hs = hs_restclient.HydroShare(auth=auth) resource_id = hs.createResource( 'GenericResource', request.POST.get('title'), resource_file=drainageline_zip, keywords=request.POST.get('keywords').split(', '), abstract=request.POST.get('abstract'), ) hs.addResourceFile(resource_id, catchment_zip) hs.resource(resource_id).functions.unzip( payload={ 'zip_with_rel_path': 'catchment_shapefile.zip', 'remove_original_zip': True }) hs.resource(resource_id).functions.unzip( payload={ 'zip_with_rel_path': 'drainageline_shapefile.zip', 'remove_original_zip': True }) hs.setAccessRules(resource_id, public=True) messages.success( request, f'Successfully Exported To New Hydroshare Resource (ID: {resource_id})' ) except hs_restclient.HydroShareArgumentException as e: print('invalid parameter') print(e) raise e except hs_restclient.HydroShareNotAuthorized as e: print('hydroshare not authorized') print(e) raise e except hs_restclient.HydroShareHTTPException as e: print('unanticipated HTTP error') print(e) raise e # add keys to the export_configs.json with open(os.path.join(proj_dir, 'export_configs.json'), 'r') as configfile: geoserver_configs = json.loads(configfile.read()) geoserver_configs[ 'url'] = 'https://geoserver.hydroshare.org/geoserver/wms' geoserver_configs['workspace'] = f'HS-{resource_id}' geoserver_configs[ 'drainage_layer_name'] = 'drainageline_shapefile ' + str.lower( project) + '_drainagelines' geoserver_configs[ 'catchment_layer_name'] = 'catchment_shapefile ' + str.lower( project) + '_catchments' geoserver_configs['exported_drainage'] = True geoserver_configs['exported_catchment'] = True with open(os.path.join(proj_dir, 'export_configs.json'), 'w') as configfile: configfile.write(json.dumps(geoserver_configs)) return redirect( reverse('geoglows_hydroviewer:project_overview') + f'?{urllib.parse.urlencode(dict(project=project))}')