Skip to content

Commit

Permalink
fix: handling otp
Browse files Browse the repository at this point in the history
  • Loading branch information
shikharish authored and proffapt committed Nov 25, 2023
1 parent 22d2ad8 commit 68ede04
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
7 changes: 2 additions & 5 deletions src/iitkgp_erp_login/erp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

from iitkgp_erp_login.endpoints import *
from iitkgp_erp_login.read_mail import getOTP
from iitkgp_erp_login.utils import get_import_location, write_tokens_to_file, get_tokens_from_file, populate_session_with_login_tokens

ROLL_NUMBER = ""
Expand Down Expand Up @@ -197,12 +196,10 @@ def login(
)

# Handling OTP
request_otp(headers=headers, session=session, login_details=login_details, log=LOGGING)

if OTP_CHECK_INTERVAL != None:
try:
if LOGGING: logging.info(" Waiting for OTP ...")
otp = getOTP(OTP_CHECK_INTERVAL)
from iitkgp_erp_login.read_mail import getOTP
otp = getOTP(OTP_CHECK_INTERVAL, headers=headers, session=session, login_details=login_details, log=LOGGING)
if LOGGING: logging.info(" Received OTP")

except Exception as e:
Expand Down
8 changes: 7 additions & 1 deletion src/iitkgp_erp_login/read_mail.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logging
import time
import base64
from googleapiclient.discovery import build
import requests
from iitkgp_erp_login.erp import LoginDetails, request_otp
from iitkgp_erp_login.utils import generate_token

SUBJECT = "OTP for Sign In in ERP Portal of IIT Kharagpur"
Expand All @@ -16,11 +19,14 @@ def getMailID(service):

return None

def getOTP(OTP_CHECK_INTERVAL):
def getOTP(OTP_CHECK_INTERVAL,headers: dict[str, str], session: requests.Session, login_details: LoginDetails, log: bool = False):
creds = generate_token()
service = build("gmail", "v1", credentials=creds)

latest_mail_id = getMailID(service)
request_otp(headers=headers, session=session, login_details=login_details, log=log)

if log: logging.info(" Waiting for OTP ...")
while True:
if (mail_id := getMailID(service)) != latest_mail_id:
break
Expand Down
48 changes: 24 additions & 24 deletions src/iitkgp_erp_login/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,27 @@ def get_import_location(caller_file: str = None):


def generate_token():
"""Generates token.json from credentials.json file with readonly access to mails."""
if len(sys.argv) == 1 and sys.argv[0] == '-c':
caller_file = None
else:
caller_file = inspect.getframeinfo(inspect.currentframe().f_back.f_back.f_back).filename
token_path = os.path.join(get_import_location(caller_file), "token.json")
credentials_path = os.path.join(get_import_location(caller_file), "credentials.json")
scopes = [f"https://www.googleapis.com/auth/gmail.readonly"]

creds = None
if os.path.exists(token_path):
creds = Credentials.from_authorized_user_file(token_path, scopes)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(credentials_path, scopes)
creds = flow.run_local_server(port=0)

if not os.path.exists(token_path):
with open(token_path, "w") as token:
token.write(creds.to_json())

return creds
"""Generates token.json from credentials.json file with readonly access to mails."""
if len(sys.argv) == 1 and sys.argv[0] == '-c':
caller_file = None
else:
caller_file = inspect.getframeinfo(inspect.currentframe().f_back.f_back.f_back).filename
token_path = os.path.join(get_import_location(caller_file), "token.json")
credentials_path = os.path.join(get_import_location(caller_file), "credentials.json")
scopes = [f"https://www.googleapis.com/auth/gmail.readonly"]

creds = None
if os.path.exists(token_path):
creds = Credentials.from_authorized_user_file(token_path, scopes)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(credentials_path, scopes)
creds = flow.run_local_server(port=0)

if not os.path.exists(token_path):
with open(token_path, "w") as token:
token.write(creds.to_json())

return creds

0 comments on commit 68ede04

Please sign in to comment.