def handler_from_scope_params(permission, extra_params):
    """
    Construct the given permission's handler using the scope-specific extra
    arguments (received from the API). This method filters the extra_params
    to the values actually needed by the handler constructor.
    """

    # Filter the list of arguments of the handler factory __call__()
    # with the arguments given by the API.
    args_to_pass = set(extra_params).intersection(permission.CALL_ARGS)

    kwargs = {key: extra_params[key] for key in args_to_pass}
    return permission(**kwargs)