diff options
author | 2018-02-15 13:44:33 +0000 | |
---|---|---|
committer | 2018-02-15 13:44:33 +0000 | |
commit | 7d6bec9cdb87b8ab31ad2c23b3d2d339595e5bb2 (patch) | |
tree | fcbc93c2f6bdc0433d540ddf978e9075961084be /pysite/decorators.py | |
parent | Fix odd typing error (diff) |
Move API validation decorator to its own file #yxdk
Diffstat (limited to 'pysite/decorators.py')
-rw-r--r-- | pysite/decorators.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/pysite/decorators.py b/pysite/decorators.py new file mode 100644 index 00000000..6951e875 --- /dev/null +++ b/pysite/decorators.py @@ -0,0 +1,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 |