1
# Copyright 2011 OpenStack Foundation.
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
17
Simple class that stores security context information in the web request.
19
Projects should subclass this class if they wish to enhance the request
20
context or provide additional information in their specific WSGI pipeline.
27
def generate_request_id():
28
return b'req-' + str(uuid.uuid4()).encode('ascii')
31
class RequestContext(object):
33
"""Helper class to represent useful information about a request context.
35
Stores information about the security context under which the user
36
accesses the system, as well as additional request information.
39
user_idt_format = '{user} {tenant} {domain} {user_domain} {p_domain}'
41
def __init__(self, auth_token=None, user=None, tenant=None, domain=None,
42
user_domain=None, project_domain=None, is_admin=False,
43
read_only=False, show_deleted=False, request_id=None,
45
self.auth_token = auth_token
49
self.user_domain = user_domain
50
self.project_domain = project_domain
51
self.is_admin = is_admin
52
self.read_only = read_only
53
self.show_deleted = show_deleted
54
self.instance_uuid = instance_uuid
56
request_id = generate_request_id()
57
self.request_id = request_id
61
self.user_idt_format.format(user=self.user or '-',
62
tenant=self.tenant or '-',
63
domain=self.domain or '-',
64
user_domain=self.user_domain or '-',
65
p_domain=self.project_domain or '-'))
67
return {'user': self.user,
68
'tenant': self.tenant,
69
'domain': self.domain,
70
'user_domain': self.user_domain,
71
'project_domain': self.project_domain,
72
'is_admin': self.is_admin,
73
'read_only': self.read_only,
74
'show_deleted': self.show_deleted,
75
'auth_token': self.auth_token,
76
'request_id': self.request_id,
77
'instance_uuid': self.instance_uuid,
78
'user_identity': user_idt}
81
def from_dict(cls, ctx):
83
auth_token=ctx.get("auth_token"),
85
tenant=ctx.get("tenant"),
86
domain=ctx.get("domain"),
87
user_domain=ctx.get("user_domain"),
88
project_domain=ctx.get("project_domain"),
89
is_admin=ctx.get("is_admin", False),
90
read_only=ctx.get("read_only", False),
91
show_deleted=ctx.get("show_deleted", False),
92
request_id=ctx.get("request_id"),
93
instance_uuid=ctx.get("instance_uuid"))
96
def get_admin_context(show_deleted=False):
97
context = RequestContext(None,
100
show_deleted=show_deleted)
104
def get_context_from_function_and_args(function, args, kwargs):
105
"""Find an arg of type RequestContext and return it.
107
This is useful in a couple of decorators where we don't
108
know much about the function we're wrapping.
111
for arg in itertools.chain(kwargs.values(), args):
112
if isinstance(arg, RequestContext):
118
def is_user_context(context):
119
"""Indicates if the request context is a normal user."""
120
if not context or context.is_admin:
122
return context.user_id and context.project_id