aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/decorators.py
blob: 6951e87571d1d4a55be4ed977e755fcee5645f6c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# coding=utf-8
import os

from flask import request

from pysite.constants import ErrorCodes


def valid_api_key(f):
    """
    Decorator to check if X-API-Key is valid.

    Should only be applied to functions on APIView routes.
    """

    def has_valid_api_key(self, *args, **kwargs):
        if not request.headers.get("X-API-Key") == os.environ.get("API_KEY"):
            return self.error(ErrorCodes.invalid_api_key)
        return f(*args, **kwargs)

    return has_valid_api_key