Package turbogears :: Package identity :: Module exceptions

Source Code for Module turbogears.identity.exceptions

  1  """Identity management exceptions.""" 
  2   
  3  # declare what should be exported 
  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   
15 -def set_identity_errors(errors):
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
21 -def get_identity_errors():
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
27 -def get_failure_url(errors=None):
28 """Get the identity failure URL from the configuration setting.""" 29 url = config.get('identity.failure_url') 30 if callable(url): 31 url = url(errors) 32 if url is None: 33 msg = "Missing URL for identity failure. Please fix this in app.cfg." 34 raise IdentityConfigurationException(msg) 35 return url
36 37
38 -class IdentityException(Exception):
39 """Base class for all Identity exceptions.""" 40 pass
41 42
43 -class RequestRequiredException(IdentityException):
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
51 - def __str__(self):
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
58 -class IdentityManagementNotEnabledException(IdentityException):
59 """User forgot to enable Identity management.""" 60
61 - def __str__(self):
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
68 -class IdentityConfigurationException(IdentityException):
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
76 - def __str__(self):
77 return self.args and self.args[0] or ( 78 "Unknown Identity configuration error.")
79 80
81 -class IdentityFailure(InternalRedirect, IdentityException):
82 """Identity failure. 83 84 Exception thrown when an access control check fails. 85 86 """
87 - def __init__(self, errors):
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 # We need to use external redirect for https since we are managed 93 # by Apache/nginx or something else that CherryPy won't find. 94 # We also need to set the forward_url, because the Referer header 95 # won't work with an external redirect. 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 # By default, use internal redirect which is quicker. 105 env = request.wsgi_environ 106 # Some info need to be saved into the WSGI environment because 107 # request and response are reset when we redirect. 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