Skip to main content

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 as HOMEPAGE_URL but when logged-in returns 404 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:

  1. Hit HOMEPAGE_URL using session.get(HOMEPAGE_URL). This step is necessary to establish the initial session and retrieve sessionToken.
  2. Hit SECRET_QUESTION_URL using session.post(SECRET_QUESTION_URL, data={'user_id': erp_creds.ROLL_NUMBER}, headers=headers). This step fetches the secret question required for authentication.
  3. Hit OTP_URL using session.post(OTP_URL, data={'typeee': 'SI', 'loginid': erp_creds.ROLL_NUMBER}, headers=headers). This step requests an OTP (One-Time Password) for authentication.
  4. Finally, hit LOGIN_URL using session.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:

  1. 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',
    }
    
  2. erp_creds: ERP Login Credentials file, which is imported.
    import erpcreds
    
  3. OTP_WAIT_INTERVAL: Interval after which api checks continuously for new OTP mail.
  4. session: requests.Session() object, to persist the session parameters throughout the workflow.
    import requests
    
    session = requests.Session()
    

Output

  1. The function returns:
  2. It also modifies the session object which now contains parameters for the logged in session, which can be used for further navigation in ERP.
  3. 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

  1. 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.

  2. 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 with readonly argument

      python3 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
    • token.json will be generated in same folder as that of credentials.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.

  1. Install the package.

    pip install iitkgp_erp_login
    
  2. Make sure erpcreds.py & token.json exist in the same directory as that of the script we are about to create.

  3. 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)
    
  4. Run the script

    python3 open_erp.py
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iitkgp_erp_login-1.0.0.tar.gz (10.7 kB view hashes)

Uploaded Source

Built Distribution

iitkgp_erp_login-1.0.0-py3-none-any.whl (8.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page