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
|
# Copyright (C) 2012, Aleksey Lim
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import json
import types
import logging
from gettext import gettext as _
import active_document as ad
from active_document import util, enforce
from restful_document import env
from restful_document.http import Request, Response
_logger = logging.getLogger('rd.router')
@ad.volume_command(method='POST', cmd='subscribe',
permissions=ad.ACCESS_AUTH)
def _subscribe():
enforce(env.subscriber is not None, _('Subscriptions are not allowed'))
return env.subscriber.subscribe()
class Router(object):
def __init__(self, volume):
self._volume = volume
self._authenticated = set()
if 'SSH_ASKPASS' in os.environ:
# Otherwise ssh-keygen will popup auth dialogs on registeration
del os.environ['SSH_ASKPASS']
def __call__(self, environ, start_response):
response = Response()
result = None
try:
request = Request(environ)
_logger.debug('Processing %s request %s: %s',
request.method, request.url,
request.content or '(no sent data)')
self._authenticate(request)
result = ad.call(self._volume, request, response)
except Exception, error:
if isinstance(error, ad.Redirect):
response.status = '303 See Other'
response['Location'] = error.location
result = ''
elif isinstance(error, ad.Unauthorized):
response.status = '401 Unauthorized'
response['WWW-Authenticate'] = 'Sugar'
elif isinstance(error, ad.Forbidden):
response.status = '403 Forbidden'
elif isinstance(error, ad.NotFound):
response.status = '404 Not Found'
elif isinstance(error, env.HTTPStatus):
response.status = error.status
response.update(error.headers or {})
result = error.result
else:
util.exception(_('Error while processing %r request'),
environ['PATH_INFO'] or '/')
response.status = '500 Internal Server Error'
if result is None:
result = {'error': str(error),
'request': environ['PATH_INFO'] or '/',
}
response.content_type = 'application/json'
start_response(response.status, response.items())
if isinstance(result, types.GeneratorType):
for i in result:
yield i
else:
if response.content_type == 'application/json':
result = json.dumps(result)
yield result
def _authenticate(self, request):
user = request.envar('sugar_user')
if not user:
ad.principal.user = None
return
if user not in self._authenticated and \
(request.path != ['user'] or request.method != 'POST'):
_logger.debug('Logging %r user', user)
enforce(self._volume['user'].exists(user), ad.Unauthorized,
_('Principal user does not exist'))
self._authenticated.add(user)
ad.principal.user = user
|