Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop to master #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
try:
import os
from flask import Flask
import flask_authgen_jwt
from dotenv import load_dotenv
except ImportError as e_imp:
print(f"The following import ERROR occurred in {__file__}: {e_imp}")

# Definir la clave secreta para firmar el token
if os.path.exists("secret.env"):
load_dotenv("secret.env")
else:
print("No secret.env file found")
raise SystemExit

app = Flask(__name__)
app.config['JSON_SORT_KEYS'] = False

# Configure Flask-Authgen-JWT to use in your Flask app
gen_auth = flask_authgen_jwt.GenJwt(json_body_token=True)
auth = flask_authgen_jwt.DecJwt()

from app import routes, error_handlers
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ gunicorn==20.1.0
pymongo[srv]==4.3.3
Flask-PyMongo==2.3.0
python-dotenv==1.0.0
Flask-Pydantic==0.11.0
Flask-Pydantic==0.11.0
Flask-authgen-jwt==4.1.2
11 changes: 2 additions & 9 deletions utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@
import os
import jwt
import json
from dotenv import load_dotenv
from datetime import datetime, timedelta
except ImportError as e_imp:
print(f"The following import ERROR occurred in {__file__}: {e_imp}")

# Definir la clave secreta para firmar el token
if os.path.exists("secret.env"):
load_dotenv("secret.env")
SECRET_KEY = os.getenv("SECRET_KEY")
else:
print("No secret.env file found")
raise SystemExit

def encode_token(username:str, password:str, days:int=1, minutes:int=0, **kwargs) -> str|None:
"""
Method to generate a token for a user with a given expiration time.
Expand All @@ -34,6 +25,7 @@ def encode_token(username:str, password:str, days:int=1, minutes:int=0, **kwargs
}
if kwargs:
payload_core |= kwargs # merge the two dictionaries
SECRET_KEY = os.getenv("SECRET_KEY")
return jwt.encode(payload_core, SECRET_KEY, algorithm=os.getenv("ALGORITHM"))
except Exception as e:
print(f"The following ERROR occurred in {__file__}: {e}")
Expand All @@ -48,6 +40,7 @@ def decode_token(token:str) -> dict[str,str]|None:
:return: The decoded token payload
"""
try:
SECRET_KEY = os.getenv("SECRET_KEY")
token = jwt.decode(token, SECRET_KEY, algorithms=[os.getenv("ALGORITHM")])
# decode iat and exp to human readable format in new keys
token["iat_readable"] = datetime.fromtimestamp(token["iat"]).strftime("%Y-%m-%d %H:%M:%S")
Expand Down
Loading