Skip to content

Commit

Permalink
add admin only decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
wuttinanhi committed Sep 22, 2022
1 parent f24c766 commit 4c99dce
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
Empty file added admin/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions admin/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
admin service
"""

import os


class AdminService:
@staticmethod
def is_valid_admin_key(key: str):
return key == os.getenv("ADMIN_KEY")
19 changes: 19 additions & 0 deletions auth/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from functools import wraps

from admin.service import AdminService
from flask import request
from jwt_wrapper.service import JwtService
from user.service import UserService
Expand All @@ -27,3 +28,21 @@ def decorated_function(*args, **kwargs):
return {"error": "Unauthorized!"}, 401

return decorated_function


def admin_only(f):
@wraps(f)
def decorated_function(*args, **kwargs):
auth_header = request.headers.get("X-API-KEY")
if auth_header == None:
return {"error": "Unauthorized!"}, 401

admin_key = auth_header
check = AdminService.is_valid_admin_key(admin_key)

if check:
return f(*args, **kwargs)

return {"error": "Unauthorized!"}, 401

return decorated_function
1 change: 1 addition & 0 deletions dev.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ENV=dev
DATABASE_URI=mysql+mysqldb://appuser:[email protected]:3306/appdb?charset=utf8mb4
ADMIN_KEY=@Dev12345

0 comments on commit 4c99dce

Please sign in to comment.