Tired of the tedious ERP login process? Wanted to automate some ERP workflow but stuck at login?
🚀 Introducing iitkgp-erp-login: Your Ultimate ERP Login Automation Module for IIT-KGP ! 🚀
Key Features:
[!Note] This package is not officially affiliated with IIT Kharagpur.
Guess the number of lines of python code it will take you to achieve this.
Reading this doc will give you the answer.
The endpoints.py file includes all the necessary endpoints for the ERP login workflow.
HOMEPAGE_URL
: The URL of the ERP homepage/loginpage.SECRET_QUESTION_URL
: The URL for retrieving the secret question for authentication.OTP_URL
: The URL for requesting the OTP (One-Time Password) for authentication.LOGIN_URL
: The URL for ERP login.WELCOMEPAGE_URL
: The URL of the welcome page, which is accessible only when the user is NOT logged in, and behaves exactly like the HOMEPAGE_URL
. However, when the user is logged in, it returns a 404
error.from iitkgp_erp_login.endpoints import *
print(HOMEPAGE_URL)
# Output: https://erp.iitkgp.ac.in/IIT_ERP3/
print(LOGIN_URL)
# Output: https://erp.iitkgp.ac.in/SSOAdministration/auth.htm
ERP login workflow is implemented in login(headers, session, ERPCREDS=None, OTP_CHECK_INTERVAL=None, LOGGING=False, SESSION_STORAGE_FILE=None)
function in erp.py.
[!Note] This function currently compiles the login workflow "ONLY for the CLI", not for web apps.
The function requires following compulsory arguments:
headers
: Headers for the post requests.
headers = {
'timeout': '20',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/51.0.2704.79 Chrome/51.0.2704.79 Safari/537.36',
}
session
: A requests.Session() object, to persist the session parameters throughout the workflow.
import requests
session = requests.Session()
The function can also be provided with these optional arguments:
ERPCREDS : ERP Login Credentials python file, which is imported into main python file. |
Default Value | None |
---|---|---|
NOT Specified | The user is prompted to enter their credentials manually | |
Specified (ERPCREDS=erpcreds ) |
The credentials are retrieved from the erpcreds.py file |
[!Note] Here,
credentials
refer to the roll number, password, and security question.
OTP_CHECK_INTERVAL : The interval (in seconds) after which the API continuously checks for new OTP emails. |
Default Value | None |
---|---|---|
NOT Specified | The user will be prompted to manually enter the received OTP | |
Specified (OTP_CHECKINTERVAL=2 ) |
The OTP will be automatically fetched and checked every 2 seconds |
LOGGING : Toggles comprehensive logging. |
Default value | False |
---|---|---|
NOT Specified | No Logging | |
Specified (LOGGING=True ) |
Logs every step in an exhaustive manner |
SESSION_STORAGE_FILE : A file where sessionToken and ssoToken - collectively referred to as "session tokens" here - are stored for direct usage. |
Default value | None |
---|---|---|
NOT Specified | The session tokens will not be stored in a file | |
Specified (SESSION_STORAGE_FILE=".session" ) |
The session tokens will be stored in .session file for later direct usage |
[!Note] The approximate expiry time for
ssoToken
is ~30 minutes and that ofsession
object is ~2.5 hours
return sessionToken, ssoToken
):
session
object, which now includes parameters for the logged-in session. This session
object can be utilized for further navigation within the ERP system.ROLL_NUMBER
is made available for further usage in the following manner.
import iitkgp_erp_login.erp as erp
sessionToken, ssoToken = erp.login(headers, session)
print('Roll Number =', erp.ROLL_NUMBER)
It is recommended to use the login
function in the following manner (optional arguments are your choice):
# Importing the erp.py file
import iitkgp_erp_login.erp as erp
# Using the login function inside erp.py
sessionToken, ssoToken = erp.login(headers, session, ERPCREDS=erpcreds, OTP_CHECK_INTERVAL=2, LOGGING=True, SESSION_STORAGE_FILE=".session")
Here are some examples combining all the aspects we have discussed so far about the login
function:
import requests
import erpcreds
import iitkgp_erp_login.erp as erp
headers = {
'timeout': '20',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/51.0.2704.79 Chrome/51.0.2704.79 Safari/537.36',
}
session = requests.Session()
sessionToken, ssoToken = erp.login(headers, session)
# Credentials: Manual | OTP: Manual | Logging: No | TokenStorage: No
sessionToken, ssoToken = erp.login(headers, session, ERPCREDS=erpcreds)
# Credentials: Automatic - from erpcreds.py | OTP: Manual | Logging: No | TokenStorage: No
sessionToken, ssoToken = erp.login(headers, session, OTP_CHECK_INTERVAL=2)
# Credentials: Manual | OTP: Automatic - checked every 2 seconds | Logging: No | TokenStorage: No
sessionToken, ssoToken = erp.login(headers, session, LOGGING=True)
# Credentials: Manual | OTP: Manual | Logging: Yes | TokenStorage: No
sessionToken, ssoToken = erp.login(headers, session, ERPCREDS=erpcreds, OTP_CHECK_INTERVAL=5)
# Credentials: Automatic - from erpcreds.py | OTP: Automatic - checked every 5 seconds | Logging: No | TokenStorage: No
sessionToken, ssoToken = erp.login(headers, session, ERPCREDS=erpcreds, OTP_CHECK_INTERVAL=2, LOGGING=True)
# Credentials: Automatic - from erpcreds.py | OTP: Automatic - checked every 2 seconds | Logging: Yes | TokenStorage: No
sessionToken, ssoToken = erp.login(headers, session, ERPCREDS=erpcreds, OTP_CHECK_INTERVAL=2, SESSION_STORAGE_FILE='.session')
# Credentials: Automatic - from erpcreds.py | OTP: Automatic - checked every 2 seconds | Logging: No | TokenStorage: in .session file
sessionToken, ssoToken = erp.login(headers, session, ERPCREDS=erpcreds, OTP_CHECK_INTERVAL=2, LOGGING=True, SESSION_STORAGE_FILE='.session')
# Credentials: Automatic - from erpcreds.py | OTP: Automatic - checked every 2 seconds | Logging: Yes | TokenStorage: in .session file
[!Note] These are just examples of how to use the login function, not satisfying the prerequisites.
Some arguments of
login()
have their own prerequisites that must be satisfied in order to use them. See "Input" section of login for complete details.
The logic for checking the status of the session is implemented in the session_alive(session)
function in erp.py. This function determines whether the given session is valid/alive or not.
The function requires following argument:
session
: requests.Session() object, to persist the session parameters throughout the workflow.
import requests
session = requests.Session()
The session_alive(session) function returns the status of the session as a boolean value: |
Status | Return Value |
---|---|---|
Valid (Alive ) |
True |
|
Not Valid (Dead ) |
False |
It is recommended to use the session_alive
function in the following manner:
# Importing the erp.py file
import iitkgp_erp_login.erp as erp
# Using the session_alive function inside erp.py
print(erp.session_alive(session))
Here's an example combining all the aspects we have discussed so far about the login
function and session_alive
function:
import requests
import time
import erpcreds
# Importing erpcreds.py, which contains ERP credentials
import iitkgp_erp_login.erp as erp
headers = {
'timeout': '20',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/51.0.2704.79 Chrome/51.0.2704.79 Safari/537.36',
}
session = requests.Session()
print("Logging into ERP for:", creds.ROLL_NUMBER)
while True:
if not erp.session_alive(session):
_, ssoToken = erp.login(headers, session, ERPCREDS=erpcreds, LOGGING=True)
else:
print("Session is alive.")
ssoToken = session.cookies.get('ssoToken')
sessionToken = session.cookies.get('JSID#/IIT_ERP3')
# Traverse ERP further using ssoToken
time.sleep(2)
[!Note] This is merely a Proof of Concept example; this exact functionality has been integrated into the login function itself from version 2.3.1 onwards.