summaryrefslogtreecommitdiff
path: root/qvisqve/management_router.py
diff options
context:
space:
mode:
Diffstat (limited to 'qvisqve/management_router.py')
-rw-r--r--qvisqve/management_router.py134
1 files changed, 134 insertions, 0 deletions
diff --git a/qvisqve/management_router.py b/qvisqve/management_router.py
new file mode 100644
index 0000000..7ae04b0
--- /dev/null
+++ b/qvisqve/management_router.py
@@ -0,0 +1,134 @@
+# Copyright (C) 2018 Lars Wirzenius
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+import os
+import urllib.parse
+
+
+import bottle
+
+
+import qvisqve
+import qvisqve_secrets
+
+
+class ManagementRouter(qvisqve.Router):
+
+ def __init__(self, storedir, baseurl):
+ super().__init__()
+ rs = qvisqve.FileStore(storedir)
+ self._clients = qvisqve.ClientManager(rs)
+ self._users = qvisqve.UserManager(rs)
+ self._apps = qvisqve.ApplicationManager(rs)
+ self._baseurl = baseurl
+
+ def get_routes(self):
+ clients = ManagementEndpoint(self._baseurl, '/clients', self._clients)
+ users = ManagementEndpoint(self._baseurl, '/users', self._users)
+ apps = ManagementEndpoint(self._baseurl, '/applications', self._apps)
+
+ return clients.get_routes() + users.get_routes() + apps.get_routes()
+
+
+class ManagementEndpoint:
+
+ def __init__(self, baseurl, path, entities):
+ self._baseurl = baseurl
+ self._path = path
+ self._entities = entities
+
+ def get_routes(self):
+ return [
+ {
+ 'method': 'POST',
+ 'path': self._path,
+ 'callback': self._create,
+ },
+ {
+ 'method': 'GET',
+ 'path': self._path,
+ 'callback': self._list,
+ },
+ {
+ 'method': 'GET',
+ 'path': '{}/<id>'.format(self._path),
+ 'callback': self._show,
+ },
+ {
+ 'method': 'PUT',
+ 'path': '{}/<id>/secret'.format(self._path),
+ 'callback': self._set_secret,
+ },
+ {
+ 'method': 'DELETE',
+ 'path': '{}/<id>'.format(self._path),
+ 'callback': self._delete,
+ },
+ ]
+
+ def _create(self, content_type, body, **kwargs):
+ qvisqve.log.log('info', msg_text='Creating', path=self._path)
+
+ entity_id = body.get('id')
+ if not entity_id:
+ return qvisqve.bad_request_response(
+ 'No {}'.format(self._entity_id_name))
+
+ self._entities.create(entity_id, body)
+
+ entity = self._entities.get(entity_id)
+ location = '{}{}/{}'.format(self._baseurl, self._path, entity_id)
+ return qvisqve.created_response(entity, location)
+
+ def _list(self, content_type, body, **kwargs):
+ qvisqve.log.log('info', msg_text='Listing', path=self._path)
+ # FIXME
+ return qvisqve.ok_response({"resources": []})
+
+ def _show(self, content_type, body, **kwargs):
+ qvisqve.log.log('info', msg_text='Showing client')
+
+ entity_id = kwargs['id']
+ try:
+ entity = self._entities.get(entity_id)
+ except qvisqve.ResourceDoesNotExist as e:
+ return qvisqve.not_found_response()
+
+ qvisqve.log.log('info', msg_text='Showing', entity=entity)
+ return qvisqve.ok_response(entity)
+
+ def _set_secret(self, content_type, body, **kwargs):
+ qvisqve.log.log('info', msg_text='Setting client secret')
+
+ entity_id = kwargs['id']
+ try:
+ entity = self._entities.get(entity_id)
+ except qvisqve.ResourceDoesNotExist as e:
+ return qvisqve.not_found_response()
+
+ secret = body.get('secret')
+ if not secret:
+ return qvisqve.bad_request_response('No secret')
+
+ self._entities.set_secret(entity_id, secret)
+ return qvisqve.ok_response(entity)
+
+ def _delete(self, content_type, body, **kwargs):
+ qvisqve.log.log('info', msg_text='Deleting', path=self._path)
+
+ entity_id = kwargs['id']
+ self._entities.delete(entity_id)
+ return qvisqve.ok_response('')