forked from qwc-services/qwc-map-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorigin_detector.py
58 lines (48 loc) · 1.63 KB
/
origin_detector.py
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
import re
class OriginDetector:
"""Request origin detection
Assigns base groups to user identity
based on request origin.
"""
def __init__(self, logger, config):
"""Constructor
:param Logger logger: Application logger
:param obj config: Rules configuration
"""
self.logger = logger
self.config = config
if 'host' in self.config:
for group, expr in self.config['host'].items():
self.config['host'][group] = re.compile(expr)
def detect(self, identity, request):
"""Assign base groups to user identity
based on request origin.
:param str identity: User identity
:param str request: Flask request object
"""
self.logger.debug("Origin detection with request '%s'" % request)
groups = []
if 'host' in self.config:
for group, expr in self.config['host'].items():
if expr.match(request.host):
groups.append(group)
self.logger.debug("Assigned groups: %s" % groups)
username = None
if identity:
if isinstance(identity, dict):
username = identity.get('username')
# NOTE: ignore group from identity
else:
# identity is username
username = identity
group = None
if groups:
group = groups[0]
else:
group = '_public_'
identity = {
'username': username,
'group': group
}
self.logger.info("identity: %s" % identity)
return identity