Esempio n. 1
0
def client_echo_valid(endpoint):
    """
    This will validate if the user's endpoint is valid and returning the echo
    data sent to it
    """
    try:
        request_handler = RequestHandler(verify_ssl=False,
                                         request_timeout=REQUEST_TIMEOUT)
        validation_key = common.generate_key()

        try:
            returned_json, status_code = request_handler.get(
                endpoint, params={'echo': validation_key})
        # pylint: disable=W0703
        except:
            return False

        if status_code != client.OK:
            return False
        if returned_json['echo'] != validation_key:
            return False
    # pylint: disable=W0703
    except Exception:
        return False

    return True
Esempio n. 2
0
def client_echo_valid(endpoint):
    """
    This will validate if the user's endpoint is valid and returning the echo
    data sent to it
    """
    try:
        request_handler = RequestHandler(
            verify_ssl=False, request_timeout=REQUEST_TIMEOUT)
        validation_key = common.generate_key()

        try:
            returned_json, status_code = request_handler.get(
                endpoint, params={'echo': validation_key})
        # pylint: disable=W0703
        except:
            return False

        if status_code != client.OK:
            return False
        if returned_json['echo'] != validation_key:
            return False
    # pylint: disable=W0703
    except Exception:
        return False

    return True
Esempio n. 3
0
    def post(self):
        """
        Creates a new account
        """
        json_data = request.get_json()

        if not client_echo_valid(json_data['endpoint']):
            return make_response(jsonify({'Error': 'Echo response failed'}),
                                 client.BAD_REQUEST)

        return insert_account(DEFAULT_ACCOUNTS_TABLE,
                              **{'username': json_data['username'],
                                 'endpoint': json_data['endpoint'],
                                 'is_admin': False,
                                 'failed_count': 0,
                                 'api_key': generate_key(),
                                 'secret_key': generate_key()})
Esempio n. 4
0
def create_admin_account():
    """
    Creates a new admin account
    """
    try:
        original_api_key = generate_key()
        secret_key = generate_key()
        hashed_api_key = generate_password_hash(original_api_key)

        Interactions.insert(DEFAULT_ACCOUNTS_TABLE,
                            **{'username': '******',
                               'endpoint': '',
                               'is_admin': True,
                               'api_key': hashed_api_key,
                               'secret_key': secret_key})

        return {'api_key': original_api_key, 'secret_key': secret_key}
    except (RqlRuntimeError, RqlDriverError) as err:
        raise err
Esempio n. 5
0
def reset_key(username, key_type):
    """
    Resets either a secret key or api key
    """
    try:
        # Note: The validate_username_in_header decorator will verify the
        # username and record. The api_key_restricted_resource will validate
        # the username as well as a valid API key
        record = Interactions.query(DEFAULT_ACCOUNTS_TABLE,
                                    filters={"username": username})
        endpoint = record[0]['endpoint']

        if not endpoint:
            return make_response(
                jsonify({'Error': 'Endpoint not found'}),
                client.NOT_FOUND
            )

        new_key = common.generate_key()
        salted_new_key = generate_password_hash(new_key)

        if not client_reset_key(endpoint, key_type, new_key):
            return make_response(
                jsonify({'Error': 'Failed to contact the endpoint or wrong '
                                  'HTTP status code returned'}),
                client.BAD_REQUEST
            )

        if key_type == 'api_key':
            update = {key_type: salted_new_key}
        else:
            update = {key_type: new_key}

        Interactions.update(DEFAULT_ACCOUNTS_TABLE,
                            filters={"username": username},
                            updates=update)

        return make_response(jsonify({'Message': 'New key sent to endpoint'}),
                             client.OK)
    except RqlRuntimeError as runtime_err:
        return make_response(jsonify({'Error': runtime_err.message}),
                             client.INTERNAL_SERVER_ERROR)
    except RqlDriverError as rql_err:
        return make_response(jsonify({'Error': rql_err.message}),
                             client.INTERNAL_SERVER_ERROR)
Esempio n. 6
0
def reset_key(username, key_type):
    """
    Resets either a secret key or api key
    """
    try:
        # Note: The validate_username_in_header decorator will verify the
        # username and record. The api_key_restricted_resource will validate
        # the username as well as a valid API key
        record = Interactions.query(DEFAULT_ACCOUNTS_TABLE,
                                    filters={"username": username})
        endpoint = record[0]['endpoint']

        if not endpoint:
            return make_response(jsonify({'Error': 'Endpoint not found'}),
                                 client.NOT_FOUND)

        new_key = common.generate_key()
        salted_new_key = generate_password_hash(new_key)

        if not client_reset_key(endpoint, key_type, new_key):
            return make_response(
                jsonify({
                    'Error':
                    'Failed to contact the endpoint or wrong '
                    'HTTP status code returned'
                }), client.BAD_REQUEST)

        if key_type == 'api_key':
            update = {key_type: salted_new_key}
        else:
            update = {key_type: new_key}

        Interactions.update(DEFAULT_ACCOUNTS_TABLE,
                            filters={"username": username},
                            updates=update)

        return make_response(jsonify({'Message': 'New key sent to endpoint'}),
                             client.OK)
    except RqlRuntimeError as runtime_err:
        return make_response(jsonify({'Error': runtime_err.message}),
                             client.INTERNAL_SERVER_ERROR)
    except RqlDriverError as rql_err:
        return make_response(jsonify({'Error': rql_err.message}),
                             client.INTERNAL_SERVER_ERROR)
Esempio n. 7
0
 def test_generate_key(self):
     self.assertTrue(generate_key())
     self.assertEqual(len(generate_key()), 40)