(20250123) Made a common API to disable all integrations.
This commit is contained in:
@@ -0,0 +1,205 @@
|
||||
"""
|
||||
|
||||
AUTHOR:
|
||||
|
||||
Khushal P Soonderji
|
||||
|
||||
DATE:
|
||||
|
||||
Thursday, 23rd Jan., 2025.
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
To disable chat accounts.
|
||||
|
||||
REFERENCES:
|
||||
|
||||
N/A
|
||||
|
||||
DOWNLOADS:
|
||||
|
||||
N/A
|
||||
|
||||
NOTES:
|
||||
|
||||
N/A
|
||||
|
||||
"""
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** IMPORT ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# To make sibling directories accessible for imports:
|
||||
import sys
|
||||
sys.path.append(".")
|
||||
sys.path.append("..")
|
||||
|
||||
# For using Quart:
|
||||
from quart import Blueprint, current_app, request
|
||||
|
||||
# My utils:
|
||||
from utils_v2.string import json
|
||||
from utils_v2.api.codes import StatusCodes, HttpCodes
|
||||
from utils_v2.api.response import ResponseModel
|
||||
from utils_v2.api.async_quart import (
|
||||
set_api_version,
|
||||
read_input,
|
||||
get_session_info,
|
||||
log_request_to_mongo,
|
||||
log_chain_to_mongo,
|
||||
should_not_be_under_maintenance,
|
||||
only_whitelisted_ips,
|
||||
limit_rate,
|
||||
validate_input,
|
||||
handle_cancelled_request
|
||||
)
|
||||
|
||||
# Common:
|
||||
from shared import constants
|
||||
|
||||
# Data Models:
|
||||
from models.core.user import CoreUserInfoModel
|
||||
from models.api.common.auth_disable import AuthTokenDisableRequestHeaders, AuthTokenDisableRequestData
|
||||
|
||||
# Helpers:
|
||||
from api.helpers.user import token_check
|
||||
|
||||
# To work with MongoDB:
|
||||
from bson import ObjectId
|
||||
|
||||
# For asynchronous activities:
|
||||
import asyncio
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MACROS / ONE-TIME INIT ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# Related to Quart:
|
||||
auth_token_disable_bp = Blueprint("auth_token_dsbl", __name__)
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** VARIABLES ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
# --- Nothing Yet
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** FUNCTIONS ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
@auth_token_disable_bp.record_once
|
||||
def init(blueprint_setup_state):
|
||||
|
||||
# This gets called when the blueprint is registered.
|
||||
# Consider this to be a one-time setup for the whole blueprint:
|
||||
pass
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
@auth_token_disable_bp.route("/auth", methods = ["DELETE"])
|
||||
@auth_token_disable_bp.route("/integration/auth", methods = ["DELETE"])
|
||||
@set_api_version(api_version = "1.0.0")
|
||||
@read_input(sanitize_headers = False, sanitize_data = False)
|
||||
@get_session_info(key = "X-Session-Token", session_coro = "get_session")
|
||||
@log_request_to_mongo(
|
||||
attr_name = "logs_mongo",
|
||||
project = constants.PROJECT_NAME,
|
||||
log_type = constants.MODULE_NAME,
|
||||
operation = "authTokDsblApi",
|
||||
log_input = True,
|
||||
log_output = True,
|
||||
sensitive_keys = ["sessionToken", "X-Session-Token", "tokenKey"]
|
||||
)
|
||||
@log_chain_to_mongo(attr_name = "logs_mongo")
|
||||
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
|
||||
@validate_input(
|
||||
header_validator = lambda x: AuthTokenDisableRequestHeaders(**x).model_dump(),
|
||||
data_validator = lambda x: AuthTokenDisableRequestData(**x)
|
||||
)
|
||||
@handle_cancelled_request()
|
||||
async def disable_auth_token(
|
||||
inbound_headers: dict | AuthTokenDisableRequestHeaders = None,
|
||||
inbound_data: dict | AuthTokenDisableRequestData = None,
|
||||
inbound_files: dict = None,
|
||||
**kwargs
|
||||
):
|
||||
|
||||
"""
|
||||
Use this when a user wants to remove/disable his account.
|
||||
:param inbound_headers: auto-extracted by the decorators.
|
||||
:param inbound_data: auto-extracted by the decorators.
|
||||
:param inbound_files: auto-extracted by the decorators.
|
||||
:param kwargs: Any number of extra inputs supplied by the decorators.
|
||||
:return: A standard response structure.
|
||||
"""
|
||||
|
||||
# ┏┓ ┓ ┏┓┓ ┓
|
||||
# ┣┫┓┏╋┣┓ ┃ ┣┓┏┓┏┃┏
|
||||
# ┛┗┗┻┗┛┗ ┗┛┛┗┗ ┗┛┗
|
||||
|
||||
# If the session token is invalid/expired:
|
||||
if kwargs.get("session_info") is None:
|
||||
return ResponseModel(
|
||||
status_code = StatusCodes.FAILED,
|
||||
http_code = HttpCodes.UNAUTHORIZED
|
||||
)
|
||||
|
||||
# Get the user's info:
|
||||
user_info = CoreUserInfoModel(**kwargs.get("session_info"))
|
||||
|
||||
# ┳┳┓ ┓•┏ ┏┓
|
||||
# ┃┃┃┏┓┏┫┓╋┓┏ ┗┓╋┏┓╋┓┏┏
|
||||
# ┛ ┗┗┛┗┻┗┛┗┫ ┗┛┗┗┻┗┗┻┛
|
||||
# ┛
|
||||
|
||||
# Update the message:
|
||||
success = await current_app.core_auth_token_controller.modify_status_by_token_key(
|
||||
sql_conn = current_app.sql_writer,
|
||||
mongo_data_conn = current_app.data_mongo,
|
||||
token_key = inbound_data.tokenKey,
|
||||
new_status = "disabled",
|
||||
additional_filter = token_check.get_authorization_filter(user_info = user_info),
|
||||
session_token = inbound_headers["X-Session-Token"]
|
||||
)
|
||||
|
||||
# ┳┓
|
||||
# ┣┫┏┓┏┏┓┏┓┏┓┏┏┓
|
||||
# ┛┗┗ ┛┣┛┗┛┛┗┛┗
|
||||
# ┛
|
||||
|
||||
# Done here:
|
||||
return ResponseModel(
|
||||
status_code = StatusCodes.OK if success else StatusCodes.FAILED,
|
||||
http_code = HttpCodes.SUCCESS if success else HttpCodes.INTERNAL_SERVER_ERROR
|
||||
)
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MAIN PROGRAM ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
pass
|
||||
@@ -153,6 +153,25 @@ async def is_not_authorized(
|
||||
return not authorized
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
def get_authorization_filter(user_info: CoreUserInfoModel) -> dict:
|
||||
|
||||
"""
|
||||
Just returns a MongoDB query filter that can be sent as an 'additional_filter' to various methods to ensure that the
|
||||
user requesting the service and the user that has rights over the resource are matching.
|
||||
:param user_info: The information of the user obtained from the session.
|
||||
:return: A filter condition.
|
||||
"""
|
||||
|
||||
return AsyncMongo.dict_to_dot_notation({
|
||||
"user": {
|
||||
"billingAccountId": user_info.billingAccountId
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** MAIN PROGRAM ***
|
||||
|
||||
@@ -144,6 +144,9 @@ from api.blueprints.finstitutions.trading.symbols.list import trading_symbols_li
|
||||
# AI Blueprints:
|
||||
from api.blueprints.ai.llm.invoke import llm_invoke_bp
|
||||
|
||||
# Common Blueprints:
|
||||
from api.blueprints.common.disable import auth_token_disable_bp
|
||||
|
||||
# Tech and Testing Blueprints:
|
||||
from api.blueprints.tech.chat_alerts import tech_chat_alert_bp
|
||||
from api.blueprints.test.callback import test_callback_bp
|
||||
@@ -213,6 +216,9 @@ app.register_blueprint(trading_oauth_request_bp, url_prefix = f"/{MODULE_BASE}/f
|
||||
app.register_blueprint(trading_oauth_callback_bp, url_prefix = f"/{MODULE_BASE}/finstitutions/trading/oauth")
|
||||
app.register_blueprint(trading_symbols_list_bp, url_prefix = f"/{MODULE_BASE}/finstitutions/trading/symbols")
|
||||
|
||||
# Common Blueprints:
|
||||
app.register_blueprint(auth_token_disable_bp, url_prefix = f"/{MODULE_BASE}")
|
||||
|
||||
# AI Blueprints:
|
||||
app.register_blueprint(llm_invoke_bp, url_prefix = f"/{MODULE_BASE}/ai")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user