Esempio n. 1
0
def refresh_api(api):
    logger.debug('Running update on api key %s' % api.api_id)
    still_valid = True
    try:
        EveApiManager.validate_api(api.api_id, api.api_key, api.user)
        # Update characters
        characters = EveApiManager.get_characters_from_api(api.api_id, api.api_key)
        EveManager.update_characters_from_list(characters)
        new_character = False
        for char in characters.result:
            # Ensure we have a model for all characters on key
            if not EveManager.check_if_character_exist(characters.result[char]['name']):
                logger.debug(
                    "API key %s has a new character on the account: %s" % (api.api_id, characters.result[char]['name']))
                new_character = True
        if new_character:
            logger.debug("Creating new character %s from api key %s" % (characters.result[char]['name'], api.api_id))
            EveManager.create_characters_from_list(characters, api.user, api.api_id)
        current_chars = EveCharacter.objects.filter(api_id=api.api_id)
        for c in current_chars:
            if not int(c.character_id) in characters.result:
                logger.info("Character %s no longer found on API ID %s" % (c, api.api_id))
                c.delete()
    except evelink.api.APIError as e:
        logger.warning('Received unexpected APIError (%s) while updating API %s' % (e.code, api.api_id))
    except EveApiManager.ApiInvalidError:
        logger.debug("API key %s is no longer valid; it and its characters will be deleted." % api.api_id)
        notify(api.user, "API Failed Validation", message="Your API key ID %s is no longer valid." % api.api_id,
               level="danger")
        still_valid = False
    except EveApiManager.ApiAccountValidationError:
        logger.info(
            "Determined api key %s for user %s no longer meets account access requirements." % (api.api_id, api.user))
        notify(api.user, "API Failed Validation",
               message="Your API key ID %s is no longer account-wide as required." % api.api_id, level="danger")
        still_valid = False
    except EveApiManager.ApiMaskValidationError as e:
        logger.info("Determined api key %s for user %s no longer meets minimum access mask as required." % (
            api.api_id, api.user))
        notify(api.user, "API Failed Validation",
               message="Your API key ID %s no longer meets access mask requirements. Required: %s Got: %s" % (
                   api.api_id, e.required_mask, e.api_mask), level="danger")
        still_valid = False
    except EveApiManager.ApiServerUnreachableError as e:
        logger.warn("Error updating API %s\n%s" % (api.api_id, str(e)))
    finally:
        if not still_valid:
            EveManager.delete_characters_by_api_id(api.api_id, api.user.id)
            EveManager.delete_api_key_pair(api.api_id, api.user.id)
            notify(api.user, "API Key Deleted",
                   message="Your API key ID %s is invalid. It and its associated characters have been deleted." % api.api_id,
                   level="danger")
Esempio n. 2
0
def refresh_api(api):
    logger.debug('Running update on api key %s' % api.api_id)
    still_valid = True
    try:
        EveApiManager.validate_api(api.api_id, api.api_key, api.user)
        # Update characters
        characters = EveManager.get_characters_from_api(api)
        for c in characters:
            try:
                EveManager.update_character_obj(c)
            except EveCharacter.DoesNotExist:
                logger.debug("API key %s has a new character on the account: %s" % (api.api_id, c))
                EveManager.create_character_obj(c, api.user, api.api_id)
        current_chars = EveCharacter.objects.filter(api_id=api.api_id)
        for c in current_chars:
            if not int(c.character_id) in [c.id for c in characters]:
                logger.info("Character %s no longer found on API ID %s" % (c, api.api_id))
                c.delete()
    except evelink.api.APIError as e:
        logger.warning('Received unexpected APIError (%s) while updating API %s' % (e.code, api.api_id))
    except EveApiManager.ApiInvalidError:
        logger.debug("API key %s is no longer valid; it and its characters will be deleted." % api.api_id)
        notify(api.user, "API Failed Validation", message="Your API key ID %s is no longer valid." % api.api_id,
               level="danger")
        still_valid = False
    except EveApiManager.ApiAccountValidationError:
        logger.info(
            "Determined api key %s for user %s no longer meets account access requirements." % (api.api_id, api.user))
        notify(api.user, "API Failed Validation",
               message="Your API key ID %s is no longer account-wide as required." % api.api_id, level="danger")
        still_valid = False
    except EveApiManager.ApiMaskValidationError as e:
        logger.info("Determined api key %s for user %s no longer meets minimum access mask as required." % (
            api.api_id, api.user))
        notify(api.user, "API Failed Validation",
               message="Your API key ID %s no longer meets access mask requirements. Required: %s Got: %s" % (
                   api.api_id, e.required_mask, e.api_mask), level="danger")
        still_valid = False
    except EveApiManager.ApiServerUnreachableError as e:
        logger.warn("Error updating API %s\n%s" % (api.api_id, str(e)))
    finally:
        if not still_valid:
            EveManager.delete_characters_by_api_id(api.api_id, api.user.id)
            EveManager.delete_api_key_pair(api.api_id, api.user.id)
            notify(api.user, "API Key Deleted",
                   message="Your API key ID %s is invalid. It and its associated characters have been deleted." % api.api_id,
                   level="danger")
Esempio n. 3
0
    def clean(self):
        super(UpdateKeyForm, self).clean()

        if EveManager.check_if_api_key_pair_exist(self.cleaned_data['api_id']):
            logger.debug("UpdateKeyForm failed cleaning as API id %s already exists." % self.cleaned_data['api_id'])
            if EveApiKeyPair.objects.get(api_id=self.cleaned_data['api_id']).user:
                # allow orphaned APIs to proceed to SSO validation upon re-entry
                raise forms.ValidationError('API key already exist')
        if settings.REJECT_OLD_APIS and not EveManager.check_if_api_key_pair_is_new(
                        self.cleaned_data['api_id'],
                        settings.REJECT_OLD_APIS_MARGIN):
            raise forms.ValidationError('API key is too old. Please create a new key')
        try:
            EveApiManager.validate_api(self.cleaned_data['api_id'], self.cleaned_data['api_key'], self.user)
            return self.cleaned_data
        except EveApiManager.ApiValidationError as e:
            raise forms.ValidationError(str(e))
        except evelink.api.APIError as e:
            logger.debug("Got error code %s while validating API %s" % (e.code, self.cleaned_data['api_id']))
            raise forms.ValidationError('Error while checking API key (%s)' % e.code)
Esempio n. 4
0
    def clean(self):
        if 'api_id' not in self.cleaned_data or 'api_key' not in self.cleaned_data:
            # need to check if api_id and vcode in cleaned_data because
            # if they fail, they get removed from the dict but this method still happens
            return self.cleaned_data

        if EveManager.check_if_api_key_pair_exist(self.cleaned_data['api_id']):
            logger.debug("UpdateKeyForm failed cleaning as API id %s already exists." % self.cleaned_data['api_id'])
            if EveApiKeyPair.objects.get(api_id=self.cleaned_data['api_id']).user:
                # allow orphaned APIs to proceed to SSO validation upon re-entry
                raise forms.ValidationError('API key already exist')
        if settings.REJECT_OLD_APIS and not EveManager.check_if_api_key_pair_is_new(
                        self.cleaned_data['api_id'],
                        settings.REJECT_OLD_APIS_MARGIN):
            raise forms.ValidationError('API key is too old. Please create a new key')
        try:
            EveApiManager.validate_api(self.cleaned_data['api_id'], self.cleaned_data['api_key'], self.user)
            return self.cleaned_data
        except EveApiManager.ApiValidationError as e:
            raise forms.ValidationError(str(e))
        except evelink.api.APIError as e:
            logger.debug("Got error code %s while validating API %s" % (e.code, self.cleaned_data['api_id']))
            raise forms.ValidationError('Error while checking API key (%s)' % e.code)