(20241221) Zerodha Auth Ready. Users can now integrate Kite.

This commit is contained in:
2024-12-21 15:22:55 +05:30
parent 8904ea394d
commit 1249841b05
15 changed files with 568 additions and 464 deletions
@@ -6,11 +6,11 @@
DATE:
Monday, 25th Nov., 2024
Saturday, 21st Dec., 2024
OBJECTIVE:
To receive callbacks (webhooks).
To receive callbacks (webhooks) from stockbrokers for trading API integrations.
REFERENCES:
@@ -81,7 +81,7 @@ import asyncio
# Related to Quart:
mail_oauth_callback_bp = Blueprint("mail_cb", __name__)
trading_oauth_callback_bp = Blueprint("trading_cb", __name__)
# *****************************************************************************************************************
@@ -101,7 +101,7 @@ mail_oauth_callback_bp = Blueprint("mail_cb", __name__)
# *****************************************************************************************************************
@mail_oauth_callback_bp.record_once
@trading_oauth_callback_bp.record_once
def init(blueprint_setup_state):
# This gets called when the blueprint is registered.
@@ -112,141 +112,14 @@ def init(blueprint_setup_state):
# ---------------------------------------------------------------------------------------------------------------------
@AsyncLoggerContext.log_it(
api_version = "1.0.0",
project = constants.PROJECT_NAME,
log_type = constants.MODULE_NAME,
operation = "gmailClbk",
log_input = 2,
log_output = 1,
sensitive_keys = ["sessionToken", "X-Session-Token"]
)
async def handle_gmail_callback() -> render_template:
"""
To handle the callbacks from GMail specifically. Refer to the individual comments to check what hap[pens at each
step of the process.
:return: A rendered template (HTML) of the final status of the authorization.
"""
# Start by assuming failure:
tokens_saved = False
# In case the user cancelled halfway through (on Google's screen):
if g.inbound_data.get("error") == "access_denied": return await render_template(
"/mail/oauth/oauth_cancelled_v2.html",
mail_client = g.mail_client.title()
)
# Generate the tokens from the callback. Google sends all the needed params in the callback as the URL's query
# params. We can simply use the exact URL that was hit to generate the tokens. In Quart (and Flask) this can be
# achieved by 'request.url' like this:
google_tokens = await current_app.gmail_client.get_authorization_tokens(
redirect_url = request.url,
scopes = None
)
if google_tokens:
# Get the e-mail id that granted authorization. We will be comparing this to the e-mail id that had been given
# to us when the authorization was initiated. We don't mind any e-mail id being used, but we need them to be the
# same at both ends:
user_profile = await current_app.gmail_client.get_user_profile(tokens = google_tokens)
if user_profile.success:
google_tokens.email = user_profile.data["emailAddress"]
google_tokens.displayName = user_profile.data["displayName"]
google_tokens.displayPictureUrl = user_profile.data["displayPictureUrl"]
# Here's where we do the checking of the e-mails,
# if they don't match, we reject the authorization:
auth_token = await current_app.mail_controller.get_token_from_key(
mongo_conn = current_app.data_mongo,
token_key = g.inbound_data["state"]
)
if (
(not auth_token) or
auth_token.clientUserId["email"] != str(google_tokens.email)
): return await render_template(
"/mail/oauth/oauth_failure_v2.html",
mail_client = g.mail_client.title(),
failure_hint = (
f"We were expecting authorization from '{auth_token.clientUserId['email']}', "
f"but got authorization from '{google_tokens.email}' instead."
)
)
# We create standard labels that we will use:
labels = [
{
"name": "TCAOFF",
"textColor": "#434343",
"backgroundColor": "#e7e7e7"
},
{
"name": "CA-Doc",
"textColor": "#434343",
"backgroundColor": "#e7e7e7"
},
{
"name": "CA-AI",
"textColor": "#434343",
"backgroundColor": "#e7e7e7"
}
]
tasks = [
current_app.gmail_client.create_label(
tokens = google_tokens,
label_name = label["name"],
label_visibility = "labelShow",
message_visibility = "show",
label_text_color = label["textColor"],
label_background_color = label["backgroundColor"]
) for label in labels
]
client_responses = await asyncio.gather(*tasks)
# Add the labels to the tokens data:
client_response = await current_app.gmail_client.list_labels(tokens = google_tokens)
google_tokens.labels = client_response.data if client_response.success else None
# Now that we have passed the check,
# we save the tokens to the database:
auth_token.clientUserId = google_tokens.client_user_id
auth_token.token = google_tokens.model_dump()
auth_token.status = "active"
tokens_saved = await current_app.mail_controller.set_token(
db_conn = current_app.sql_writer,
mongo_conn = current_app.data_mongo,
session_token = g.inbound_headers.get("X-Session-Token"),
token_key = g.inbound_data["state"],
auth_token = auth_token
)
# Return an HTML response for success:
if tokens_saved: return await render_template(
"/mail/oauth/oauth_success_v2.html",
mail_client = g.mail_client.title()
)
# Return an HTML response for failure:
else: return await render_template(
"/mail/oauth/oauth_failure_v2.html",
mail_client = g.mail_client.title(),
failure_hint = f"Unknown error. Please use log-id '{g.log_id}' to check with the support team."
)
# ---------------------------------------------------------------------------------------------------------------------
@mail_oauth_callback_bp.route("/callback/<mail_client>", methods = ["POST", "GET"])
@trading_oauth_callback_bp.route("oauth/callback/<trading_client>", methods = ["POST", "GET"])
@set_api_version(api_version = "1.0.0")
@read_input(sanitize_headers = False, sanitize_data = False)
@log_request_to_mongo(
attr_name = "logs_mongo",
project = constants.PROJECT_NAME,
log_type = constants.MODULE_NAME,
operation = "gmailCllBckApi",
operation = "trdngOauthCllBckApi",
log_input = True,
log_output = True,
sensitive_keys = None
@@ -254,8 +127,8 @@ async def handle_gmail_callback() -> render_template:
@log_chain_to_mongo(attr_name = "logs_mongo")
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
@handle_cancelled_request()
async def mail_auth_callback(
mail_client: str = None,
async def trading_oauth_callback(
trading_client: str = None,
inbound_headers: dict = None,
inbound_data: dict = None,
inbound_files: dict = None,
@@ -263,8 +136,9 @@ async def mail_auth_callback(
):
"""
Use this when authorizing access to someone's GMail account. This can be used to capture the authentication token.
:param mail_client: The mail company/brand that you want the authorization from.
This endpoint gets triggered by the stockbroker's servers to let you know when a user accepted or rejected an
authorization request.
:param trading_client: The name of the stockbroker that you have received the callback from.
:param inbound_headers: auto-extracted by the decorators.
:param inbound_data: auto-extracted by the decorators.
:param inbound_files: auto-extracted by the decorators.
@@ -272,45 +146,36 @@ async def mail_auth_callback(
:return: A standard response structure.
"""
# ┓┏ ┓┓ ┓┏ • ┓ ┓
# ┣┫┏┓┏┓┏┫┃┏┓ ┃┃┏┓┏┓┓┏┓┣┓┃┏┓┏
# ┛┗┗┻┛┗┗┻┗┗ ┗┛┗┻┛ ┗┗┻┗┛┗┗ ┛
# Start by assuming failure:
success = None
# Here we make various variables available in the scope of the current request through 'g':
g.log_id = kwargs.get("log_id")
g.inbound_headers = inbound_headers
g.inbound_data = inbound_data
g.mail_client = mail_client
# ┏┓ ┏┓ ┓┓ ┓┏┓•
# ┣ ┏┓┏┓ ┏┛┏┓┏┓┏┓┏┫┣┓┏┓ ┃┫ ┓╋┏┓
# ┻ ┗┛┛ ┗┛┗ ┛ ┗┛┗┻┛┗┗┻ ┛┗┛┗┗┗
# ┓┏ ┓┓ ┏┓ ┓┓┓ ┓
# ┣┫┏┓┏┓┏┫┃┏┓ ┃ ┏┓┃┃┣┓┏┓┏┃┏┏
# ┛┗┗┻┛┗┗┻┗┗ ┗┛┗┻┗┗┗┛┗┻┗┛┗┛
try:
if mail_client == "gmail": return await handle_gmail_callback()
# If something goes wrong:
except Exception as exception: return await render_template(
"/mail/oauth/oauth_failure_v2.html",
mail_client = mail_client.title(),
failure_hint = (
f"An internal server error occurred. "
f"Please use log-id '{g.log_id}' to check with the support team."
if trading_client == "zerodha":
success = await current_app.zerodha_kite_controller.handle_authorization_callback(
sql_conn = current_app.sql_writer,
mongo_data_conn = current_app.data_mongo,
inbound_data = inbound_data
)
# ┳┓
# ┣┫┏┓┏┏┓┏┓┏┓┏┏┓
# ┛┗┗ ┛┣┛┗┛┛┗┛┗
# ┛
# No valid client:
if success is None: return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.BAD_REQUEST,
message = f"Invalid/unimplemented client '{trading_client}'."
)
# ┓┏ ┓┓ ┳ ┓• ┓ ┏┓┓•
# ┣┫┏┓┏┓┏┫┃┏┓ ┃┏┓┓┏┏┓┃┓┏┫ ┃ ┃┓┏┓┏┓╋
# ┛┗┗┻┛┗┗┻┗┗ ┻┛┗┗┛┗┻┗┗┗┻ ┗┛┗┗┗ ┛┗┗
return await render_template(
"/mail/oauth/oauth_failure_v2.html",
mail_client = mail_client.title(),
failure_hint = (
f"Invalid client '{mail_client}' selected. "
f"Please use log-id '{g.log_id}' to check with the support team."
)
# When the client was valid:
return ResponseModel(
status_code = StatusCodes.OK if success else StatusCodes.FAILED,
http_code = HttpCodes.SUCCESS if success else HttpCodes.INTERNAL_SERVER_ERROR,
)
@@ -59,18 +59,15 @@ from utils_v2.api.async_quart import (
handle_cancelled_request
)
# GMail-related utils:
from utils_v2.goog.controllers.gmail.gmail_client import SCOPES_GMAIL_MAIL_MANAGEMENT
# Common:
from shared import constants
# Data Models:
from models.api.mail.oauth import (
OAuthMailAuthorizationRequestHeaders,
OAuthMailAuthorizationRequestData
)
from models.core.auth_token import CoreAuthTokenModel
from models.api.finstitutions.trading.auth import (
TradingAuthRequestHeaders,
TradingAuthRequestData
)
# For asynchronous activities:
import asyncio
@@ -131,13 +128,13 @@ def init(blueprint_setup_state):
@log_chain_to_mongo(attr_name = "logs_mongo")
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
@validate_input(
header_validator = lambda x: OAuthMailAuthorizationRequestHeaders(**x).model_dump(),
data_validator = lambda x: OAuthMailAuthorizationRequestData(**x)
header_validator = lambda x: TradingAuthRequestHeaders(**x).model_dump(),
data_validator = lambda x: TradingAuthRequestData(**x)
)
@handle_cancelled_request()
async def request_oauth_authorization_url(
inbound_headers: dict | OAuthMailAuthorizationRequestHeaders = None,
inbound_data: dict | OAuthMailAuthorizationRequestData = None,
inbound_headers: dict | TradingAuthRequestHeaders = None,
inbound_data: dict | TradingAuthRequestData = None,
inbound_files: dict = None,
**kwargs
):
@@ -168,48 +165,47 @@ async def request_oauth_authorization_url(
# Start by assuming failure:
auth_url = None
# ┏┓ ┏┳┓ ┓ ┓┏┓
# ┃┓┏┓┏┓┏┓┏┓┏┓┏┓ ┃ ┏┓┃┏┏┓┏┓ ┃┫ ┏┓┓┏
# ┗┛┗ ┛┗ ┛ ┗┻┗┗ ┻ ┗┛┛┗┗ ┛┗ ┛┗┛┗ ┗┫
# ┛
# ┏┓ ┏┓ ┓┓ ┓┏┓
# ┏┓┏┓ ┏┛┏┓┏┓┏┓┏┫┣┓┏┓ ┃┫ ┓╋┏┓
# ┻ ┗┛┛ ┗┛┗ ┛ ┗┛┗┻┗┗┻ ┛┗┛┗┗┗
# Make a user identifier from the session info:
token_key = await current_app.mail_controller.generate_token_key(
db_conn = current_app.sql_writer,
mongo_conn = current_app.data_mongo,
auth_token = CoreAuthTokenModel(
serviceType = "email",
client = inbound_data.mailClient,
authType = "oauth",
user = kwargs["session_info"],
clientUserId = {"email": inbound_data.mailId},
status = "pending",
syncFreq = inbound_data.syncFreq,
),
session_token = inbound_headers["X-Session-Token"]
)
if token_key is None:
return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.INTERNAL_SERVER_ERROR,
message = "Failed to generate token key."
# PLANNED FLOW FOR ZERODHA-KITE:
# Step 01.: (One time) The user will go to the integrations page and add his API Key and SPI Secret there. We store
# these values without verification.
# Step 02.: (Daily) The user will go to the investments tab and click on his Zerodha account, which will give him
# a URL that will take him to Zerodha's official site to log in. When he logs in, Zerodha will hit our
# callback URL and give us the authentication details.
if inbound_data.client == "zerodhaKite":
# Prepare the inputs:
auth_url = await current_app.zerodha_kite_controller.get_authorization_url(api_key = inbound_data.auth.apiKey)
# Immediately save the details against that token id:
success = await current_app.zerodha_kite_controller.set_token_direct(
sql_conn = current_app.sql_writer,
mongo_data_conn = current_app.data_mongo,
auth_token = CoreAuthTokenModel(
serviceType = "stockTrading",
client = inbound_data.client,
authType = "oauth",
user = kwargs["session_info"],
clientUserId = {
"apiKey": inbound_data.auth.apiKey
},
auth = inbound_data.auth.model_dump(),
status = "active",
syncFreq = 1500
),
token_notes = {
"apiKey": inbound_data.auth.apiKey,
"authUrl": auth_url
},
session_token = inbound_headers["X-Session-Token"]
)
# ┏┓ ┏┓┳┳┓ •┓
# ┣ ┏┓┏┓ ┃┓┃┃┃┏┓┓┃
# ┻ ┗┛┛ ┗┛┛ ┗┗┻┗┗
if inbound_data.mailClient == "gmail":
# Get the authorization URL:
auth_url = await current_app.gmail_client.get_authorization_url(
scopes = SCOPES_GMAIL_MAIL_MANAGEMENT,
state = str(token_key),
access_type = "offline",
approval_prompt = "force",
include_granted_scopes = "true",
user_email = inbound_data.mailId
)
# Check if things were successful:
if not success: auth_url = None
# ┳┓
# ┣┫┏┓┏┏┓┏┓┏┓┏┏┓
@@ -221,7 +217,7 @@ async def request_oauth_authorization_url(
status_code = StatusCodes.OK if auth_url else StatusCodes.FAILED,
http_code = HttpCodes.SUCCESS if auth_url else HttpCodes.INTERNAL_SERVER_ERROR,
data = {
"client": inbound_data.mailClient,
"client": inbound_data.client,
"authorizationUrl": auth_url
}
)