1 """Identity management exceptions."""
2
3
4 __all__ = [
5 'IdentityConfigurationException',
6 'IdentityException',
7 'IdentityFailure',
8 'IdentityManagementNotEnabledException',
9 'RequestRequiredException',
10 'get_failure_url',
11 'get_identity_errors',
12 'set_identity_errors',
13 ]
14
15 import cherrypy
16
17 import turbogears
18 from turbogears import config
19
24
26 return getattr(cherrypy.request, 'identity_errors', [])
27
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 """
51 return "An attempt was made to use a facility of the TurboGears " \
52 "Identity Management framework that relies on an HTTP request " \
53 "outside of a request."
54
55
57 """User forgot to enable Identity management."""
58
60 return "An attempt was made to use a facility of the TurboGears " \
61 "Identity Management framework, but identity management hasn't " \
62 "been enabled in the config file [via identity.on]."
63
64
66 """Incorrect configuration.
67
68 Exception thrown when the Identity management system hasn't been configured
69 correctly. Mostly, when failure_url is not specified.
70
71 """
72 args = ()
73
75 return (self.args and self.args[0] or
76 'Unknown Identity configuration error')
77
78
80 """Identity failure.
81
82 Exception thrown when an access control check fails.
83
84 """
86 """Set up identity errors on the request and get URL from config."""
87 set_identity_errors(errors)
88 url = get_failure_url(errors)
89
90
91
92 cherrypy.response.status = 401
93
94 if config.get('identity.force_external_redirect', False):
95
96
97
98
99 params = cherrypy.request.original_params
100 params['forward_url'] = cherrypy.request.path_info
101 raise cherrypy.HTTPRedirect(turbogears.url(url, params))
102 else:
103 if config.get('identity.http_basic_auth', False):
104 cherrypy.response.status = 401
105 cherrypy.response.headers['WWW-Authenticate'] = \
106 'Basic realm="%s"' % config.get('identity.http_auth_realm',
107 'TurboGears')
108 else:
109 cherrypy.response.status = 403
110
111 cherrypy.InternalRedirect.__init__(self, url)
112