def check_api_key(*args, **kwargs): g.zenodo = None try: req = request.get_json(force=True) except Exception as e: logger.error(e, exc_info=True) req = request.form.to_dict() logger.debug("got request data: {}".format(req)) try: service, userId, apiKey = Util.parseUserId(req.get("userId")) except Exception as e: logger.error(e, exc_info=True) apiKey = Util.loadToken( req.get("userId"), "port-zenodo").access_token if apiKey is None: logger.error("apiKey or userId not found.") abort(401) logger.debug("found apiKey") g.zenodo = Zenodo(apiKey, address=current_app.zenodo_address) return api_method(*args, **kwargs)
def check_api_key(*args, **kwargs): g.zenodo = None try: req = request.get_json(force=True, cache=True) except: req = request.form.to_dict() try: service, userId, apiKey = Util.parseUserId(req.get("userId")) except: apiKey = Util.loadToken(req.get("userId"), "port-openscienceframework").access_token logger.debug("req data: {}".format(req)) if apiKey is None: logger.error("apiKey or userId not found.") abort(401) logger.debug("found apiKey") g.osf = OSF( token=apiKey, address=os.getenv( "OPENSCIENCEFRAMEWORK_API_ADDRESS", "https://api.test.osf.io/v2" ), ) return api_method(*args, **kwargs)
def post(project_id): # trigger upload on datasafe req = request.get_json(force=True, silent=True, cache=True) if req is None: req = request.form.to_dict() logger.debug("got request body: {}", req) try: service, userId, password = Util.parseUserId(req["userId"]) if service != "port-datasafe": logger.debug("got wrong service token") raise ValueError except ValueError: token = Util.loadToken(req["userId"], "port-datasafe") userId = token.user.username password = token.access_token owncloud_token = Util.loadToken(req["username"], "port-owncloud") data = Util.parseToken(owncloud_token) data.update({ "filepath": "{}/ro-crate-metadata.json".format(req["folder"]) }) logger.debug("send data: {}".format(data)) metadata = json.loads( BytesIO( requests.get( "http://circle1-{}/storage/file".format("port-owncloud"), json=data, verify=(os.environ.get("VERIFY_SSL", "True") == "True"), ).content ) .read() .decode("UTF-8") ) logger.debug("got metadata: {}".format(metadata)) doc = ROParser(metadata) logger.debug("parsed metadata: {}".format(doc)) datasafe = Datasafe( userId, owncloud_token.access_token, doc.getElement(doc.rootIdentifier, expand=True, clean=True), req["folder"], os.getenv("DATASAFE_PUBLICKEY"), os.getenv("DATASAFE_PRIVATEKEY") ) logger.debug("Trigger file upload") success = datasafe.triggerUploadForProject() logger.debug(f"Finished trigger, result was: {success}") return jsonify({"success": success}), 200 if success else 500
def test_parseUserId(self): user = ("port-owncloud", "admin", "huhu") example = "{}://{}:{}".format(*user) self.assertEqual(Util.parseUserId(example), user) user = ("port-owncloud", "admin", "huhu") example = "{}://{}:{}".format(*user) self.assertEqual(Util.parseUserId(example), user) user = ("port-owncloud", "admin", None) example = "{}://{}:{}".format(*user) self.assertEqual(Util.parseUserId(example), user) user = ("port-owncloud", "admin", "None") example = "{}://{}:{}".format(*user) self.assertEqual(Util.parseUserId(example), ("port-owncloud", "admin", None)) user = ("port-owncloud", "", "") example = "{}://{}:{}".format(*user) self.assertEqual(Util.parseUserId(example), ("port-owncloud", None, None)) user = ("port-owncloud", "", "") example = "{}://{}:{}".format(*user) self.assertEqual(Util.parseUserId(example), ("port-owncloud", None, None))
def index(): json = request.json try: service, userId, apiKey = Util.parseUserId(json.get("userId")) except: apiKey = Util.loadToken(json.get("userId"), "port-owncloud").access_token filepath = json.get("filepath") logger.debug(f"userid {userId}") files = OwncloudUser(userId, apiKey).getFolder(filepath) return jsonify({"files": files})
def check_api_key(*args, **kwargs): g.zenodo = None try: req = request.get_json(force=True, cache=True) except: req = request.form.to_dict() service, userId, apiKey = Util.parseUserId(req.get("userId")) logger.debug("req data: {}".format(req)) if apiKey is None and userId is not None: apiKey = Util.loadToken(userId, "port-owncloud").access_token if apiKey is None: logger.error("apiKey or userId not found.") abort(401) logger.debug("found apiKey") g.apiKey = apiKey return api_method(*args, **kwargs)
def index(): json = request.json try: service, userId, apiKey = Util.parseUserId(json.get("userId")) except: userId = json.get("userId") service = "port-owncloud" apiKey = Util.loadToken(userId, service).access_token filepath = json.get("filepath") logger.debug(f"userid {userId} for service {service}") import os file = OwncloudUser(userId, apiKey).getFile(filepath) rv = send_file(file, attachment_filename=os.path.basename(filepath), as_attachment=False, mimetype="multipart/form-data") rv.direct_passthrough = False logger.debug("send response") return rv