Repository: smicallef/spiderfoot
File: modules/sfp_twilio.py (Python)

Code before:
        return ["HUMAN_NAME", "RAW_RIR_DATA"]

    # When querying third parties, it's best to have a dedicated function
    # to do so and avoid putting it in handleEvent()
    def queryPhoneNumber(self, phoneNumber):   
        
        token = (base64.b64encode(self.opts['api_key_account_sid'].encode('utf8') + ":".encode('utf-8') + self.opts['api_key_auth_token'].encode('utf-8'))).decode('utf-8')

        headers = {
            'Accept': "application/json",
            'Authorization': "Basic " + token
        }

        res = self.sf.fetchUrl(
          'https://lookups.twilio.com/v1/PhoneNumbers/' + phoneNumber,
          headers=headers,
          timeout=15,
          useragent=self.opts['_useragent']
        )

        if res['code'] == '400':
            self.sf.error("Bad request.", False)
            return None
        
        if res['code'] == '404':
            self.sf.error("Invalid phone number.", False)
            return None
        
        if res['code'] == '429':
            self.sf.error("API usage limit reached.", False)
            return None

        if res['code'] == '503':
            self.sf.error("Service unavailable.", False)
            return None

        if not res['code'] == '200':
            self.sf.error("Could not fetch data.", False)
            return None

        return res.get('content')

    # Handle events sent to this module
    def handleEvent(self, event):
        eventName = event.eventType
        srcModuleName = event.module
        eventData = event.data

        if self.errorState:
            return None


Review comment (bug):
Would 404 really be an invalid phone number? Wouldn't it just be one that wasn't found? In that case, it shouldn't be an error, just a debug message.

Code after:
        return ["HUMAN_NAME", "RAW_RIR_DATA"]

    # When querying third parties, it's best to have a dedicated function
    # to do so and avoid putting it in handleEvent()
    def queryPhoneNumber(self, phoneNumber):   
        
        token = (base64.b64encode(self.opts['api_key_account_sid'].encode('utf8') + ":".encode('utf-8') + self.opts['api_key_auth_token'].encode('utf-8'))).decode('utf-8')

        headers = {
            'Accept': "application/json",
            'Authorization': "Basic " + token
        }

        res = self.sf.fetchUrl(
          'https://lookups.twilio.com/v1/PhoneNumbers/' + phoneNumber,
          headers=headers,
          timeout=15,
          useragent=self.opts['_useragent']
        )

        if res['code'] == '400':
            self.sf.error("Bad request.", False)
            return None
        
        if res['code'] == '404':
            self.sf.debug("Phone number not found.")
            return None
        
        if res['code'] == '429':
            self.sf.error("API usage limit reached.", False)
            return None

        if res['code'] == '503':
            self.sf.error("Service unavailable.", False)
            return None

        if not res['code'] == '200':
            self.sf.error("Could not fetch data.", False)
            return None

        return res.get('content')

    # Handle events sent to this module
    def handleEvent(self, event):
        eventName = event.eventType
        srcModuleName = event.module
        eventData = event.data

        if self.errorState:
            return None