mirror of
https://github.com/nikdoof/test-auth.git
synced 2025-12-14 06:42:16 +00:00
Support for Incarna v1.1, Customizable API Keys
- Support CAK keys and their spinoffs - Add support for the Employment History data available in CharacterInfo.xml.aspx - Add a Gargoyle flag to disable backend processing when needed - Clean up a few inconsistancies with how the models are handled
This commit is contained in:
@@ -5,6 +5,7 @@ import logging
|
||||
|
||||
from celery.decorators import task
|
||||
from celery.task.sets import TaskSet
|
||||
from gargoyle import gargoyle
|
||||
|
||||
from eve_proxy.exceptions import *
|
||||
from eve_proxy.models import CachedDocument
|
||||
@@ -27,18 +28,26 @@ def queue_apikey_updates(update_delay=86400, batch_size=50):
|
||||
"""
|
||||
|
||||
log = queue_apikey_updates.get_logger()
|
||||
|
||||
if gargoyle.is_active('api-disableprocessing'):
|
||||
log.info("Backend processing disabled, exiting")
|
||||
return
|
||||
|
||||
# Update all the eve accounts and related corps
|
||||
delta = timedelta(seconds=update_delay)
|
||||
log.info("Updating APIs older than %s" % (datetime.now() - delta))
|
||||
|
||||
accounts = EVEAccount.objects.filter(api_last_updated__lt=(datetime.now() - delta)).exclude(api_status=API_STATUS_ACC_EXPIRED).exclude(api_status=API_STATUS_AUTH_ERROR).order_by('api_last_updated')[:batch_size]
|
||||
if gargoyle.is_active('eve-cak'):
|
||||
accounts = EVEAccount.objects.filter(api_last_updated__lt=(datetime.now() - delta)).exclude(api_status=API_STATUS_ACC_EXPIRED).exclude(api_status=API_STATUS_AUTH_ERROR).order_by('api_last_updated')[:batch_size]
|
||||
else:
|
||||
accounts = EVEAccount.objects.filter(api_last_updated__lt=(datetime.now() - delta)).exclude(api_status=API_STATUS_ACC_EXPIRED).exclude(api_status=API_STATUS_AUTH_ERROR).exclude(api_keytype__gt=2).order_by('api_last_updated')[:batch_size]
|
||||
log.info("%s account(s) to update" % accounts.count())
|
||||
for acc in accounts:
|
||||
log.debug("Queueing UserID %s for update" % acc.api_user_id)
|
||||
log.debug("Queueing UserID %s for update" % acc.pk)
|
||||
if not acc.user:
|
||||
acc.delete()
|
||||
continue
|
||||
import_apikey.delay(api_key=acc.api_key, api_userid=acc.api_user_id)
|
||||
import_apikey.delay(api_key=acc.api_key, api_userid=acc.pk)
|
||||
|
||||
|
||||
@task(ignore_result=True)
|
||||
@@ -76,78 +85,123 @@ def import_apikey_result(api_userid, api_key, user=None, force_cache=False, call
|
||||
def import_apikey_func(api_userid, api_key, user=None, force_cache=False, log=logging.getLogger(__name__)):
|
||||
log.info('Importing %s/%s' % (api_userid, api_key))
|
||||
|
||||
auth_params = {'userid': api_userid, 'apikey': api_key}
|
||||
account_doc = CachedDocument.objects.api_query('/account/Characters.xml.aspx', params=auth_params, no_cache=force_cache)
|
||||
try:
|
||||
account = EVEAccount.objects.get(pk=api_userid)
|
||||
except EVEAccount.DoesNotExist:
|
||||
account = None
|
||||
|
||||
doc = basic_xml_parse_doc(account_doc)['eveapi']
|
||||
# Use CAK if enabled and either a new key or flagged as so
|
||||
if (gargoyle.is_active('eve-cak') and (not account or account.is_cak)):
|
||||
auth_params = {'keyid': api_userid, 'vcode': api_key}
|
||||
keycheck = CachedDocument.objects.api_query('/account/APIKeyInfo.xml.aspx', params=auth_params, no_cache=True)
|
||||
doc = basic_xml_parse_doc(keycheck)['eveapi']
|
||||
|
||||
# Checks for a document error
|
||||
if 'error' in doc:
|
||||
try:
|
||||
account = EVEAccount.objects.get(api_user_id=api_userid)
|
||||
except EVEAccount.DoesNotExist:
|
||||
# If no Account exists in the DB, just ignore it
|
||||
return
|
||||
if not 'error' in doc:
|
||||
if not account:
|
||||
account, created = EVEAccount.objects.get_or_create(pk=api_userid)
|
||||
if user:
|
||||
account.user = User.objects.get(id=user)
|
||||
if not account.api_key == api_key:
|
||||
account.api_key = api_key
|
||||
|
||||
keydoc = doc['result']['key']
|
||||
if keydoc['type'] == 'Character':
|
||||
account.api_keytype = API_KEYTYPE_CHARACTER
|
||||
elif keydoc['type'] == 'Corporation':
|
||||
account.api_keytype = API_KEYTYPE_CORPORATION
|
||||
elif keydoc['type'] == 'Account':
|
||||
account.api_keytype = API_KEYTYPE_ACCOUNT
|
||||
account.api_accessmask = keydoc['accessMask']
|
||||
if not keydoc['expires'] == '':
|
||||
account.api_expiry = datetime.strptime(keydoc['expires'], '%Y-%m-%d %H:%M:%S')
|
||||
account.api_status = API_STATUS_OK
|
||||
|
||||
# Remove deleted or traded characters
|
||||
newcharlist = [int(char['characterID']) for char in doc['result']['key']['characters']]
|
||||
for char in account.characters.all().exclude(id__in=newcharlist):
|
||||
account.characters.remove(char)
|
||||
|
||||
# Schedule a task to update the characters
|
||||
if account.user:
|
||||
cb = update_user_access.subtask(kwargs={'user': account.user.id })
|
||||
else:
|
||||
cb = None
|
||||
import_eve_characters.delay(account.characters.all().values_list('id', flat=True), key_id=account.pk, callback=cb)
|
||||
|
||||
error = doc['error']['code']
|
||||
if int(error) >= 500:
|
||||
# API disabled, down or rejecting, return without changes
|
||||
return
|
||||
elif error in ['202', '203', '204', '205', '212']:
|
||||
account.api_status = API_STATUS_AUTH_ERROR
|
||||
elif error == '211':
|
||||
account.api_status = API_STATUS_ACC_EXPIRED
|
||||
else:
|
||||
account.api_status = API_STATUS_OTHER_ERROR
|
||||
account.api_last_updated = datetime.utcnow()
|
||||
account.save()
|
||||
if account.user:
|
||||
update_user_access.delay(account.user.id)
|
||||
return account
|
||||
# No account object, just return
|
||||
if not account:
|
||||
return
|
||||
|
||||
# Create or retrieve the account last to make sure everything
|
||||
# before here is good to go.
|
||||
account, created = EVEAccount.objects.get_or_create(api_user_id=api_userid)
|
||||
if not account.api_key == api_key:
|
||||
account.api_key = api_key
|
||||
account.api_keytype = API_KEYTYPE_UNKNOWN
|
||||
account.api_status = API_STATUS_OK
|
||||
if user and created:
|
||||
account.user = User.objects.get(id=user)
|
||||
error = doc['error']['code']
|
||||
if int(error) >= 500:
|
||||
# API disabled, down or rejecting, return without changes
|
||||
return
|
||||
elif error in ['202', '203', '204', '205', '212']:
|
||||
account.api_status = API_STATUS_AUTH_ERROR
|
||||
elif error == '211':
|
||||
account.api_status = API_STATUS_ACC_EXPIRED
|
||||
else:
|
||||
account.api_status = API_STATUS_OTHER_ERROR
|
||||
|
||||
if account.user:
|
||||
update_user_access.delay(account.user.id)
|
||||
|
||||
else:
|
||||
auth_params = {'userid': api_userid, 'apikey': api_key}
|
||||
account_doc = CachedDocument.objects.api_query('/account/Characters.xml.aspx', params=auth_params, no_cache=force_cache)
|
||||
doc = basic_xml_parse_doc(account_doc)['eveapi']
|
||||
|
||||
if not 'error' in doc:
|
||||
if not account:
|
||||
account, created = EVEAccount.objects.get_or_create(pk=api_userid)
|
||||
if user:
|
||||
account.user = User.objects.get(id=user)
|
||||
if not account.api_key == api_key:
|
||||
account.api_key = api_key
|
||||
account.api_status = API_STATUS_OK
|
||||
|
||||
keycheck = CachedDocument.objects.api_query('/account/AccountStatus.xml.aspx', params=auth_params, no_cache=True)
|
||||
keydoc = basic_xml_parse_doc(keycheck)['eveapi']
|
||||
if 'error' in keydoc:
|
||||
account.api_keytype = API_KEYTYPE_LIMITED
|
||||
elif not 'error' in keydoc:
|
||||
account.api_keytype = API_KEYTYPE_FULL
|
||||
else:
|
||||
account.api_keytype = API_KEYTYPE_UNKNOWN
|
||||
|
||||
# Remove deleted or traded characters
|
||||
newcharlist = [int(char['characterID']) for char in doc['result']['characters']]
|
||||
for char in account.characters.all().exclude(id__in=newcharlist):
|
||||
account.characters.remove(char)
|
||||
|
||||
# Schedule a task to update the characters
|
||||
if account.user:
|
||||
cb = update_user_access.subtask(kwargs={'user': account.user.id })
|
||||
else:
|
||||
cb = None
|
||||
import_eve_characters.delay(account.characters.all().values_list('id', flat=True), key_id=account.pk, callback=cb)
|
||||
|
||||
else:
|
||||
# No account object, just return
|
||||
if not account:
|
||||
return
|
||||
|
||||
error = doc['error']['code']
|
||||
if int(error) >= 500:
|
||||
# API disabled, down or rejecting, return without changes
|
||||
return
|
||||
elif error in ['202', '203', '204', '205', '212']:
|
||||
account.api_status = API_STATUS_AUTH_ERROR
|
||||
elif error == '211':
|
||||
account.api_status = API_STATUS_ACC_EXPIRED
|
||||
else:
|
||||
account.api_status = API_STATUS_OTHER_ERROR
|
||||
|
||||
if account.user:
|
||||
update_user_access.delay(account.user.id)
|
||||
|
||||
account.api_last_updated = datetime.utcnow()
|
||||
account.save()
|
||||
|
||||
# Check API keytype if we have a character and a unknown key status
|
||||
if account.api_keytype == API_KEYTYPE_UNKNOWN:
|
||||
keycheck = CachedDocument.objects.api_query('/account/AccountStatus.xml.aspx', params=auth_params, no_cache=True)
|
||||
keydoc = basic_xml_parse_doc(keycheck)['eveapi']
|
||||
|
||||
if 'error' in keydoc:
|
||||
account.api_keytype = API_KEYTYPE_LIMITED
|
||||
elif not 'error' in keydoc:
|
||||
account.api_keytype = API_KEYTYPE_FULL
|
||||
else:
|
||||
account.api_keytype = API_KEYTYPE_UNKNOWN
|
||||
|
||||
account.api_last_updated = datetime.utcnow()
|
||||
account.save()
|
||||
|
||||
tasklist = []
|
||||
|
||||
# Process the account's character list
|
||||
charlist = set(account.characters.all().values_list('id', flat=True))
|
||||
newcharlist = [int(char['characterID']) for char in doc['result']['characters']]
|
||||
|
||||
log.info("[CHAR] Current %s, New: %s, Remove: %s" % (charlist, newcharlist, set(charlist - set(newcharlist))))
|
||||
|
||||
for char in account.characters.filter(id__in=set(charlist - set(newcharlist))):
|
||||
account.characters.remove(char)
|
||||
|
||||
if account.user:
|
||||
cb = update_user_access.subtask(kwargs={'user': account.user.id })
|
||||
else:
|
||||
cb = None
|
||||
import_eve_characters.delay(newcharlist, api_key, api_userid, callback=cb)
|
||||
|
||||
return account
|
||||
|
||||
|
||||
Reference in New Issue
Block a user