Exemplo n.º 1
0
    def synchronize_eveapis(user=None):
        seat_all_keys = SeatManager.get_all_seat_eveapis()
        userinfo = None
        # retrieve only user-specific api keys if user is specified
        if user:
            keypars = EveManager.get_api_key_pairs(user)
            try:
                userinfo = SeatManager.check_user_status(user.seat.username)
            except ObjectDoesNotExist:
                pass
        else:
            # retrieve all api keys instead
            keypars = EveManager.get_all_api_key_pairs()
        if keypars:
            for keypar in keypars:
                if keypar.api_id not in seat_all_keys.keys():
                    #Add new keys
                    logger.debug("Adding Api Key with ID %s" % keypar.api_id)
                    ret = SeatManager.exec_request('key', 'post', key_id=keypar.api_id, v_code=keypar.api_key)
                    logger.debug(ret)
                else:
                    # remove it from the list so it doesn't get deleted in the last step
                    seat_all_keys.pop(keypar.api_id)
                if not userinfo:  # TODO: should the following be done only for new keys?
                    # Check the key's user status
                    logger.debug("Retrieving user name from Auth's SeAT users database")
                    try:
                        if keypar.user.seat.username:
                            logger.debug("Retrieving user %s info from SeAT users database" % keypar.user.seat.username)
                            userinfo = SeatManager.check_user_status(keypar.user.seat.username)
                    except ObjectDoesNotExist:
                        pass
                if userinfo:
                    try:
                        # If the user has activated seat, assign the key to him.
                        logger.debug("Transferring Api Key with ID %s to user %s with ID %s " % (
                            keypar.api_id,
                            keypar.user.seat.username,
                            userinfo['id']))
                        ret = SeatManager.exec_request('key/transfer/{}/{}'.format(keypar.api_id, userinfo['id']),
                                                       'get')
                        logger.debug(ret)
                    except ObjectDoesNotExist:
                        logger.debug("User does not have SeAT activated, could not assign key to user")

        if bool(seat_all_keys) and not user and hasattr(settings, 'SEAT_PURGE_DELETED') and settings.SEAT_PURGE_DELETED:
            # remove from SeAT keys that were removed from Auth
            for key, key_user in iteritems(seat_all_keys):
                # Remove the key only if it is an account or character key
                ret = SeatManager.exec_request('key/{}'.format(key), 'get')
                logger.debug(ret)
                try:
                    if (ret['info']['type'] == "Account") or (ret['info']['type'] == "Character"):
                        logger.debug("Removing api key %s from SeAT database" % key)
                        ret = SeatManager.exec_request('key/{}'.format(key), 'delete')
                        logger.debug(ret)
                except KeyError:
                    pass
Exemplo n.º 2
0
    def synchronize_eveapis(user=None):
        seat_all_keys = SeatManager.get_all_seat_eveapis()
        userinfo = None
        # retrieve only user-specific api keys if user is specified
        if user:
            auth, c = AuthServicesInfo.objects.get_or_create(user=user.id)
            keypars = EveManager.get_api_key_pairs(user)
            if auth.seat_username:
                userinfo = SeatManager.check_user_status(auth.seat_username)
        else:
            # retrieve all api keys instead
            keypars = EveManager.get_all_api_key_pairs()
        if keypars:
            for keypar in keypars:
                if keypar.api_id not in seat_all_keys.keys():
                    #Add new keys
                    logger.debug("Adding Api Key with ID %s" % keypar.api_id)
                    ret = SeatManager.exec_request('key', 'post', key_id=keypar.api_id, v_code=keypar.api_key)
                    logger.debug(ret)
                else:
                    # remove it from the list so it doesn't get deleted in the last step
                    seat_all_keys.pop(keypar.api_id)
                if not userinfo:  # TODO: should the following be done only for new keys?
                    # Check the key's user status
                    logger.debug("Retrieving user name from Auth's SeAT users database")
                    auth, c = AuthServicesInfo.objects.get_or_create(user=keypar.user)
                    if auth.seat_username:
                        logger.debug("Retrieving user %s info from SeAT users database" % auth.seat_username)
                        userinfo = SeatManager.check_user_status(auth.seat_username)
                if userinfo:
                    # If the user has activated seat, assign the key to him.
                    logger.debug("Transferring Api Key with ID %s to user %s with ID %s " % (keypar.api_id, auth.seat_username,
                                                                                             userinfo['id']))
                    ret = SeatManager.exec_request('key/transfer/' + keypar.api_id + '/' + userinfo['id'], 'get')
                    logger.debug(ret)

        if bool(seat_all_keys) & (not user):
            # remove from SeAT keys that were removed from Auth
            for key, key_user in seat_all_keys.iteritems():
                # Remove the key only if it is an account or character key
                ret = SeatManager.exec_request('key/'+key, 'get')
                logger.debug(ret)
                try:
                    if (ret['info']['type'] == "Account") or (ret['info']['type'] == "Character"):
                        logger.debug("Removing api key %s from SeAT database" % key)
                        ret = SeatManager.exec_request('key' + "/" + key, 'delete')
                        logger.debug(ret)
                except KeyError:
                    pass
Exemplo n.º 3
0
    def synchronize_eveapis(cls, user=None):

        # Fetch all of the API keys stored in SeAT already
        seat_all_keys = cls.get_all_seat_eveapis()

        # retrieve only user-specific api keys if user is specified
        if user:
            keypairs = EveManager.get_api_key_pairs(user)
        else:
            # retrieve all api keys instead
            keypairs = EveManager.get_all_api_key_pairs()

        for keypair in keypairs:
            # Transfer the key if it isn't already in SeAT
            if keypair.api_id not in seat_all_keys.keys():
                # Add new keys
                logger.debug("Adding Api Key with ID %s" % keypair.api_id)
                try:
                    ret = cls.exec_request('key',
                                           'post',
                                           key_id=keypair.api_id,
                                           v_code=keypair.api_key,
                                           raise_for_status=True)
                    logger.debug(ret)
                except requests.HTTPError as e:
                    if e.response.status_code == 400:
                        logger.debug("API key already exists")
                    else:
                        logger.exception("API key sync failed")
                        continue  # Skip the rest of the key processing
            else:
                # remove it from the list so it doesn't get deleted in the last step
                seat_all_keys.pop(keypair.api_id)

            # Attach API key to the users SeAT account, if possible
            try:
                userinfo = cache.get_or_set(
                    'seat_user_status_' +
                    cls.username_hash(keypair.user.seat.username),
                    lambda: cls.check_user_status(keypair.user.seat.username),
                    300)  # Cache for 5 minutes

                if not bool(userinfo):
                    # No SeAT account, skip
                    logger.debug(
                        "Could not find users SeAT id, cannot assign key to them"
                    )
                    continue

                # If the user has activated seat, assign the key to them
                logger.debug(
                    "Transferring Api Key with ID %s to user %s with ID %s " %
                    (keypair.api_id, keypair.user.seat.username,
                     userinfo['id']))
                ret = cls.exec_request(
                    'key/transfer/{}/{}'.format(keypair.api_id,
                                                userinfo['id']), 'get')
                logger.debug(ret)
            except ObjectDoesNotExist:
                logger.debug(
                    "User does not have SeAT activated, could not assign key to user"
                )

        if bool(seat_all_keys) and not user and getattr(
                settings, 'SEAT_PURGE_DELETED', False):
            # remove from SeAT keys that were removed from Auth
            for key, key_user in iteritems(seat_all_keys):
                # Remove the key only if it is an account or character key
                ret = cls.exec_request('key/{}'.format(key), 'get')
                logger.debug(ret)
                try:
                    if (ret['info']['type']
                            == "Account") or (ret['info']['type']
                                              == "Character"):
                        logger.debug("Removing api key %s from SeAT database" %
                                     key)
                        ret = cls.exec_request('key/{}'.format(key), 'delete')
                        logger.debug(ret)
                except KeyError:
                    pass
Exemplo n.º 4
0
    def synchronize_eveapis(user=None):
        seat_all_keys = SeatManager.get_all_seat_eveapis()
        userinfo = None
        # retrieve only user-specific api keys if user is specified
        if user:
            keypars = EveManager.get_api_key_pairs(user)
            try:
                userinfo = SeatManager.check_user_status(user.seat.username)
            except ObjectDoesNotExist:
                pass
        else:
            # retrieve all api keys instead
            keypars = EveManager.get_all_api_key_pairs()
        if keypars:
            for keypar in keypars:
                if keypar.api_id not in seat_all_keys.keys():
                    #Add new keys
                    logger.debug("Adding Api Key with ID %s" % keypar.api_id)
                    ret = SeatManager.exec_request('key',
                                                   'post',
                                                   key_id=keypar.api_id,
                                                   v_code=keypar.api_key)
                    logger.debug(ret)
                else:
                    # remove it from the list so it doesn't get deleted in the last step
                    seat_all_keys.pop(keypar.api_id)
                if not userinfo:  # TODO: should the following be done only for new keys?
                    # Check the key's user status
                    logger.debug(
                        "Retrieving user name from Auth's SeAT users database")
                    try:
                        if keypar.user.seat.username:
                            logger.debug(
                                "Retrieving user %s info from SeAT users database"
                                % keypar.user.seat.username)
                            userinfo = SeatManager.check_user_status(
                                keypar.user.seat.username)
                    except ObjectDoesNotExist:
                        pass
                if userinfo:
                    try:
                        # If the user has activated seat, assign the key to him.
                        logger.debug(
                            "Transferring Api Key with ID %s to user %s with ID %s "
                            % (keypar.api_id, keypar.user.seat.username,
                               userinfo['id']))
                        ret = SeatManager.exec_request(
                            'key/transfer/{}/{}'.format(
                                keypar.api_id, userinfo['id']), 'get')
                        logger.debug(ret)
                    except ObjectDoesNotExist:
                        logger.debug(
                            "User does not have SeAT activated, could not assign key to user"
                        )

        if bool(seat_all_keys) and not user and hasattr(
                settings,
                'SEAT_PURGE_DELETED') and settings.SEAT_PURGE_DELETED:
            # remove from SeAT keys that were removed from Auth
            for key, key_user in iteritems(seat_all_keys):
                # Remove the key only if it is an account or character key
                ret = SeatManager.exec_request('key/{}'.format(key), 'get')
                logger.debug(ret)
                try:
                    if (ret['info']['type']
                            == "Account") or (ret['info']['type']
                                              == "Character"):
                        logger.debug("Removing api key %s from SeAT database" %
                                     key)
                        ret = SeatManager.exec_request('key/{}'.format(key),
                                                       'delete')
                        logger.debug(ret)
                except KeyError:
                    pass
Exemplo n.º 5
0
    def synchronize_eveapis(cls, user=None):

        # Fetch all of the API keys stored in SeAT already
        seat_all_keys = cls.get_all_seat_eveapis()

        # retrieve only user-specific api keys if user is specified
        if user:
            keypairs = EveManager.get_api_key_pairs(user)
        else:
            # retrieve all api keys instead
            keypairs = EveManager.get_all_api_key_pairs()

        for keypair in keypairs:
            # Transfer the key if it isn't already in SeAT
            if keypair.api_id not in seat_all_keys.keys():
                # Add new keys
                logger.debug("Adding Api Key with ID %s" % keypair.api_id)
                try:
                    ret = cls.exec_request('key', 'post',
                                           key_id=keypair.api_id,
                                           v_code=keypair.api_key,
                                           raise_for_status=True)
                    logger.debug(ret)
                except requests.HTTPError as e:
                    if e.response.status_code == 400:
                        logger.debug("API key already exists")
                    else:
                        logger.exception("API key sync failed")
                        continue  # Skip the rest of the key processing
            else:
                # remove it from the list so it doesn't get deleted in the last step
                seat_all_keys.pop(keypair.api_id)

            # Attach API key to the users SeAT account, if possible
            try:
                userinfo = cache.get_or_set('seat_user_status_' + cls.username_hash(keypair.user.seat.username),
                                            lambda: cls.check_user_status(keypair.user.seat.username),
                                            300)  # Cache for 5 minutes

                if not bool(userinfo):
                    # No SeAT account, skip
                    logger.debug("Could not find users SeAT id, cannot assign key to them")
                    continue

                # If the user has activated seat, assign the key to them
                logger.debug("Transferring Api Key with ID %s to user %s with ID %s " % (
                    keypair.api_id,
                    keypair.user.seat.username,
                    userinfo['id']))
                ret = cls.exec_request('key/transfer/{}/{}'.format(keypair.api_id, userinfo['id']),
                                       'get')
                logger.debug(ret)
            except ObjectDoesNotExist:
                logger.debug("User does not have SeAT activated, could not assign key to user")

        if bool(seat_all_keys) and not user and getattr(settings, 'SEAT_PURGE_DELETED', False):
            # remove from SeAT keys that were removed from Auth
            for key, key_user in iteritems(seat_all_keys):
                # Remove the key only if it is an account or character key
                ret = cls.exec_request('key/{}'.format(key), 'get')
                logger.debug(ret)
                try:
                    if (ret['info']['type'] == "Account") or (ret['info']['type'] == "Character"):
                        logger.debug("Removing api key %s from SeAT database" % key)
                        ret = cls.exec_request('key/{}'.format(key), 'delete')
                        logger.debug(ret)
                except KeyError:
                    pass