def new_token():
    credentials = PartnerCredentials(config["consumer_key"],
                                     config["consumer_secret"],
                                     config["rsa_key"])
    print(credentials.url)
    verifier = input("verifier: ")
    credentials.verify(verifier)
    return credentials
Beispiel #2
0
    def do_GET(self):
        """
        Handle GET request
        """
        consumer_key = os.environ.get('XERO_CONSUMER_KEY')
        consumer_secret = os.environ.get('XERO_CONSUMER_SECRET')
        private_key_path = os.environ.get('XERO_RSA_CERT_KEY_PATH')

        if consumer_key is None or consumer_secret is None:
            raise ValueError(
                'Please define both XERO_CONSUMER_KEY and XERO_CONSUMER_SECRET environment variables'
            )

        if not private_key_path:
            raise ValueError(
                'Use the XERO_RSA_CERT_KEY_PATH env variable to specify the path to your RSA '
                'certificate private key file')

        with open(private_key_path, 'r') as f:
            rsa_key = f.read()
            f.close()

        print("Serving path: {}".format(self.path))
        path = urlparse(self.path)

        if path.path == '/do-auth':
            credentials = PartnerCredentials(
                consumer_key,
                consumer_secret,
                rsa_key,
                callback_uri='http://localhost:8000/oauth')

            # Save generated credentials details to persistent storage
            for key, value in credentials.state.items():
                OAUTH_PERSISTENT_SERVER_STORAGE.update({key: value})

            # Redirect to Xero at url provided by credentials generation
            self.redirect_response(credentials.url)
            return

        elif path.path == '/oauth':
            params = dict(parse_qsl(path.query))
            if 'oauth_token' not in params or 'oauth_verifier' not in params or 'org' not in params:
                self.send_error(500, message='Missing parameters required.')
                return

            stored_values = OAUTH_PERSISTENT_SERVER_STORAGE
            stored_values.update({'rsa_key': rsa_key})

            credentials = PartnerCredentials(**stored_values)

            try:
                credentials.verify(params['oauth_verifier'])

                # Resave our verified credentials
                for key, value in credentials.state.items():
                    OAUTH_PERSISTENT_SERVER_STORAGE.update({key: value})

            except XeroException as e:
                self.send_error(500,
                                message='{}: {}'.format(
                                    e.__class__, e.message))
                return

            # Once verified, api can be invoked with xero = Xero(credentials)
            self.redirect_response('/verified')
            return

        elif path.path == '/verified':

            stored_values = OAUTH_PERSISTENT_SERVER_STORAGE
            stored_values.update({'rsa_key': rsa_key})
            credentials = PartnerCredentials(**stored_values)

            # Partner credentials expire after 30 minutes. Here's how to re-activate on expiry
            if credentials.expired():
                credentials.refresh()

            try:
                xero = Xero(credentials)

            except XeroException as e:
                self.send_error(500,
                                message='{}: {}'.format(
                                    e.__class__, e.message))
                return

            page_body = 'Your contacts:<br><br>'

            contacts = xero.contacts.all()

            if contacts:
                page_body += '<br>'.join(
                    [str(contact) for contact in contacts])
            else:
                page_body += 'No contacts'
            self.page_response(title='Xero Contacts', body=page_body)
            return

        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
Beispiel #3
0
    def do_GET(self):
        """
        Handle GET request
        """
        consumer_key = os.environ.get('XERO_CONSUMER_KEY')
        consumer_secret = os.environ.get('XERO_CONSUMER_SECRET')
        private_key_path = os.environ.get('XERO_RSA_CERT_KEY_PATH')
        entrust_cert_path = os.environ.get('XERO_ENTRUST_CERT_PATH')
        entrust_private_key_path = os.environ.get('XERO_ENTRUST_PRIVATE_KEY_PATH')

        if consumer_key is None or consumer_secret is None:
            raise ValueError(
                'Please define both XERO_CONSUMER_KEY and XERO_CONSUMER_SECRET environment variables')

        if not private_key_path:
            raise ValueError(
                'Use the XERO_RSA_CERT_KEY_PATH env variable to specify the path to your RSA '
                'certificate private key file')

        if not entrust_cert_path:
            raise ValueError(
                'Use the XERO_ENTRUST_CERT_PATH env variable to specify the path to your Entrust '
                'certificate')

        if not entrust_private_key_path:
            raise ValueError(
                'Use the XERO_ENTRUST_PRIVATE_KEY_PATH env variable to specify the path to your '
                'Entrust private no-pass key')

        with open(private_key_path, 'r') as f:
            rsa_key = f.read()
            f.close()

        client_cert = (entrust_cert_path, entrust_private_key_path)

        print("Serving path: {}".format(self.path))
        path = urlparse(self.path)

        if path.path == '/do-auth':
            client_cert = (entrust_cert_path, entrust_private_key_path)
            credentials = PartnerCredentials(
                consumer_key, consumer_secret, rsa_key, client_cert,
                callback_uri='http://localhost:8000/oauth')

            # Save generated credentials details to persistent storage
            for key, value in credentials.state.items():
                OAUTH_PERSISTENT_SERVER_STORAGE.update({key: value})

            # Redirect to Xero at url provided by credentials generation
            self.redirect_response(credentials.url)
            return

        elif path.path == '/oauth':
            params = dict(parse_qsl(path.query))
            if 'oauth_token' not in params or 'oauth_verifier' not in params or 'org' not in params:
                self.send_error(500, message='Missing parameters required.')
                return

            stored_values = OAUTH_PERSISTENT_SERVER_STORAGE
            client_cert = (entrust_cert_path, entrust_private_key_path)
            stored_values.update({'rsa_key': rsa_key, 'client_cert': client_cert})

            credentials = PartnerCredentials(**stored_values)

            try:
                credentials.verify(params['oauth_verifier'])

                # Resave our verified credentials
                for key, value in credentials.state.items():
                    OAUTH_PERSISTENT_SERVER_STORAGE.update({key: value})

            except XeroException as e:
                self.send_error(500, message='{}: {}'.format(e.__class__, e.message))
                return

            # Once verified, api can be invoked with xero = Xero(credentials)
            self.redirect_response('/verified')
            return

        elif path.path == '/verified':

            stored_values = OAUTH_PERSISTENT_SERVER_STORAGE
            stored_values.update({'rsa_key': rsa_key, 'client_cert': client_cert})
            credentials = PartnerCredentials(**stored_values)

            # Partner credentials expire after 30 minutes. Here's how to re-activate on expiry
            if credentials.expired():
                credentials.refresh()

            try:
                xero = Xero(credentials)

            except XeroException as e:
                self.send_error(500, message='{}: {}'.format(e.__class__, e.message))
                return

            page_body = 'Your contacts:<br><br>'

            contacts = xero.contacts.all()

            if contacts:
                page_body += '<br>'.join([str(contact) for contact in contacts])
            else:
                page_body += 'No contacts'
            self.page_response(title='Xero Contacts', body=page_body)
            return

        SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)