diff options
Diffstat (limited to 'src/wsgi.py')
-rwxr-xr-x | src/wsgi.py | 89 |
1 files changed, 55 insertions, 34 deletions
diff --git a/src/wsgi.py b/src/wsgi.py index 701f77d..0aff8f4 100755 --- a/src/wsgi.py +++ b/src/wsgi.py @@ -34,9 +34,11 @@ class CollectorResource(): def user_auth(self, auth_header, authfun): if not auth_header: return None, None # Fail. + BAlit, b64 = auth_header.split() if BAlit != "Basic": return None, None # Fail + userbytes, pwbytes = b64decode(b64).split(b':') try: user = userbytes.decode('utf-8') @@ -47,36 +49,55 @@ class CollectorResource(): class EPGet(CollectorResource): - def on_get(self, req, resp): + def on_get(self, req, resp, key=None): out = list() selectors = dict() - resp.status = falcon.HTTP_200 - resp.content_type = falcon.MEDIA_JSON - print(req.context) - if 'domains' in req.context['user']: - orgs = req.context['user']['domains'] +<< << << < HEAD + resp.status = falcon.HTTP_200 + resp.content_type = falcon.MEDIA_JSON - if not orgs: - resp.status = falcon.HTTP_401 - resp.text = json.dumps( - {'status': 'error', 'message': 'Invalid username or password\n'}) - return + print(req.context) + if 'domains' in req.context['user']: + orgs = req.context['user']['domains'] +== == == = + limit = 25 + skip = 0 - for param in req.params: - for i in index.indexes: - for j in i['index']['fields']: - if j == param: - selectors[param] = req.params[param] + orgs = self.user_auth(req.auth, self._users.read_perms) +>>>>>> > main - for org in orgs: - selectors['domain'] = org - data = self._db.search(**selectors) - if data: - out.append(data) + if not orgs: + resp.status = falcon.HTTP_401 + resp.text = json.dumps({ + 'status': 'error', + 'message': 'Invalid username or password\n' + }) + return + if key: + out = self._db.get(key) resp.text = json.dumps({'status': 'success', 'data': out}) + return + + for param in req.params: + if param == 'limit': + limit = req.params['limit'] + elif param == 'skip': + skip = req.params['skip'] + for i in index.indexes: + for j in i['index']['fields']: + if j == param: + selectors[param] = req.params[param] + + for org in orgs: + selectors['domain'] = org + data = self._db.search(**selectors, limit=limit, skip=skip) + if data: + out += data + + resp.text = json.dumps({'status': 'success', 'data': out}) class EPAdd(CollectorResource): @@ -102,6 +123,7 @@ class EPAdd(CollectorResource): # NOTE: Reading the whole body in one go instead of streaming # it nicely. rawin = req.bounded_stream.read() + try: decodedin = rawin.decode('UTF-8') except Exception: @@ -133,25 +155,24 @@ class EPAdd(CollectorResource): def main(port=8000, wsgi_helper=False): db = DictDB(database, hostname, username, password) users = authn.UserDB('wsgi_demo_users.yaml') - resources_map = [ - ('/sc/v0/add', EPAdd(db, users)), - ('/sc/v0/get', EPGet(db, users)) - ] app = falcon.App(cors_enable=True, middleware=middleware_jwt) + app.add_route('/sc/v0/add', EPAdd(db, users)) + app.add_route('/sc/v0/get', EPGet(db, users)) + app.add_route('/sc/v0/get/{key}', EPGet(db, users)) - for url, res in resources_map: - app.add_route(url, res) - - if not wsgi_helper: - print('Serving on port 8000...') - httpd = make_server('', port, app) - httpd.serve_forever() + if wsgi_helper: + return app - return app + print('Serving on port 8000...') + httpd = make_server('', port, app) + httpd.serve_forever() if __name__ == '__main__': - sys.exit(main()) + try: + sys.exit(main()) + except KeyboardInterrupt: + print('\nBye!') else: app = main(port=8000, wsgi_helper=True) |