# Path: models/access_key.py
# class AccessKey(ndb.Model):
#     """Models an instance of an API key."""
#     access_key = ndb.StringProperty()
#     description = ndb.TextProperty()
# 
#     @staticmethod
#     def generate_uuid():
#         return uuid4().hex
# 
#     @classmethod
#     def create(cls, description):
#         new_key = cls()
#         new_key.access_key = cls.generate_uuid()
#         new_key.description = description
#         new_key.put()
# 
#         return new_key
# 
# Path: util/csrf.py
# def check_csrf_protection():
#     """Make sure POST requests are sent with a CSRF token unless they're part of the API.
#     In the future we might want to think about a system where we can disable CSRF protection
#     on a per-view basis, maybe with a decorator.
#     """
#     if request.method == 'POST':
#         token = session.pop('_csrf_token', None)
#         if not token or token != request.form.get('_csrf_token'):
#             abort(403)


from functools import wraps
from flask import abort
from flask import redirect
from flask import request
from flask.helpers import make_response
from google.appengine.api import users
from models.access_key import AccessKey
from util.csrf import check_csrf_protection




def user_required(func):
    @wraps(func)
    def decorated_view(*args, **kwargs):
        if not users.get_current_user():
            return redirect(users.create_login_url(request.url))
        return func(*args, **kwargs)
    return decorated_view


def admin_required(func):
    @wraps(func)
    def decorated_view(*args, **kwargs):
        if users.get_current_user():
            if not users.is_current_user_admin():
                abort(401)  # Unauthorized
            return func(*args, **kwargs)
        return redirect(users.create_login_url(request.url))
    return decorated_view


def api_key_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        api_key = request.form.get('api_key') or request.args.get('api_key')
        valid_api_key = False
        if api_key is not None:

# Path: models/access_key.py
# class AccessKey(ndb.Model):
#     """Models an instance of an API key."""
#     access_key = ndb.StringProperty()
#     description = ndb.TextProperty()
# 
#     @staticmethod
#     def generate_uuid():
#         return uuid4().hex
# 
#     @classmethod
#     def create(cls, description):
#         new_key = cls()
#         new_key.access_key = cls.generate_uuid()
#         new_key.description = description
#         new_key.put()
# 
#         return new_key
# 
# Path: util/csrf.py
# def check_csrf_protection():
#     """Make sure POST requests are sent with a CSRF token unless they're part of the API.
#     In the future we might want to think about a system where we can disable CSRF protection
#     on a per-view basis, maybe with a decorator.
#     """
#     if request.method == 'POST':
#         token = session.pop('_csrf_token', None)
#         if not token or token != request.form.get('_csrf_token'):
#             abort(403)


# Path: util/decorators.py
from functools import wraps
from flask import abort
from flask import redirect
from flask import request
from flask.helpers import make_response
from google.appengine.api import users
from models.access_key import AccessKey
from util.csrf import check_csrf_protection





def user_required(func):
    @wraps(func)
    def decorated_view(*args, **kwargs):
        if not users.get_current_user():
            return redirect(users.create_login_url(request.url))
        return func(*args, **kwargs)
    return decorated_view


def admin_required(func):
    @wraps(func)
    def decorated_view(*args, **kwargs):
        if users.get_current_user():
            if not users.is_current_user_admin():
                abort(401)  # Unauthorized
            return func(*args, **kwargs)
        return redirect(users.create_login_url(request.url))
    return decorated_view


def api_key_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        api_key = request.form.get('api_key') or request.args.get('api_key')
        valid_api_key = False
        if api_key is not None:

            valid_api_key = AccessKey.query(AccessKey.access_key == api_key).get(keys_only=True) is not None