Exemple #1
0
    def contact_corp(self, corp_url):
        try:
            my_corp = Corporation.objects.mine()
            url = urlparse.urljoin(corp_url, '/corp/contact/')

            client = HttpClient()
            LOG.info('Sending our public info to %s...' % url)
            # first GET request to fetch CSRF cookie
            response = client.get(url)
            # second POST request to send our public info
            response = client.post(url, json.dumps(my_corp.get_public_info()))

            LOG.info('Fetching public info from %s...' % url)
            # the response should contain the corp's public info
            public_info = json.load(response)
            self.corporationID = public_info['corporationID']
            self.corporationName = public_info['corporationName']
            self.ticker = public_info['ticker']
            self.alliance_id = public_info['alliance']
            self.public_key = public_info['public_key']
            self.key_fingerprint = public_info['key_fingerprint']
            self.is_my_corp = False
            self.is_trusted = True

            LOG.info('Corp %s accepted our contact request.' %
                     self.corporationName)
            LOG.info('Wait until they confirm that they trust us '
                     'before you can exchange data with them.')
        except urllib2.HTTPError, e:
            message = 'URL: %s, Response: %s %s "%s"' % (e.url, e.code,
                                                         e.reason, e.read())
            LOG.exception(message)
            raise ValidationError(message)
Exemple #2
0
def update_one_corp(corp):

    my_corp = Corporation.objects.mine()

    auth_url = urlparse.urljoin(corp.ecm_url, '/corp/auth/startsession/')
    client = HttpClient()

    LOG.debug('Establishing secure data exchange with %r...' % corp.ecm_url)
    response = client.get(
        auth_url,
        headers={'Authorization': 'RSA %s' % my_corp.key_fingerprint})
    cipher_txt_in = response.read()

    # we decrypt the response with our private key
    session_secret = crypto.rsa_decrypt(my_corp.private_key, cipher_txt_in)
    # and encrypt it back with the corp's public key
    cipher_txt_out = crypto.rsa_encrypt(corp.public_key, session_secret)

    # then send it to the server
    client.post(auth_url, cipher_txt_out)

    LOG.debug('Fetching which data %r is sharing with us...' % corp)
    # now we fetch the urls we're allowed to pull from this corporation
    response = client.get(
        urlparse.urljoin(corp.ecm_url, '/corp/share/allowed/'))
    data = crypto.aes_decrypt(session_secret, response.read())
    allowed_urls = json.loads(data)

    if not allowed_urls:
        LOG.warning('%r is not sharing any data with us' %
                    corp.corporationName)
    for url in allowed_urls:
        try:
            shared_data = SharedData.objects.get(url=url)

            LOG.debug('Fetching shared data %r...' % url)
            response = client.get(
                urlparse.urljoin(corp.ecm_url, shared_data.url))

            raw_data = crypto.aes_decrypt(session_secret, response.read())

            if response.info().getheader(
                    'content-type') == 'application/gzip-compressed':
                raw_data = zlib.decompress(raw_data)

            shared_data.call_handler(corp, json.loads(raw_data))
        except SharedData.DoesNotExist:
            LOG.error('Unknown SharedData with url=%r' % url)
        except:
            LOG.exception('')

    LOG.debug('Ending secure session with %r...' % corp.ecm_url)
    # finally destroy our session info to be sure nobody will steal it :)
    client.get(urlparse.urljoin(corp.ecm_url, '/corp/auth/endsession/'))