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__ = [ 
  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   
20 -def set_identity_errors(errors):
21 if isinstance(errors, basestring): 22 errors = [errors] 23 cherrypy.request.identity_errors = list(errors)
24
25 -def get_identity_errors():
26 return getattr(cherrypy.request, 'identity_errors', [])
27
28 -def get_failure_url(errors=None):
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 - def __str__(self):
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
56 -class IdentityManagementNotEnabledException(IdentityException):
57 """User forgot to enable Identity management.""" 58
59 - def __str__(self):
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
65 -class IdentityConfigurationException(IdentityException):
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
74 - def __str__(self):
75 return (self.args and self.args[0] or 76 'Unknown Identity configuration error')
77 78
79 -class IdentityFailure(cherrypy.InternalRedirect, IdentityException):
80 """Identity failure. 81 82 Exception thrown when an access control check fails. 83 84 """
85 - def __init__(self, errors):
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 # Set the status for the response inside the exception instead of 90 # setting this in the login handler. Because it is not normal 91 # that a simple GET to /login receives a 401 http code. 92 cherrypy.response.status = 401 93 94 if config.get('identity.force_external_redirect', False): 95 # We need to use external redirect for https since we are managed 96 # by Apache/nginx or something else that CherryPy won't find. 97 # We also need to set the forward_url, because the Referer header 98 # won't work with an external redirect. 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 # use internal redirect which is quicker 111 cherrypy.InternalRedirect.__init__(self, url)
112