Use function decorators to check authentication tokens.
This commit is contained in:
33
dsst/dsst_server/auth.py
Normal file
33
dsst/dsst_server/auth.py
Normal file
@@ -0,0 +1,33 @@
|
||||
READ_TOKENS = []
|
||||
WRITE_TOKENS = []
|
||||
|
||||
|
||||
class AuthenticationError(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
def get_response(self):
|
||||
return {
|
||||
'success': False,
|
||||
'message': 'Authentication Failed:\n'.format(self.message)
|
||||
}
|
||||
|
||||
|
||||
def check_read(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
token = args[0]
|
||||
if token in READ_TOKENS + WRITE_TOKENS:
|
||||
return func(*args[1:], **kwargs)
|
||||
else:
|
||||
raise AuthenticationError('Token "{}" has no read access on database.'.format(token))
|
||||
return wrapper
|
||||
|
||||
|
||||
def check_write(func):
|
||||
def wrapper(*args, **kwargs):
|
||||
token = args[0]
|
||||
if token in WRITE_TOKENS:
|
||||
return func(*args[1:], **kwargs)
|
||||
else:
|
||||
raise AuthenticationError('Token "{}" has no write access on database.'.format(token))
|
||||
return wrapper
|
||||
Reference in New Issue
Block a user