Exemplo n.º 1
0
def generatejwt(d):
    #Create JWT
    print("inside jwt creation function")
    s = 0
    f = None
    t = None #message to front end
    response = None
    res_to_send = 'fail'
    print(d)
    con, cur, s1, f1 = db.mydbopncon()
    s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
    s1, f1 = 0, None
    print("DB connection established", s,f,t)
    
    natseckey = "secret"
    
    if s <= 0:
        command = cur.mogrify("""
                                SELECT json_agg(a) FROM (
                                SELECT secretcode,seccdid FROM ncapp.secrettkn 
                                WHERE entityid = %s AND countryid =%s
                                ) as a
                            """,(d["ei"], d["ci"]))
        print(command)

        cur, s1, f1 = db.mydbfunc(con,cur,command)
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print('----------------')
        print(s)
        print(f)
        print('----------------')
        if s > 0:
            s, f, t = errhand.get_status(s, 200, f, "secret fetch failed with DB error", t, "no")
    print(s,f)
    
    db_rec = None
    if s <= 0:
        db_rec = cur.fetchall()[0][0]
        
    
        if len(db_rec) < 1:
            s, f, t= errhand.get_status(s, 100, f, "Unable to get secret", t, "no")            
        else:
            print(db_rec)
            db_rec = db_rec[0]
            print("got the secret string successfully")
            pass            
    
    print(s,f)

    if s <= 0:
        secretcode = db_rec.get("secretcode", None)
        if secretcode == None:
            s, f, t = errhand.get_status(s, 200, f, "unable to get secret code", t, "no")

        seccdid = db_rec.get("seccdid", None)
        if seccdid == None:
            s, f, t = errhand.get_status(s, 200, f, "unable to get secret code id", t, "no")
    print("@@@@@@@@@@@@@")
    print(secretcode)
    print("@@@@@@@@@@@@@")
    if s <= 0:
        #Call JWT to generate JWT START
        natjwt =  jwt.encode(
                            { 
                              "iss": "ncj",
                              "exp": d["exp"],
                              "iat": datetime.now().strftime('%d%m%Y%H%M%S%f'),                            
                              "passtkn": d["passtkn"],
                              "skd": seccdid,
                              "eid": d["ei"], 
                              "cid": d["ci"],
                              "ncuserid": d["ncuserid"]
                            }, 
                            secretcode, 
                            algorithm='HS256')          
    print("printing nat jwt")
    print(natjwt)
    #Call JWT to generate JWT END
    db.mydbcloseall(con,cur)
    return (json.dumps({"ncjwt" :natjwt.decode("utf-8")}))
Exemplo n.º 2
0
def app_appauth(criteria_json):
    print("inside appauth function")
    s = 0
    f = None
    t = None  #message to front end
    response = None
    res_to_send = 'fail'
    payload1 = criteria_json.get("payload", None)
    print(criteria_json)
    print(payload1)
    print(s)
    if s <= 0:
        if criteria_json.get("entityid", None) != None:
            origin_entityid = criteria_json['entityid']
        else:
            origin_entityid = None
            s, f, t = errhand.get_status(s, 100, f,
                                         "Origin entity id not provided", t,
                                         "yes")

        if criteria_json.get("cntryid", None) != None:
            origin_cntryid = criteria_json['cntryid']
        else:
            origin_cntryid = None
            s, f, t = errhand.get_status(s, 100, f,
                                         "Origin cntry code is not provided",
                                         t, "yes")

        print('origin', origin_entityid, origin_cntryid)

        #Use the installed entity and country code for further operatios
        entityid = config.INSTALLDATA[config.LIVE]["entityid"]
        cntryid = config.INSTALLDATA[config.LIVE]["countryid"]

        if payload1 == None:
            s, f, t = errhand.get_status(
                s, 100, f, "App data not sent.  Please try again", t, "yes")
        else:
            if payload1.get("userauthtkn", None) != None:
                userauthtkn = payload1['userauthtkn']
            else:
                userauthtkn = None
                s, f, t = errhand.get_status(
                    s, 100, f, "User login success authtkn not provided", t,
                    "yes")

            if payload1.get("appid", None) != None:
                appid = payload1['appid']
            else:
                appid = None
                s, f, t = errhand.get_status(s, 100, f, "app id not provided",
                                             t, "yes")

            if payload1.get("appkey", None) != None:
                appkey = payload1['appkey']
            else:
                appkey = None
                s, f, t = errhand.get_status(s, 100, f,
                                             "appkey is not provided", t,
                                             "yes")

            if payload1.get("redirecturi", None) != None:
                redirecturi = payload1['redirecturi']
            else:
                redirecturi = None
                s, f, t = errhand.get_status(s, 100, f,
                                             "redirecturi is not provided", t,
                                             "yes")
                # update or create are the values

    print(userauthtkn, appid, redirecturi, appkey, entityid, cntryid)

    if s <= 0:
        con, cur, s1, f1 = db.mydbopncon()
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print("DB connection established", s, f, t)

    if s <= 0:
        command = cur.mogrify(
            """
                                SELECT json_agg(a) FROM (
                                SELECT *
                                FROM ncapp.appdetail
                                WHERE delflg != 'Y' AND expirydate >= CURRENT_DATE
                                AND appid = %s AND appkey = %s AND redirecturi = %s
                                AND entityid = %s AND countryid = %s
                                ) as a
                            """, (
                appid,
                appkey,
                redirecturi,
                entityid,
                cntryid,
            ))
        print(command)
        cur, s1, f1 = db.mydbfunc(con, cur, command)
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print('----------------')
        print(s)
        print(f)
        print('----------------')
        if s > 0:
            s, f, t = errhand.get_status(
                s, 200, f, "App Name data fetch failed with DB error", t, "no")
    print(s, f)

    app_db_rec = None
    if s <= 0:
        app_db_rec = cur.fetchall()[0][0]
        print("get db details")
        print(app_db_rec)

        if len(app_db_rec) < 1:
            s, f, t = errhand.get_status(s, 100, f,
                                         "Unable to locate the app id", t,
                                         "yes")
        else:
            app_db_rec = app_db_rec[0]
            print("appauth.py line 161 App id identified successfully")
            print(app_db_rec)

    print(s, f)
    appuserid = app_db_rec.get("appuserid", None)
    '''
    if app_db_rec["appusertype"] == "D":
        useridts = appuserid
    elif app_db_rec["appusertype"] == "A":
        useridts = appuserid
    elif app_db_rec["appusertype"] == "P":
        useridts = appuserid
    elif app_db_rec["appusertype"] == "I":
        useridts = appuserid
    elif app_db_rec["appusertype"] == "T":
        useridts = appuserid
    '''

    if s <= 0:
        command = cur.mogrify(
            """
                            SELECT json_agg(a) FROM (
                            SELECT *
                            FROM ncusr.userauth
                            WHERE tknexpiry >= CURRENT_TIMESTAMP
                            AND userauthtkn = %s AND appid = %s
                            AND entityid = %s AND countryid = %s
                            ) as a
                        """, (
                userauthtkn,
                appid,
                entityid,
                cntryid,
            ))
        print(command)
        cur, s1, f1 = db.mydbfunc(con, cur, command)
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print('----------------')
        print(s)
        print(f)
        print('----------------')
        if s > 0:
            s, f, t = errhand.get_status(
                s, 200, f, "User authtoke fetch failed with DB error", t, "no")
    print(s, f)

    usr_db_rec = None
    if s <= 0:
        usr_db_rec = cur.fetchall()[0][0]
        print(usr_db_rec)

        if len(usr_db_rec) < 1:
            s, f, t = errhand.get_status(
                s, 100, f,
                "Unable to locate the user auth details OR Token expired", t,
                "yes")
        else:
            usr_db_rec = usr_db_rec[0]
            print("User auth token validated successfully")
            useridts = usr_db_rec["userid"]
    #We are ready to generate API pass token
    print(s, f)
    i = 0
    cur_time = datetime.now().strftime('%Y%m%d%H%M%S')
    passtknset = False
    pass_tkn = None

    while i < 50:
        r = ''.join(
            random.choice(string.ascii_letters + string.digits)
            for x in range(6))
        pass_tkn = create_signature("md5", "nirunidhapasstkn" + r,
                                    userauthtkn + cur_time, appuserid + appid)

        command = cur.mogrify(
            """
                                SELECT count(1)
                                FROM ncapp.appusrauth
                                WHERE passwordtkn = %s
                            """, (pass_tkn, ))
        print(command)
        cur, s1, f1 = db.mydbfunc(con, cur, command)
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print('----------------')
        print(s)
        print(f)
        print('----------------')
        if s > 0:
            s, f, t = errhand.get_status(
                s, 200, f, "Password token data fetch failed with DB error", t,
                "no")
        print(s, f)

        if s <= 0:
            db_rec = cur.fetchall()[0][0]
            print(db_rec)

            if db_rec > 0:
                s, f, t = errhand.get_status(
                    s, 100, f,
                    "Pass token Already exists. Retrying time: " + i, t, "no")
                i = i + 1
                continue
            else:
                print("Pass code is unique.  Generation task completed")
                passtknset = True
                break
        else:
            # Some error occured, so no point looping
            passtknset = False
            break

    print(s, f, t)

    appusrtype = app_db_rec.get("appusertype", None)
    if appusrtype == None:
        s, f, t = errhand.get_status(s, 200, f, "app user type is not known",
                                     t, "yes")

    if s <= 0 and passtknset:
        s1, f1 = db.mydbbegin(con, cur)
        print(s1, f1)

        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None

        if s <= 0:
            passexpiry = get_expiry_time(appusrtype)

            command = cur.mogrify(
                """
            INSERT into ncapp.appusrauth (userauthtkn,appid,passwordtkn,passwordtknexpiry,entityid,countryid,octime,lmtime)
            VALUES(%s, %s, %s, %s, %s, %s, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
            ON CONFLICT ON CONSTRAINT unq_comb_auauth
            DO
                UPDATE SET passwordtkn = %s, passwordtknexpiry = %s, lmtime = CURRENT_TIMESTAMP 
            """, (
                    userauthtkn,
                    appid,
                    pass_tkn,
                    passexpiry,
                    entityid,
                    cntryid,
                    pass_tkn,
                    passexpiry,
                ))
            print(command)

            cur, s1, f1 = db.mydbfunc(con, cur, command)
            s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
            s1, f1 = 0, None

            if s > 0:
                s, f, t = errhand.get_status(s, 200, f,
                                             "passtoken update failed", t,
                                             "no")

            print('Insert or update is successful')

        if s <= 0:
            con.commit()

    if s <= 0:
        data_for_jwt = {
            "exp": passexpiry.strftime('%d%m%Y%H%M%S'),
            "passtkn": pass_tkn,
            "ei": entityid,
            "ci": cntryid,
            "ncuserid": useridts
        }
        natjwt = jwtf.generatejwt(data_for_jwt)

    if s > 0:
        res_to_send = 'fail'
        result_date = []
        response = {
            'result_data': result_date,
            'status': res_to_send,
            'status_code': s,
            'usrmsg': errhand.error_msg_reporting(s, t)
        }
    else:
        res_to_send = 'success'
        result_date = natjwt
        response = {
            'result_data': result_date,
            'status': res_to_send,
            'status_code': 0,
            'usrmsg': "pass Token generation successful"
        }

    print(res_to_send, response)

    return (res_to_send, response)
Exemplo n.º 3
0
def fn_appname(criteria_json):
    print("inside fn_appname function")
    s = 0
    f = None
    t = None  #message to front end
    response = None
    res_to_send = 'fail'
    payload = criteria_json.get("payload", None)

    print(s)
    if s <= 0:
        if criteria_json.get("entityid", None) != None:
            entityid = criteria_json['entityid']
        else:
            entityid = None
            s, f, t = errhand.get_status(s, 100, f, "entity id not provided",
                                         t, "yes")

        if criteria_json.get("cntryid", None) != None:
            cntryid = criteria_json['cntryid']
        else:
            cntryid = None
            s, f, t = errhand.get_status(s, 100, f,
                                         "cntry code is not provided", t,
                                         "yes")

        if payload == None:
            s, f, t = errhand.get_status(
                s, 100, f, "App data not sent.  Please try again", t, "yes")
        else:
            if payload.get("appid", None) != None:
                appid = payload['appid']
            else:
                appid = None
                s, f, t = errhand.get_status(s, 100, f, "app id not provided",
                                             t, "yes")

            if payload.get("redirecturi", None) != None:
                redirecturi = payload['redirecturi']
            else:
                redirecturi = None
                s, f, t = errhand.get_status(s, 100, f,
                                             "redirecturi is not provided", t,
                                             "yes")
                # update or create are the values

    print(appid, redirecturi, entityid, cntryid)

    if s <= 0:
        con, cur, s1, f1 = db.mydbopncon()
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print("DB connection established", s, f, t)

    if s <= 0:
        command = cur.mogrify(
            """
                                SELECT json_agg(a) FROM (
                                SELECT *
                                FROM ncapp.appdetail
                                WHERE delflg != 'Y' AND expirydate >= CURRENT_DATE AND approved != 'N'
                                AND appid = %s AND redirecturi = %s
                                AND entityid = %s AND countryid = %s
                                ) as a
                            """, (
                appid,
                redirecturi,
                entityid,
                cntryid,
            ))
        print(command)
        cur, s1, f1 = db.mydbfunc(con, cur, command)
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print('----------------')
        print(s)
        print(f)
        print('----------------')
        if s > 0:
            s, f, t = errhand.get_status(
                s, 200, f, "App Name data fetch failed with DB error", t, "no")
    print(s, f)

    db_rec = None
    if s <= 0:
        db_rec = cur.fetchall()[0][0]
        print(db_rec)

        if db_rec == None or len(db_rec) > 1:
            s, f, t = errhand.get_status(s, 100, f,
                                         "Unable to locate the app id", t,
                                         "yes")
        else:
            db_rec = db_rec[0]
            print("auth.py line 136 App id identified successfully")
            pass

    print(s, f)
    if s > 0:
        res_to_send = 'fail'
        result_date = []
        response = {
            'result_data': result_date,
            'status': res_to_send,
            'status_code': s,
            'usrmsg': errhand.error_msg_reporting(s, t)
        }
    else:
        res_to_send = 'success'
        result_data = {"appname": db_rec["appname"]}
        response = {
            'result_data': result_data,
            'status': res_to_send,
            'status_code': 0,
            'usrmsg': "Token generation successful"
        }

    print(res_to_send, response)

    return (res_to_send, response)
Exemplo n.º 4
0
def app_userauth(criteria_json):
    # Generate a user auth token
    # input
    #   criteria_json = {
    #        "entityid" : entityid,
    #        "cntryid"  : cntryid,
    #        "payload" : payload  => {appid,redirecturi,userid,expiremin<tokenexipry in mins>}
    #   }
    # Output
    #    response = {
    #                'result_data' : result_data, => succ-> {'authtkn': auth_tkn} : err->[]
    #                'status': res_to_send, => success/fail
    #                'status_code': 0,
    #                'usrmsg': "Token generation successful" <=for success:  error msg in case of error
    #    }

    print("inside userauth function")
    s = 0
    f = None
    t = None  #message to front end
    response = None
    res_to_send = 'fail'
    payload = criteria_json.get("payload", None)

    print(s)
    if s <= 0:
        if criteria_json.get("entityid", None) != None:
            entityid = criteria_json['entityid']
        else:
            entityid = None
            s, f, t = errhand.get_status(s, 100, f, "entity id not provided",
                                         t, "yes")

        if criteria_json.get("cntryid", None) != None:
            cntryid = criteria_json['cntryid']
        else:
            cntryid = None
            s, f, t = errhand.get_status(s, 100, f,
                                         "cntry code is not provided", t,
                                         "yes")

        if payload == None:
            s, f, t = errhand.get_status(
                s, 100, f, "App data not sent.  Please try again", t, "yes")
        else:
            if payload.get("appid", None) != None:
                appid = payload['appid']
            else:
                appid = None
                s, f, t = errhand.get_status(s, 100, f, "app id not provided",
                                             t, "yes")

            if payload.get("redirecturi", None) != None:
                redirecturi = payload['redirecturi']
            else:
                redirecturi = None
                s, f, t = errhand.get_status(s, 100, f,
                                             "redirecturi is not provided", t,
                                             "yes")

            if payload.get("userid", None) != None:
                userid = payload['userid']
            else:
                userid = None
                s, f, t = errhand.get_status(s, 100, f,
                                             "userid is not provided", t,
                                             "yes")

            expiremin = payload.get("expiremin", None)

    print(appid, redirecturi, entityid, cntryid, userid)

    if s <= 0:
        con, cur, s1, f1 = db.mydbopncon()
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print("DB connection established", s, f, t)

    if s <= 0:
        command = cur.mogrify(
            """
                                SELECT json_agg(a) FROM (
                                SELECT *
                                FROM ncapp.appdetail
                                WHERE delflg != 'Y' AND expirydate >= CURRENT_DATE AND approved != 'N'
                                AND appid = %s AND redirecturi = %s
                                AND entityid = %s AND countryid = %s
                                ) as a
                            """, (
                appid,
                redirecturi,
                entityid,
                cntryid,
            ))
        print(command)
        cur, s1, f1 = db.mydbfunc(con, cur, command)
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print('----------------')
        print(s)
        print(f)
        print('----------------')
        if s > 0:
            s, f, t = errhand.get_status(
                s, 200, f, "App Name data fetch failed with DB error", t, "no")
    print(s, f)

    app_db_rec = None
    if s <= 0:
        app_db_rec = cur.fetchall()[0][0]
        print(app_db_rec)
        if app_db_rec != None:
            print(len(app_db_rec))

        if app_db_rec == None or len(app_db_rec) < 1:
            s, f, t = errhand.get_status(s, 100, f,
                                         "Unable to locate the app id", t,
                                         "yes")
        else:
            app_db_rec = app_db_rec[0]
            print("auth.py line 319 App id identified successfully")
            pass

    print(s, f)

    #appuserid = app_db_rec.get("appuserid", None)
    if s <= 0:
        command = cur.mogrify(
            """
                            SELECT json_agg(a) FROM (
                            SELECT *
                            FROM ncusr.userlogin
                            WHERE userid = %s
                            AND entityid = %s AND countryid = %s
                            ) as a
                        """, (
                userid,
                entityid,
                cntryid,
            ))
        print(command)
        cur, s1, f1 = db.mydbfunc(con, cur, command)
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print('----------------')
        print(s)
        print(f)
        print('----------------')
        if s > 0:
            s, f, t = errhand.get_status(s, 200, f,
                                         "User fetch failed with DB error", t,
                                         "no")
    print(s, f)

    usr_db_rec = None
    if s <= 0:
        usr_db_rec = cur.fetchall()[0][0]
        print(usr_db_rec)

        if usr_db_rec == None or len(usr_db_rec) < 1:
            s, f, t = errhand.get_status(s, 100, f,
                                         "Unable to locate the user details",
                                         t, "yes")
        else:
            usr_db_rec = usr_db_rec[0]
            print("User details fetch successfull")
            pass

    if s <= 0:
        if usr_db_rec["userstatus"] == 'B':
            #B-Blocked , I-Deleteduser
            s, f, t = errhand.get_status(s, 100, f, "User is blocked", t,
                                         "yes")
        elif usr_db_rec["userstatus"] == 'I':
            #B-Blocked , I-Deleteduser
            s, f, t = errhand.get_status(s, 100, f, "User is Deleted", t,
                                         "yes")

    #We are ready to generate API pass token
    print(s, f)

    if s <= 0:
        i = 0
        cur_time = datetime.now().strftime('%Y%m%d%H%M%S')
        authtknset = False
        auth_tkn = None

        while i < 50:
            r = ''.join(
                random.choice(string.ascii_letters + string.digits)
                for x in range(6))
            auth_tkn = create_signature("md5", "nirunidhausrtkn" + r,
                                        userid + cur_time, appid + cur_time)

            command = cur.mogrify(
                """
                                    SELECT count(1)
                                    FROM ncusr.userauth
                                    WHERE userauthtkn = %s
                                """, (auth_tkn, ))
            print(command)
            cur, s1, f1 = db.mydbfunc(con, cur, command)
            s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
            s1, f1 = 0, None
            print('----------------')
            print(s)
            print(f)
            print('----------------')
            if s > 0:
                s, f, t = errhand.get_status(
                    s, 200, f, "auth token data fetch failed with DB error", t,
                    "no")
            print(s, f)

            if s <= 0:
                db_rec = cur.fetchall()[0][0]
                print(db_rec)

                if db_rec > 0:
                    s, f, t = errhand.get_status(
                        s, 100, f,
                        "auth token Already exists. Retrying time: " + i, t,
                        "no")
                    i = i + 1
                    continue
                else:
                    print("Auth token is unique.  Generation task completed")
                    authtknset = True
                    break
            else:
                # Some error occured, so no point looping
                authtknset = False
                break

    print(s, f, t)

    appusrtype = None if app_db_rec == None else app_db_rec.get(
        "appusertype", None)

    if appusrtype == None:
        s, f, t = errhand.get_status(s, 200, f, "app user type is not known",
                                     t, "yes")

    if s <= 0 and authtknset:
        s1, f1 = db.mydbbegin(con, cur)
        print(s1, f1)

        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None

        if s <= 0:
            passexpiry = get_expiry_time("authtkn", appusrtype, expiremin)
            # VALUES(%s, %s, %s, %(timestamp)s, %s, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
            command = cur.mogrify(
                """
                        INSERT into ncusr.userauth (userid,appid,userauthtkn,tknexpiry,entityid,countryid,octime,lmtime)
                        VALUES(%s, %s, %s, %s, %s, %s, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
                        ON CONFLICT ON CONSTRAINT unq_comb_uauth
                        DO
                            UPDATE SET userauthtkn = %s, tknexpiry = %s, lmtime = CURRENT_TIMESTAMP 
                        """, (
                    userid,
                    appid,
                    auth_tkn,
                    passexpiry,
                    entityid,
                    cntryid,
                    auth_tkn,
                    passexpiry,
                ))
            print(command)

            cur, s1, f1 = db.mydbfunc(con, cur, command)
            s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
            s1, f1 = 0, None

            if s > 0:
                s, f, t = errhand.get_status(s, 200, f,
                                             "authtoken update failed", t,
                                             "no")

            print('Insert or update is successful')

        if s <= 0:
            con.commit()

    if s > 0:
        res_to_send = 'fail'
        result_data = []
        response = {
            'result_data': result_data,
            'status': res_to_send,
            'status_code': s,
            'usrmsg': errhand.error_msg_reporting(s, t)
        }
    else:
        res_to_send = 'success'
        result_data = {'authtkn': auth_tkn}
        response = {
            'result_data': result_data,
            'status': res_to_send,
            'status_code': 0,
            'usrmsg': "Token generation successful"
        }

    print(res_to_send, response)

    return (res_to_send, response)
Exemplo n.º 5
0
def app_register(criteria_json):
    print("inside login GET")
    s = 0
    f = None
    t = None  #message to front end
    response = None
    res_to_send = 'fail'
    payload = criteria_json.get("payload", None)

    print(s)
    if s <= 0:
        if criteria_json.get("userid", None) != None:
            userid = criteria_json['userid']
        else:
            userid = None
            s, f, t = errhand.get_status(s, 100, f, "user id not provided", t,
                                         "yes")

        if criteria_json.get("entityid", None) != None:
            entityid = criteria_json['entityid']
        else:
            entityid = None
            s, f, t = errhand.get_status(s, 100, f, "entity id not provided",
                                         t, "yes")

        if criteria_json.get("cntryid", None) != None:
            cntryid = criteria_json['cntryid']
        else:
            cntryid = None
            s, f, t = errhand.get_status(s, 100, f,
                                         "cntry code is not provided", t,
                                         "yes")

        if payload == None:
            s, f, t = errhand.get_status(
                s, 100, f, "App data not sent.  Please try again", t, "yes")
        else:
            if payload.get("appname", None) != None:
                appname = payload['appname']
            else:
                appname = None
                s, f, t = errhand.get_status(s, 100, f, "No App name provided",
                                             t, "yes")

            if payload.get("appusertype", None) != None:
                appusertype = payload['appusertype']
            else:
                appusertype = None
                s, f, t = errhand.get_status(s, 100, f,
                                             "App user type not provided", t,
                                             "yes")

            if payload.get("redirecturi", None) != None:
                redirecturi = payload['redirecturi']
            else:
                redirecturi = None
                s, f, t = errhand.get_status(s, 100, f,
                                             "Redirect URI not provided", t,
                                             "yes")

            if payload.get("postbackuri", None) != None:
                postbackuri = payload['postbackuri']
            else:
                postbackuri = None
                s, f, t = errhand.get_status(s, 0, f,
                                             "postbackuri not provided", t,
                                             "no")

            if payload.get("description", None) != None:
                description = payload['description']
            else:
                description = None
                s, f, t = errhand.get_status(s, -100, f,
                                             "description not provided", t,
                                             "no")

            if payload.get("starmfdet", None) != None:
                starmfdet = payload['starmfdet']
            else:
                starmfdet = None
                if appusertype not in ['D', 'A']:
                    s, f, t = errhand.get_status(s, -100, f,
                                                 "star mf data not provided",
                                                 t, "yes")
                else:
                    s, f, t = errhand.get_status(s, -100, f,
                                                 "star mf data not provided",
                                                 t, "no")

            if payload.get("product", None) != None:
                product = payload['product']
            else:
                product = None
                s, f, t = errhand.get_status(s, -100, f,
                                             "product not provided", t, "no")

            if payload.get("operation", None) != None:
                operation = payload['operation']
            else:
                operation = None
                s, f, t = errhand.get_status(s, -100, f,
                                             "operation not provided", t, "no")
            # update or create are the values

            if operation == "delete" or operation == "update":
                if payload.get("appid", None) != None:
                    appid = payload['appid']
                else:
                    appid = None
                    s, f, t = errhand.get_status(s, -100, f,
                                                 "appid not provided", t, "no")

                if payload.get("appkey", None) != None:
                    appkey = payload['appkey']
                else:
                    appkey = None
                    s, f, t = errhand.get_status(s, -100, f,
                                                 "appkey not provided", t,
                                                 "no")
            else:
                appid = None
                appkey = None

        if appusertype == 'T':
            approved = 'N'
        else:
            approved = 'Y'

    print(appid, "oiipoi", appkey)
    cur_time = datetime.now().strftime('%Y%m%d%H%M%S')
    print(appname, appusertype, redirecturi, postbackuri, description,
          starmfdet)

    if s <= 0:
        con, cur, s1, f1 = db.mydbopncon()
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print("connection statment done", s, f, t)

    if s <= 0:
        command = cur.mogrify(
            """
                                SELECT count(1)
                                FROM ncapp.appdetail a
                                WHERE delflg != 'Y'
                                AND (
                                        appname = %s
                                    )
                                AND appuserid = %s AND entityid = %s AND countryid = %s
                            """, (
                appname,
                userid,
                entityid,
                cntryid,
            ))
        print(command)
        cur, s1, f1 = db.mydbfunc(con, cur, command)
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print('----------------')
        print(s)
        print(f)
        print('----------------')
        if s > 0:
            s, f, t = errhand.get_status(
                s, 200, f, "App Name data fetch failed with DB error", t, "no")
    print(s, f)

    if s <= 0:
        db_rec = cur.fetchall()[0][0]
        print(db_rec)

        if db_rec > 0:
            if operation == "create":
                s, f, t = errhand.get_status(
                    s, 100, f, "App name Already exists for this user", t,
                    "yes")

        else:
            if operation == "update" or operation == "delete":
                s, f, t = errhand.get_status(
                    s, 100, f, "App name doesn't exists for this user", t,
                    "yes")
            print("no records satifying the current user inputs")
    print(s, f)

    appikset = False
    i = 0
    if s <= 0 and operation == "create":
        while i < 50:
            r = ''.join(
                random.choice(string.ascii_letters + string.digits)
                for x in range(6))
            appid = create_signature("sha256", "nirunidhaappid" + r,
                                     userid + cur_time, userid)
            appkey = create_signature("md5", "nirunidhaappkey" + r,
                                      userid + cur_time, userid)

            command = cur.mogrify(
                """
                                    SELECT count(1)
                                    FROM ncapp.appdetail
                                    WHERE delflg != 'Y'
                                    AND (
                                            appid = %s OR appkey = %s
                                        )
                                """, (
                    appid,
                    appkey,
                ))
            print(command)
            cur, s1, f1 = db.mydbfunc(con, cur, command)
            s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
            s1, f1 = 0, None
            print('----------------')
            print(s)
            print(f)
            print('----------------')
            if s > 0:
                s, f, t = errhand.get_status(
                    s, 200, f, "App Name data fetch failed with DB error", t,
                    "no")
            print(s, f)

            if s <= 0:
                db_rec = cur.fetchall()[0][0]
                print(db_rec)

                if db_rec > 0:
                    s, f, t = errhand.get_status(
                        s, 100, f,
                        "Appid or key Already exists for retrying time: " + i,
                        t, "no")
                    i = i + 1
                    continue
                else:
                    print("no records satifying the current user inputs")
                    appikset = True
                    break
            else:
                # Some error occured, so no point looping
                appikset = False
                break
    print(s, f, t)

    if s <= 0 and operation == "create" and appikset:
        s1, f1 = db.mydbbegin(con, cur)
        print(s1, f1)

        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None

        if s <= 0:
            command = cur.mogrify(
                """
                        INSERT INTO ncapp.appdetail (appname, appusertype, redirecturi, postbackuri, description, starmfdet, appid, appkey, expirydate, approved, product, delflg, appuserid, octime, lmtime, entityid, countryid) 
                        VALUES (%s,%s,%s,%s,%s,%s,%s,%s,CURRENT_DATE + INTERVAL'1 month', %s, %s, 'N',%s,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,%s,%s);
                        """, (
                    appname,
                    appusertype,
                    redirecturi,
                    postbackuri,
                    description,
                    starmfdet,
                    appid,
                    appkey,
                    approved,
                    product,
                    userid,
                    entityid,
                    cntryid,
                ))
            print(command)
            print(appname, appusertype, redirecturi, postbackuri, description,
                  starmfdet, userid)
            cur, s1, f1 = db.mydbfunc(con, cur, command)
            s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
            s1, f1 = 0, None

            if s > 0:
                s, f, t = errhand.get_status(s, 200, f, "SIGNUP update failed",
                                             t, "no")

            print('Insert or update is successful')

        if s <= 0:
            con.commit()
            #validate PAN adn store PAN number

    if s <= 0 and operation == "update":
        s1, f1 = db.mydbbegin(con, cur)
        print(s1, f1)

        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None

        if s <= 0:
            command = cur.mogrify(
                """
                        UPDATE ncapp.appdetail SET redirecturi = %s, postbackuri = %s, description = %s, starmfdet = %s, lmtime = CURRENT_TIMESTAMP
                        WHERE  appname = %s AND appusertype = %s AND appid =%s AND appkey = %s AND product = %s AND appuserid = %s AND entityid = %s AND countryid = %s;
                        """, (
                    redirecturi,
                    postbackuri,
                    description,
                    starmfdet,
                    appname,
                    appusertype,
                    appid,
                    appkey,
                    product,
                    userid,
                    entityid,
                    cntryid,
                ))
            print(command)
            print(appname, appusertype, redirecturi, postbackuri, description,
                  starmfdet, userid)
            cur, s1, f1 = db.mydbfunc(con, cur, command)
            s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
            s1, f1 = 0, None

            if s > 0:
                s, f, t = errhand.get_status(s, 200, f,
                                             "APP details update failed", t,
                                             "no")

            print('Insert or update is successful')

        if s <= 0:
            con.commit()
            print("commit done")
            #validate PAN adn store PAN number

    if s <= 0 and operation == "delete":
        s1, f1 = db.mydbbegin(con, cur)
        print(s1, f1)

        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None

        if s <= 0:
            command = cur.mogrify(
                """
                        UPDATE ncapp.appdetail SET delflg = 'Y', lmtime = CURRENT_TIMESTAMP
                        WHERE  appname = %s AND appusertype = %s AND appid =%s AND appkey = %s AND product = %s AND appuserid = %s AND entityid = %s AND countryid = %s;
                        """, (
                    appname,
                    appusertype,
                    appid,
                    appkey,
                    product,
                    userid,
                    entityid,
                    cntryid,
                ))
            print(command)
            print(appname, appusertype, redirecturi, postbackuri, description,
                  starmfdet, userid)
            cur, s1, f1 = db.mydbfunc(con, cur, command)
            s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
            s1, f1 = 0, None

            if s > 0:
                s, f, t = errhand.get_status(s, 200, f,
                                             "APP details update failed", t,
                                             "no")

            print('Insert or update is successful')

        if s <= 0:
            con.commit()
            #validate PAN adn store PAN number
    usrmg_fstr = None
    if s > 0:
        res_to_send = 'fail'
        result_date = []
        response = {
            'result_data': result_date,
            'status': res_to_send,
            'status_code': s,
            'usrmsg': errhand.error_msg_reporting(s, t)
        }
    else:
        res_to_send = 'success'
        result_date = [{'appname': appname, 'appid': appid}]
        print("**********************")
        print(operation)
        print("**********************")
        if operation == "create":
            usrmg_fstr = ") creation is successful"
        elif operation == "update":
            usrmg_fstr = ") updation is successful"
        elif operation == "delete":
            usrmg_fstr = ") deletion is successful"

        response = {
            'result_data': result_date,
            'status': res_to_send,
            'status_code': 0,
            'usrmsg': 'App (' + appname + usrmg_fstr
        }

    print(res_to_send, response)

    return (res_to_send, response)
Exemplo n.º 6
0
def app_detail_fetch(criteria_json):
    # payload = {'appid': xyz, 'login': <[noauth] to get data without user id>}
    # entity id and country id will come in header which are mandator. user id comes in jwt
    # Output =  { 'result_data' : [success -> ncapp.appdetail] [Failure -> ""]
    #             'status': success/fail,  'status_code': 0,     'usrmsg': ''/error message }
    print("inside app_detail_fetch common function")
    s = 0
    f = None
    t = None  #message to front end
    payload = criteria_json.get("payload", None)
    print(payload)

    if s <= 0:
        if payload == None:
            appid = None
            login = None
            # s, f, t= errhand.get_status(s, 100, f, "no payload provided", t, "yes")
        else:
            if payload.get("appid", None) != None:
                appid = payload['appid']
            else:
                appid = None

            if payload.get("login", None) != None:
                login = payload['login']
            else:
                login = None
        print(appid, login, s)

    if s <= 0:
        if criteria_json.get("entityid", None) != None:
            entityid = criteria_json['entityid']
        else:
            entityid = None
            s, f, t = errhand.get_status(s, 100, f, "entity id not provided",
                                         t, "yes")

        if criteria_json.get("cntryid", None) != None:
            cntryid = criteria_json['cntryid']
        else:
            cntryid = None
            s, f, t = errhand.get_status(s, 100, f,
                                         "cntry code is not provided", t,
                                         "yes")

        if login != "nologin":
            if criteria_json.get("userid", None) != None:
                userid = criteria_json['userid']
            else:
                # To get app details before login for entity and cntry
                userid = None
                s, f, t = errhand.get_status(s, 100, f, "user id not provided",
                                             t, "yes")
        else:
            userid = None

    if s <= 0:
        con, cur, s1, f1 = db.mydbopncon()
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None

    if s <= 0:
        if appid == None:
            command = cur.mogrify(
                """
                                    SELECT json_agg(a) FROM (
                                    SELECT *,
                                            CASE 
                                            WHEN expirydate < CURRENT_TIMESTAMP THEN 'EXPIRED'
                                            ELSE 'ACTIVE'
                                            END AS appexp
                                    FROM ncapp.appdetail                                
                                    WHERE appuserid = %s AND entityid = %s AND countryid = %s
                                    AND delflg = 'N'
                                    ) as a
                                """, (
                    userid,
                    entityid,
                    cntryid,
                ))
        elif userid == None:
            command = cur.mogrify(
                """
                                    SELECT json_agg(a) FROM (
                                    SELECT *,
                                            CASE 
                                            WHEN expirydate < CURRENT_TIMESTAMP THEN 'EXPIRED'
                                            ELSE 'ACTIVE'
                                            END AS appexp                                    
                                    FROM ncapp.appdetail                                
                                    WHERE appid = %s AND entityid = %s AND countryid = %s
                                    AND delflg = 'N'
                                    ) as a
                                """, (
                    appid,
                    entityid,
                    cntryid,
                ))
        else:
            command = cur.mogrify(
                """
                                    SELECT json_agg(a) FROM (
                                    SELECT *,
                                            CASE 
                                            WHEN expirydate < CURRENT_TIMESTAMP THEN 'EXPIRED'
                                            ELSE 'ACTIVE'
                                            END AS appexp                                    
                                    FROM ncapp.appdetail                                
                                    WHERE appuserid = %s AND entityid = %s AND countryid = %s AND appid = %s
                                    AND delflg = 'N'
                                    ) as a
                                """, (
                    userid,
                    entityid,
                    cntryid,
                    appid,
                ))

        print(command)
        cur, s1, f1 = db.mydbfunc(con, cur, command)
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print('----------------')
        print(s)
        print(f)
        print('----------------')
        if s > 0:
            s, f, t = errhand.get_status(
                s, 200, f, "APP data fetch failed with DB error", t, "no")
    print(s, f)

    if s <= 0:
        db_json_rec = cur.fetchall()[0][0]
        print(db_json_rec)

    if s > 0:
        res_to_send = 'fail'
        response = {
            'result_data': "",
            'status': res_to_send,
            'status_code': s,
            'usrmsg': errhand.error_msg_reporting(s, t)
        }
    else:
        res_to_send = 'success'
        response = {
            'result_data': db_json_rec,
            'status': res_to_send,
            'status_code': 0,
            'usrmsg': ''
        }

    print(res_to_send, response)

    return (res_to_send, response)
Exemplo n.º 7
0
def fetch_app_data_only_wth_tkn(criteria_json):
    # Description : Fetch app data
    # Functional use :
    # Called from : appfuncs.py->ncappfetchfrmtkn
    # Request data <criteria_json>:
    # criteria_json = {"entityid": entityid, "countryid": countryid, "payload": <as per below>}
    #         payload = {"userauthtkn": callback_data["regdata"], "appid": settings.NCAPPID,"appkey":settings.NCAPPKEY}
    # Response from this endpoint:
    #     Field Name         success                     fail
    # -----------------------------------------------------------
    #  {
    #    "userauthtkn":  new_userauthtkn,                BLANK
    #     "tknexpiry":   usr_db_rec["tknexpiry"],        BLANK
    #     "userid":      more_usr_db_rec["userid"],      BLANK
    #     "username":    more_usr_db_rec["username"],    BLANK
    #     "emailid":     more_usr_db_rec["sinupemail"],  BLANK
    #     "status":      success                         fail
    #     "msg":         BLANK                           fail message
    #   }
    # called functions: None

    print("inside fetch_app_data_only_wth_tkn function")
    s = 0
    f = None
    t = None  #message to front end
    print(criteria_json)
    payload = criteria_json.get("payload", None)
    print(payload)

    if payload == None:
        appid = None
        appkey = None
        userauthtkn = None

        # s, f, t= errhand.get_status(s, 100, f, "no payload provided", t, "yes")
    else:
        if payload.get("appid", None) != None:
            appid = payload['appid']
        else:
            appid = None
            s, f, t = errhand.get_status(s, 100, f, "appid not provided", t,
                                         "yes")

        if payload.get("appkey", None) != None:
            appkey = payload['appkey']
        else:
            appkey = None
            s, f, t = errhand.get_status(s, 100, f, "appkey not provided", t,
                                         "yes")

        if payload.get("userauthtkn", None) != None:
            userauthtkn = payload['userauthtkn']
        else:
            userauthtkn = None
            s, f, t = errhand.get_status(s, 100, f,
                                         "usertoken is not provided", t, "yes")
    print(appid, appkey, userauthtkn)

    if s <= 0:
        con, cur, s1, f1 = db.mydbopncon()
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print("DB connection established", s, f, t)

    if s <= 0:
        command = cur.mogrify(
            """
                                SELECT json_agg(a) FROM (
                                SELECT *
                                FROM ncusr.userauth
                                WHERE tknexpiry >= current_timestamp
                                AND appid = %s AND userauthtkn = %s
                                AND entityid = %s AND countryid = %s
                                ) as a
                            """, (
                appid,
                userauthtkn,
                config.INSTALLDATA[config.LIVE]["entityid"],
                config.INSTALLDATA[config.LIVE]["countryid"],
            ))
        print(command)
        cur, s1, f1 = db.mydbfunc(con, cur, command)
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print('----------------')
        print(s)
        print(f)
        print('----------------')
        if s > 0:
            s, f, t = errhand.get_status(
                s, 200, f, "User auth token data fetch failed with DB error",
                t, "no")
    print(s, f)

    usr_db_rec = None
    if s <= 0:
        usr_db_rec = cur.fetchall()[0][0]
        print(usr_db_rec)
        if usr_db_rec != None:
            print(len(usr_db_rec))

        if usr_db_rec == None or len(usr_db_rec) < 1:
            s, f, t = errhand.get_status(s, 100, f,
                                         "User auth token is not valid", t,
                                         "yes")
        else:
            usr_db_rec = usr_db_rec[0]
            print("Userauth token verified successfully")
            pass

    print(s, f)

    if s <= 0:
        command = cur.mogrify(
            """
                                SELECT json_agg(a) FROM (
                                SELECT *
                                FROM ncapp.appdetail
                                WHERE appid = %s AND appkey = %s
                                AND entityid = %s AND countryid = %s
                                AND delflg != 'Y'
                                ) as a
                            """, (
                appid,
                appkey,
                config.INSTALLDATA[config.LIVE]["entityid"],
                config.INSTALLDATA[config.LIVE]["countryid"],
            ))
        print(command)
        cur, s1, f1 = db.mydbfunc(con, cur, command)
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print('----------------')
        print(s)
        print(f)
        print('----------------')
        if s > 0:
            s, f, t = errhand.get_status(
                s, 200, f, "User auth token data fetch failed with DB error",
                t, "no")
    print(s, f)

    app_db_rec = None
    if s <= 0:
        app_db_rec = cur.fetchall()[0][0]
        print(app_db_rec)
        if app_db_rec != None:
            print(len(app_db_rec))

        if app_db_rec == None or len(app_db_rec) < 1:
            s, f, t = errhand.get_status(s, 100, f, "App id is not valid", t,
                                         "yes")
        else:
            app_db_rec = app_db_rec[0]
            if app_db_rec["approved"] == 'N':
                s, f, t = errhand.get_status(s, 100, f,
                                             "App id not approved yet", t,
                                             "yes")
            else:
                print("App id verified successfully")

    print(s, f)

    if s <= 0:
        command = cur.mogrify(
            """
                                SELECT json_agg(a) FROM (
                                SELECT *
                                FROM ncusr.userdetails a, ncusr.userlogin b
                                WHERE a.userid = %s AND a.entityid = %s AND a.countryid = %s
                                AND a.userid = b.userid AND a.entityid = b.entityid AND a.countryid = b.countryid
                                ) as a
                            """, (
                usr_db_rec["userid"],
                config.INSTALLDATA[config.LIVE]["entityid"],
                config.INSTALLDATA[config.LIVE]["countryid"],
            ))
        print(command)
        cur, s1, f1 = db.mydbfunc(con, cur, command)
        s, f, t = errhand.get_status(s, s1, f, f1, t, "no")
        s1, f1 = 0, None
        print('----------------')
        print(s)
        print(f)
        print('----------------')
        if s > 0:
            s, f, t = errhand.get_status(
                s, 200, f, "User details data fetch failed with DB error", t,
                "no")
    print(s, f)

    more_usr_db_rec = None
    if s <= 0:
        more_usr_db_rec = cur.fetchall()[0][0]
        print(more_usr_db_rec)
        print(len(more_usr_db_rec))

        if more_usr_db_rec == None or len(more_usr_db_rec) < 1:
            s, f, t = errhand.get_status(
                s, 100, f,
                "User details not available for the given auth token", t,
                "yes")
        else:
            more_usr_db_rec = more_usr_db_rec[0]
            print("user details fetched successfully")
            pass

    print(s, f)

    if s <= 0:
        #Validate the user status
        if more_usr_db_rec["userstatus"] == 'B':
            #B-Blocked , I-Deleteduser
            s, f, t = errhand.get_status(s, 100, f, "User is blocked", t,
                                         "yes")
        elif more_usr_db_rec["userstatus"] == 'I':
            #B-Blocked , I-Deleteduser
            s, f, t = errhand.get_status(s, 100, f, "User is Deleted", t,
                                         "yes")

    if s <= 0:
        data_to_auth_tkn = {
            "entityid": config.INSTALLDATA[config.LIVE]["entityid"],
            "cntryid": config.INSTALLDATA[config.LIVE]["countryid"],
            "payload": {
                "appid": app_db_rec["appid"],
                "redirecturi": app_db_rec["redirecturi"],
                "userid": more_usr_db_rec["userid"]
            }
        }

        ath_tkn_status, ath_tkn_detail = myauth.app_userauth(data_to_auth_tkn)
        print("new ath_tkn_detail")
        print(ath_tkn_detail)

        if ath_tkn_status == "success":
            s, f, t = errhand.get_status(s, 0, f,
                                         "User auth token regenerated", t,
                                         "no")
            new_userauthtkn = ath_tkn_detail["result_data"]["authtkn"]
            print(new_userauthtkn)
        else:
            s, f, t = errhand.get_status(
                s, 100, f, "error in User auth token regeneration", t, "no")
            new_userauthtkn = None

    res_status = None
    if s <= 0:
        res_status = "success"
        user_auth_detais = {
            "userauthtkn": new_userauthtkn,
            "tknexpiry": usr_db_rec["tknexpiry"],
            "userid": more_usr_db_rec["userid"],
            "username": more_usr_db_rec["username"],
            "emailid": more_usr_db_rec["sinupemail"],
            "status": res_status,
            "msg": ""
        }
    else:
        res_status = "fail"
        user_auth_detais = {
            "userauthtkn": "",
            "userid": "",
            "username": "",
            "emailid": "",
            "status": res_status,
            "msg": t
        }
    print("rached end")
    return res_status, user_auth_detais