Example #1
0
    def clean(self):
        super(UpdateKeyForm, self).clean()

        if 'api_id' in self.cleaned_data and 'api_key' in 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'])
                raise forms.ValidationError(u'API key already exist')
            if EveApiManager.api_key_is_valid(self.cleaned_data['api_id'], self.cleaned_data['api_key']) is False:
                raise forms.ValidationError(u'API key is invalid')
            chars = EveApiManager.get_characters_from_api(self.cleaned_data['api_id'], self.cleaned_data['api_key']).result
            states = []
            states.append(self.user_state)
            for char in chars:
                evechar = EveCharacter()
                evechar.character_name = chars[char]['name']
                evechar.corporation_id = chars[char]['corp']['id']
                evechar.alliance_id = chars[char]['alliance']['id']
                state = determine_membership_by_character(evechar)
                logger.debug("API ID %s character %s has state %s" % (self.cleaned_data['api_id'], evechar, state))
                states.append(state)

            if 'MEMBER' in states:
                if EveApiManager.validate_member_api(self.cleaned_data['api_id'], self.cleaned_data['api_key']) is False:
                    raise forms.ValidationError(u'API must meet member requirements')
            if 'BLUE' in states:
                if EveApiManager.validate_blue_api(self.cleaned_data['api_id'], self.cleaned_data['api_key']) is False:
                    raise forms.ValidationError(u'API must meet blue requirements')
        return self.cleaned_data
Example #2
0
 def validate_api(api_id, api_key, user):
     try:
         info = EveApiManager.get_api_info(api_id, api_key).result
         chars = EveApiManager.get_characters_from_api(api_id, api_key).result
     except evelink.api.APIError as e:
         if int(e.code) == 222:
             raise EveApiManager.ApiInvalidError(api_id)
         raise e
     except (requests.exceptions.RequestExeception, HTTPError, URLError) as e:
         raise EveApiManager.ApiServerUnreachableError(e)
     auth, c = AuthServicesInfo.objects.get_or_create(user=user)
     states = [auth.state]
     from authentication.tasks import determine_membership_by_character  # circular import issue
     for char in chars:
         evechar = EveCharacter()
         evechar.character_name = chars[char]['name']
         evechar.corporation_id = chars[char]['corp']['id']
         evechar.alliance_id = chars[char]['alliance']['id']
         states.append(determine_membership_by_character(evechar))
     if MEMBER_STATE not in states and BLUE_STATE not in states:
         # default to requiring member keys for applications
         states.append(MEMBER_STATE)
     logger.debug('Checking API %s for states %s' % (api_id, states))
     for state in states:
         if (state == MEMBER_STATE and settings.MEMBER_API_ACCOUNT) or (
                         state == BLUE_STATE and settings.BLUE_API_ACCOUNT):
             if info['type'] != 'account':
                 raise EveApiManager.ApiAccountValidationError(api_id)
         if state == MEMBER_STATE:
             if int(info['access_mask']) & int(settings.MEMBER_API_MASK) != int(settings.MEMBER_API_MASK):
                 raise EveApiManager.ApiMaskValidationError(settings.MEMBER_API_MASK, info['access_mask'], api_id)
         elif state == BLUE_STATE:
             if int(info['access_mask']) & int(settings.BLUE_API_MASK) != int(settings.BLUE_API_MASK):
                 raise EveApiManager.ApiMaskValidationError(settings.BLUE_API_MASK, info['access_mask'], api_id)
     return True
Example #3
0
    def clean(self):
        super(UpdateKeyForm, self).clean()

        if 'api_id' in self.cleaned_data and 'api_key' in self.cleaned_data:
            try:
                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'])
                    raise forms.ValidationError(u'API key already exist')
                if EveApiManager.api_key_is_valid(
                        self.cleaned_data['api_id'],
                        self.cleaned_data['api_key']) is False:
                    raise forms.ValidationError(u'API key is invalid')
                if (settings.REJECT_OLD_APIS
                        and EveManager.check_if_api_key_pair_is_new(
                            self.cleaned_data['api_id'],
                            settings.REJECT_OLD_APIS_MARGIN) is False):
                    raise forms.ValidationError(
                        u'API key is too old. Please create a new key')
                chars = EveApiManager.get_characters_from_api(
                    self.cleaned_data['api_id'],
                    self.cleaned_data['api_key']).result
                states = []
                states.append(self.user_state)
                for char in chars:
                    evechar = EveCharacter()
                    evechar.character_name = chars[char]['name']
                    evechar.corporation_id = chars[char]['corp']['id']
                    evechar.alliance_id = chars[char]['alliance']['id']
                    state = determine_membership_by_character(evechar)
                    logger.debug("API ID %s character %s has state %s" %
                                 (self.cleaned_data['api_id'], evechar, state))
                    states.append(state)

                if 'MEMBER' in states:
                    if EveApiManager.validate_member_api(
                            self.cleaned_data['api_id'],
                            self.cleaned_data['api_key']) is False:
                        raise forms.ValidationError(
                            u'API must meet member requirements')
                if 'BLUE' in states:
                    if EveApiManager.validate_blue_api(
                            self.cleaned_data['api_id'],
                            self.cleaned_data['api_key']) is False:
                        raise forms.ValidationError(
                            u'API must meet blue requirements')
                return self.cleaned_data
            except evelink.api.APIError as e:
                logger.debug("Got error code %s while validating API %s" %
                             (e.code, self.cleaned_data['api_id']))
                if int(e.code) in [221, 222]:
                    raise forms.ValidationError("API key failed validation")
                else:
                    raise forms.ValidationError("Failed to reach API servers")
Example #4
0
    def clean(self):
        super(UpdateKeyForm, self).clean()

        if "api_id" in self.cleaned_data and "api_key" in self.cleaned_data:
            try:
                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"]
                    )
                    raise forms.ValidationError(u"API key already exist")
                if EveApiManager.api_key_is_valid(self.cleaned_data["api_id"], self.cleaned_data["api_key"]) is False:
                    raise forms.ValidationError(u"API key is invalid")
                chars = EveApiManager.get_characters_from_api(
                    self.cleaned_data["api_id"], self.cleaned_data["api_key"]
                ).result
                states = []
                states.append(self.user_state)
                for char in chars:
                    evechar = EveCharacter()
                    evechar.character_name = chars[char]["name"]
                    evechar.corporation_id = chars[char]["corp"]["id"]
                    evechar.alliance_id = chars[char]["alliance"]["id"]
                    state = determine_membership_by_character(evechar)
                    logger.debug("API ID %s character %s has state %s" % (self.cleaned_data["api_id"], evechar, state))
                    states.append(state)

                if "MEMBER" in states:
                    if (
                        EveApiManager.validate_member_api(self.cleaned_data["api_id"], self.cleaned_data["api_key"])
                        is False
                    ):
                        raise forms.ValidationError(u"API must meet member requirements")
                if "BLUE" in states:
                    if (
                        EveApiManager.validate_blue_api(self.cleaned_data["api_id"], self.cleaned_data["api_key"])
                        is False
                    ):
                        raise forms.ValidationError(u"API must meet blue requirements")
                return self.cleaned_data
            except evelink.api.APIError as e:
                logger.debug("Got error code %s while validating API %s" % (e.code, self.cleaned_data["api_id"]))
                if int(e.code) in [221, 222]:
                    raise forms.ValidationError("API key failed validation")
                else:
                    raise forms.ValidationError("Failed to reach API servers")
Example #5
0
 def create_character(character_id, character_name, corporation_id,
                      corporation_name, corporation_ticker, alliance_id,
                      alliance_name, user, api_id):
     logger.debug("Creating model for character %s id %s" % (character_name, character_id))
     if not EveCharacter.objects.filter(character_id=character_id).exists():
         eve_char = EveCharacter()
         eve_char.character_id = character_id
         eve_char.character_name = character_name
         eve_char.corporation_id = corporation_id
         eve_char.corporation_name = corporation_name
         eve_char.corporation_ticker = corporation_ticker
         eve_char.alliance_id = alliance_id
         eve_char.alliance_name = alliance_name
         eve_char.user = user
         eve_char.api_id = api_id
         eve_char.save()
         logger.info("Created new character model %s for user %s" % (eve_char, user))
     else:
         logger.warn("Attempting to create existing character model with id %s" % character_id)
Example #6
0
    def clean(self):
        super(UpdateKeyForm, self).clean()

        if 'api_id' in self.cleaned_data and 'api_key' in 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'])
                raise forms.ValidationError(u'API key already exist')
            if EveApiManager.api_key_is_valid(
                    self.cleaned_data['api_id'],
                    self.cleaned_data['api_key']) is False:
                raise forms.ValidationError(u'API key is invalid')
            chars = EveApiManager.get_characters_from_api(
                self.cleaned_data['api_id'],
                self.cleaned_data['api_key']).result
            states = []
            states.append(self.user_state)
            for char in chars:
                evechar = EveCharacter()
                evechar.character_name = chars[char]['name']
                evechar.corporation_id = chars[char]['corp']['id']
                evechar.alliance_id = chars[char]['alliance']['id']
                state = determine_membership_by_character(evechar)
                logger.debug("API ID %s character %s has state %s" %
                             (self.cleaned_data['api_id'], evechar, state))
                states.append(state)

            if 'MEMBER' in states:
                if EveApiManager.validate_member_api(
                        self.cleaned_data['api_id'],
                        self.cleaned_data['api_key']) is False:
                    raise forms.ValidationError(
                        u'API must meet member requirements')
            if 'BLUE' in states:
                if EveApiManager.validate_blue_api(
                        self.cleaned_data['api_id'],
                        self.cleaned_data['api_key']) is False:
                    raise forms.ValidationError(
                        u'API must meet blue requirements')
        return self.cleaned_data
Example #7
0
    def clean(self):
        super(UpdateKeyForm, self).clean()

        if 'api_id' in self.cleaned_data and 'api_key' in self.cleaned_data:
            try:
                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'])
                    raise forms.ValidationError(u'API key already exist')
                if EveApiManager.api_key_is_valid(self.cleaned_data['api_id'], self.cleaned_data['api_key']) is False:
                    raise forms.ValidationError(u'API key is invalid')
                if (settings.REJECT_OLD_APIS and
                    EveManager.check_if_api_key_pair_is_new(self.cleaned_data['api_id'], settings.REJECT_OLD_APIS_MARGIN) is False):
                    raise forms.ValidationError(u'API key is too old. Please create a new key')
                chars = EveApiManager.get_characters_from_api(self.cleaned_data['api_id'], self.cleaned_data['api_key']).result
                states = []
                states.append(self.user_state)
                for char in chars:
                    evechar = EveCharacter()
                    evechar.character_name = chars[char]['name']
                    evechar.corporation_id = chars[char]['corp']['id']
                    evechar.alliance_id = chars[char]['alliance']['id']
                    state = determine_membership_by_character(evechar)
                    logger.debug("API ID %s character %s has state %s" % (self.cleaned_data['api_id'], evechar, state))
                    states.append(state)

                if 'MEMBER' in states:
                    if EveApiManager.validate_member_api(self.cleaned_data['api_id'], self.cleaned_data['api_key']) is False:
                        raise forms.ValidationError(u'API must meet member requirements')
                if 'BLUE' in states:
                    if EveApiManager.validate_blue_api(self.cleaned_data['api_id'], self.cleaned_data['api_key']) is False:
                        raise forms.ValidationError(u'API must meet blue requirements')
                if 'BLUE_10' in states:
                    if EveApiManager.validate_blue_api(self.cleaned_data['api_id'], self.cleaned_data['api_key']) is False:
                        raise forms.ValidationError(u'API must meet blue requirements')
                return self.cleaned_data
            except evelink.api.APIError as e:
                logger.debug("Got error code %s while validating API %s" % (e.code, self.cleaned_data['api_id']))
                if int(e.code) in [221, 222]:
                    raise forms.ValidationError("API key failed validation")
                else:
                    raise forms.ValidationError("Failed to reach API servers")
Example #8
0
 def create_character(character_id, character_name, corporation_id,
                      corporation_name, corporation_ticker, alliance_id,
                      alliance_name, user, api_id):
     logger.debug("Creating model for character %s id %s" %
                  (character_name, character_id))
     if not EveCharacter.objects.filter(character_id=character_id).exists():
         eve_char = EveCharacter()
         eve_char.character_id = character_id
         eve_char.character_name = character_name
         eve_char.corporation_id = corporation_id
         eve_char.corporation_name = corporation_name
         eve_char.corporation_ticker = corporation_ticker
         eve_char.alliance_id = alliance_id
         eve_char.alliance_name = alliance_name
         eve_char.user = user
         eve_char.api_id = api_id
         eve_char.save()
         logger.info("Created new character model %s for user %s" %
                     (eve_char, user))
     else:
         logger.warn(
             "Attempting to create existing character model with id %s" %
             character_id)