1
0
mirror of https://github.com/falk-werner/webfuse synced 2024-10-27 20:34:10 +00:00
falk-werner_webfuse/lib/webfuse/adapter/impl/jsonrpc/server.c
Falk Werner 87f34fa768 feat(authentication): provide an authentication mechanism (#19)
* moves server into session

* renames jsonrpc server to jsonrpc proxy

* moves server into session

* renames jsonrpc server to jsonrpc proxy

* adds json rpc server

* removes obsolete proxy from protocol

* changes interface of jsonrpc_proxy_onresult to accept previously parsed messages

* adds infrastructure to process incoming requests; fixes invalid read of ill formatted responses

* adds description of authentication request

* adds authentication request

* adds userdb for authentication purposes

* removes debug code: console.log()

* fixes cmake build error (missing openssl symbols)

* fixes typo

* replaces ASCII art by UML diagram

* renames BAD_NOACCESS to BAD_ACCESS_DENIED

* fixes style

* adds docu of authentication

* ignored false positives of flawfinder

* fixes style issues

* fixes javascript style issues
2019-04-01 22:15:12 +02:00

96 lines
2.9 KiB
C

#include "webfuse/adapter/impl/jsonrpc/server.h"
#include "webfuse/adapter/impl/jsonrpc/method.h"
#include "webfuse/adapter/impl/jsonrpc/request.h"
#include "webfuse/core/util.h"
#include <string.h>
void wf_impl_jsonrpc_server_init(
struct wf_impl_jsonrpc_server * server)
{
server->methods = NULL;
}
void wf_impl_jsonrpc_server_cleanup(
struct wf_impl_jsonrpc_server * server)
{
struct wf_impl_jsonrpc_method * current = server->methods;
while (NULL != current)
{
struct wf_impl_jsonrpc_method * next = current->next;
wf_impl_jsonrpc_method_dispose(current);
current = next;
}
server->methods = NULL;
}
void wf_impl_jsonrpc_server_add(
struct wf_impl_jsonrpc_server * server,
char const * method_name,
wf_impl_jsonrpc_method_invoke_fn * invoke,
void * user_data)
{
struct wf_impl_jsonrpc_method * method = wf_impl_jsonrpc_method_create(method_name, invoke, user_data);
method->next = server->methods;
server->methods = method;
}
static void wf_impl_jsonrpc_server_invalid_method_invoke(
struct wf_impl_jsonrpc_request * request,
char const * WF_UNUSED_PARAM(method_name),
json_t * WF_UNUSED_PARAM(params),
void * WF_UNUSED_PARAM(user_data))
{
wf_impl_jsonrpc_respond_error(request, WF_BAD_NOTIMPLEMENTED);
}
static struct wf_impl_jsonrpc_method const wf_impl_jsonrpc_server_invalid_method =
{
.next = NULL,
.name = "<invalid>",
.invoke = &wf_impl_jsonrpc_server_invalid_method_invoke,
.user_data = NULL
};
static struct wf_impl_jsonrpc_method const * wf_impl_jsonrpc_server_get_method(
struct wf_impl_jsonrpc_server * server,
char const * method_name)
{
struct wf_impl_jsonrpc_method const * current = server->methods;
while (NULL != current)
{
if (0 == strcmp(method_name, current->name))
{
return current;
}
current = current->next;
}
return &wf_impl_jsonrpc_server_invalid_method;
}
void wf_impl_jsonrpc_server_process(
struct wf_impl_jsonrpc_server * server,
json_t * request_data,
wf_impl_jsonrpc_send_fn * send,
void * user_data)
{
json_t * method_holder = json_object_get(request_data, "method");
json_t * params = json_object_get(request_data, "params");
json_t * id_holder = json_object_get(request_data, "id");
if (json_is_string(method_holder) &&
(json_is_array(params) || (json_is_object(params))) &&
json_is_integer(id_holder))
{
char const * method_name = json_string_value(method_holder);
int id = json_integer_value(id_holder);
struct wf_impl_jsonrpc_request * request = wf_impl_jsonrpc_request_create(id, send, user_data);
struct wf_impl_jsonrpc_method const * method = wf_impl_jsonrpc_server_get_method(server, method_name);
method->invoke(request, method_name, params, method->user_data);
}
}