auth: Allow Python classes as request validators (see #4)
In addition to setuptools entry point names, the authentication subsystem now accepts Python classes directly as the value of the `milla.request_validator` configuration setting. This removes the dependency on setuptools for authentication, and allows more flexibility in application configuration.master
parent
86b19bb9e7
commit
94b98a0620
|
@ -18,9 +18,12 @@
|
||||||
'''
|
'''
|
||||||
|
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from milla.auth import RequestValidator, NotAuthorized, permissions
|
from milla.auth import permissions
|
||||||
import milla
|
import milla.auth
|
||||||
import pkg_resources
|
try:
|
||||||
|
import pkg_resources
|
||||||
|
except ImportError:
|
||||||
|
pkg_resources = None
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['auth_required', 'require_perms']
|
__all__ = ['auth_required', 'require_perms']
|
||||||
|
@ -40,20 +43,32 @@ def _find_request(*args, **kwargs):
|
||||||
|
|
||||||
def _validate_request(func, requirement, *args, **kwargs):
|
def _validate_request(func, requirement, *args, **kwargs):
|
||||||
request = _find_request(*args, **kwargs)
|
request = _find_request(*args, **kwargs)
|
||||||
ep_name = request.config.get('request_validator', 'default')
|
|
||||||
|
|
||||||
# Override the RequestVariable name with a class from the specified
|
rv = request.config.get('request_validator', 'default')
|
||||||
# entry point, if one is available. Otherwise, the default is used.
|
if hasattr(rv, 'validate'):
|
||||||
for ep in pkg_resources.iter_entry_points(VALIDATOR_EP_GROUP, ep_name):
|
# Config specifies a request validator class explicitly instead
|
||||||
try:
|
# of an entry point name, so use it directly
|
||||||
RequestValidator = ep.load()
|
validator = rv()
|
||||||
except:
|
elif pkg_resources:
|
||||||
continue
|
for ep in pkg_resources.iter_entry_points(VALIDATOR_EP_GROUP, rv):
|
||||||
|
try:
|
||||||
|
validator = ep.load()()
|
||||||
|
break
|
||||||
|
except:
|
||||||
|
# Ignore errors loading entry points or creating instances
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
# No entry point loaded or request validator instance
|
||||||
|
# created, use the default
|
||||||
|
validator = milla.auth.RequestValidator()
|
||||||
|
else:
|
||||||
|
# config does not specify a request validator class, and
|
||||||
|
# setuptools is not available, use the default
|
||||||
|
validator = milla.auth.RequestValidator()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
validator = RequestValidator()
|
|
||||||
validator.validate(request, requirement)
|
validator.validate(request, requirement)
|
||||||
except NotAuthorized as e:
|
except milla.auth.NotAuthorized as e:
|
||||||
return e(request)
|
return e(request)
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue