From 43b6965ae152f1522dba061109dcf24c66c19553 Mon Sep 17 00:00:00 2001 From: ansengarvin <45224464+ansengarvin@users.noreply.github.com> Date: Thu, 25 Jan 2024 22:07:58 -0800 Subject: [PATCH 01/11] Beginning to add authentication stuff for login. Code is broken right now. --- backend/src/api/users.py | 39 +++++++++++++++++++++++++++++++++++++++ backend/src/api_types.py | 7 +++++++ backend/src/auth.py | 14 ++++++++++++++ backend/src/server.py | 6 +++++- 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 backend/src/api/users.py create mode 100644 backend/src/auth.py diff --git a/backend/src/api/users.py b/backend/src/api/users.py new file mode 100644 index 00000000..9b133d20 --- /dev/null +++ b/backend/src/api/users.py @@ -0,0 +1,39 @@ +from fastapi import APIRouter +import logging as log +from api_types import LoginBody, UploadError +from db import Database, bytea_to_str, str_to_bytea + +router = APIRouter() + +def validateUser(email, password): + #TODO: Implement validate user + pass + +@router.get("/users/login", response_model=UploadError) +def login(body: LoginBody): + with Database() as db: + try: + authenticated = validateUser( + body.email, + body.password + ) + if authenticated: + query = """SELECT users.admin WHERE users.email = %s""" + entry_sql = db.execute_return(query, [body.email]) + log.warn(entry_sql) + + # Proceed only if we got a result back + if entry_sql is not None and len(entry_sql) != 0: + #return the only entry + only_returned_entry = entry_sql[0] + admin = only_returned_entry + + # TODO: Generate an authentication token + token = 1 + + user = 0 + # Grab user from DB + admin = 0 if + + except Exception as e: + log.error(e) \ No newline at end of file diff --git a/backend/src/api_types.py b/backend/src/api_types.py index 69152eac..d5f5e2d1 100644 --- a/backend/src/api_types.py +++ b/backend/src/api_types.py @@ -62,3 +62,10 @@ class EditBody(CamelModel): new_species_name: str new_content: str | None = None new_refs: str | None = None + +class LoginBody(CamelModel): + email: str + password: str + +class ResponseToken(CamelModel): + token: str \ No newline at end of file diff --git a/backend/src/auth.py b/backend/src/auth.py new file mode 100644 index 00000000..1da88da2 --- /dev/null +++ b/backend/src/auth.py @@ -0,0 +1,14 @@ +import jwt +import datetime + +#TODO: This method of secret key generation is, obviously, extremely unsafe. +# This needs to be changed. +secretKey = "SuperSecret" + +def generateAuthToken(userId, admin): + payload = { + "email": userId, + "admin": admin, + "exp": datetime.datetime.now(tz=timezone.utc) + datetime.timedelta(seconds=30) + } + return \ No newline at end of file diff --git a/backend/src/server.py b/backend/src/server.py index 953c4676..a3a88971 100644 --- a/backend/src/server.py +++ b/backend/src/server.py @@ -6,11 +6,15 @@ from .db import Database, bytea_to_str, str_to_bytea from .protein import parse_protein_pdb, pdb_file_name, protein_name_found, pdb_to_fasta from .setup import disable_cors, init_fastapi_app +from fastapi import APIRouter app = init_fastapi_app() disable_cors(app, origins=[os.environ["PUBLIC_FRONTEND_URL"]]) +# Allow routing + + @app.get("/pdb/{protein_name:str}") def get_pdb_file(protein_name: str): @@ -267,7 +271,7 @@ def get_all_species(): return [d[0] for d in entry_sql] except Exception: return - + def export_app_for_docker(): """Needed for the [docker-compose.yml](../../docker-compose.yml) From 3bef761222111289abbc2840cf76bebdaa7bf297 Mon Sep 17 00:00:00 2001 From: ansengarvin <45224464+ansengarvin@users.noreply.github.com> Date: Thu, 25 Jan 2024 22:19:13 -0800 Subject: [PATCH 02/11] Authentication token generation seems to be functional, based on a quick online test. Going to see about decoding. --- backend/src/auth.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/backend/src/auth.py b/backend/src/auth.py index 1da88da2..b525c6bb 100644 --- a/backend/src/auth.py +++ b/backend/src/auth.py @@ -1,14 +1,20 @@ import jwt -import datetime +from datetime import datetime, timezone, timedelta #TODO: This method of secret key generation is, obviously, extremely unsafe. # This needs to be changed. -secretKey = "SuperSecret" +secret_key = "SuperSecret" def generateAuthToken(userId, admin): payload = { "email": userId, "admin": admin, - "exp": datetime.datetime.now(tz=timezone.utc) + datetime.timedelta(seconds=30) + "exp": datetime.now(tz=timezone.utc) + timedelta(seconds=30) } - return \ No newline at end of file + return jwt.encode(payload, secret_key, algorithm="HS256") + +if __name__ == "__main__": + userId = "ansengarvin@gmail.com" + admin = True + token = generateAuthToken(userId, admin) + print(token) \ No newline at end of file From 805916fc309cfc030a7a6679097958b23ce14743 Mon Sep 17 00:00:00 2001 From: ansengarvin <45224464+ansengarvin@users.noreply.github.com> Date: Thu, 25 Jan 2024 22:35:20 -0800 Subject: [PATCH 03/11] Changed expiration on token to 24 hours --- backend/src/auth.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/backend/src/auth.py b/backend/src/auth.py index b525c6bb..f5609ca6 100644 --- a/backend/src/auth.py +++ b/backend/src/auth.py @@ -9,12 +9,6 @@ def generateAuthToken(userId, admin): payload = { "email": userId, "admin": admin, - "exp": datetime.now(tz=timezone.utc) + timedelta(seconds=30) + "exp": datetime.now(tz=timezone.utc) + timedelta(hours=24) } - return jwt.encode(payload, secret_key, algorithm="HS256") - -if __name__ == "__main__": - userId = "ansengarvin@gmail.com" - admin = True - token = generateAuthToken(userId, admin) - print(token) \ No newline at end of file + return jwt.encode(payload, secret_key, algorithm="HS256") \ No newline at end of file From a59aac0208a0ca4f2fbb7f3804f616c192171b0b Mon Sep 17 00:00:00 2001 From: ansengarvin <45224464+ansengarvin@users.noreply.github.com> Date: Thu, 25 Jan 2024 22:45:07 -0800 Subject: [PATCH 04/11] Added ability to authenticate tokens. Will be useful for locking endpoints. --- backend/src/auth.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/backend/src/auth.py b/backend/src/auth.py index f5609ca6..177a74c6 100644 --- a/backend/src/auth.py +++ b/backend/src/auth.py @@ -11,4 +11,14 @@ def generateAuthToken(userId, admin): "admin": admin, "exp": datetime.now(tz=timezone.utc) + timedelta(hours=24) } - return jwt.encode(payload, secret_key, algorithm="HS256") \ No newline at end of file + return jwt.encode(payload, secret_key, algorithm="HS256") + +def authenticateToken(token): + # Return the decoded token if it's valid. + try: + decoded = jwt.decode(token, secret_key, algorithms="HS256") + return decoded + + # If the token is invalid, return None. + except Exception: + return None \ No newline at end of file From 3b6f7bbd59830393e764e3b67897f8de3e6c983e Mon Sep 17 00:00:00 2001 From: ansengarvin <45224464+ansengarvin@users.noreply.github.com> Date: Fri, 26 Jan 2024 00:59:45 -0800 Subject: [PATCH 05/11] User login route theoretically working. Need to test now. --- backend/init.sql | 2 +- backend/poetry.lock | 38 ++++++++++++++++++++++++++++-- backend/pyproject.toml | 2 ++ backend/src/api/users.py | 50 ++++++++++++++++++---------------------- backend/src/server.py | 7 +++--- 5 files changed, 65 insertions(+), 34 deletions(-) diff --git a/backend/init.sql b/backend/init.sql index ae26fadf..c4a0b26c 100644 --- a/backend/init.sql +++ b/backend/init.sql @@ -68,4 +68,4 @@ INSERT INTO species(name) VALUES ('unknown'); /* * Inserts test user into user table */ -INSERT INTO users(username, email, pword, admin) VALUES ('test', 'garvina@oregonstate.edu', 'password', '1'); \ No newline at end of file +INSERT INTO users(username, email, pword, admin) VALUES ('test', 'garvina@oregonstate.edu', '', '1'); \ No newline at end of file diff --git a/backend/poetry.lock b/backend/poetry.lock index 42cdcd50..b641bb52 100644 --- a/backend/poetry.lock +++ b/backend/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.7.0 and should not be changed by hand. [[package]] name = "annotated-types" @@ -216,6 +216,23 @@ files = [ {file = "packaging-23.2.tar.gz", hash = "sha256:048fb0e9405036518eaaf48a55953c750c11e1a1b68e0dd1a9d62ed0c092cfc5"}, ] +[[package]] +name = "passlib" +version = "1.7.4" +description = "comprehensive password hashing framework supporting over 30 schemes" +optional = false +python-versions = "*" +files = [ + {file = "passlib-1.7.4-py2.py3-none-any.whl", hash = "sha256:aa6bca462b8d8bda89c70b382f0c298a20b5560af6cbfa2dce410c0a2fb669f1"}, + {file = "passlib-1.7.4.tar.gz", hash = "sha256:defd50f72b65c5402ab2c573830a6978e5f202ad0d984793c8dde2c4152ebe04"}, +] + +[package.extras] +argon2 = ["argon2-cffi (>=18.2.0)"] +bcrypt = ["bcrypt (>=3.1.0)"] +build-docs = ["cloud-sptheme (>=1.10.1)", "sphinx (>=1.6)", "sphinxcontrib-fulltoc (>=1.2.0)"] +totp = ["cryptography"] + [[package]] name = "pluggy" version = "1.3.0" @@ -405,6 +422,23 @@ files = [ [package.dependencies] typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0" +[[package]] +name = "pyjwt" +version = "2.8.0" +description = "JSON Web Token implementation in Python" +optional = false +python-versions = ">=3.7" +files = [ + {file = "PyJWT-2.8.0-py3-none-any.whl", hash = "sha256:59127c392cc44c2da5bb3192169a91f429924e17aff6534d70fdc02ab3e04320"}, + {file = "PyJWT-2.8.0.tar.gz", hash = "sha256:57e28d156e3d5c10088e0c68abb90bfac3df82b40a71bd0daa20c65ccd5c23de"}, +] + +[package.extras] +crypto = ["cryptography (>=3.4.0)"] +dev = ["coverage[toml] (==5.0.4)", "cryptography (>=3.4.0)", "pre-commit", "pytest (>=6.0.0,<7.0.0)", "sphinx (>=4.5.0,<5.0.0)", "sphinx-rtd-theme", "zope.interface"] +docs = ["sphinx (>=4.5.0,<5.0.0)", "sphinx-rtd-theme", "zope.interface"] +tests = ["coverage[toml] (==5.0.4)", "pytest (>=6.0.0,<7.0.0)"] + [[package]] name = "pytest" version = "7.4.3" @@ -511,4 +545,4 @@ standard = ["colorama (>=0.4)", "httptools (>=0.5.0)", "python-dotenv (>=0.13)", [metadata] lock-version = "2.0" python-versions = ">=3.9,<3.13" -content-hash = "5205b8a79d683ad48c7f3d8792b12ae0229e567d835e8bdf198d39f2cf764ef8" +content-hash = "31741f9165cc8c8ac8edd53ac269cf5fd49014a12d078ae86c171cfd4f8e7219" diff --git a/backend/pyproject.toml b/backend/pyproject.toml index b5ec2b65..a02ee58d 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -13,6 +13,8 @@ fastapi = "^0.104.0" psycopg = "^3.1.12" psycopg-pool = "^3.1.8" biopython = "^1.81" +PyJWT = "^2.8.0" +passlib = "^1.7.4" [tool.poetry.group.dev.dependencies] diff --git a/backend/src/api/users.py b/backend/src/api/users.py index 9b133d20..38e18923 100644 --- a/backend/src/api/users.py +++ b/backend/src/api/users.py @@ -1,39 +1,35 @@ from fastapi import APIRouter import logging as log -from api_types import LoginBody, UploadError -from db import Database, bytea_to_str, str_to_bytea +from passlib.hash import bcrypt +from ..api_types import LoginBody, UploadError, ResponseToken +from ..db import Database, bytea_to_str, str_to_bytea +from ..auth import authenticateToken, generateAuthToken router = APIRouter() -def validateUser(email, password): - #TODO: Implement validate user - pass - -@router.get("/users/login", response_model=UploadError) +#TODO: Change response model? +@router.post("/users/login", response_model=UploadError) def login(body: LoginBody): with Database() as db: try: - authenticated = validateUser( - body.email, - body.password - ) - if authenticated: - query = """SELECT users.admin WHERE users.email = %s""" - entry_sql = db.execute_return(query, [body.email]) - log.warn(entry_sql) + email = body.email + password = body.password - # Proceed only if we got a result back - if entry_sql is not None and len(entry_sql) != 0: - #return the only entry - only_returned_entry = entry_sql[0] - admin = only_returned_entry + query = """SELECT users.password, users.admin WHERE users.email = %s;""" + entry_sql = db.execute_return(query, [email]) - # TODO: Generate an authentication token - token = 1 - - user = 0 - # Grab user from DB - admin = 0 if + password_hash, admin = entry_sql + # If the password is not correct, return something else. + if not bcrypt.verify(password, hash): + # TODO: Return something better than query error + return UploadError.QUERY_ERROR + + # Generates the token and returns + token = generateAuthToken(email, password) + return ResponseToken(token=token) + except Exception as e: - log.error(e) \ No newline at end of file + log.error(e) + # TODO: Return something better than query error + return UploadError.QUERY_ERROR \ No newline at end of file diff --git a/backend/src/server.py b/backend/src/server.py index a3a88971..2c4badb2 100644 --- a/backend/src/server.py +++ b/backend/src/server.py @@ -6,14 +6,13 @@ from .db import Database, bytea_to_str, str_to_bytea from .protein import parse_protein_pdb, pdb_file_name, protein_name_found, pdb_to_fasta from .setup import disable_cors, init_fastapi_app -from fastapi import APIRouter +from .api import users app = init_fastapi_app() disable_cors(app, origins=[os.environ["PUBLIC_FRONTEND_URL"]]) -# Allow routing - +app.include_router(users.router) @app.get("/pdb/{protein_name:str}") @@ -271,7 +270,7 @@ def get_all_species(): return [d[0] for d in entry_sql] except Exception: return - + def export_app_for_docker(): """Needed for the [docker-compose.yml](../../docker-compose.yml) From 4c0ef30eccd10427fd08fa3c89b7dfb7347e6ec7 Mon Sep 17 00:00:00 2001 From: ansengarvin <45224464+ansengarvin@users.noreply.github.com> Date: Fri, 26 Jan 2024 01:35:31 -0800 Subject: [PATCH 06/11] Login endpoint appears functional --- backend/init.sql | 2 +- backend/src/api/users.py | 23 ++++++++++++++--------- backend/src/api_types.py | 6 ++++++ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/backend/init.sql b/backend/init.sql index c4a0b26c..afb7be22 100644 --- a/backend/init.sql +++ b/backend/init.sql @@ -68,4 +68,4 @@ INSERT INTO species(name) VALUES ('unknown'); /* * Inserts test user into user table */ -INSERT INTO users(username, email, pword, admin) VALUES ('test', 'garvina@oregonstate.edu', '', '1'); \ No newline at end of file +INSERT INTO users(username, email, pword, admin) VALUES ('test', 'garvina@oregonstate.edu', '$2b$12$2Z74k3vqzchWB..McZbdUOp4BXd6RGsWcS0atZJfVVheGexvH7i0O', '1'); \ No newline at end of file diff --git a/backend/src/api/users.py b/backend/src/api/users.py index 38e18923..818ac0ff 100644 --- a/backend/src/api/users.py +++ b/backend/src/api/users.py @@ -1,35 +1,40 @@ from fastapi import APIRouter import logging as log from passlib.hash import bcrypt -from ..api_types import LoginBody, UploadError, ResponseToken +from ..api_types import LoginBody, LoginError, ResponseToken from ..db import Database, bytea_to_str, str_to_bytea from ..auth import authenticateToken, generateAuthToken router = APIRouter() #TODO: Change response model? -@router.post("/users/login", response_model=UploadError) +@router.post("/users/login", response_model=ResponseToken | LoginError) def login(body: LoginBody): with Database() as db: try: email = body.email password = body.password - query = """SELECT users.password, users.admin WHERE users.email = %s;""" + query = """SELECT users.pword, users.admin FROM users WHERE users.email = %s;""" entry_sql = db.execute_return(query, [email]) - password_hash, admin = entry_sql + + if entry_sql is None or len(entry_sql) == 0: + # TODO: Once we're done testing this, change this from DEBUG_ACCOUNT to INCORRECT + return LoginError.DEBUG_ACCOUNT + + password_hash, admin = entry_sql[0] # If the password is not correct, return something else. - if not bcrypt.verify(password, hash): - # TODO: Return something better than query error - return UploadError.QUERY_ERROR + if not bcrypt.verify(password, password_hash): + # TODO: Once we're done testing this, change this from DEBUG_PASSWORD to INCORRECT + return LoginError.DEBUG_PASSWORD # Generates the token and returns - token = generateAuthToken(email, password) + token = generateAuthToken(email, admin) return ResponseToken(token=token) except Exception as e: log.error(e) # TODO: Return something better than query error - return UploadError.QUERY_ERROR \ No newline at end of file + return LoginError.QUERY_ERROR \ No newline at end of file diff --git a/backend/src/api_types.py b/backend/src/api_types.py index d5f5e2d1..82d1f681 100644 --- a/backend/src/api_types.py +++ b/backend/src/api_types.py @@ -67,5 +67,11 @@ class LoginBody(CamelModel): email: str password: str +class LoginError(str, enum.Enum): + DEBUG_ACCOUNT = "Debug: Account Not Found" + DEBUG_PASSWORD = "Debug: Incorrect password" + INCORRECT = "Invalid Email or Password" + QUERY_ERROR = "QUERY_ERROR" + class ResponseToken(CamelModel): token: str \ No newline at end of file From 8fbe30a085bc8480e3c40ad6042cf4ed160878f3 Mon Sep 17 00:00:00 2001 From: ansengarvin <45224464+ansengarvin@users.noreply.github.com> Date: Fri, 26 Jan 2024 01:46:01 -0800 Subject: [PATCH 07/11] Cleaned up users API code --- backend/src/api/users.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/backend/src/api/users.py b/backend/src/api/users.py index 818ac0ff..db9b618c 100644 --- a/backend/src/api/users.py +++ b/backend/src/api/users.py @@ -7,7 +7,6 @@ router = APIRouter() -#TODO: Change response model? @router.post("/users/login", response_model=ResponseToken | LoginError) def login(body: LoginBody): with Database() as db: @@ -18,17 +17,16 @@ def login(body: LoginBody): query = """SELECT users.pword, users.admin FROM users WHERE users.email = %s;""" entry_sql = db.execute_return(query, [email]) - + # Returns "incorrect email/password" message if there is no such account. if entry_sql is None or len(entry_sql) == 0: - # TODO: Once we're done testing this, change this from DEBUG_ACCOUNT to INCORRECT - return LoginError.DEBUG_ACCOUNT + return LoginError.INCORRECT + # Grabs the stored hash and admin. password_hash, admin = entry_sql[0] - # If the password is not correct, return something else. + # Returns "incorrect email/password" message if password is incorrect. if not bcrypt.verify(password, password_hash): - # TODO: Once we're done testing this, change this from DEBUG_PASSWORD to INCORRECT - return LoginError.DEBUG_PASSWORD + return LoginError.INCORRECT # Generates the token and returns token = generateAuthToken(email, admin) From 9b6403a6435cf8b3412577fe10cf032f10f461ee Mon Sep 17 00:00:00 2001 From: ansengarvin <45224464+ansengarvin@users.noreply.github.com> Date: Fri, 26 Jan 2024 01:47:35 -0800 Subject: [PATCH 08/11] Added TODO reminder for authentication --- backend/src/auth.py | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/src/auth.py b/backend/src/auth.py index 177a74c6..b793d9c6 100644 --- a/backend/src/auth.py +++ b/backend/src/auth.py @@ -13,6 +13,7 @@ def generateAuthToken(userId, admin): } return jwt.encode(payload, secret_key, algorithm="HS256") +#TODO: Find out how FastAPI handles middleware functions, and turn this into one. def authenticateToken(token): # Return the decoded token if it's valid. try: From 2fe0eb3fa7e66819b88dbecb29c70ac69a523f36 Mon Sep 17 00:00:00 2001 From: ansengarvin <45224464+ansengarvin@users.noreply.github.com> Date: Fri, 26 Jan 2024 02:33:28 -0800 Subject: [PATCH 09/11] Removed unused imports to appease ruff. --- backend/src/api/users.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/api/users.py b/backend/src/api/users.py index db9b618c..d3a0cc18 100644 --- a/backend/src/api/users.py +++ b/backend/src/api/users.py @@ -2,8 +2,8 @@ import logging as log from passlib.hash import bcrypt from ..api_types import LoginBody, LoginError, ResponseToken -from ..db import Database, bytea_to_str, str_to_bytea -from ..auth import authenticateToken, generateAuthToken +from ..db import Database +from ..auth import generateAuthToken router = APIRouter() From fd05ef8ca145fb519df5f418c84ac7562f382331 Mon Sep 17 00:00:00 2001 From: ansengarvin <45224464+ansengarvin@users.noreply.github.com> Date: Fri, 26 Jan 2024 02:49:57 -0800 Subject: [PATCH 10/11] Ran Ruff linter to make code look better --- backend/src/api/users.py | 15 +++++++++------ backend/src/api_types.py | 5 ++++- backend/src/auth.py | 12 +++++++----- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/backend/src/api/users.py b/backend/src/api/users.py index d3a0cc18..f40c0f37 100644 --- a/backend/src/api/users.py +++ b/backend/src/api/users.py @@ -1,12 +1,13 @@ from fastapi import APIRouter import logging as log from passlib.hash import bcrypt -from ..api_types import LoginBody, LoginError, ResponseToken +from ..api_types import LoginBody, LoginError, ResponseToken from ..db import Database from ..auth import generateAuthToken router = APIRouter() + @router.post("/users/login", response_model=ResponseToken | LoginError) def login(body: LoginBody): with Database() as db: @@ -14,25 +15,27 @@ def login(body: LoginBody): email = body.email password = body.password - query = """SELECT users.pword, users.admin FROM users WHERE users.email = %s;""" + query = ( + """SELECT users.pword, users.admin FROM users WHERE users.email = %s;""" + ) entry_sql = db.execute_return(query, [email]) # Returns "incorrect email/password" message if there is no such account. if entry_sql is None or len(entry_sql) == 0: return LoginError.INCORRECT - + # Grabs the stored hash and admin. password_hash, admin = entry_sql[0] # Returns "incorrect email/password" message if password is incorrect. if not bcrypt.verify(password, password_hash): return LoginError.INCORRECT - + # Generates the token and returns token = generateAuthToken(email, admin) return ResponseToken(token=token) - + except Exception as e: log.error(e) # TODO: Return something better than query error - return LoginError.QUERY_ERROR \ No newline at end of file + return LoginError.QUERY_ERROR diff --git a/backend/src/api_types.py b/backend/src/api_types.py index 82d1f681..ae9db3ac 100644 --- a/backend/src/api_types.py +++ b/backend/src/api_types.py @@ -63,15 +63,18 @@ class EditBody(CamelModel): new_content: str | None = None new_refs: str | None = None + class LoginBody(CamelModel): email: str password: str + class LoginError(str, enum.Enum): DEBUG_ACCOUNT = "Debug: Account Not Found" DEBUG_PASSWORD = "Debug: Incorrect password" INCORRECT = "Invalid Email or Password" QUERY_ERROR = "QUERY_ERROR" + class ResponseToken(CamelModel): - token: str \ No newline at end of file + token: str diff --git a/backend/src/auth.py b/backend/src/auth.py index b793d9c6..f1fabb72 100644 --- a/backend/src/auth.py +++ b/backend/src/auth.py @@ -1,25 +1,27 @@ import jwt from datetime import datetime, timezone, timedelta -#TODO: This method of secret key generation is, obviously, extremely unsafe. +# TODO: This method of secret key generation is, obviously, extremely unsafe. # This needs to be changed. secret_key = "SuperSecret" + def generateAuthToken(userId, admin): payload = { "email": userId, "admin": admin, - "exp": datetime.now(tz=timezone.utc) + timedelta(hours=24) + "exp": datetime.now(tz=timezone.utc) + timedelta(hours=24), } return jwt.encode(payload, secret_key, algorithm="HS256") -#TODO: Find out how FastAPI handles middleware functions, and turn this into one. + +# TODO: Find out how FastAPI handles middleware functions, and turn this into one. def authenticateToken(token): # Return the decoded token if it's valid. try: decoded = jwt.decode(token, secret_key, algorithms="HS256") return decoded - + # If the token is invalid, return None. except Exception: - return None \ No newline at end of file + return None From 8c6ddce83a0aec97c31d136ceef8e83717a80b4f Mon Sep 17 00:00:00 2001 From: ansengarvin <45224464+ansengarvin@users.noreply.github.com> Date: Fri, 26 Jan 2024 02:52:27 -0800 Subject: [PATCH 11/11] Ran ruff linter again --- backend/src/api/users.py | 2 +- backend/src/auth.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/api/users.py b/backend/src/api/users.py index f40c0f37..bf6639fc 100644 --- a/backend/src/api/users.py +++ b/backend/src/api/users.py @@ -1,4 +1,4 @@ -from fastapi import APIRouter +from fastapi import APIRouter import logging as log from passlib.hash import bcrypt from ..api_types import LoginBody, LoginError, ResponseToken diff --git a/backend/src/auth.py b/backend/src/auth.py index f1fabb72..2a1437cd 100644 --- a/backend/src/auth.py +++ b/backend/src/auth.py @@ -1,4 +1,4 @@ -import jwt +import jwt from datetime import datetime, timezone, timedelta # TODO: This method of secret key generation is, obviously, extremely unsafe.