-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4236140
commit f1968bb
Showing
5 changed files
with
70 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import jwt | ||
import os | ||
from exceptions.UserException import ForbiddenUser, UnauthorizedUser | ||
|
||
|
||
TOKEN_FIELD_NAME = "x-access-token" | ||
|
||
|
||
class AuthService(): | ||
def __init__(self): | ||
self.__secret = os.environ.get("JWT_SECRET") | ||
|
||
def authenticate(self, user_id: int, request): | ||
token = self._get_token(request.headers) | ||
if not token: | ||
raise UnauthorizedUser() | ||
try: | ||
payload = jwt.decode(token, self.__secret, algorithms=["HS256"]) | ||
|
||
except jwt.ExpiredSignatureError: | ||
raise UnauthorizedUser() | ||
|
||
except jwt.InvalidTokenError: | ||
raise UnauthorizedUser() | ||
|
||
if payload.get("user_id") != user_id: | ||
raise ForbiddenUser() | ||
|
||
return | ||
|
||
def _get_token(self, headers: dict): | ||
keyName = None | ||
for key in headers.keys(): | ||
if key.lower() == TOKEN_FIELD_NAME: | ||
keyName = key | ||
if not keyName: | ||
return None | ||
return headers.get(keyName) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters