1 """Identity management exceptions."""
2
3
4 __all__ = ['IdentityConfigurationException',
5 'IdentityException', 'IdentityFailure',
6 'IdentityManagementNotEnabledException', 'RequestRequiredException',
7 'get_failure_url', 'get_identity_errors', 'set_identity_errors']
8
9 from cherrypy import request, response, HTTPRedirect, InternalRedirect
10
11 import turbogears
12 from turbogears import config
13
14
16 """Save the identity errors in the CherryPy request and WSGI environment."""
17 request.identity_errors = request.wsgi_environ['identity.errors'
18 ] = isinstance(errors, basestring) and [errors] or list(errors)
19
20
22 """Get the identity errors from the CherryPy request or WSGI environment."""
23 return getattr(request, 'identity_errors',
24 request.wsgi_environ.get('identity.errors', []))
25
26
36
37
39 """Base class for all Identity exceptions."""
40 pass
41
42
44 """No request present.
45
46 An attempt was made to use a facility of Identity that requires the
47 presence of an HTTP request.
48
49 """
50
52 return self.args and self.args[0] or (
53 "An attempt was made to use a facility of the TurboGears "
54 "Identity Management framework that relies on an HTTP request "
55 "outside of a request.")
56
57
59 """User forgot to enable Identity management."""
60
62 return self.args and self.args[0] or (
63 "An attempt was made to use a facility of the TurboGears "
64 "Identity Management framework, but identity management hasn't "
65 "been enabled in the config file (via identity.on).")
66
67
69 """Incorrect configuration.
70
71 Exception thrown when the Identity management system hasn't been configured
72 correctly. Mostly, when failure_url is not specified.
73
74 """
75
77 return self.args and self.args[0] or (
78 "Unknown Identity configuration error.")
79
80
82 """Identity failure.
83
84 Exception thrown when an access control check fails.
85
86 """
88 """Set up identity errors on the request and get URL from config."""
89 set_identity_errors(errors)
90 url = get_failure_url(errors)
91 if config.get('identity.force_external_redirect', False):
92
93
94
95
96 try:
97 params = request.original_params
98 except AttributeError:
99 params = request.params
100 params['forward_url'] = request.path_info
101 url = turbogears.url(url, params)
102 raise HTTPRedirect(url)
103 else:
104
105 env = request.wsgi_environ
106
107
108 if config.get('identity.http_basic_auth', False):
109 env['identity.status'] = '401 Unauthorized'
110 env['identity.auth_realm'] = 'Basic realm="%s"' % config.get(
111 'identity.http_auth_realm', 'TurboGears')
112 else:
113 env['identity.status'] = '403 Forbidden'
114 env['identity.path_info'] = request.path_info
115 env['identity.params'] = request.params
116 InternalRedirect.__init__(self, url)
117