2019-12-02 03:32:03 +00:00
|
|
|
import base64
|
|
|
|
import binascii
|
2018-03-31 00:32:46 +00:00
|
|
|
from functools import wraps
|
2021-10-20 13:18:30 +00:00
|
|
|
from flask import g, request, abort, current_app, Response
|
2019-12-02 03:32:03 +00:00
|
|
|
from flask_login import current_user
|
2018-03-31 00:32:46 +00:00
|
|
|
|
2019-12-21 14:43:03 +00:00
|
|
|
from .models import User, ApiKey, Setting, Domain, Setting
|
2019-12-02 03:32:03 +00:00
|
|
|
from .lib.errors import RequestIsNotJSON, NotEnoughPrivileges
|
|
|
|
from .lib.errors import DomainAccessForbidden
|
2018-03-31 00:32:46 +00:00
|
|
|
|
|
|
|
def admin_role_required(f):
|
2018-08-31 04:57:06 +00:00
|
|
|
"""
|
|
|
|
Grant access if user is in Administrator role
|
|
|
|
"""
|
2018-03-31 00:32:46 +00:00
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
2019-12-02 03:32:03 +00:00
|
|
|
if current_user.role.name != 'Administrator':
|
|
|
|
abort(403)
|
2018-03-31 00:32:46 +00:00
|
|
|
return f(*args, **kwargs)
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2018-03-31 00:32:46 +00:00
|
|
|
return decorated_function
|
|
|
|
|
|
|
|
|
2018-08-31 04:57:06 +00:00
|
|
|
def operator_role_required(f):
|
|
|
|
"""
|
|
|
|
Grant access if user is in Operator role or higher
|
|
|
|
"""
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
2019-12-02 03:32:03 +00:00
|
|
|
if current_user.role.name not in ['Administrator', 'Operator']:
|
|
|
|
abort(403)
|
2018-08-31 04:57:06 +00:00
|
|
|
return f(*args, **kwargs)
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2018-08-31 04:57:06 +00:00
|
|
|
return decorated_function
|
|
|
|
|
|
|
|
|
2021-03-27 18:33:11 +00:00
|
|
|
def history_access_required(f):
|
|
|
|
"""
|
|
|
|
Grant access if user is in Operator role or higher, or Users can view history
|
|
|
|
"""
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
if current_user.role.name not in [
|
|
|
|
'Administrator', 'Operator'
|
|
|
|
] and not Setting().get('allow_user_view_history'):
|
|
|
|
abort(403)
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
|
|
|
|
return decorated_function
|
|
|
|
|
|
|
|
|
2018-03-31 00:32:46 +00:00
|
|
|
def can_access_domain(f):
|
2018-08-31 04:57:06 +00:00
|
|
|
"""
|
|
|
|
Grant access if:
|
|
|
|
- user is in Operator role or higher, or
|
|
|
|
- user is in granted Account, or
|
|
|
|
- user is in granted Domain
|
|
|
|
"""
|
2018-03-31 00:32:46 +00:00
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
2019-12-02 03:32:03 +00:00
|
|
|
if current_user.role.name not in ['Administrator', 'Operator']:
|
2018-03-31 00:32:46 +00:00
|
|
|
domain_name = kwargs.get('domain_name')
|
2019-12-02 03:32:03 +00:00
|
|
|
domain = Domain.query.filter(Domain.name == domain_name).first()
|
|
|
|
|
|
|
|
if not domain:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
valid_access = Domain(id=domain.id).is_valid_access(
|
|
|
|
current_user.id)
|
2018-03-31 00:32:46 +00:00
|
|
|
|
2019-12-02 03:32:03 +00:00
|
|
|
if not valid_access:
|
|
|
|
abort(403)
|
2018-03-31 00:32:46 +00:00
|
|
|
|
|
|
|
return f(*args, **kwargs)
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2018-03-31 00:32:46 +00:00
|
|
|
return decorated_function
|
2018-06-07 02:28:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def can_configure_dnssec(f):
|
2018-08-31 04:57:06 +00:00
|
|
|
"""
|
|
|
|
Grant access if:
|
|
|
|
- user is in Operator role or higher, or
|
|
|
|
- dnssec_admins_only is off
|
|
|
|
"""
|
2018-06-07 02:28:14 +00:00
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
2019-12-02 03:32:03 +00:00
|
|
|
if current_user.role.name not in [
|
|
|
|
'Administrator', 'Operator'
|
|
|
|
] and Setting().get('dnssec_admins_only'):
|
|
|
|
abort(403)
|
2018-06-07 02:28:14 +00:00
|
|
|
|
|
|
|
return f(*args, **kwargs)
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2018-06-07 02:28:14 +00:00
|
|
|
return decorated_function
|
2018-09-01 10:53:05 +00:00
|
|
|
|
2021-10-30 19:21:45 +00:00
|
|
|
def can_remove_domain(f):
|
|
|
|
"""
|
|
|
|
Grant access if:
|
|
|
|
- user is in Operator role or higher, or
|
|
|
|
- allow_user_remove_domain is on
|
|
|
|
"""
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
if current_user.role.name not in [
|
|
|
|
'Administrator', 'Operator'
|
|
|
|
] and not Setting().get('allow_user_remove_domain'):
|
|
|
|
abort(403)
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
|
|
|
|
return decorated_function
|
|
|
|
|
|
|
|
|
2018-09-01 10:53:05 +00:00
|
|
|
|
|
|
|
def can_create_domain(f):
|
|
|
|
"""
|
|
|
|
Grant access if:
|
|
|
|
- user is in Operator role or higher, or
|
|
|
|
- allow_user_create_domain is on
|
|
|
|
"""
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
2019-12-02 03:32:03 +00:00
|
|
|
if current_user.role.name not in [
|
|
|
|
'Administrator', 'Operator'
|
|
|
|
] and not Setting().get('allow_user_create_domain'):
|
|
|
|
abort(403)
|
2019-03-01 22:49:31 +00:00
|
|
|
return f(*args, **kwargs)
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2019-03-01 22:49:31 +00:00
|
|
|
return decorated_function
|
|
|
|
|
|
|
|
|
|
|
|
def api_basic_auth(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
auth_header = request.headers.get('Authorization')
|
|
|
|
if auth_header:
|
|
|
|
auth_header = auth_header.replace('Basic ', '', 1)
|
|
|
|
|
|
|
|
try:
|
|
|
|
auth_header = str(base64.b64decode(auth_header), 'utf-8')
|
|
|
|
username, password = auth_header.split(":")
|
2019-12-02 03:32:03 +00:00
|
|
|
except binascii.Error as e:
|
|
|
|
current_app.logger.error(
|
|
|
|
'Invalid base64-encoded of credential. Error {0}'.format(
|
|
|
|
e))
|
|
|
|
abort(401)
|
2019-03-01 22:49:31 +00:00
|
|
|
except TypeError as e:
|
2019-12-02 03:32:03 +00:00
|
|
|
current_app.logger.error('Error: {0}'.format(e))
|
2019-03-01 22:49:31 +00:00
|
|
|
abort(401)
|
|
|
|
|
2019-12-02 03:32:03 +00:00
|
|
|
user = User(username=username,
|
|
|
|
password=password,
|
|
|
|
plain_text_password=password)
|
2019-03-01 22:49:31 +00:00
|
|
|
|
|
|
|
try:
|
2019-12-21 14:43:03 +00:00
|
|
|
if Setting().get('verify_user_email') and user.email and not user.confirmed:
|
|
|
|
current_app.logger.warning(
|
|
|
|
'Basic authentication failed for user {} because of unverified email address'
|
|
|
|
.format(username))
|
|
|
|
abort(401)
|
|
|
|
|
2019-03-01 22:49:31 +00:00
|
|
|
auth_method = request.args.get('auth_method', 'LOCAL')
|
|
|
|
auth_method = 'LDAP' if auth_method != 'LOCAL' else 'LOCAL'
|
2019-12-02 03:32:03 +00:00
|
|
|
auth = user.is_validate(method=auth_method,
|
|
|
|
src_ip=request.remote_addr)
|
2019-03-01 22:49:31 +00:00
|
|
|
|
|
|
|
if not auth:
|
2019-12-02 03:32:03 +00:00
|
|
|
current_app.logger.error('Checking user password failed')
|
2019-03-01 22:49:31 +00:00
|
|
|
abort(401)
|
|
|
|
else:
|
|
|
|
user = User.query.filter(User.username == username).first()
|
2019-12-08 11:23:36 +00:00
|
|
|
current_user = user # lgtm [py/unused-local-variable]
|
2019-03-01 22:49:31 +00:00
|
|
|
except Exception as e:
|
2019-12-02 03:32:03 +00:00
|
|
|
current_app.logger.error('Error: {0}'.format(e))
|
2019-03-01 22:49:31 +00:00
|
|
|
abort(401)
|
|
|
|
else:
|
2019-12-02 03:32:03 +00:00
|
|
|
current_app.logger.error('Error: Authorization header missing!')
|
2019-03-01 22:49:31 +00:00
|
|
|
abort(401)
|
|
|
|
|
|
|
|
return f(*args, **kwargs)
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2019-03-01 22:49:31 +00:00
|
|
|
return decorated_function
|
|
|
|
|
|
|
|
|
|
|
|
def is_json(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
if request.method in ['POST', 'PUT', 'PATCH']:
|
|
|
|
if not request.is_json:
|
|
|
|
raise RequestIsNotJSON()
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
|
|
|
|
return decorated_function
|
|
|
|
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2021-12-06 21:38:16 +00:00
|
|
|
def callback_if_request_body_contains_key(callback, http_methods=[], keys=[]):
|
|
|
|
"""
|
|
|
|
If request body contains one or more of specified keys, call
|
|
|
|
:param callback
|
|
|
|
"""
|
|
|
|
def decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
check_current_http_method = not http_methods or request.method in http_methods
|
|
|
|
if (check_current_http_method and
|
|
|
|
set(request.get_json(force=True).keys()).intersection(set(keys))
|
|
|
|
):
|
|
|
|
callback(*args, **kwargs)
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return decorated_function
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2020-01-27 12:38:38 +00:00
|
|
|
def api_role_can(action, roles=None, allow_self=False):
|
|
|
|
"""
|
|
|
|
Grant access if:
|
|
|
|
- user is in the permitted roles
|
|
|
|
- allow_self and kwargs['user_id'] = current_user.id
|
|
|
|
- allow_self and kwargs['username'] = current_user.username
|
|
|
|
"""
|
|
|
|
if roles is None:
|
|
|
|
roles = ['Administrator', 'Operator']
|
|
|
|
|
|
|
|
def decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
user_id = int(kwargs.get('user_id'))
|
|
|
|
except:
|
|
|
|
user_id = None
|
|
|
|
try:
|
|
|
|
username = kwargs.get('username')
|
|
|
|
except:
|
|
|
|
username = None
|
|
|
|
if (
|
|
|
|
(current_user.role.name in roles) or
|
|
|
|
(allow_self and user_id and current_user.id == user_id) or
|
|
|
|
(allow_self and username and current_user.username == username)
|
|
|
|
):
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
msg = (
|
|
|
|
"User {} with role {} does not have enough privileges to {}"
|
|
|
|
).format(current_user.username, current_user.role.name, action)
|
|
|
|
raise NotEnoughPrivileges(message=msg)
|
|
|
|
return decorated_function
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2019-03-01 22:49:31 +00:00
|
|
|
def api_can_create_domain(f):
|
|
|
|
"""
|
|
|
|
Grant access if:
|
|
|
|
- user is in Operator role or higher, or
|
|
|
|
- allow_user_create_domain is on
|
|
|
|
"""
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
2019-12-02 03:32:03 +00:00
|
|
|
if current_user.role.name not in [
|
|
|
|
'Administrator', 'Operator'
|
|
|
|
] and not Setting().get('allow_user_create_domain'):
|
2019-03-01 22:49:31 +00:00
|
|
|
msg = "User {0} does not have enough privileges to create domain"
|
2019-12-02 03:32:03 +00:00
|
|
|
current_app.logger.error(msg.format(current_user.username))
|
2019-03-01 22:49:31 +00:00
|
|
|
raise NotEnoughPrivileges()
|
|
|
|
return f(*args, **kwargs)
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2019-03-01 22:49:31 +00:00
|
|
|
return decorated_function
|
|
|
|
|
|
|
|
|
2021-11-27 22:58:22 +00:00
|
|
|
def apikey_can_create_domain(f):
|
|
|
|
"""
|
|
|
|
Grant access if:
|
|
|
|
- user is in Operator role or higher, or
|
|
|
|
- allow_user_create_domain is on
|
|
|
|
"""
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
if g.apikey.role.name not in [
|
|
|
|
'Administrator', 'Operator'
|
|
|
|
] and not Setting().get('allow_user_create_domain'):
|
|
|
|
msg = "ApiKey #{0} does not have enough privileges to create domain"
|
|
|
|
current_app.logger.error(msg.format(g.apikey.id))
|
|
|
|
raise NotEnoughPrivileges()
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
|
|
|
|
return decorated_function
|
|
|
|
|
|
|
|
|
|
|
|
def apikey_can_remove_domain(http_methods=[]):
|
|
|
|
"""
|
|
|
|
Grant access if:
|
|
|
|
- user is in Operator role or higher, or
|
|
|
|
- allow_user_remove_domain is on
|
|
|
|
"""
|
|
|
|
def decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
check_current_http_method = not http_methods or request.method in http_methods
|
|
|
|
|
|
|
|
if (check_current_http_method and
|
|
|
|
g.apikey.role.name not in ['Administrator', 'Operator'] and
|
|
|
|
not Setting().get('allow_user_remove_domain')
|
|
|
|
):
|
|
|
|
msg = "ApiKey #{0} does not have enough privileges to remove domain"
|
|
|
|
current_app.logger.error(msg.format(g.apikey.id))
|
|
|
|
raise NotEnoughPrivileges()
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return decorated_function
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2019-03-01 22:49:31 +00:00
|
|
|
def apikey_is_admin(f):
|
|
|
|
"""
|
|
|
|
Grant access if user is in Administrator role
|
|
|
|
"""
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
if g.apikey.role.name != 'Administrator':
|
|
|
|
msg = "Apikey {0} does not have enough privileges to create domain"
|
2019-12-02 03:32:03 +00:00
|
|
|
current_app.logger.error(msg.format(g.apikey.id))
|
2019-03-01 22:49:31 +00:00
|
|
|
raise NotEnoughPrivileges()
|
|
|
|
return f(*args, **kwargs)
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2019-03-01 22:49:31 +00:00
|
|
|
return decorated_function
|
|
|
|
|
|
|
|
|
|
|
|
def apikey_can_access_domain(f):
|
2021-12-06 21:38:16 +00:00
|
|
|
"""
|
|
|
|
Grant access if:
|
|
|
|
- user has Operator role or higher, or
|
|
|
|
- user has explicitly been granted access to domain
|
|
|
|
"""
|
2019-03-01 22:49:31 +00:00
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
if g.apikey.role.name not in ['Administrator', 'Operator']:
|
2020-12-07 21:06:37 +00:00
|
|
|
zone_id = kwargs.get('zone_id').rstrip(".")
|
2021-12-06 21:38:16 +00:00
|
|
|
domain_names = [item.name for item in g.apikey.domains]
|
2019-03-01 22:49:31 +00:00
|
|
|
|
2021-12-06 21:38:16 +00:00
|
|
|
accounts = g.apikey.accounts
|
2021-12-03 14:12:11 +00:00
|
|
|
accounts_domains = [domain.name for a in accounts for domain in a.domains]
|
|
|
|
|
|
|
|
allowed_domains = set(domain_names + accounts_domains)
|
|
|
|
|
|
|
|
if zone_id not in allowed_domains:
|
2019-03-01 22:49:31 +00:00
|
|
|
raise DomainAccessForbidden()
|
|
|
|
return f(*args, **kwargs)
|
2019-12-02 03:32:03 +00:00
|
|
|
|
2019-03-01 22:49:31 +00:00
|
|
|
return decorated_function
|
|
|
|
|
|
|
|
|
2021-12-06 21:38:16 +00:00
|
|
|
def apikey_can_configure_dnssec(http_methods=[]):
|
|
|
|
"""
|
|
|
|
Grant access if:
|
|
|
|
- user is in Operator role or higher, or
|
|
|
|
- dnssec_admins_only is off
|
|
|
|
"""
|
|
|
|
def decorator(f=None):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
check_current_http_method = not http_methods or request.method in http_methods
|
|
|
|
|
|
|
|
if (check_current_http_method and
|
|
|
|
g.apikey.role.name not in ['Administrator', 'Operator'] and
|
|
|
|
Setting().get('dnssec_admins_only')
|
|
|
|
):
|
|
|
|
msg = "ApiKey #{0} does not have enough privileges to configure dnssec"
|
|
|
|
current_app.logger.error(msg.format(g.apikey.id))
|
|
|
|
raise DomainAccessForbidden(message=msg)
|
|
|
|
return f(*args, **kwargs) if f else None
|
|
|
|
return decorated_function
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2019-03-01 22:49:31 +00:00
|
|
|
def apikey_auth(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
auth_header = request.headers.get('X-API-KEY')
|
|
|
|
if auth_header:
|
|
|
|
try:
|
|
|
|
apikey_val = str(base64.b64decode(auth_header), 'utf-8')
|
2019-12-02 03:32:03 +00:00
|
|
|
except binascii.Error as e:
|
|
|
|
current_app.logger.error(
|
|
|
|
'Invalid base64-encoded of credential. Error {0}'.format(
|
|
|
|
e))
|
|
|
|
abort(401)
|
2019-03-01 22:49:31 +00:00
|
|
|
except TypeError as e:
|
2019-12-02 03:32:03 +00:00
|
|
|
current_app.logger.error('Error: {0}'.format(e))
|
2019-03-01 22:49:31 +00:00
|
|
|
abort(401)
|
|
|
|
|
2019-12-02 03:32:03 +00:00
|
|
|
apikey = ApiKey(key=apikey_val)
|
2019-03-01 22:49:31 +00:00
|
|
|
apikey.plain_text_password = apikey_val
|
|
|
|
|
|
|
|
try:
|
|
|
|
auth_method = 'LOCAL'
|
2019-12-02 03:32:03 +00:00
|
|
|
auth = apikey.is_validate(method=auth_method,
|
|
|
|
src_ip=request.remote_addr)
|
2019-03-01 22:49:31 +00:00
|
|
|
|
|
|
|
g.apikey = auth
|
|
|
|
except Exception as e:
|
2019-12-02 03:32:03 +00:00
|
|
|
current_app.logger.error('Error: {0}'.format(e))
|
2019-03-01 22:49:31 +00:00
|
|
|
abort(401)
|
|
|
|
else:
|
2019-12-02 03:32:03 +00:00
|
|
|
current_app.logger.error('Error: API key header missing!')
|
2019-03-01 22:49:31 +00:00
|
|
|
abort(401)
|
2018-09-01 10:53:05 +00:00
|
|
|
|
|
|
|
return f(*args, **kwargs)
|
2019-12-02 03:32:03 +00:00
|
|
|
|
|
|
|
return decorated_function
|
|
|
|
|
|
|
|
|
|
|
|
def dyndns_login_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
if current_user.is_authenticated is False:
|
2021-10-20 13:18:30 +00:00
|
|
|
return Response(headers={'WWW-Authenticate': 'Basic'}, status=401)
|
2019-12-02 03:32:03 +00:00
|
|
|
return f(*args, **kwargs)
|
|
|
|
|
2018-09-01 10:53:05 +00:00
|
|
|
return decorated_function
|
2021-01-16 19:37:11 +00:00
|
|
|
|
|
|
|
def apikey_or_basic_auth(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
api_auth_header = request.headers.get('X-API-KEY')
|
|
|
|
if api_auth_header:
|
|
|
|
return apikey_auth(f)(*args, **kwargs)
|
|
|
|
else:
|
|
|
|
return api_basic_auth(f)(*args, **kwargs)
|
|
|
|
return decorated_function
|