Fixed auth bug
This commit is contained in:
		@@ -53,4 +53,5 @@ secret = ""
 | 
			
		||||
prd_instance_id = 1
 | 
			
		||||
product_guid = "asdf"
 | 
			
		||||
product_name = "EFT"
 | 
			
		||||
product_version ="8.1.0.9"
 | 
			
		||||
product_version ="8.1.0.9"
 | 
			
		||||
prd_ext_tenant_id = "e71851c2-593f-4f49-9c07-91727b1be94b"
 | 
			
		||||
							
								
								
									
										8
									
								
								inex.py
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								inex.py
									
									
									
									
									
								
							@@ -44,6 +44,7 @@ class Inex:
 | 
			
		||||
                self.logPath = self.config["logging"]["logPath"]
 | 
			
		||||
                self.logLevel = self.config["logging"]["logLevel"]
 | 
			
		||||
                self.prdInstanceID = self.config["immutables"]["prd_instance_id"]
 | 
			
		||||
                self.prd_ext_tenant_id = self.config["immutables"]["prd_ext_tenant_id"]
 | 
			
		||||
                self.productGUID = self.config["immutables"]["product_guid"]
 | 
			
		||||
                self.productName = self.config["immutables"]["product_name"]
 | 
			
		||||
                self.productVersion = self.config["immutables"]["product_version"]
 | 
			
		||||
@@ -74,10 +75,11 @@ class Inex:
 | 
			
		||||
        self.data = self.ic.inexSql.databaseQuery(self, self.cursor, self.sq.sqlQuerymodel.queryData(self.queryOverride,self.dbQuery, self.queryDaystopull))
 | 
			
		||||
 | 
			
		||||
        self.modifiedData = processData(self.data, dataTemplate, prd_instance_id=self.prdInstanceID,\
 | 
			
		||||
                                         product_guid=self.productGUID,product_name=self.productName,product_version=self.productVersion)
 | 
			
		||||
        
 | 
			
		||||
                                         product_guid=self.productGUID,product_name=self.productName,\
 | 
			
		||||
                                         product_version=self.productVersion,prd_ext_tenant_id=self.prd_ext_tenant_id)
 | 
			
		||||
 | 
			
		||||
        if self.pushToplatform:
 | 
			
		||||
            inexConnect.fortraEFC.pushPayload(self)
 | 
			
		||||
            inexConnect.fortraEFC.__init__(self)
 | 
			
		||||
 | 
			
		||||
        # TODO: move this to its own function
 | 
			
		||||
        if self.useLog:
 | 
			
		||||
 
 | 
			
		||||
@@ -43,33 +43,46 @@ class inexSql:
 | 
			
		||||
        return r
 | 
			
		||||
 | 
			
		||||
class fortraEFC:
 | 
			
		||||
    def getToken(self):
 | 
			
		||||
        self.tokenData = self.r.post(self.platformConfig["idp"], data={"grant_type":"client_credentials",\
 | 
			
		||||
                                                                              "client_id": self.platformConfig["client_id"],\
 | 
			
		||||
                                                                              "client_secret": self.platformConfig["secret"],})
 | 
			
		||||
    def writeToken(self):
 | 
			
		||||
        fortraEFC.getToken(self)
 | 
			
		||||
        with open(self.tokenFilepath, "w") as f:
 | 
			
		||||
            self.j.dump(self.tokenData.json(), f, indent = 2)
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        # Check if .token file is present
 | 
			
		||||
        if fortraEFC.readToken(self) == 1:
 | 
			
		||||
            # Get fresh token. First run.
 | 
			
		||||
            fortraEFC.getToken(self)
 | 
			
		||||
            fortraEFC.writeToken(self)
 | 
			
		||||
        # Push data with token
 | 
			
		||||
        self.pushPayloadresponse = fortraEFC.pushPayload(self)
 | 
			
		||||
        if self.pushPayloadresponse == 401:
 | 
			
		||||
            fortraEFC.getToken(self)
 | 
			
		||||
            fortraEFC.writeToken(self)
 | 
			
		||||
            fortraEFC.pushPayload(self)
 | 
			
		||||
 | 
			
		||||
    def readToken(self):
 | 
			
		||||
        if self.os.path.exists(self.tokenFilepath):
 | 
			
		||||
            with open(self.tokenFilepath, 'rb') as t:
 | 
			
		||||
                self.tokenData = self.j.load(t)
 | 
			
		||||
                # print(self.tokenData["access_token"])
 | 
			
		||||
                self.il.debug(f'readToken {self.tokenData["access_token"]}')
 | 
			
		||||
            return 0
 | 
			
		||||
        else:
 | 
			
		||||
            fortraEFC.writeToken(self)
 | 
			
		||||
            return 1
 | 
			
		||||
    
 | 
			
		||||
    def getToken(self):
 | 
			
		||||
        self.tokenData = self.r.post(self.platformConfig["idp"], data={"grant_type":"client_credentials",\
 | 
			
		||||
                                                                              "client_id": self.platformConfig["client_id"],\
 | 
			
		||||
                                                                              "client_secret": self.platformConfig["secret"],})
 | 
			
		||||
        self.tokenData = self.tokenData.json()
 | 
			
		||||
        self.il.debug(f'getToken {self.tokenData["access_token"]}')
 | 
			
		||||
 | 
			
		||||
    def writeToken(self):
 | 
			
		||||
        fortraEFC.getToken(self)
 | 
			
		||||
        with open(self.tokenFilepath, "w") as f:
 | 
			
		||||
            self.j.dump(self.tokenData, f, indent = 2)
 | 
			
		||||
            self.il.debug(f'writeToken {self.tokenData["access_token"]}')
 | 
			
		||||
 | 
			
		||||
    def pushPayload(self):
 | 
			
		||||
        fortraEFC.readToken(self)
 | 
			
		||||
        print(self.tokenData)
 | 
			
		||||
        try:
 | 
			
		||||
            url = f'{self.platformConfig["efc_url"]}/api/v1/unity/data/{self.platformConfig["tenant_id"]}/machine_event'
 | 
			
		||||
            pushPayloadResponse = self.r.post(url, headers={'Authorization': f'bearer {self.tokenData["access_token"]}'},\
 | 
			
		||||
                                               json=self.j.dumps(self.modifiedData,indent = 2, cls=self.e))
 | 
			
		||||
            return pushPayloadResponse.status_code
 | 
			
		||||
        except self.r.exceptions.HTTPError as errh:
 | 
			
		||||
            print ("Http Error:",errh)
 | 
			
		||||
            if "401" in errh:
 | 
			
		||||
                fortraEFC.writeToken(self)
 | 
			
		||||
                fortraEFC.pushPayload(self)
 | 
			
		||||
        self.il.debug(f'pushPayload {self.tokenData["access_token"]}')
 | 
			
		||||
        url = f'{self.platformConfig["efc_url"]}/api/v1/unity/data/{self.platformConfig["tenant_id"]}/machine_event'
 | 
			
		||||
        pushPayloadResponse = self.r.post(url, headers={'Authorization': f'Bearer {self.tokenData["access_token"]}'},\
 | 
			
		||||
                                           data=self.j.dumps(self.modifiedData, cls=self.e))
 | 
			
		||||
        self.il.debug(pushPayloadResponse.status_code)
 | 
			
		||||
        self.il.debug(pushPayloadResponse.text)
 | 
			
		||||
        return pushPayloadResponse.status_code
 | 
			
		||||
@@ -14,7 +14,7 @@ def processData(data, template, **kwargs):
 | 
			
		||||
            continue
 | 
			
		||||
        try:
 | 
			
		||||
            processedData.append(template(identifyUtype(row.get('Command')),\
 | 
			
		||||
                prd_ext_tenant_id='',\
 | 
			
		||||
                prd_ext_tenant_id=kwargs.get('prd_ext_tenant_id'),\
 | 
			
		||||
                status_code=row.get('ResultID'),\
 | 
			
		||||
                file_size=row.get('FileSize'),\
 | 
			
		||||
                file_path=row.get('PhysicalFolderName'),\
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user