1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 OpenStack LLC.
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
19
Simple class that stores security context information in the web request.
21
Projects should subclass this class if they wish to enhance the request
22
context or provide additional information in their specific WSGI pipeline.
29
def generate_request_id():
30
return 'req-' + str(uuid.uuid4())
33
class RequestContext(object):
36
Stores information about the security context under which the user
37
accesses the system, as well as additional request information.
40
def __init__(self, auth_tok=None, user=None, tenant=None, is_admin=False,
41
read_only=False, show_deleted=False, request_id=None):
42
self.auth_tok = auth_tok
45
self.is_admin = is_admin
46
self.read_only = read_only
47
self.show_deleted = show_deleted
49
request_id = generate_request_id()
50
self.request_id = request_id
53
return {'user': self.user,
54
'tenant': self.tenant,
55
'is_admin': self.is_admin,
56
'read_only': self.read_only,
57
'show_deleted': self.show_deleted,
58
'auth_token': self.auth_tok,
59
'request_id': self.request_id}
62
def get_admin_context(show_deleted="no"):
63
context = RequestContext(None,
66
show_deleted=show_deleted)
70
def get_context_from_function_and_args(function, args, kwargs):
71
"""Find an arg of type RequestContext and return it.
73
This is useful in a couple of decorators where we don't
74
know much about the function we're wrapping.
77
for arg in itertools.chain(kwargs.values(), args):
78
if isinstance(arg, RequestContext):