diff options
| author | Frederick Muriuki Muriithi | 2026-08-27 10:51:16 -0500 |
|---|---|---|
| committer | Frederick Muriuki Muriithi | 2026-08-27 12:51:31 -0500 |
| commit | 800913ddee45fcb0b0e3a9d8c3917d3cf9644ca4 (patch) | |
| tree | 30b78dca5002899cce2d2f875bffe70a74933c87 | |
| parent | c9d6594750ad7a01b1e23af77e4f847e23f26306 (diff) | |
| download | gn-auth-800913ddee45fcb0b0e3a9d8c3917d3cf9644ca4.tar.gz | |
Cleanup deprecation warnings: Switch to gn_libs.sqlite3
The gn_auth.auth.db.sqlite3 module is deprecated and should be removed. This cleanup goes a ways towards that goal.
18 files changed, 38 insertions, 34 deletions
diff --git a/gn_auth/auth/authentication/oauth2/endpoints/revocation.py b/gn_auth/auth/authentication/oauth2/endpoints/revocation.py index 0979694..e647214 100644 --- a/gn_auth/auth/authentication/oauth2/endpoints/revocation.py +++ b/gn_auth/auth/authentication/oauth2/endpoints/revocation.py @@ -1,9 +1,9 @@ """Handle token revocation.""" from flask import current_app +from gn_libs import sqlite3 as db from authlib.oauth2.rfc7009 import RevocationEndpoint as _RevocationEndpoint -from gn_auth.auth.db import sqlite3 as db from gn_auth.auth.authentication.oauth2.models.oauth2token import ( save_token, OAuth2Token, revoke_token) diff --git a/gn_auth/auth/authentication/oauth2/endpoints/utilities.py b/gn_auth/auth/authentication/oauth2/endpoints/utilities.py index 490c141..82fd8e1 100644 --- a/gn_auth/auth/authentication/oauth2/endpoints/utilities.py +++ b/gn_auth/auth/authentication/oauth2/endpoints/utilities.py @@ -3,8 +3,8 @@ from typing import Optional from flask import current_app from pymonad.maybe import Nothing +from gn_libs import sqlite3 as db -from gn_auth.auth.db import sqlite3 as db from gn_auth.auth.authentication.oauth2.models.oauth2token import ( OAuth2Token, token_by_access_token, token_by_refresh_token) diff --git a/gn_auth/auth/authentication/oauth2/models/jwt_bearer_token.py b/gn_auth/auth/authentication/oauth2/models/jwt_bearer_token.py index 71769e1..3a93265 100644 --- a/gn_auth/auth/authentication/oauth2/models/jwt_bearer_token.py +++ b/gn_auth/auth/authentication/oauth2/models/jwt_bearer_token.py @@ -3,9 +3,10 @@ import uuid import time from typing import Optional +from flask import current_app as app from authlib.oauth2.rfc7523 import JWTBearerToken as _JWTBearerToken +from gn_libs.sqlite3 import with_db_connection -from gn_auth.auth.db.sqlite3 import with_db_connection from gn_auth.auth.authentication.users import user_by_id from gn_auth.auth.authentication.oauth2.models.oauth2client import ( client as fetch_client) @@ -19,8 +20,10 @@ class JWTBearerToken(_JWTBearerToken): # OAuth2Client is a dataclass super().__init__(payload, header, options, params) self.user = with_db_connection( + app.config["SQL_URI"], lambda conn:user_by_id(conn, uuid.UUID(payload["sub"]))) self.client = with_db_connection( + app.config["SQL_URI"], lambda conn: fetch_client( conn, uuid.UUID(payload["oauth2_client_id"]) ) diff --git a/gn_auth/auth/authentication/oauth2/models/oauth2client.py b/gn_auth/auth/authentication/oauth2/models/oauth2client.py index b3ec91f..818399d 100644 --- a/gn_auth/auth/authentication/oauth2/models/oauth2client.py +++ b/gn_auth/auth/authentication/oauth2/models/oauth2client.py @@ -13,9 +13,9 @@ from requests.exceptions import JSONDecodeError from authlib.jose import KeySet, JsonWebKey from authlib.oauth2.rfc6749 import ClientMixin from pymonad.maybe import Just, Maybe, Nothing +from gn_libs import sqlite3 as db from gn_libs.debug import make_peeker -from gn_auth.auth.db import sqlite3 as db from gn_auth.auth.errors import NotFoundError from gn_auth.auth.authentication.users import (User, fetch_users, diff --git a/gn_auth/auth/authentication/oauth2/models/oauth2token.py b/gn_auth/auth/authentication/oauth2/models/oauth2token.py index 6ec5c3d..eb13f43 100644 --- a/gn_auth/auth/authentication/oauth2/models/oauth2token.py +++ b/gn_auth/auth/authentication/oauth2/models/oauth2token.py @@ -8,8 +8,8 @@ from typing import Optional from authlib.oauth2.rfc6749 import TokenMixin from pymonad.tools import monad_from_none_or_value from pymonad.maybe import Just, Maybe, Nothing +from gn_libs import sqlite3 as db -from gn_auth.auth.db import sqlite3 as db from gn_auth.auth.errors import NotFoundError from gn_auth.auth.authentication.users import User, user_by_id diff --git a/gn_auth/auth/authentication/oauth2/resource_server.py b/gn_auth/auth/authentication/oauth2/resource_server.py index edab02c..c09f6b6 100644 --- a/gn_auth/auth/authentication/oauth2/resource_server.py +++ b/gn_auth/auth/authentication/oauth2/resource_server.py @@ -9,8 +9,8 @@ from authlib.oauth2.rfc6750 import BearerTokenValidator as _BearerTokenValidator from authlib.oauth2.rfc7523 import ( JWTBearerTokenValidator as _JWTBearerTokenValidator) from authlib.integrations.flask_oauth2 import ResourceProtector +from gn_libs import sqlite3 as db -from gn_auth.auth.db import sqlite3 as db from gn_auth.auth.jwks import list_jwks, jwks_directory from gn_auth.auth.authentication.oauth2.models.jwt_bearer_token import ( JWTBearerToken) diff --git a/gn_auth/auth/authentication/oauth2/views.py b/gn_auth/auth/authentication/oauth2/views.py index 8cc123f..e26fc95 100644 --- a/gn_auth/auth/authentication/oauth2/views.py +++ b/gn_auth/auth/authentication/oauth2/views.py @@ -16,9 +16,9 @@ from flask import ( Blueprint, render_template, current_app as app) +from gn_libs import sqlite3 as db +from gn_libs.sqlite3 import with_db_connection -from gn_auth.auth.db import sqlite3 as db -from gn_auth.auth.db.sqlite3 import with_db_connection from gn_auth.auth.jwks import jwks_directory, list_jwks from gn_auth.auth.errors import NotFoundError, ForbiddenAccess from gn_auth.auth.authentication.users import valid_login, user_by_email @@ -68,6 +68,7 @@ def authorise(): redirect_uri=request.args["redirect_uri"], source_uri=f"{_src.scheme}://{_src.netloc}/", display_forgot_password=with_db_connection( + app.config["SQL_URI"], __forgot_password_table_exists__)) form = request.form @@ -102,7 +103,7 @@ def authorise(): flash(email_passwd_msg, "alert alert-danger") return redirect_response # type: ignore[return-value] - return with_db_connection(__authorise__) + return with_db_connection(app.config["SQL_URI"], __authorise__) except InvalidClientError as ice: return render_template( "oauth2/oauth2_error.html", error=ice), ice.status_code diff --git a/gn_auth/auth/authentication/users.py b/gn_auth/auth/authentication/users.py index fded79f..5c6a430 100644 --- a/gn_auth/auth/authentication/users.py +++ b/gn_auth/auth/authentication/users.py @@ -7,8 +7,8 @@ from dataclasses import dataclass import sqlite3 from argon2 import PasswordHasher from argon2.exceptions import VerifyMismatchError +from gn_libs import sqlite3 as db -from gn_auth.auth.db import sqlite3 as db from gn_auth.auth.errors import NotFoundError diff --git a/gn_auth/auth/authorisation/checks.py b/gn_auth/auth/authorisation/checks.py index 66bb723..15d4b99 100644 --- a/gn_auth/auth/authorisation/checks.py +++ b/gn_auth/auth/authorisation/checks.py @@ -2,12 +2,12 @@ from functools import wraps from typing import Callable +from gn_libs import sqlite3 as db from flask import request, current_app as app from gn_auth.auth.errors import InvalidData, AuthorisationError from . import privileges as auth_privs -from ..db import sqlite3 as db from ..authentication.oauth2.resource_server import require_oauth def __system_privileges_in_roles__(conn, user): # TODO: Remove this hack. diff --git a/gn_auth/auth/authorisation/data/genotypes.py b/gn_auth/auth/authorisation/data/genotypes.py index b6f0464..d5af3ae 100644 --- a/gn_auth/auth/authorisation/data/genotypes.py +++ b/gn_auth/auth/authorisation/data/genotypes.py @@ -6,10 +6,9 @@ from functools import reduce from dataclasses import asdict from gn_libs import mysqldb as gn3db +from gn_libs import sqlite3 as authdb from MySQLdb.cursors import DictCursor -from gn_auth.auth.db import sqlite3 as authdb - from gn_auth.auth.authorisation.checks import authorised_p from gn_auth.auth.authorisation.resources.groups.models import Group diff --git a/gn_auth/auth/authorisation/data/mrna.py b/gn_auth/auth/authorisation/data/mrna.py index 7cced10..dd589e0 100644 --- a/gn_auth/auth/authorisation/data/mrna.py +++ b/gn_auth/auth/authorisation/data/mrna.py @@ -6,10 +6,9 @@ from functools import reduce from dataclasses import asdict from gn_libs import mysqldb as gn3db +from gn_libs import sqlite3 as authdb from MySQLdb.cursors import DictCursor -from gn_auth.auth.db import sqlite3 as authdb - from gn_auth.auth.authorisation.checks import authorised_p from gn_auth.auth.authorisation.resources.groups.models import Group diff --git a/gn_auth/auth/authorisation/data/views.py b/gn_auth/auth/authorisation/data/views.py index 1184d63..0ffc08e 100644 --- a/gn_auth/auth/authorisation/data/views.py +++ b/gn_auth/auth/authorisation/data/views.py @@ -14,6 +14,7 @@ from flask import request, jsonify, Response, Blueprint, current_app as app from gn_libs import mysqldb as gn3db from gn_libs import sqlite3 as db +from gn_libs.sqlite3 import with_db_connection from gn_auth import jobs from gn_auth.commands import run_async_cmd @@ -22,8 +23,6 @@ from gn_auth.auth.requests import request_json from gn_auth.auth.errors import InvalidData, NotFoundError from gn_auth.auth.authorisation.resources.groups.models import group_by_id -from gn_auth.auth.db.sqlite3 import with_db_connection # Replace this with gn_libs alternative - from ..checks import require_json from ...authentication.users import User @@ -207,7 +206,7 @@ def __search_mrna__(): ungrouped_mrna_data, gn3conn=gn3conn, search_query=query, selected=__request_key_list__("selected"), limit=limit, offset=offset) - return jsonify(with_db_connection(__ungrouped__)) + return jsonify(with_db_connection(app.config["SQL_URI"], __ungrouped__)) def __request_key__(key: str, default: Any = ""): if bool(request_json()): @@ -232,7 +231,7 @@ def __search_genotypes__(): ungrouped_genotype_data, gn3conn=gn3conn, search_query=query, selected=__request_key_list__("selected"), limit=limit, offset=offset) - return jsonify(with_db_connection(__ungrouped__)) + return jsonify(with_db_connection(app.config["SQL_URI"], __ungrouped__)) def __search_phenotypes__(): # launch the external process to search for phenotypes @@ -307,6 +306,7 @@ def link_genotypes() -> Response: return link_genotype_data(conn, group_by_id(conn, group_id), datasets) return jsonify(with_db_connection( + app.config["SQL_URI"], partial(__link__, **__values__(request_json())))) @data.route("/link/mrna", methods=["POST"]) @@ -332,6 +332,7 @@ def link_mrna() -> Response: return link_mrna_data(conn, group_by_id(conn, group_id), datasets) return jsonify(with_db_connection( + app.config["SQL_URI"], partial(__link__, **__values__(request_json())))) @data.route("/link/phenotype", methods=["POST"]) @@ -374,4 +375,5 @@ def link_phenotype() -> Response: pheno_traits_from_db(gn3conn, traits)) return jsonify(with_db_connection( + app.config["SQL_URI"], partial(__link__, **__values__(request_json())))) diff --git a/gn_auth/auth/authorisation/privileges/models.py b/gn_auth/auth/authorisation/privileges/models.py index 77be7c0..cd23a0c 100644 --- a/gn_auth/auth/authorisation/privileges/models.py +++ b/gn_auth/auth/authorisation/privileges/models.py @@ -3,8 +3,8 @@ from dataclasses import dataclass from typing import Iterable, Optional import sqlite3 +from gn_libs import sqlite3 as db -from gn_auth.auth.db import sqlite3 as db from gn_auth.auth.authentication.users import User diff --git a/gn_auth/auth/authorisation/resources/checks.py b/gn_auth/auth/authorisation/resources/checks.py index 252df2f..6dfd388 100644 --- a/gn_auth/auth/authorisation/resources/checks.py +++ b/gn_auth/auth/authorisation/resources/checks.py @@ -11,7 +11,6 @@ from gn_libs.privileges import check from .base import Resource from .system.models import system_resource -from ...db import sqlite3 as db from ...authentication.users import User from ..privileges.models import db_row_to_privilege @@ -31,7 +30,7 @@ def __organise_privileges_by_resource_id__(rows): return reduce(__organise__, rows, {}) -def authorised_for(conn: db.DbConnection, +def authorised_for(conn: authdb.DbConnection, user: User, privileges: tuple[str, ...], resource_ids: Sequence[uuid.UUID]) -> dict[uuid.UUID, bool]: @@ -42,7 +41,7 @@ def authorised_for(conn: db.DbConnection, warnings.warn(DeprecationWarning( f"The function `{__name__}.authorised_for` is deprecated. Please use " f"`{__name__}.authorised_for_spec`")) - with db.cursor(conn) as cursor: + with authdb.cursor(conn) as cursor: cursor.execute( ("SELECT ur.*, rp.privilege_id FROM " "user_roles AS ur " @@ -66,7 +65,7 @@ def authorised_for(conn: db.DbConnection, def authorised_for2( - conn: db.DbConnection, + conn: authdb.DbConnection, user: User, resource: Resource, privileges: tuple[str, ...] @@ -77,7 +76,7 @@ def authorised_for2( warnings.warn(DeprecationWarning( f"The function `{__name__}.authorised_for2` is deprecated. Please use " f"`{__name__}.authorised_for_spec`")) - with db.cursor(conn) as cursor: + with authdb.cursor(conn) as cursor: _query = ( "SELECT resources.resource_id, user_roles.user_id, roles.role_id, " "privileges.* " @@ -101,7 +100,7 @@ def authorised_for2( def authorised_for_spec( - conn: db.DbConnection, + conn: authdb.DbConnection, user_id: uuid.UUID, resource_id: uuid.UUID, auth_spec: str @@ -110,7 +109,7 @@ def authorised_for_spec( Check that a user, identified with `user_id`, has a set of privileges that satisfy the `auth_spec` for the resource identified with `resource_id`. """ - with db.cursor(conn) as cursor: + with authdb.cursor(conn) as cursor: _query = ( "SELECT resources.resource_id, user_roles.user_id, roles.role_id, " "privileges.* " diff --git a/gn_auth/auth/authorisation/resources/groups/models.py b/gn_auth/auth/authorisation/resources/groups/models.py index 60d4634..79bead4 100644 --- a/gn_auth/auth/authorisation/resources/groups/models.py +++ b/gn_auth/auth/authorisation/resources/groups/models.py @@ -11,8 +11,8 @@ from flask import g from pymonad.maybe import Just, Maybe, Nothing from pymonad.either import Left, Right, Either from pymonad.tools import monad_from_none_or_value +from gn_libs import sqlite3 as db -from gn_auth.auth.db import sqlite3 as db from gn_auth.auth.authentication.users import User, user_by_id from gn_auth.auth.authorisation.checks import authorised_p diff --git a/gn_auth/auth/authorisation/roles/models.py b/gn_auth/auth/authorisation/roles/models.py index 89403f8..89556a6 100644 --- a/gn_auth/auth/authorisation/roles/models.py +++ b/gn_auth/auth/authorisation/roles/models.py @@ -4,12 +4,12 @@ from functools import reduce from dataclasses import dataclass from typing import Sequence, Iterable, Optional +from gn_libs import sqlite3 as db from pymonad.either import Left, Right, Either from gn_auth.auth.errors import NotFoundError, AuthorisationError from gn_auth.auth.authorisation.resources.base import Resource -from ...db import sqlite3 as db from ...authentication.users import User from ..checks import authorised_p diff --git a/gn_auth/auth/authorisation/users/views.py b/gn_auth/auth/authorisation/users/views.py index 5cdb482..e454a80 100644 --- a/gn_auth/auth/authorisation/users/views.py +++ b/gn_auth/auth/authorisation/users/views.py @@ -23,6 +23,8 @@ from flask import ( make_response, render_template) +from gn_libs import sqlite3 as db +from gn_libs.sqlite3 import with_db_connection from gn_libs.privileges.system import can_create_or_delete_user from gn_libs.privileges.resources import can_assign_role @@ -30,8 +32,6 @@ from gn_auth.smtp import send_message, build_email_message from gn_auth.auth.requests import request_json -from gn_auth.auth.db import sqlite3 as db -from gn_auth.auth.db.sqlite3 import with_db_connection from gn_auth.auth.authorisation.resources.system.models import ( system_resource, @@ -366,8 +366,9 @@ def user_join_request_exists(): "exists": False } with require_oauth.acquire("profile group") as the_token: - return jsonify(with_db_connection(partial( - __request_exists__, user=the_token.user))) + return jsonify(with_db_connection( + current_app.config["SQL_URI"], + partial(__request_exists__, user=the_token.user))) @users.route("/list", methods=["GET"]) @require_oauth("profile user") diff --git a/gn_auth/hooks.py b/gn_auth/hooks.py index bd7380b..f15107f 100644 --- a/gn_auth/hooks.py +++ b/gn_auth/hooks.py @@ -5,7 +5,7 @@ from typing import List from flask import request_finished from flask import request, current_app -from gn_auth.auth.db import sqlite3 as db +from gn_libs import sqlite3 as db def register_hooks(app): """Initialise hooks system on the application.""" |
