This commit is contained in:
Andy Pack 2022-11-27 23:36:50 +00:00
parent 2ba341ae7f
commit a23d797f90
Signed by: sarsoo
GPG Key ID: A55BA3536A5E0ED7
5 changed files with 12 additions and 7 deletions

View File

@ -14,6 +14,7 @@ spotframework_logger = logging.getLogger('spotframework')
fmframework_logger = logging.getLogger('fmframework') fmframework_logger = logging.getLogger('fmframework')
spotfm_logger = logging.getLogger('spotfm') spotfm_logger = logging.getLogger('spotfm')
def init_log(cloud=False, console=False): def init_log(cloud=False, console=False):
if cloud: if cloud:
import google.cloud.logging import google.cloud.logging
@ -44,5 +45,6 @@ def init_log(cloud=False, console=False):
fmframework_logger.addHandler(stream_handler) fmframework_logger.addHandler(stream_handler)
spotfm_logger.addHandler(stream_handler) spotfm_logger.addHandler(stream_handler)
if os.environ.get('DEPLOY_DESTINATION', None) == 'PROD': if os.environ.get('DEPLOY_DESTINATION', None) == 'PROD':
init_log(cloud=True) init_log(cloud=True)

View File

@ -78,6 +78,7 @@ def login_or_jwt(func):
return login_or_jwt_wrapper return login_or_jwt_wrapper
def jwt_required(func): def jwt_required(func):
@functools.wraps(func) @functools.wraps(func)
def jwt_required_wrapper(*args, **kwargs): def jwt_required_wrapper(*args, **kwargs):

View File

@ -113,9 +113,9 @@ def jwt_token():
else: else:
expiry = config.jwt_default_length expiry = config.jwt_default_length
token = generate_key(user, timeout=timedelta(seconds=expiry)) generated_token = generate_key(user, timeout=timedelta(seconds=expiry))
return jsonify({"token": token, "status": "success"}), 200 return jsonify({"token": generated_token, "status": "success"}), 200
else: else:
logger.warning(f'failed token attempt {username}') logger.warning(f'failed token attempt {username}')
return jsonify({"message": 'authentication failed', "status": "error"}), 401 return jsonify({"message": 'authentication failed', "status": "error"}), 401
@ -140,7 +140,7 @@ def register():
if username is None or password is None or password_again is None: if username is None or password is None or password_again is None:
if (request_json := request.get_json()) != None: if (request_json := request.get_json()) is not None:
username = request_json.get('username', None) username = request_json.get('username', None)
password = request_json.get('password', None) password = request_json.get('password', None)
password_again = request_json.get('password_again', None) password_again = request_json.get('password_again', None)

View File

@ -1,9 +1,9 @@
from ctypes import Union
from datetime import timedelta, datetime, timezone from datetime import timedelta, datetime, timezone
import jwt import jwt
from music.model.user import User from music.model.user import User
from music.model.config import Config from music.model.config import Config
def get_jwt_secret_key() -> str: def get_jwt_secret_key() -> str:
config = Config.collection.get("config/music-tools") config = Config.collection.get("config/music-tools")
@ -13,6 +13,7 @@ def get_jwt_secret_key() -> str:
return config.jwt_secret_key return config.jwt_secret_key
def generate_key(user: User, timeout: datetime | timedelta = timedelta(minutes=60)) -> str: def generate_key(user: User, timeout: datetime | timedelta = timedelta(minutes=60)) -> str:
if isinstance(timeout, timedelta): if isinstance(timeout, timedelta):
@ -28,6 +29,7 @@ def generate_key(user: User, timeout: datetime | timedelta = timedelta(minutes=6
return jwt.encode(payload, get_jwt_secret_key(), algorithm="HS512") return jwt.encode(payload, get_jwt_secret_key(), algorithm="HS512")
def validate_key(key: str) -> dict: def validate_key(key: str) -> dict:
try: try:
@ -37,5 +39,5 @@ def validate_key(key: str) -> dict:
return decoded return decoded
except Exception as e: except jwt.exceptions.PyJWTError as e:
pass pass

View File

@ -1,5 +1,5 @@
from fireo.models import Model from fireo.models import Model
from fireo.fields import TextField, BooleanField, DateTime, NumberField, ListField from fireo.fields import TextField, NumberField
class Config(Model): class Config(Model):