From 1519ee00c1d16d3ff1d47a4bca4f66b3ec0741ac Mon Sep 17 00:00:00 2001 From: khushal Date: Fri, 22 Nov 2024 18:54:01 +0530 Subject: [PATCH] (20241122) Port no. adjusted. --- run.sh | 2 +- utils_v2/oauth/__init__.py | 0 utils_v2/oauth/base.py | 128 +++++++++++++++++++++++ utils_v2/oauth/services/__init__.py | 0 utils_v2/oauth/services/goog.py | 152 ++++++++++++++++++++++++++++ 5 files changed, 281 insertions(+), 1 deletion(-) create mode 100644 utils_v2/oauth/__init__.py create mode 100644 utils_v2/oauth/base.py create mode 100644 utils_v2/oauth/services/__init__.py create mode 100644 utils_v2/oauth/services/goog.py diff --git a/run.sh b/run.sh index 1a44bf7..3130dae 100644 --- a/run.sh +++ b/run.sh @@ -2,7 +2,7 @@ # Use this to run the microservice without any docker setup. source .venv/bin/activate -python3 "$(pwd)/api/main.py" --host "127.0.0.1" --port 5108 --workers 4 --script-id "kps_cnv_KBC3MoaU" & +python3 "$(pwd)/api/main.py" --host "127.0.0.1" --port 5106 --workers 4 --script-id "kps_cnv_KBC3MoaU" & deactivate # All done: diff --git a/utils_v2/oauth/__init__.py b/utils_v2/oauth/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils_v2/oauth/base.py b/utils_v2/oauth/base.py new file mode 100644 index 0000000..588d4f2 --- /dev/null +++ b/utils_v2/oauth/base.py @@ -0,0 +1,128 @@ +""" + + AUTHOR: + + Khushal P Soonderji + + DATE: + + Friday, 22nd Nov., 2024 + + OBJECTIVE: + + To provide a structured way to handle OAuth2.0 behaviour for various services. + + REFERENCES: + + N/A + + DOWNLOADS: + + N/A + +""" + + +# ***************************************************************************************************************** +# ***** **** +# *** IMPORT *** +# ***** **** +# ***************************************************************************************************************** + + +# To make sibling directories accessible for imports: +import sys +sys.path.append(".") +sys.path.append("..") + +# System-level activities: +import io + +# For defining the class's structure: +from abc import ABC, abstractmethod + +# For debugging: +from icecream import IceCreamDebugger + + +# ***************************************************************************************************************** +# ***** **** +# *** MACROS / ONE-TIME INIT *** +# ***** **** +# ***************************************************************************************************************** + + +# --- Nothing Yet + + +# ***************************************************************************************************************** +# ***** **** +# *** VARIABLES *** +# ***** **** +# ***************************************************************************************************************** + + +# --- Nothing Yet + + +# ***************************************************************************************************************** +# ***** **** +# *** FUNCTIONS *** +# ***** **** +# ***************************************************************************************************************** + + +# --- Nothing Yet + + +# ***************************************************************************************************************** +# ***** **** +# *** CLASSES *** +# ***** **** +# ***************************************************************************************************************** + + +class OAuthBase(ABC): + + def __init__( + self, + config: dict, + redirect_url: str, + debug = True, + debug_prefix = "OAuth | ", + debug_only_errors = True + ): + + # Prepare the debugging utility: + self._debug_prefix = debug_prefix + self._printer = IceCreamDebugger(prefix = debug_prefix, includeContext = True) + if not debug: self._printer.disable() + self._debug_only_errors = debug_only_errors + + # Accept the input config: + self._config = config + self._redirect_url = redirect_url + + def enable_debug(self): + self._printer.enable() + + def disable_debug(self): + self._printer.disable() + + def debug_only_errors(self): + self._debug_only_errors = True + + def debug_everything(self): + self._debug_only_errors = False + + +# ***************************************************************************************************************** +# ***** **** +# *** MAIN PROGRAM *** +# ***** **** +# ***************************************************************************************************************** + + +if __name__ == "__main__": + + pass diff --git a/utils_v2/oauth/services/__init__.py b/utils_v2/oauth/services/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils_v2/oauth/services/goog.py b/utils_v2/oauth/services/goog.py new file mode 100644 index 0000000..9db12aa --- /dev/null +++ b/utils_v2/oauth/services/goog.py @@ -0,0 +1,152 @@ +""" + + AUTHOR: + + Khushal P Soonderji + + DATE: + + Friday, 22nd Nov., 2024 + + OBJECTIVE: + + To handle OAuth2.0 activities for Google's services. + + REFERENCES: + + 1. https://developers.google.com/calendar/api/quickstart/python + + DOWNLOADS: + + N/A + +""" + + +# ***************************************************************************************************************** +# ***** **** +# *** IMPORT *** +# ***** **** +# ***************************************************************************************************************** + + +# To make sibling directories accessible for imports: +import sys +sys.path.append(".") +sys.path.append("..") + +# System-level activities: +import io + +# My utils: +from utils_v2.string import json + +# The base model: +from utils_v2.oauth.base import OAuthBase + +# Related to Google: +from google.auth.transport.requests import Request +from google.oauth2.credentials import Credentials +from google_auth_oauthlib.flow import InstalledAppFlow + +# For asynchronous activities: +import asyncio + + +# ***************************************************************************************************************** +# ***** **** +# *** MACROS / ONE-TIME INIT *** +# ***** **** +# ***************************************************************************************************************** + + +# Google Scopes: +SCOPES_GMAIL_MAIL_MANAGEMENT = [ + r"https://www.googleapis.com/auth/gmail.modify", + r"https://www.googleapis.com/auth/gmail.labels" +] +SCOPES_GMAIL_FULL = [r"https://mail.google.com/"] + + +# ***************************************************************************************************************** +# ***** **** +# *** VARIABLES *** +# ***** **** +# ***************************************************************************************************************** + + +# --- Nothing Yet + + +# ***************************************************************************************************************** +# ***** **** +# *** FUNCTIONS *** +# ***** **** +# ***************************************************************************************************************** + + +# --- Nothing Yet + + +# ***************************************************************************************************************** +# ***** **** +# *** CLASSES *** +# ***** **** +# ***************************************************************************************************************** + + +class GoogleOAuth(OAuthBase): + + # Class variables: + __flow = None + + async def init( + self, + scopes = None + ): + + # Initialize your Google App: + self._printer("Initializing flow.") + self.__flow = InstalledAppFlow.from_client_config( + self._config, + scopes = scopes or SCOPES_GMAIL_MAIL_MANAGEMENT, + redirect_uri = self._redirect_url + ) + + async def get_authorization_url(self): + + authorization_url, state = self.__flow.authorization_url( + access_type = "offline", + include_granted_scopes = "true" + ) + + return authorization_url + + +# ***************************************************************************************************************** +# ***** **** +# *** MAIN PROGRAM *** +# ***** **** +# ***************************************************************************************************************** + + +if __name__ == "__main__": + + secrets_file = r"../../../creds/google_converse_test_oauth.json" + secrets_dict = json.from_file(secrets_file) + + my_goog = GoogleOAuth( + config = secrets_dict, + # redirect_url = r"https://v2.api.bicree.com/user/callback/test", + redirect_url = r"127.0.0.1", + debug = True, + debug_prefix = "OAuth (Goog) | " + ) + + async def main(): + + await my_goog.init() + print("AUTH URL:", await my_goog.get_authorization_url()) + + + asyncio.run(main())