def refresh_api(api_key_pair): logger.debug("Running update on api key %s" % api_key_pair.api_id) user = api_key_pair.user if EveApiManager.api_key_is_valid(api_key_pair.api_id, api_key_pair.api_key): #check to ensure API key meets min spec logger.info("Determined api key %s is still active." % api_key_pair.api_id) still_valid = True state = determine_membership_by_user(user) if state == "BLUE": if settings.BLUE_API_ACCOUNT: if not EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key): logger.info("Determined api key %s for blue user %s is no longer type account as requred." % (api_key_pair.api_id, user)) still_valid = False notify(user, "API Failed Validation", message="Your API key ID %s is not account-wide as required." % api_key_pair.api_id, level="danger") if not EveApiManager.check_blue_api_is_full(api_key_pair.api_id, api_key_pair.api_key): logger.info("Determined api key %s for blue user %s no longer meets minimum access mask as required." % (api_key_pair.api_id, user)) still_valid = False notify(user, "API Failed Validation", message="Your API key ID %s does not meet access mask requirements." % api_key_pair.api_id, level="danger") elif state == "MEMBER": if settings.MEMBER_API_ACCOUNT: if not EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key): logger.info("Determined api key %s for user %s is no longer type account as required." % (api_key_pair.api_id, user)) still_valid = False notify(user, "API Failed Validation", message="Your API key ID %s is not account-wide as required." % api_key_pair.api_id, level="danger") if not EveApiManager.check_api_is_full(api_key_pair.api_id, api_key_pair.api_key): logger.info("Determined api key %s for user %s no longer meets minimum access mask as required." % (api_key_pair.api_id, user)) still_valid = False notify(user, "API Failed Validation", message="Your API key ID %s does not meet access mask requirements." % api_key_pair.api_id, level="danger") if not still_valid: logger.debug("API key %s has failed validation; it and its characters will be deleted." % api_key_pair.api_id) EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id) EveManager.delete_api_key_pair(api_key_pair.api_id, user.id) notify(user, "API Key Deleted", message="Your API key ID %s has failed validation. It and its associated characters have been deleted." % api_key_pair.api_id, level="danger") else: logger.info("Determined api key %s still meets requirements." % api_key_pair.api_id) # Update characters characters = EveApiManager.get_characters_from_api(api_key_pair.api_id, api_key_pair.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']): new_character = True logger.debug("API key %s has a new character on the account: %s" % (api_key_pair.api_id, characters.result[char]['name'])) if new_character: logger.debug("Creating new character %s from api key %s" % (characters.result[char]['name'], api_key_pair.api_id)) EveManager.create_characters_from_list(characters, user, api_key_pair.api_id) current_chars = EveCharacter.objects.filter(api_id=api_key_pair.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_key_pair.api_id)) c.delete() else: logger.debug("API key %s is no longer valid; it and its characters will be deleted." % api_key_pair.api_id) EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id) EveManager.delete_api_key_pair(api_key_pair.api_id, user.id) notify(user, "API Key Deleted", message="Your API key ID %s is invalid. It and its associated characters have been deleted." % api_key_pair.api_id, level="danger")
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")
def add_api_key(request): logger.debug("add_api_key called by user %s" % request.user) if request.method == 'POST': form = UpdateKeyForm(request.user, request.POST) logger.debug("Request type POST with form valid: %s" % form.is_valid()) if form.is_valid(): if EveApiKeyPair.objects.filter(api_id=form.cleaned_data['api_id'], api_key=form.cleaned_data['api_key']).exists(): # allow orphaned keys to proceed to SSO validation upon re-entry api_key = EveApiKeyPair.objects.get(api_id=form.cleaned_data['api_id'], api_key=form.cleaned_data['api_key']) elif EveApiKeyPair.objects.filter(api_id=form.cleaned_data['api_id']).exists(): logger.warn('API %s re-added with different vcode.' % form.cleaned_data['api_id']) EveApiKeyPair.objects.filter(api_id=form.cleaned_data['api_id']).delete() api_key = EveApiKeyPair.objects.create(api_id=form.cleaned_data['api_id'], api_key=form.cleaned_data['api_key']) else: api_key = EveApiKeyPair.objects.create(api_id=form.cleaned_data['api_id'], api_key=form.cleaned_data['api_key']) owner = None if not settings.API_SSO_VALIDATION: # set API and character owners if SSO validation not requested api_key.user = request.user api_key.save() owner = request.user # Grab characters associated with the key pair characters = EveApiManager.get_characters_from_api(form.cleaned_data['api_id'], form.cleaned_data['api_key']) EveManager.create_characters_from_list(characters, owner, form.cleaned_data['api_id']) logger.info("Successfully processed api add form for user %s" % request.user) if not settings.API_SSO_VALIDATION: messages.success(request, 'Added API key %s to your account.' % form.cleaned_data['api_id']) auth = AuthServicesInfo.objects.get_or_create(user=request.user)[0] if not auth.main_char_id: return redirect('auth_characters') return redirect("/api_key_management/") else: logger.debug('Requesting SSO validation of API %s by user %s' % (api_key.api_id, request.user)) return render(request, 'registered/apisso.html', context={'api':api_key}) else: logger.debug("Form invalid: returning to form.") else: logger.debug("Providing empty update key form for user %s" % request.user) form = UpdateKeyForm(request.user) context = {'form': form, 'apikeypairs': EveManager.get_api_key_pairs(request.user.id)} return render(request, 'registered/addapikey.html', context=context)
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")
def run_api_refresh(): users = User.objects.all() logger.debug("Running api refresh on %s users." % len(users)) for user in users: # Check if the api server is online logger.debug("Running api refresh for user %s" % user) if EveApiManager.check_if_api_server_online(): api_key_pairs = EveManager.get_api_key_pairs(user.id) logger.debug("User %s has api key pairs %s" % (user, api_key_pairs)) if api_key_pairs: authserviceinfo, c = AuthServicesInfo.objects.get_or_create(user=user) logger.debug("User %s has api keys. Proceeding to refresh." % user) for api_key_pair in api_key_pairs: logger.debug("Running update on api key %s" % api_key_pair.api_id) if EveApiManager.api_key_is_valid(api_key_pair.api_id, api_key_pair.api_key): # check to ensure API key meets min spec logger.info("Determined api key %s is still active." % api_key_pair.api_id) still_valid = True state = determine_membership_by_user(user) if state == "BLUE": if settings.BLUE_API_ACCOUNT: type = EveApiManager.check_api_is_type_account( api_key_pair.api_id, api_key_pair.api_key ) if type == None: api_key_pair.error_count += 1 api_key_pair.save() logger.info( "API key %s incurred an error checking if type account. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count) ) still_valid = None elif type == False: logger.info( "Determined api key %s for blue user %s is no longer type account as requred." % (api_key_pair.api_id, user) ) still_valid = False full = EveApiManager.check_blue_api_is_full(api_key_pair.api_id, api_key_pair.api_key) if full == None: api_key_pair.error_count += 1 api_key_pair.save() logger.info( "API key %s incurred an error checking if meets mask requirements. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count) ) still_valid = None elif full == False: logger.info( "Determined api key %s for blue user %s no longer meets minimum access mask as required." % (api_key_pair.api_id, user) ) still_valid = False elif state == "MEMBER": if settings.MEMBER_API_ACCOUNT: type = EveApiManager.check_api_is_type_account( api_key_pair.api_id, api_key_pair.api_key ) if type == None: api_key_pair.error_count += 1 api_key_pair.save() logger.info( "API key %s incurred an error checking if type account. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count) ) still_valid = None elif type == False: logger.info( "Determined api key %s for user %s is no longer type account as required." % (api_key_pair.api_id, user) ) still_valid = False full = EveApiManager.check_api_is_full(api_key_pair.api_id, api_key_pair.api_key) if full == None: api_key_pair.error_count += 1 api_key_pair.save() logger.info( "API key %s incurred an error checking if meets mask requirements. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count) ) still_valid = None elif full == False: logger.info( "Determined api key %s for user %s no longer meets minimum access mask as required." % (api_key_pair.api_id, user) ) still_valid = False if still_valid == None: if api_key_pair.error_count >= 3: logger.info( "API key %s has incurred 3 or more errors. Assuming invalid." % api_key_pair.api_id ) still_valid = False if still_valid == False: logger.debug( "API key %s has failed validation; it and its characters will be deleted." % api_key_pair.api_id ) EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id) EveManager.delete_api_key_pair(api_key_pair.api_id, user.id) elif still_valid == True: if api_key_pair.error_count != 0: logger.info( "Clearing error count for api %s as it passed validation" % api_key_pair.api_id ) api_key_pair.error_count = 0 api_key_pair.save() logger.info("Determined api key %s still meets requirements." % api_key_pair.api_id) # Update characters characters = EveApiManager.get_characters_from_api( api_key_pair.api_id, api_key_pair.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"]): new_character = True logger.debug( "API key %s has a new character on the account: %s" % (api_key_pair.api_id, characters.result[char]["name"]) ) if new_character: logger.debug( "Creating new character %s from api key %s" % (characters.result[char]["name"], api_key_pair.api_id) ) EveManager.create_characters_from_list(characters, user, api_key_pair.api_key) else: logger.debug( "API key %s is no longer valid; it and its characters will be deleted." % api_key_pair.api_id ) EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id) EveManager.delete_api_key_pair(api_key_pair.api_id, user.id) # Check our main character if EveCharacter.objects.filter(character_id=authserviceinfo.main_char_id).exists() is False: logger.info( "User %s main character id %s missing model. Clearning main character." % (user, authserviceinfo.main_char_id) ) authserviceinfo.main_char_id = "" authserviceinfo.save() else: logger.warn("User %s has no main character id, unable to validate membership.") set_state(user)
def run_api_refresh(): users = User.objects.all() logger.debug("Running api refresh on %s users." % len(users)) for user in users: # Check if the api server is online logger.debug("Running api refresh for user %s" % user) if EveApiManager.check_if_api_server_online(): api_key_pairs = EveManager.get_api_key_pairs(user.id) logger.debug("User %s has api key pairs %s" % (user, api_key_pairs)) if api_key_pairs: valid_key = False authserviceinfo = AuthServicesInfo.objects.get(user=user) logger.debug("User %s has api keys. Proceeding to refresh." % user) if authserviceinfo.main_char_id: if authserviceinfo.main_char_id != "": #preserve old corp ID for corp change test on members oldcorp_id = 0 if EveManager.get_character_by_id(authserviceinfo.main_char_id): oldcorp_id = EveCharacter.objects.get(character_id=authserviceinfo.main_char_id).corporation_id logger.debug("Determined user %s current main corp id %s" % (user, oldcorp_id)) for api_key_pair in api_key_pairs: logger.debug("Running update on api key %s" % api_key_pair.api_id) if EveApiManager.api_key_is_valid(api_key_pair.api_id, api_key_pair.api_key): #check to ensure API key meets min spec logger.info("Determined api key %s is still active." % api_key_pair.api_id) still_valid = True if authserviceinfo.is_blue: if settings.BLUE_API_ACCOUNT: if not EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key): logger.info("Determined api key %s for blue user %s is no longer type account as requred." % (api_key_pair.api_id, user)) still_valid = False if not EveApiManager.check_blue_api_is_full(api_key_pair.api_id, api_key_pair.api_key): logger.info("Determined api key %s for blue user %s no longer meets minimum access mask as required." % (api_key_pair.api_id, user)) still_valid = False else: if settings.MEMBER_API_ACCOUNT: if not EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key): logger.info("Determined api key %s for user %s is no longer type account as required." % (api_key_pair.api_id, user)) still_valid = False if not EveApiManager.check_api_is_full(api_key_pair.api_id, api_key_pair.api_key): logger.info("Determined api key %s for user %s no longer meets minimum access mask as required." % (api_key_pair.api_id, user)) still_valid = False if still_valid is not True: logger.debug("API key %s has failed validation; it and its characters will be deleted." % api_key_pair.api_id) EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id) EveManager.delete_api_key_pair(api_key_pair.api_id, user.id) else: logger.info("Determined api key %s still meets requirements." % api_key_pair.api_id) # Update characters characters = EveApiManager.get_characters_from_api(api_key_pair.api_id, api_key_pair.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']): new_character = True logger.debug("API key %s has a new character on the account: %s" % (api_key_pair.api_id, characters.result[char]['name'])) if new_character: logger.debug("Creating new character %s from api key %s" % (characters.result[char]['name'], api_key_pair.api_id)) EveManager.create_characters_from_list(characters, user, api_key_pair.api_key) valid_key = True else: logger.debug("API key %s is no longer active; it and its characters will be deleted." % api_key_pair.api_id) EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id) EveManager.delete_api_key_pair(api_key_pair.api_id, user.id) if valid_key: # Check our main character character = EveManager.get_character_by_id(authserviceinfo.main_char_id) logger.debug("User %s has valid api key, checking main character %s" % (user, character)) if character is not None and EveManager.check_if_corporation_exists_by_id(character.corporation_id): corp = EveManager.get_corporation_info_by_id(character.corporation_id) main_corp_id = EveManager.get_charater_corporation_id_by_id(authserviceinfo.main_char_id) main_alliance_id = EveManager.get_charater_alliance_id_by_id(authserviceinfo.main_char_id) logger.debug("User %s main character %s has corp %s with alliance id %s" % (user, character, corp, main_alliance_id)) if (settings.IS_CORP and main_corp_id == settings.CORP_ID) or (not settings.IS_CORP and main_alliance_id == settings.ALLIANCE_ID): logger.debug("User %s corp or alliance meets membership requirements. Ensuring has required permissions and groups." % user) if not check_if_user_has_permission(user, "member"): #transition from none or blue to member if check_if_user_has_permission(user, "blue_member"): #strip blue status logger.debug("Removing user %s blue permission and group to prepare for member transition." % user) remove_member_permission(user, "blue_member") remove_user_from_group(user, settings.DEFAULT_BLUE_GROUP) AuthServicesInfoManager.update_is_blue(False, user) #add to auth group add_member_permission(user, "member") add_user_to_group(user, settings.DEFAULT_AUTH_GROUP) #add to required corp group add_user_to_group(user, generate_corp_group_name(character.corporation_name)) logger.info("User %s transitioned to full member during api refresh." % user) elif corp.corporation_id != oldcorp_id: #changed corps, both corps auth'd, need to change group assignment logger.debug("User %s main character changed corp from id %s to %s, both meet membership requirements. Updating corp group." % (user, oldcorp_id, corp.corporation_id)) oldcorp = EveCorporationInfo.objects.get(corporation_id=oldcorp_id) remove_user_from_group(user, generate_corp_group_name(oldcorp.corporation_name)) add_user_to_group(user, generate_corp_group_name(character.corporation_name)) #reset services to force new mumble names and group assignments deactivate_services(user) logger.info("User %s transferred corps from member to member. Reassigned groups." % user) elif corp is not None: logger.debug("User %s main corp %s does not meet membership requirements." % (user, corp)) if corp.is_blue is not True: if check_if_user_has_permission(user, "member"): #transition from member to nobody disable_alliance_member(user, authserviceinfo.main_char_id) logger.info("User %s no longer member: main has left member corp/alliance." % user) elif check_if_user_has_permission(user, "blue_member"): #transition from blue to nobody disable_blue_member(user) logger.info("User %s no longer blue: main has left blue entities." % user) else: #stay nobody, make sure no services deactivate_services(user) logger.debug("Ensured non-member %s has no services." % user) else: if check_if_user_has_permission(user, "member"): #remove auth member to prepare for member to blue transition disable_alliance_member(user, authserviceinfo.main_char_id) logger.debug("Removed user %s member group/permissions to transition to blue." % user) if not check_if_user_has_permission(user, "blue_member"): #perform nobody to blue transition add_member_permission(user, "blue_member") add_user_to_group(user, settings.DEFAULT_BLUE_GROUP) AuthServicesInfoManager.update_is_blue(True, user) logger.info("User %s transitioned to blue member during api refresh." % user) else: # disable accounts with missing corp model (not blue or member) if check_if_user_has_permission(user, "member"): disable_alliance_member(user, authserviceinfo.main_char_id) logger.info("User %s disabled (previously member) as unable to check missing corp model." % user) elif check_if_user_has_permission(user, "blue_member"): disable_blue_member(user) logger.info("User %s disabled (previously blue) as unable to check missing corp model." % user) else: deactivate_services(user) logger.debug("Ensured non-member %s has no services." % user) else: # nuke it, the hard way disable_expired_member(user) logger.info("User %s disabled due to missing main character or corp model." % user) else: # disable accounts with invalid keys disable_expired_member(user) logger.info("User %s has no valid api keys and has been disabled." % user) else: logger.warn("User %s has no main character id, unable to validate membership.")
def refresh_api(api_key_pair): logger.debug("Running update on api key %s" % api_key_pair.api_id) user = api_key_pair.user if EveApiManager.api_key_is_valid(api_key_pair.api_id, api_key_pair.api_key): #check to ensure API key meets min spec logger.info("Determined api key %s is still active." % api_key_pair.api_id) still_valid = True state = determine_membership_by_user(user) if state == "BLUE": if settings.BLUE_API_ACCOUNT: type = EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key) if type == None: api_key_pair.error_count += 1 api_key_pair.save() logger.info("API key %s incurred an error checking if type account. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count)) still_valid = None elif type == False: logger.info("Determined api key %s for blue user %s is no longer type account as requred." % (api_key_pair.api_id, user)) still_valid = False notify(user, "API Failed Validation", message="Your API key ID %s is not account-wide as required." % api_key_pair.api_id, level="danger") full = EveApiManager.check_blue_api_is_full(api_key_pair.api_id, api_key_pair.api_key) if full == None: api_key_pair.error_count += 1 api_key_pair.save() logger.info("API key %s incurred an error checking if meets mask requirements. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count)) still_valid = None elif full == False: logger.info("Determined api key %s for blue user %s no longer meets minimum access mask as required." % (api_key_pair.api_id, user)) still_valid = False notify(user, "API Failed Validation", message="Your API key ID %s does not meet access mask requirements." % api_key_pair.api_id, level="danger") elif state == "MEMBER": if settings.MEMBER_API_ACCOUNT: type = EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key) if type == None: api_key_pair.error_count += 1 api_key_pair.save() logger.info("API key %s incurred an error checking if type account. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count)) still_valid = None elif type == False: logger.info("Determined api key %s for user %s is no longer type account as required." % (api_key_pair.api_id, user)) still_valid = False notify(user, "API Failed Validation", message="Your API key ID %s is not account-wide as required." % api_key_pair.api_id, level="danger") full = EveApiManager.check_api_is_full(api_key_pair.api_id, api_key_pair.api_key) if full == None: api_key_pair.error_count += 1 api_key_pair.save() logger.info("API key %s incurred an error checking if meets mask requirements. Error count is now %s" % (api_key_pair.api_id, api_key_pair.error_count)) still_valid = None elif full == False: logger.info("Determined api key %s for user %s no longer meets minimum access mask as required." % (api_key_pair.api_id, user)) still_valid = False notify(user, "API Failed Validation", message="Your API key ID %s does not meet access mask requirements." % api_key_pair.api_id, level="danger") if still_valid == None: if api_key_pair.error_count >= 3: logger.info("API key %s has incurred 3 or more errors. Assuming invalid." % api_key_pair.api_id) still_valid = False notify(user, "API Failed Validation", message="Your API key ID %s has accumulated too many errors during refresh and is assumed to be invalid." % api_key_pair.api_id, level="danger") if still_valid == False: logger.debug("API key %s has failed validation; it and its characters will be deleted." % api_key_pair.api_id) EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id) EveManager.delete_api_key_pair(api_key_pair.api_id, user.id) notify(user, "API Key Deleted", message="Your API key ID %s has failed validation. It and its associated characters have been deleted." % api_key_pair.api_id, level="danger") elif still_valid == True: if api_key_pair.error_count != 0: logger.info("Clearing error count for api %s as it passed validation" % api_key_pair.api_id) api_key_pair.error_count = 0 api_key_pair.save() logger.info("Determined api key %s still meets requirements." % api_key_pair.api_id) # Update characters characters = EveApiManager.get_characters_from_api(api_key_pair.api_id, api_key_pair.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']): new_character = True logger.debug("API key %s has a new character on the account: %s" % (api_key_pair.api_id, characters.result[char]['name'])) if new_character: logger.debug("Creating new character %s from api key %s" % (characters.result[char]['name'], api_key_pair.api_id)) EveManager.create_characters_from_list(characters, user, api_key_pair.api_key) else: logger.debug("API key %s is no longer valid; it and its characters will be deleted." % api_key_pair.api_id) EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id) EveManager.delete_api_key_pair(api_key_pair.api_id, user.id) notify(user, "API Key Deleted", message="Your API key ID %s is invalid. It and its associated characters have been deleted." % api_key_pair.api_id, level="danger")
def add_api_key(request): logger.debug("add_api_key called by user %s" % request.user) if request.method == 'POST': form = UpdateKeyForm(request.user, request.POST) logger.debug("Request type POST with form valid: %s" % form.is_valid()) if form.is_valid(): if EveApiKeyPair.objects.filter( api_id=form.cleaned_data['api_id'], api_key=form.cleaned_data['api_key']).exists(): # allow orphaned keys to proceed to SSO validation upon re-entry api_key = EveApiKeyPair.objects.get( api_id=form.cleaned_data['api_id'], api_key=form.cleaned_data['api_key']) elif EveApiKeyPair.objects.filter( api_id=form.cleaned_data['api_id']).exists(): logger.warn('API %s re-added with different vcode.' % form.cleaned_data['api_id']) EveApiKeyPair.objects.filter( api_id=form.cleaned_data['api_id']).delete() api_key = EveApiKeyPair.objects.create( api_id=form.cleaned_data['api_id'], api_key=form.cleaned_data['api_key']) else: api_key = EveApiKeyPair.objects.create( api_id=form.cleaned_data['api_id'], api_key=form.cleaned_data['api_key']) owner = None if not settings.API_SSO_VALIDATION: # set API and character owners if SSO validation not requested api_key.user = request.user api_key.save() owner = request.user # Grab characters associated with the key pair characters = EveApiManager.get_characters_from_api( form.cleaned_data['api_id'], form.cleaned_data['api_key']) EveManager.create_characters_from_list(characters, owner, form.cleaned_data['api_id']) logger.info("Successfully processed api add form for user %s" % request.user) if not settings.API_SSO_VALIDATION: messages.success( request, 'Added API key %s to your account.' % form.cleaned_data['api_id']) auth = AuthServicesInfo.objects.get_or_create( user=request.user)[0] if not auth.main_char_id: return redirect('auth_characters') return redirect("/api_key_management/") else: logger.debug('Requesting SSO validation of API %s by user %s' % (api_key.api_id, request.user)) return render(request, 'registered/apisso.html', context={'api': api_key}) else: logger.debug("Form invalid: returning to form.") else: logger.debug("Providing empty update key form for user %s" % request.user) form = UpdateKeyForm(request.user) context = { 'form': form, 'apikeypairs': EveManager.get_api_key_pairs(request.user.id) } return render(request, 'registered/addapikey.html', context=context)
def run_api_refresh(): users = User.objects.all() for user in users: # Check if the api server is online if EveApiManager.check_if_api_server_online(): api_key_pairs = EveManager.get_api_key_pairs(user.id) if api_key_pairs: valid_key = False authserviceinfo = AuthServicesInfo.objects.get(user=user) print 'Running update on user: '******'Running on ' + api_key_pair.api_id + ':' + api_key_pair.api_key if EveApiManager.api_key_is_valid(api_key_pair.api_id, api_key_pair.api_key): #check to ensure API key meets min spec still_valid = True if authserviceinfo.is_blue: if settings.BLUE_API_ACCOUNT: if not EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key): still_valid = False if not EveApiManager.check_blue_api_is_full(api_key_pair.api_id, api_key_pair.api_key): still_valid = False else: if settings.MEMBER_API_ACCOUNT: if not EveApiManager.check_api_is_type_account(api_key_pair.api_id, api_key_pair.api_key): still_valid = False if not EveApiManager.check_api_is_full(api_key_pair.api_id, api_key_pair.api_key): still_valid = False if still_valid is not True: EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id) EveManager.delete_api_key_pair(api_key_pair.api_id, user.id) else: # Update characters characters = EveApiManager.get_characters_from_api(api_key_pair.api_id, api_key_pair.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']): new_character = True if new_character: EveManager.create_characters_from_list(characters, user, api_key_pair.api_key) valid_key = True else: EveManager.delete_characters_by_api_id(api_key_pair.api_id, user.id) EveManager.delete_api_key_pair(api_key_pair.api_id, user.id) if valid_key: # Check our main character character = EveManager.get_character_by_id(authserviceinfo.main_char_id) if character is not None and EveManager.check_if_corporation_exists_by_id(character.corporation_id): corp = EveManager.get_corporation_info_by_id(character.corporation_id) main_corp_id = EveManager.get_charater_corporation_id_by_id(authserviceinfo.main_char_id) main_alliance_id = EveManager.get_charater_alliance_id_by_id(authserviceinfo.main_char_id) if (settings.IS_CORP and main_corp_id == settings.CORP_ID) or (not settings.IS_CORP and main_alliance_id == settings.ALLIANCE_ID): if not check_if_user_has_permission(user, "member"): #transition from none or blue to member if check_if_user_has_permission(user, "blue_member"): #strip blue status remove_member_permission(user, "blue_member") remove_user_from_group(user, settings.DEFAULT_BLUE_GROUP) AuthServicesInfoManager.update_is_blue(False, user) #add to auth group add_member_permission(user, "member") add_user_to_group(user, settings.DEFAULT_AUTH_GROUP) #add to required corp group add_user_to_group(user, generate_corp_group_name(character.corporation_name)) elif corp.corporation_id != oldcorp_id: #changed corps, both corps auth'd, need to change group assignment oldcorp = EveCorporationInfo.objects.get(corporation_id=oldcorp_id) remove_user_from_group(user, generate_corp_group_name(oldcorp.corporation_name)) add_user_to_group(user, generate_corp_group_name(character.corporation_name)) #reset services to force new mumble names and group assignments deactivate_services(user) elif corp is not None: if corp.is_blue is not True: if check_if_user_has_permission(user, "member"): #transition from member to nobody disable_alliance_member(user, authserviceinfo.main_char_id) elif check_if_user_has_permission(user, "blue_member"): #transition from blue to nobody disable_blue_member(user) else: #stay nobody, make sure no services deactivate_services(user) else: if check_if_user_has_permission(user, "member"): #remove auth member to prepare for member to blue transition disable_alliance_member(user, authserviceinfo.main_char_id) if not check_if_user_has_permission(user, "blue_member"): #perform nobody to blue transition add_member_permission(user, "blue_member") add_user_to_group(user, settings.DEFAULT_BLUE_GROUP) AuthServicesInfoManager.update_is_blue(True, user) else: # disable accounts with missing corp model (not blue or member) if check_if_user_has_permission(user, "member"): disable_alliance_member(user, authserviceinfo.main_char_id) elif check_if_user_has_permission(user, "blue_member"): disable_blue_member(user) else: deactivate_services(user) else: # nuke it, the hard way disable_expired_member(user) else: # disable accounts with invalid keys disable_expired_member(user) else: print 'No main_char_id set'