A package to automate login process in ERP for IIT-KGP
Project description
ERP Login Module
A python package/module to automate login process in ERP for IIT-KGP.
Table of Contents
Description
iitkgp_erp_login
├── endpoints.py
├── erp.py
└── read_mail.py
read_mail.py contains implementation of getOTP(OTP_WAIT_INTERVAL)
function along with various helper functions for it. These functions are not intended to be used by you - the user - as the only case, where, OTP will be required is during the login process which is handled by functions in erp.py. Hence, let this script be an abstraction for general user. Obviously, if you want to tweak the OTP fetching process feel free to have a look at the script read_mail.py.
Endpoints
endpoints.py contains all the required endpoints for the ERP login workflow.
About
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 only accessible when the user is NOT logged-in and behaves exactly asHOMEPAGE_URL
but when logged-in returns404
error.
Usage
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
Usage in login workflow
To automate login into ERP, the endpoints are hit in the following order:
- Hit
HOMEPAGE_URL
usingsession.get(HOMEPAGE_URL)
. This step is necessary to establish the initial session and retrievesessionToken
. - Hit
SECRET_QUESTION_URL
usingsession.post(SECRET_QUESTION_URL, data={'user_id': erp_creds.ROLL_NUMBER}, headers=headers)
. This step fetches the secret question required for authentication. - Hit
OTP_URL
usingsession.post(OTP_URL, data={'typeee': 'SI', 'loginid': erp_creds.ROLL_NUMBER}, headers=headers)
. This step requests an OTP (One-Time Password) for authentication. - Finally, hit
LOGIN_URL
usingsession.post(LOGIN_URL, data=login_details, headers=headers)
. This step performs the actual ERP login with the provided login details and OTP.
Note
session
= requests.Session() is used to persist the session parameters throughout the workflow
Login
ERP Login workflow is implemented in : login(headers, erp_creds, OTP_WAIT_INTERVAL, session)
inside erp.py.
The input and output specifications for the function are mentioned below.
Input
The function requires following 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', }
erp_creds
: ERP Login Credentials file, which is imported.import erpcreds
OTP_WAIT_INTERVAL
: Interval after which api checks continuously for new OTP mail.session
: requests.Session() object, to persist the session parameters throughout the workflow.import requests session = requests.Session()
Output
- The function returns:
- It also modifies the
session
object which now contains parameters for the logged in session, which can be used for further navigation in ERP. - Has exhastive loggin inbuilt. It prints the status of each step.
Usage
It is recommended to use the login function as shown below:
# importing the erp.py file
import iitkgp_erp_login.erp as erp
# using the login function inside erp.py
sessionToken, ssoToken = erp.login(headers, erpcreds, 2, session)
Combining everything we looked about the login
function till now:
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, erpcreds, 2, session)
print(sessionToken, ssoToken)
Session status check
The logic for status check of session is implemented in session_alive(session)
. It tells whether the given session is valid/alive or not.
The input and output specifications for the function are mentioned below.
Input
The function requires following argument:
session
: requests.Session() object, to persist the session parameters throughout the workflow.import requests session = requests.Session()
Output
Returns the status of session in boolean. True if alive and False if not.
Usage
It is recommended to use the session_alive function as shown below:
# 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))
Combining everything we looked about the session_alive
and login
functions till now:
import requests
import time
import creds
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()
while True:
if not erp.session_alive(session):
erp.login(headers, creds, 2, session)
else:
print("Session is alive.")
time.sleep(2)
Usage
Prerequisites
Following scripts are required, and MUST be present in the same directory as that of the script in which iitkgp_erp_login
(yes, this module) is being imported.
ERP credentials file
Creating
Create a .py
file with your ERP credentials stored in it.
Following are the instructions for creating this file:
- Name of the file can be any valid python file's name
- Variable names must not be changed, copy the below format and ONLY update values inside
"
- double quotes.# ERP Credentials ROLL_NUMBER = "XXYYXXXXX" PASSWORD = "**********" SECURITY_QUESTIONS_ANSWERS = { "Q1" : "A1", "Q2" : "A2", "Q3" : "A3", }
Using
Let's suppose you saved the erp creds file as erpcreds.py
. Then in order to use it you will just have to import it:
import erpcreds
print(erpcreds.ROLL_NUMBER)
# Output: XXYYXXXXX
print(erpcreds.SECURITY_QUESTIONS_ANSWERS["Q2"])
# Output: A2
Generating token for GMail enabled googleapi
-
Follow the steps at Gmail API - Python Quickstart guide to get
credentials.json
.Note
credentials.json
is permanent until you delete it in your google clound console. -
Follow the steps below to generate
token.json
:-
Download gentokenjson.py in the same folder containing
credentials.json
-
Import the required module
pip install google-auth-oauthlib
-
Execute
gentokenjson.py
withreadonly
argumentpython3 gentokenjson.py readonly
-
Browser window will open and ask you to select the account, choose the one receiving OTP for login
-
Allow permission on that email to use just enabled GMAIL API
- Click on
Continue
instead of Back To Safety - Then press
Continue
again
- Click on
-
token.json
will be generated in same folder as that ofcredentials.json
Warning
token.json
expires after sometime. So make sure to check that in your projects and keep refreshing it. -
Example
Now, we will create a script which will open ERP on your default browser with a logged in session.
-
Install the package.
pip install iitkgp_erp_login
-
Make sure erpcreds.py & token.json exist in the same directory as that of the script we are about to create.
-
Create
open_erp.py
and append the following content into it.import requests import webbrowser import erpcreds import iitkgp_erp_login.erp as erp from iitkgp_erp_login.endpoints import HOMEPAGE_URL 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() _, ssoToken = erp.login(headers, erpcreds, 2, session) logged_in_url = f"{HOMEPAGE_URL}?ssoToken={ssoToken}" webbrowser.open(logged_in_url)
-
Run the script
python3 open_erp.py
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for iitkgp_erp_login-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68ab38eaf67c983aefaa83448ca1b99a6f0a6023acdba2ae99bbb571cd64ddea |
|
MD5 | 5bdc4a72f43913e94a308326cfd071bc |
|
BLAKE2b-256 | 772207acc295fbc2565dc233ac7ce210911868b871166af649d23b82b938de4f |