Separated scope from other decorators

This commit is contained in:
0mar
2020-10-27 09:43:43 +01:00
parent b6fa353201
commit dece64d248

View File

@@ -4,6 +4,7 @@
import asyncio import asyncio
import concurrent.futures import concurrent.futures
import errno import errno
import functools
import hashlib import hashlib
import inspect import inspect
import os import os
@@ -298,18 +299,31 @@ def metrics_authentication(self):
raise web.HTTPError(403) raise web.HTTPError(403)
@auth_decorator def needs_scope(scope):
def needs_scope(self, scope, **kwargs):
"""Decorator to restrict access to users or services with the required scope""" """Decorator to restrict access to users or services with the required scope"""
if scope not in self.current_scopes:
# Check if access is not restricted to user/server/group def scope_decorator(func):
match_string = re.compile("^" + re.escape(scope) + r"!.+=.+$") @functools.wraps(func)
subscopes = filter(lambda s: re.search(match_string, s), self.current_scopes) def _auth_func(self, *args, **kwargs):
subset = [subscope.split('=')[1] for subscope in subscopes] if scope not in self.current_scopes:
if not subset: # Check if access is not restricted to user/server/group
raise web.HTTPError(403, "Action is not authorized with current scopes") match_string = re.compile("^" + re.escape(scope) + r"!.+=.+$")
else: subscopes = filter(
kwargs['subset'] = subset lambda s: re.search(match_string, s), self.current_scopes
)
subset = [subscope.split('=')[1] for subscope in subscopes]
if not subset:
raise web.HTTPError(
403, "Action is not authorized with current scopes"
)
else:
kwargs['subset'] = subset
result = func(self, *args, **kwargs)
return result
return _auth_func
return scope_decorator
# Token utilities # Token utilities