summaryrefslogtreecommitdiff
path: root/qvisqve/management_router.py
blob: 7ae04b0c509d0e9ebaef446c332cb78d9113b00f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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('')