def authenticate(self, token=None):
        if not token:
            return None

        try:
            eve = get_crest_connection()
            crest_con = eve.authorize(token)
            userdata = crest_con.whoami()
            log.debug('{} logged in, username {}.'.format(userdata['CharacterName'],
                                                          userdata['CharacterOwnerHash']))
        except:
            log.error('Could not authenticate with CREST.')
            return None

        capsuler, created = Capsuler.objects.get_or_create(username=userdata['CharacterOwnerHash'],
                                                           character_name=userdata['CharacterName'])
        if created:
            log.info('Created user "{}" for character "{}".'.format(
                capsuler.username,
                capsuler.character_name
            ))

        timeout = (datetime.utcfromtimestamp(crest_con.expires) -
                   datetime.now()).total_seconds()

        cache.set('{}.userdata'.format(capsuler.username), userdata, timeout)
        store_connection(capsuler.username, crest_con)

        return capsuler
Exemple #2
0
    def get_crest(self):
        conn = cache.get("{}.conndata".format(self.username), None)
        if conn is None:
            log.debug("{} crest conn has expired".format(self.character_name))
            return None
        eve = get_crest_connection()
        expires_in = self._get_timeout(conn["expires"])
        crest = eve.temptoken_authorize(conn["access_token"], expires_in, conn["refresh_token"])

        if expires_in > 0 and expires_in < 180:
            log.debug("Refreshing token for {0}".format(self.character_name))
            crest.refresh()
            store_connection(self.username, crest)

        return crest