-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Revamped JWTManager * adapted User_management code to new implementation of the common JWT manager * Added the common folder to UM docker * Added the common folder to user management in github actions --------- Co-authored-by: aLeuleu <[email protected]>
- Loading branch information
Showing
12 changed files
with
172 additions
and
91 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
Empty file.
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,65 @@ | ||
from datetime import datetime, timedelta, timezone | ||
|
||
import jwt | ||
|
||
import common.src.settings as settings | ||
|
||
|
||
class JWTManager: | ||
def __init__(self, | ||
private_key: str | None, | ||
public_key: str | None, | ||
algorithm: str, | ||
expiration_time_minutes: int | None): | ||
self.private_key = private_key | ||
self.public_key = public_key | ||
self.algorithm = algorithm | ||
self.expiration_time_minutes = expiration_time_minutes | ||
|
||
def generate_jwt(self, payload_arg: dict) -> (bool, str | None, list[str] | None): | ||
""" returns: Success, jwt, [error messages] """ | ||
|
||
now = datetime.now(timezone.utc) | ||
expiration_time_minutes = now + timedelta(minutes=self.expiration_time_minutes) | ||
|
||
payload = {'exp': expiration_time_minutes} | ||
for key, value in payload_arg.items(): | ||
payload[key] = value | ||
|
||
try: | ||
token = jwt.encode(payload, self.private_key, algorithm=self.algorithm) | ||
except Exception as e: | ||
return False, None, [str(e)] | ||
return True, token, None | ||
|
||
def decode_jwt(self, encoded_jwt: str) -> (bool, dict | None, list[str] | None): | ||
""" returns: Success, payload, error message """ | ||
|
||
try: | ||
decoded_payload = jwt.decode(encoded_jwt, self.public_key, algorithms=[self.algorithm]) | ||
if decoded_payload.get('exp') is None: | ||
return False, None, ["No expiration date found"] | ||
return True, decoded_payload, None | ||
except Exception as e: | ||
return False, None, [str(e)] | ||
|
||
|
||
class UserAccessJWTDecoder: | ||
JWT_MANAGER = JWTManager(None, | ||
settings.ACCESS_PUBLIC_KEY, | ||
settings.ACCESS_ALGORITHM, | ||
None) | ||
|
||
@staticmethod | ||
def authenticate(encoded_jwt: str) -> (bool, dict, list[str] | None): | ||
""" returns: Success, payload, error message """ | ||
|
||
success, decoded_payload, error_decode = UserAccessJWTDecoder.JWT_MANAGER.decode_jwt(encoded_jwt) | ||
if not success: | ||
return False, None, error_decode | ||
|
||
user_id = decoded_payload.get('user_id') | ||
if user_id is None or user_id == '': | ||
return False, None, ['No user_id in payload'] | ||
|
||
return True, decoded_payload, None |
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,10 @@ | ||
ACCESS_PUBLIC_KEY = """-----BEGIN PUBLIC KEY----- | ||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl88VVar5X6lAlHjj4o4r | ||
r3WoAQloSNbxjgyUd6dU3z3a8JbLibihyl/LjrfAJXCT39FzBbjcWHw7dnDkBeU0 | ||
xX8pPNESkfJI7wxzkc1WcPk1KMwvy1dTaoCub7fZxNl2oOObdzTGpic8co7VOUqa | ||
5cJks3MTL/8ipxaf4HVJ4luvcySvPflL1woWO3QfTomL/B/Xnu9fmj2ynn8DptfY | ||
wJEe4eFA/jx+TP3coPBgs/XYG3stdyislm574U+5QvfRi1uii8jkFgpIxwUnxYbx | ||
mZW+X8IdGmaUnucNeF1pLZjEIcr7MkzP3zm1auQww71DObGTPaLLJNjTPdP3rWYJ | ||
mQIDAQAB | ||
-----END PUBLIC KEY-----""" | ||
ACCESS_ALGORITHM = 'RS256' |
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
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
Oops, something went wrong.