コード例 #1
0
def address():
    """Get the address of a certain user.

    From the users and servers tables, necessary details are extracted from
    entries containing the given username.

    Returns:
        JSON response containing the address details of a certain user.
        If the user is not found or the server is non existant, a failed JSON
        response is returned.
    """
    username = request.args.get('username')

    # If username is not given, use the logged in username.
    if username is None or username == '':
        username = auth_username()

    if username is None or username == '':
        return bad_json_response("Bad request: Missing parameter 'username'.")

    if users.exists(username=username):
        server_id = users.export_one('server_id', username=username)

        if not servers.exists(id=server_id):
            bad_json_response('Server is not registered.')

        name, address = servers.export_one('name', 'address', id=server_id)
        return good_json_response({
            'name': name,
            'address': address,
            'username': username
        })
    else:
        return bad_json_response('User is not found.')
コード例 #2
0
def edit():
    """Edit a certain users details in the central server.

    The entry with the certain users username is edited in the users table in
    the database.

    Returns:
        Success JSON response if the operation is successful.
        Else a failed JSON response is returned with the correct error message.
    """
    username = get_jwt_identity()

    if users.exists(username=username):
        if 'new_address' in request.form:
            new_address = request.form['new_address']
            if 'new_address' != '':
                if servers.exists(address=new_address):
                    new_id = servers.export_one('id', address=new_address)
                    users.update({'server_id': new_id}, username=username)
                    return good_json_response({'new_address': new_address})
                else:
                    return bad_json_response(
                        'This address does not exist in the database.')
            else:
                return bad_json_response('Address undefined.')
        else:
            return bad_json_response('Incorrect form.')
    else:
        return bad_json_response('No user found with the username ' +
                                 username + '.')
コード例 #3
0
def register():
    """Register a user to the central server.

    For this registration, the username and server address are requested in the
    form. A check is performed to see if the server is live. Then the user is
    inserted into the users table.

    Returns:
        Success JSON response if the operation is successful.
        If the username is valid or the server is not live, a failed JSON
        response is returned.
    """
    username = request.form['username']
    address = request.form['server_address']

    if not servers.exists(address=address):
        return bad_json_response('Server is not registered.')

    server_id = servers.export_one('id', address=address)

    if ping(address):
        if not users.exists(username=username):
            users.insert(username=username, server_id=server_id)
        else:
            return bad_json_response(
                'Username is already taken. Try again :).')
    else:
        return bad_json_response('This data server is not available. '
                                 'Please contact the server owner.')

    return good_json_response('success')
コード例 #4
0
def get_pub_key(username):
    """Helper function to get the public key of a user.
    
    Returns:
        The public key of a user if available, else a bad JSON response.
    """
    server_id = users.export_one('server_id', username=username)
    if server_id is None:
        return bad_json_response('No server_id')

    pub = servers.export_one('pub_key', id=server_id)
    if pub is None:
        return bad_json_response('No pub')

    return pub
コード例 #5
0
def register():
    """Register a data server to the central server.

    For this registration, the server name and address are requested in the
    form. A check is performed to see if the server is live. Then the server
    is inserted into the servers table if it does not already exists.
    
    Returns:
        JSON response containing the server id and the public key
        at success, else a bad JSON response containing the error message.
    """
    name = request.form['name']
    address = request.form['address']

    pub_key = ping(address)
    if pub_key:
        if not servers.exists(address=address):
            result = servers.insert(
                name=name, address=address, pub_key=pub_key)
            return good_json_response({
                'server_id': result,
                'pub_key': pub_key
            })
        else:
            name = servers.export_one('name', address=address)
            return bad_json_response(
                'The data server at "'
                + address
                + '" is already registered by the name "'
                + name
                + '".'
            )
    else:
        return bad_json_response(
            'The data server at "'
            + address +
            '" did not respond. Is the installation correct?'
        )