-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
139 lines (116 loc) · 4.13 KB
/
server.py
File metadata and controls
139 lines (116 loc) · 4.13 KB
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
135
136
137
138
139
#!/usr/bin/env python
import logging
import os
from datetime import datetime
import falcon
from falcon_cors import CORS
from config import config
from model.auth import Auth
from model.db import DatabaseFactory
from model.mailer import Mailer
from model.view_api import ViewsApi
cors = CORS(
allow_origins_list=config['allowOrigins'],
allow_headers_list=['Content-Type', 'Authorization'],
allow_methods_list=['GET', 'POST', 'PUT', 'PATCH', 'DELETE']
)
class RequireJSON:
def process_request(self, req: falcon.Request, resp: falcon.Response):
if not req.client_accepts_json:
raise falcon.HTTPNotAcceptable(
'This API only supports responses encoded as JSON.',
href='http://docs.examples.com/api/json')
if req.method in ('POST', 'PUT', 'PATCH'):
if not req.content_type or 'application/json' not in req.content_type:
raise falcon.HTTPUnsupportedMediaType(
'This API only supports requests encoded as JSON.',
href='http://docs.examples.com/api/json')
class MaxBody:
def __init__(self, max_size=1*1024*1025):
self._max_size = max_size
def process_request(self, req: falcon.Request, resp: falcon.Response):
length = req.content_length
if length is not None and length > self._max_size:
msg = ('The size of the request is too large. The body must not '
'exceed ' + str(self._max_size) + ' bytes in length.')
raise falcon.HTTPPayloadTooLarge('Request body is too large', msg)
logging.basicConfig(level=logging.INFO)
if os.environ.get('TEST_USER_DATABASE') == "1":
from db_mock import MockDatabaseFactory
db_factory = MockDatabaseFactory(config['ldap'], mod_timestamp=datetime(2019, 1, 1))
auth_view = config['views'][config['auth']['view']]
view_prefix = auth_view['dn'] + "," + config['ldap']['prefix']
db_factory.connection.add(view_prefix, ['top', 'organizationalUnit'], {'ou': ['groups']})
else:
db_factory = DatabaseFactory(config['ldap'])
views = ViewsApi(db_factory, config['views'])
auth = Auth(views.views, db_factory, config['auth'])
app = falcon.API(
middleware=[cors.middleware, auth.auth_middleware, RequireJSON(), MaxBody()],
)
mailer = Mailer(config['mail'])
views.register(app, auth.relogin)
auth.register(app, mailer)
if os.environ.get('TEST_USER_DATABASE') == "1":
users_view = views.views['users']
groups_view = views.views['groups']
user = {
permission: True
for permission in config['views'][config['auth']['view']]['permissions']
}
user[config['views'][config['auth']['view']]['primaryKey']] = 'unknown'
groups_view.create_detail(user, {
'group': {
'cn': 'admin'
}
})
groups_view.create_detail(user, {
'group': {
'cn': 'superuser'
}
})
groups_view.create_detail(user, {
'group': {
'cn': 'new'
}
})
users_view.create_detail(
user=user,
assignments={
'user': {
'uid': 'test',
'givenName': 'Test',
'sn': 'Tester',
'mail': 'tester@localhost.localdomain',
'mobile': '0123 456789',
'isAdmin': True,
'isSuperuser': True,
'isNew': False,
},
'password': {
'_enabled': True,
'userPassword': 'blablubbbla',
},
'memberOfGroups': {'add': ['admin', 'superuser']},
}
)
users_view.create_detail(
user=user,
assignments={
'user': {
'uid': 'test2',
'givenName': 'Test',
'sn': 'Tester-Two',
'mail': 'tester2@localhost.localdomain',
'mobile': '0123 456789',
'isAdmin': False,
'isSuperuser': False,
'isNew': False,
},
'password': {
'_enabled': True,
'userPassword': 'blablubbbla',
},
'memberOfGroups': {'add': []},
}
)