summaryrefslogtreecommitdiffstats
path: root/frest/decorators.py
diff options
context:
space:
mode:
authorSanto Cariotti <dcariotti24@gmail.com>2020-03-20 10:06:20 +0000
committerSanto Cariotti <dcariotti24@gmail.com>2020-03-20 10:06:24 +0000
commit6f3c1d94a6a8858369256b63cec90d42d61706ae (patch)
tree7224f1b95b8794ef40a97b55e650d38bac9d7201 /frest/decorators.py
parent9f8faa7c8e7aab724450a281ce3b5583c2c25e6a (diff)
feat: add auth
Diffstat (limited to 'frest/decorators.py')
-rw-r--r--frest/decorators.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/frest/decorators.py b/frest/decorators.py
new file mode 100644
index 0000000..181b62d
--- /dev/null
+++ b/frest/decorators.py
@@ -0,0 +1,40 @@
+from flask import request, abort
+from auth.models import Token
+from functools import wraps
+
+
+def check_token(f):
+ @wraps(f)
+ def inner(*args, **kwargs):
+ userid = request.url.split('/')[-1]
+ headers = request.headers
+ if not headers.get("Authentication"):
+ abort(403)
+
+ auth = request.headers.get("Authentication")
+ token = Token.query.filter_by(string=auth).first()
+ if not token:
+ abort(403)
+
+ if userid.isdigit():
+ if int(userid) != token.user.userId and not token.user.is_admin:
+ abort(403)
+
+ return f(*args, **kwargs)
+
+ return inner
+
+
+def admin_required(f):
+ @wraps(f)
+ def inner(*args, **kwargs):
+ header = request.headers
+
+ auth = request.headers.get("Authentication")
+ token = Token.query.filter_by(string=auth).first()
+ if not token.user.is_admin:
+ abort(403)
+
+ return f(*args, **kwargs)
+
+ return inner