Automatic rejection of old API IDs (#441)

Automatic rejection of old API IDs
Closes #436
This commit is contained in:
moriartyj 2016-05-27 06:24:35 -07:00 committed by Adarnof
parent 59a5105e5a
commit f6a177295d
3 changed files with 19 additions and 0 deletions

View File

@ -335,11 +335,15 @@ ALLIANCE_NAME = os.environ.get('AA_ALLIANCE_NAME', '')
# MEMBER_API_ACCOUNT - Require API to be for Account and not character restricted # MEMBER_API_ACCOUNT - Require API to be for Account and not character restricted
# BLUE_API_MASK - Numeric value of minimum API mask required for blues # BLUE_API_MASK - Numeric value of minimum API mask required for blues
# BLUE_API_ACCOUNT - Require API to be for Account and not character restricted # BLUE_API_ACCOUNT - Require API to be for Account and not character restricted
# REJECT_OLD_APIS - Require each submitted API be newer than the latest submitted API
# REJECT_OLD_APIS_MARGIN - Margin from latest submitted API ID within which a newly submitted API is still accepted
####################### #######################
MEMBER_API_MASK = os.environ.get('AA_MEMBER_API_MASK', 268435455) MEMBER_API_MASK = os.environ.get('AA_MEMBER_API_MASK', 268435455)
MEMBER_API_ACCOUNT = 'True' == os.environ.get('AA_MEMBER_API_ACCOUNT', 'True') MEMBER_API_ACCOUNT = 'True' == os.environ.get('AA_MEMBER_API_ACCOUNT', 'True')
BLUE_API_MASK = os.environ.get('AA_BLUE_API_MASK', 8388608) BLUE_API_MASK = os.environ.get('AA_BLUE_API_MASK', 8388608)
BLUE_API_ACCOUNT = 'True' == os.environ.get('AA_BLUE_API_ACCOUNT', 'False') BLUE_API_ACCOUNT = 'True' == os.environ.get('AA_BLUE_API_ACCOUNT', 'False')
REJECT_OLD_APIS = 'True' == os.environ.get('AA_REJECT_OLD_APIS', 'False')
REJECT_OLD_APIS_MARGIN = os.environ.get('AA_REJECT_OLD_APIS_MARGIN', 50)
########################## ##########################
# Pathfinder Configuration # Pathfinder Configuration

View File

@ -35,6 +35,9 @@ class UpdateKeyForm(forms.Form):
raise forms.ValidationError(u'API key already exist') 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: 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') 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 chars = EveApiManager.get_characters_from_api(self.cleaned_data['api_id'], self.cleaned_data['api_key']).result
states = [] states = []
states.append(self.user_state) states.append(self.user_state)

View File

@ -157,6 +157,18 @@ class EveManager:
logger.debug("Determined api id %s does not exist." % api_id) logger.debug("Determined api id %s does not exist." % api_id)
return False return False
@staticmethod
def check_if_api_key_pair_is_new(api_id, fudge_factor):
if EveApiKeyPair.objects.count() == 0:
return True
latest_api_id = int(EveApiKeyPair.objects.order_by('-api_id')[0].api_id) - fudge_factor
if latest_api_id >= api_id:
logger.debug("api key (%d) is older than latest API key (%d). Rejecting" % (api_id, latest_api_id) )
return False
else:
logger.debug("api key (%d) is new. Accepting" % api_id )
return True
@staticmethod @staticmethod
def delete_api_key_pair(api_id, user_id): def delete_api_key_pair(api_id, user_id):
logger.debug("Deleting api id %s" % api_id) logger.debug("Deleting api id %s" % api_id)