mirror of
https://github.com/falk-werner/webfuse-provider
synced 2026-03-02 04:09:18 +00:00
Feature/authentication (#14)
* makes wsfs_server_config opaque * feature: try to create mount point, if not present * fixes server start failure due to existing mountpoint * added basic authentication infrastructure * makes wsfs_server_config opaque * feature: try to create mount point, if not present * fixes server start failure due to existing mountpoint * added basic authentication infrastructure * added unit tests for credentials * added unit tests for authenticators * propagates authenticators to server protocol * enabled username authentication in daemon example * adds example to compute password hash * adds infrastructure to execute commands * added userdb to encapsulate authentication stuff * adds session and session_manager * fixes warning about unused param * moves some logic from server_protocol to session * makes wsfs_server_config opaque * feature: try to create mount point, if not present * fixes server start failure due to existing mountpoint * added basic authentication infrastructure * makes wsfs_server_config opaque * added unit tests for credentials * added unit tests for authenticators * propagates authenticators to server protocol * enabled username authentication in daemon example * adds example to compute password hash * adds infrastructure to execute commands * added userdb to encapsulate authentication stuff * adds session and session_manager * fixes warning about unused param * moves some logic from server_protocol to session * updates libcrypto to version 1.1.0
This commit is contained in:
49
lib/wsfs/adapter/authenticator.c
Normal file
49
lib/wsfs/adapter/authenticator.c
Normal file
@@ -0,0 +1,49 @@
|
||||
#include "wsfs/adapter/authenticator.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "wsfs/adapter/credentials_intern.h"
|
||||
|
||||
struct wsfs_authenticator * wsfs_authenticator_create(
|
||||
char const * type,
|
||||
wsfs_authenticate_fn * authenticate,
|
||||
void * user_data)
|
||||
{
|
||||
struct wsfs_authenticator * authenticator = malloc(sizeof(struct wsfs_authenticator));
|
||||
if (NULL != authenticator)
|
||||
{
|
||||
authenticator->type = strdup(type);
|
||||
authenticator->authenticate = authenticate;
|
||||
authenticator->user_data = user_data;
|
||||
authenticator->next = NULL;
|
||||
}
|
||||
|
||||
return authenticator;
|
||||
}
|
||||
|
||||
void wsfs_authenticator_dispose(
|
||||
struct wsfs_authenticator * authenticator)
|
||||
{
|
||||
free(authenticator->type);
|
||||
free(authenticator);
|
||||
}
|
||||
|
||||
bool wsfs_authenticator_autenticate(
|
||||
struct wsfs_authenticator * authenticator,
|
||||
struct wsfs_credentials * credentials)
|
||||
{
|
||||
bool result;
|
||||
|
||||
if (0 == strcmp(authenticator->type, credentials->type))
|
||||
{
|
||||
result = authenticator->authenticate(credentials, authenticator->user_data);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = false;
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
40
lib/wsfs/adapter/authenticator.h
Normal file
40
lib/wsfs/adapter/authenticator.h
Normal file
@@ -0,0 +1,40 @@
|
||||
#ifndef WSFS_ADAPTER_AUTHENTICATOR_H
|
||||
#define WSFS_ADAPTER_AUTHENTICATOR_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#include "wsfs/adapter/authenticate.h"
|
||||
|
||||
struct wsfs_authenticator
|
||||
{
|
||||
char * type;
|
||||
wsfs_authenticate_fn * authenticate;
|
||||
void * user_data;
|
||||
struct wsfs_authenticator * next;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern struct wsfs_authenticator * wsfs_authenticator_create(
|
||||
char const * type,
|
||||
wsfs_authenticate_fn * authenticate,
|
||||
void * user_data);
|
||||
|
||||
extern void wsfs_authenticator_dispose(
|
||||
struct wsfs_authenticator * authenticator);
|
||||
|
||||
extern bool wsfs_authenticator_autenticate(
|
||||
struct wsfs_authenticator * authenticator,
|
||||
struct wsfs_credentials * credentials);
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
101
lib/wsfs/adapter/authenticators.c
Normal file
101
lib/wsfs/adapter/authenticators.c
Normal file
@@ -0,0 +1,101 @@
|
||||
#include "wsfs/adapter/authenticators.h"
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "wsfs/adapter/authenticator.h"
|
||||
#include "wsfs/adapter/credentials_intern.h"
|
||||
|
||||
static struct wsfs_authenticator * wsfs_authenticators_find(
|
||||
struct wsfs_authenticators * authenticators,
|
||||
char const * type)
|
||||
{
|
||||
struct wsfs_authenticator * result = NULL;
|
||||
|
||||
struct wsfs_authenticator * actual = authenticators->first;
|
||||
while ((NULL == result) && (NULL != actual))
|
||||
{
|
||||
struct wsfs_authenticator * next = actual->next;
|
||||
if (0 == strcmp(type, actual->type))
|
||||
{
|
||||
result = actual;
|
||||
}
|
||||
|
||||
actual = next;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void wsfs_authenticators_init(
|
||||
struct wsfs_authenticators * authenticators)
|
||||
{
|
||||
authenticators->first = NULL;
|
||||
}
|
||||
|
||||
void wsfs_authenticators_cleanup(
|
||||
struct wsfs_authenticators * authenticators)
|
||||
{
|
||||
struct wsfs_authenticator * actual = authenticators->first;
|
||||
while (NULL != actual)
|
||||
{
|
||||
struct wsfs_authenticator * next = actual->next;
|
||||
wsfs_authenticator_dispose(actual);
|
||||
actual = next;
|
||||
}
|
||||
|
||||
authenticators->first = NULL;
|
||||
}
|
||||
|
||||
void wsfs_authenticators_clone(
|
||||
struct wsfs_authenticators * authenticators,
|
||||
struct wsfs_authenticators * other)
|
||||
{
|
||||
wsfs_authenticators_init(other);
|
||||
|
||||
struct wsfs_authenticator * actual = authenticators->first;
|
||||
while (NULL != actual)
|
||||
{
|
||||
struct wsfs_authenticator * next = actual->next;
|
||||
wsfs_authenticators_add(other,
|
||||
actual->type, actual->authenticate, actual->user_data);
|
||||
actual = next;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extern void wsfs_authenticators_move(
|
||||
struct wsfs_authenticators * authenticators,
|
||||
struct wsfs_authenticators * other)
|
||||
{
|
||||
other->first = authenticators->first;
|
||||
authenticators->first = NULL;
|
||||
}
|
||||
|
||||
void wsfs_authenticators_add(
|
||||
struct wsfs_authenticators * authenticators,
|
||||
char const * type,
|
||||
wsfs_authenticate_fn * authenticate,
|
||||
void * user_data)
|
||||
{
|
||||
struct wsfs_authenticator * authenticator = wsfs_authenticator_create(type, authenticate, user_data);
|
||||
authenticator->next = authenticators->first;
|
||||
authenticators->first = authenticator;
|
||||
}
|
||||
|
||||
bool wsfs_authenticators_authenticate(
|
||||
struct wsfs_authenticators * authenticators,
|
||||
struct wsfs_credentials * credentials)
|
||||
{
|
||||
bool result = (NULL == authenticators->first);
|
||||
|
||||
if (NULL != credentials)
|
||||
{
|
||||
struct wsfs_authenticator * authenticator = wsfs_authenticators_find(authenticators, credentials->type);
|
||||
if (NULL != authenticator)
|
||||
{
|
||||
result = wsfs_authenticator_autenticate(authenticator, credentials);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
51
lib/wsfs/adapter/authenticators.h
Normal file
51
lib/wsfs/adapter/authenticators.h
Normal file
@@ -0,0 +1,51 @@
|
||||
#ifndef WSFS_ADAPTER_AUTHENTICATORS_H
|
||||
#define WSFS_ADAPTER_AUTHENTICATORS_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#include "wsfs/adapter/authenticate.h"
|
||||
|
||||
struct wsfs_authenticator;
|
||||
struct wsfs_credentials;
|
||||
|
||||
struct wsfs_authenticators
|
||||
{
|
||||
struct wsfs_authenticator * first;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wsfs_authenticators_init(
|
||||
struct wsfs_authenticators * authenticators);
|
||||
|
||||
extern void wsfs_authenticators_cleanup(
|
||||
struct wsfs_authenticators * authenticators);
|
||||
|
||||
extern void wsfs_authenticators_clone(
|
||||
struct wsfs_authenticators * authenticators,
|
||||
struct wsfs_authenticators * other);
|
||||
|
||||
extern void wsfs_authenticators_move(
|
||||
struct wsfs_authenticators * authenticators,
|
||||
struct wsfs_authenticators * other);
|
||||
|
||||
extern void wsfs_authenticators_add(
|
||||
struct wsfs_authenticators * authenticators,
|
||||
char const * type,
|
||||
wsfs_authenticate_fn * authenticate,
|
||||
void * user_data);
|
||||
|
||||
extern bool wsfs_authenticators_authenticate(
|
||||
struct wsfs_authenticators * authenticators,
|
||||
struct wsfs_credentials * credentials);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
40
lib/wsfs/adapter/credentials.c
Normal file
40
lib/wsfs/adapter/credentials.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "wsfs/adapter/credentials_intern.h"
|
||||
#include <string.h>
|
||||
|
||||
void wsfs_credentials_init(
|
||||
struct wsfs_credentials * credentials,
|
||||
char const * type,
|
||||
json_t * data)
|
||||
{
|
||||
credentials->type = strdup(type);
|
||||
credentials->data = data;
|
||||
json_incref(credentials->data);
|
||||
}
|
||||
|
||||
void wsfs_credentials_cleanup(
|
||||
struct wsfs_credentials * credentials)
|
||||
{
|
||||
free(credentials->type);
|
||||
json_decref(credentials->data);
|
||||
}
|
||||
|
||||
char const * wsfs_credentials_type(
|
||||
struct wsfs_credentials const * credentials)
|
||||
{
|
||||
return credentials->type;
|
||||
}
|
||||
|
||||
char const * wsfs_credentials_get(
|
||||
struct wsfs_credentials const * credentials,
|
||||
char const * key)
|
||||
{
|
||||
char const * result = NULL;
|
||||
|
||||
json_t * value_holder = json_object_get(credentials->data, key);
|
||||
if (json_is_string(value_holder))
|
||||
{
|
||||
result = json_string_value(value_holder);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
30
lib/wsfs/adapter/credentials_intern.h
Normal file
30
lib/wsfs/adapter/credentials_intern.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef WSFS_ADAPTER_CREDENTIALS_INTERN_H
|
||||
#define WSFS_ADAPTER_CREDENTIALS_INTERN_H
|
||||
|
||||
#include "wsfs/adapter/credentials.h"
|
||||
#include <jansson.h>
|
||||
|
||||
struct wsfs_credentials
|
||||
{
|
||||
char * type;
|
||||
json_t * data;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wsfs_credentials_init(
|
||||
struct wsfs_credentials * credentials,
|
||||
char const * type,
|
||||
json_t * data);
|
||||
|
||||
extern void wsfs_credentials_cleanup(
|
||||
struct wsfs_credentials * credentials);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -4,7 +4,11 @@
|
||||
#include <stdbool.h>
|
||||
#include <libwebsockets.h>
|
||||
|
||||
#include "wsfs/adapter/server_config.h"
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "wsfs/adapter/server_config_intern.h"
|
||||
#include "wsfs/adapter/server_protocol_intern.h"
|
||||
|
||||
#define WSFS_DISABLE_LWS_LOG 0
|
||||
@@ -74,24 +78,50 @@ static struct lws_context * wsfs_server_context_create(
|
||||
|
||||
}
|
||||
|
||||
static bool wsfs_server_check_mountpoint(
|
||||
struct wsfs_server_config * config)
|
||||
{
|
||||
bool result = false;
|
||||
|
||||
if (NULL != config->mount_point)
|
||||
{
|
||||
struct stat info;
|
||||
int const rc = stat(config->mount_point, &info);
|
||||
result = ((0 == rc) && (S_ISDIR(info.st_mode)));
|
||||
|
||||
if (!result)
|
||||
{
|
||||
result = (0 == mkdir(config->mount_point, 0755));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
struct wsfs_server * wsfs_server_create(
|
||||
struct wsfs_server_config * config)
|
||||
{
|
||||
struct wsfs_server * server = malloc(sizeof(struct wsfs_server));
|
||||
if (NULL != server)
|
||||
{
|
||||
if (wsfs_server_protocol_init(&server->protocol, config->mount_point))
|
||||
struct wsfs_server * server = NULL;
|
||||
|
||||
if (wsfs_server_check_mountpoint(config))
|
||||
{
|
||||
server = malloc(sizeof(struct wsfs_server));
|
||||
if (NULL != server)
|
||||
{
|
||||
server->shutdown_requested = false;
|
||||
wsfs_server_config_clone(config, &server->config);
|
||||
server->context = wsfs_server_context_create(server);
|
||||
}
|
||||
else
|
||||
{
|
||||
free(server);
|
||||
server = NULL;
|
||||
}
|
||||
}
|
||||
if (wsfs_server_protocol_init(&server->protocol, config->mount_point))
|
||||
{
|
||||
server->shutdown_requested = false;
|
||||
wsfs_server_config_clone(config, &server->config);
|
||||
wsfs_authenticators_move(&server->config.authenticators, &server->protocol.authenticators);
|
||||
server->context = wsfs_server_context_create(server);
|
||||
}
|
||||
else
|
||||
{
|
||||
free(server);
|
||||
server = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "wsfs/adapter/server_config.h"
|
||||
#include "wsfs/adapter/server_config_intern.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -18,11 +18,15 @@ void wsfs_server_config_init(
|
||||
struct wsfs_server_config * config)
|
||||
{
|
||||
memset(config, 0, sizeof(struct wsfs_server_config));
|
||||
|
||||
wsfs_authenticators_init(&config->authenticators);
|
||||
}
|
||||
|
||||
void wsfs_server_config_cleanup(
|
||||
struct wsfs_server_config * config)
|
||||
{
|
||||
wsfs_authenticators_cleanup(&config->authenticators);
|
||||
|
||||
free(config->mount_point);
|
||||
free(config->document_root);
|
||||
free(config->key_path);
|
||||
@@ -42,4 +46,82 @@ void wsfs_server_config_clone(
|
||||
clone->cert_path = wsfs_server_config_strdup(config->cert_path);
|
||||
clone->vhost_name = wsfs_server_config_strdup(config->vhost_name);
|
||||
clone->port = config->port;
|
||||
|
||||
wsfs_authenticators_clone(&config->authenticators, &clone->authenticators);
|
||||
}
|
||||
|
||||
struct wsfs_server_config * wsfs_server_config_create(void)
|
||||
{
|
||||
struct wsfs_server_config * config = malloc(sizeof(struct wsfs_server_config));
|
||||
if (NULL != config)
|
||||
{
|
||||
wsfs_server_config_init(config);
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
void wsfs_server_config_dispose(
|
||||
struct wsfs_server_config * config)
|
||||
{
|
||||
wsfs_server_config_cleanup(config);
|
||||
free(config);
|
||||
}
|
||||
|
||||
void wsfs_server_config_set_mountpoint(
|
||||
struct wsfs_server_config * config,
|
||||
char const * mount_point)
|
||||
{
|
||||
free(config->mount_point);
|
||||
config->mount_point = strdup(mount_point);
|
||||
}
|
||||
|
||||
void wsfs_server_config_set_documentroot(
|
||||
struct wsfs_server_config * config,
|
||||
char const * document_root)
|
||||
{
|
||||
free(config->document_root);
|
||||
config->document_root = strdup(document_root);
|
||||
}
|
||||
|
||||
void wsfs_server_config_set_keypath(
|
||||
struct wsfs_server_config * config,
|
||||
char const * key_path)
|
||||
{
|
||||
free(config->key_path);
|
||||
config->key_path = strdup(key_path);
|
||||
}
|
||||
|
||||
void wsfs_server_config_set_certpath(
|
||||
struct wsfs_server_config * config,
|
||||
char const * cert_path)
|
||||
{
|
||||
free(config->cert_path);
|
||||
config->cert_path = strdup(cert_path);
|
||||
}
|
||||
|
||||
void wsfs_server_config_set_vhostname(
|
||||
struct wsfs_server_config * config,
|
||||
char const * vhost_name)
|
||||
{
|
||||
free(config->vhost_name);
|
||||
config->vhost_name = strdup(vhost_name);
|
||||
}
|
||||
|
||||
void wsfs_server_config_set_port(
|
||||
struct wsfs_server_config * config,
|
||||
int port)
|
||||
{
|
||||
config->port = port;
|
||||
}
|
||||
|
||||
void wsfs_server_config_add_authenticator(
|
||||
struct wsfs_server_config * config,
|
||||
char const * type,
|
||||
wsfs_authenticate_fn * authenticate,
|
||||
void * user_data
|
||||
)
|
||||
{
|
||||
wsfs_authenticators_add(&config->authenticators, type, authenticate, user_data);
|
||||
}
|
||||
|
||||
|
||||
37
lib/wsfs/adapter/server_config_intern.h
Normal file
37
lib/wsfs/adapter/server_config_intern.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef WSFS_ADAPTER_SERVER_CONFIG_INTERN_H
|
||||
#define WSFS_ADAPTER_SERVER_CONFIG_INTERN_H
|
||||
|
||||
#include "wsfs/adapter/server_config.h"
|
||||
#include "wsfs/adapter/authenticators.h"
|
||||
|
||||
struct wsfs_server_config
|
||||
{
|
||||
char * mount_point;
|
||||
char * document_root;
|
||||
char * key_path;
|
||||
char * cert_path;
|
||||
char * vhost_name;
|
||||
int port;
|
||||
struct wsfs_authenticators authenticators;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern void wsfs_server_config_init(
|
||||
struct wsfs_server_config * config);
|
||||
|
||||
extern void wsfs_server_config_cleanup(
|
||||
struct wsfs_server_config * config);
|
||||
|
||||
extern void wsfs_server_config_clone(
|
||||
struct wsfs_server_config * config,
|
||||
struct wsfs_server_config * clone);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -19,6 +19,7 @@ static int wsfs_server_protocol_callback(
|
||||
struct wsfs_server_protocol * protocol = ws_protocol->user;
|
||||
|
||||
wsfs_timeout_manager_check(&protocol->timeout_manager);
|
||||
struct wsfs_session * session = wsfs_session_manager_get(&protocol->session_manager, wsi);
|
||||
|
||||
switch (reason)
|
||||
{
|
||||
@@ -33,33 +34,31 @@ static int wsfs_server_protocol_callback(
|
||||
}
|
||||
break;
|
||||
case LWS_CALLBACK_ESTABLISHED:
|
||||
if (NULL == protocol->wsi)
|
||||
{
|
||||
protocol->wsi = wsi;
|
||||
}
|
||||
session = wsfs_session_manager_add(
|
||||
&protocol->session_manager,
|
||||
wsi,
|
||||
&protocol->authenticators,
|
||||
&protocol->rpc);
|
||||
|
||||
if (NULL != session)
|
||||
{
|
||||
wsfs_session_authenticate(session, NULL);
|
||||
}
|
||||
break;
|
||||
case LWS_CALLBACK_CLOSED:
|
||||
if (wsi == protocol->wsi)
|
||||
{
|
||||
protocol->wsi = NULL;
|
||||
wsfs_message_queue_cleanup(&protocol->queue);
|
||||
}
|
||||
wsfs_session_manager_remove(&protocol->session_manager, wsi);
|
||||
break;
|
||||
case LWS_CALLBACK_SERVER_WRITEABLE:
|
||||
if ((wsi == protocol->wsi) && (!wsfs_message_queue_empty(&protocol->queue)))
|
||||
if (NULL != session)
|
||||
{
|
||||
struct wsfs_message * message = wsfs_message_queue_pop(&protocol->queue);
|
||||
lws_write(wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
|
||||
wsfs_message_dispose(message);
|
||||
|
||||
if (!wsfs_message_queue_empty(&protocol->queue))
|
||||
{
|
||||
lws_callback_on_writable(wsi);
|
||||
}
|
||||
wsfs_session_onwritable(session);
|
||||
}
|
||||
break;
|
||||
case LWS_CALLBACK_RECEIVE:
|
||||
wsfs_jsonrpc_server_onresult(&protocol->rpc, in, len);
|
||||
if (NULL != session)
|
||||
{
|
||||
wsfs_session_receive(session, in, len);
|
||||
}
|
||||
break;
|
||||
case LWS_CALLBACK_RAW_RX_FILE:
|
||||
wsfs_filesystem_process_request(&protocol->filesystem);
|
||||
@@ -75,20 +74,11 @@ static bool wsfs_server_protocol_invoke(
|
||||
void * user_data,
|
||||
json_t const * request)
|
||||
{
|
||||
bool result = false;
|
||||
struct wsfs_server_protocol * protocol = user_data;
|
||||
struct wsfs_session * session = &protocol->session_manager.session;
|
||||
struct wsfs_message * message = wsfs_message_create(request);
|
||||
|
||||
if (NULL != protocol->wsi)
|
||||
{
|
||||
struct wsfs_message * message = wsfs_message_create(request);
|
||||
if (NULL != message)
|
||||
{
|
||||
wsfs_message_queue_push(&protocol->queue, message);
|
||||
lws_callback_on_writable(protocol->wsi);
|
||||
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
bool const result = wsfs_session_send(session, message);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -130,10 +120,9 @@ bool wsfs_server_protocol_init(
|
||||
struct wsfs_server_protocol * protocol,
|
||||
char * mount_point)
|
||||
{
|
||||
protocol->wsi = NULL;
|
||||
wsfs_message_queue_init(&protocol->queue);
|
||||
|
||||
wsfs_timeout_manager_init(&protocol->timeout_manager);
|
||||
wsfs_session_manager_init(&protocol->session_manager);
|
||||
wsfs_authenticators_init(&protocol->authenticators);
|
||||
|
||||
wsfs_jsonrpc_server_init(&protocol->rpc, &protocol->timeout_manager);
|
||||
wsfs_jsonrpc_server_add(&protocol->rpc, "lookup", &wsfs_server_protocol_invoke, protocol);
|
||||
@@ -149,8 +138,9 @@ bool wsfs_server_protocol_init(
|
||||
if (!success)
|
||||
{
|
||||
wsfs_jsonrpc_server_cleanup(&protocol->rpc);
|
||||
wsfs_authenticators_cleanup(&protocol->authenticators);
|
||||
wsfs_timeout_manager_cleanup(&protocol->timeout_manager);
|
||||
wsfs_message_queue_cleanup(&protocol->queue);
|
||||
wsfs_session_manager_cleanup(&protocol->session_manager);
|
||||
}
|
||||
|
||||
return success;
|
||||
@@ -162,6 +152,15 @@ void wsfs_server_protocol_cleanup(
|
||||
wsfs_filesystem_cleanup(&protocol->filesystem);
|
||||
wsfs_jsonrpc_server_cleanup(&protocol->rpc);
|
||||
wsfs_timeout_manager_cleanup(&protocol->timeout_manager);
|
||||
wsfs_message_queue_cleanup(&protocol->queue);
|
||||
protocol->wsi = NULL;
|
||||
wsfs_authenticators_cleanup(&protocol->authenticators);
|
||||
wsfs_session_manager_cleanup(&protocol->session_manager);
|
||||
}
|
||||
|
||||
void wsfs_server_protocol_add_authenticator(
|
||||
struct wsfs_server_protocol * protocol,
|
||||
char const * type,
|
||||
wsfs_authenticate_fn * authenticate,
|
||||
void * user_data)
|
||||
{
|
||||
wsfs_authenticators_add(&protocol->authenticators, type, authenticate, user_data);
|
||||
}
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
#ifndef WSFS_ADAPTER_SERVER_PROTOCOL_INTERN_H
|
||||
#define WSFS_ADAPTER_SERVER_PROTOCOL_INTERN_H
|
||||
|
||||
#include "wsfs/message_queue.h"
|
||||
|
||||
#include "wsfs/adapter/server_protocol.h"
|
||||
#include "wsfs/adapter/filesystem.h"
|
||||
#include "wsfs/adapter/jsonrpc/server.h"
|
||||
#include "wsfs/adapter/time/timeout_manager.h"
|
||||
#include "wsfs/adapter/authenticators.h"
|
||||
#include "wsfs/adapter/session_manager.h"
|
||||
|
||||
struct wsfs_server_protocol
|
||||
{
|
||||
struct wsfs_timeout_manager timeout_manager;
|
||||
struct wsfs_filesystem filesystem;
|
||||
struct wsfs_jsonrpc_server rpc;
|
||||
struct wsfs_message_queue queue;
|
||||
struct lws * wsi;
|
||||
struct wsfs_authenticators authenticators;
|
||||
struct wsfs_session_manager session_manager;
|
||||
};
|
||||
|
||||
extern bool wsfs_server_protocol_init(
|
||||
|
||||
84
lib/wsfs/adapter/session.c
Normal file
84
lib/wsfs/adapter/session.c
Normal file
@@ -0,0 +1,84 @@
|
||||
#include "wsfs/adapter/session.h"
|
||||
#include "wsfs/adapter/authenticators.h"
|
||||
#include "wsfs/message_queue.h"
|
||||
#include "wsfs/message.h"
|
||||
#include "wsfs/adapter/jsonrpc/server.h"
|
||||
|
||||
#include <libwebsockets.h>
|
||||
#include <stddef.h>
|
||||
|
||||
void wsfs_session_init(
|
||||
struct wsfs_session * session,
|
||||
struct lws * wsi,
|
||||
struct wsfs_authenticators * authenticators,
|
||||
struct wsfs_jsonrpc_server * rpc)
|
||||
{
|
||||
session->wsi = wsi;
|
||||
session->is_authenticated = false;
|
||||
session->authenticators = authenticators;
|
||||
session->rpc = rpc;
|
||||
wsfs_message_queue_init(&session->queue);
|
||||
}
|
||||
|
||||
void wsfs_session_cleanup(
|
||||
struct wsfs_session * session)
|
||||
{
|
||||
wsfs_message_queue_cleanup(&session->queue);
|
||||
session->is_authenticated = false;
|
||||
session->wsi = NULL;
|
||||
session->authenticators = NULL;
|
||||
session->rpc = NULL;
|
||||
}
|
||||
|
||||
void wsfs_session_authenticate(
|
||||
struct wsfs_session * session,
|
||||
struct wsfs_credentials * creds)
|
||||
{
|
||||
session->is_authenticated = wsfs_authenticators_authenticate(session->authenticators, creds);
|
||||
}
|
||||
|
||||
bool wsfs_session_send(
|
||||
struct wsfs_session * session,
|
||||
struct wsfs_message * message)
|
||||
{
|
||||
bool result = (session->is_authenticated) && (NULL != session->wsi);
|
||||
|
||||
if (result)
|
||||
{
|
||||
wsfs_message_queue_push(&session->queue, message);
|
||||
lws_callback_on_writable(session->wsi);
|
||||
|
||||
result = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
wsfs_message_dispose(message);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void wsfs_session_onwritable(
|
||||
struct wsfs_session * session)
|
||||
{
|
||||
if (!wsfs_message_queue_empty(&session->queue))
|
||||
{
|
||||
struct wsfs_message * message = wsfs_message_queue_pop(&session->queue);
|
||||
lws_write(session->wsi, (unsigned char*) message->data, message->length, LWS_WRITE_TEXT);
|
||||
wsfs_message_dispose(message);
|
||||
|
||||
if (!wsfs_message_queue_empty(&session->queue))
|
||||
{
|
||||
lws_callback_on_writable(session->wsi);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void wsfs_session_receive(
|
||||
struct wsfs_session * session,
|
||||
char const * data,
|
||||
size_t length)
|
||||
{
|
||||
wsfs_jsonrpc_server_onresult(session->rpc, data, length);
|
||||
}
|
||||
64
lib/wsfs/adapter/session.h
Normal file
64
lib/wsfs/adapter/session.h
Normal file
@@ -0,0 +1,64 @@
|
||||
#ifndef WSFS_ADAPTER_SESSION_H
|
||||
#define WSFS_ADAPTER_SESSION_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
using std::size_t;
|
||||
#endif
|
||||
|
||||
#include "wsfs/message_queue.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct lws;
|
||||
struct wsfs_message;
|
||||
struct wsfs_credentials;
|
||||
struct wsfs_authenticators;
|
||||
struct wsfs_jsonrpc_server;
|
||||
|
||||
struct wsfs_session
|
||||
{
|
||||
struct lws * wsi;
|
||||
bool is_authenticated;
|
||||
struct wsfs_message_queue queue;
|
||||
struct wsfs_authenticators * authenticators;
|
||||
struct wsfs_jsonrpc_server * rpc;
|
||||
};
|
||||
|
||||
extern void wsfs_session_init(
|
||||
struct wsfs_session * session,
|
||||
struct lws * wsi,
|
||||
struct wsfs_authenticators * authenticators,
|
||||
struct wsfs_jsonrpc_server * rpc);
|
||||
|
||||
extern void wsfs_session_authenticate(
|
||||
struct wsfs_session * session,
|
||||
struct wsfs_credentials * creds);
|
||||
|
||||
extern bool wsfs_session_send(
|
||||
struct wsfs_session * session,
|
||||
struct wsfs_message * message);
|
||||
|
||||
extern void wsfs_session_receive(
|
||||
struct wsfs_session * session,
|
||||
char const * data,
|
||||
size_t length);
|
||||
|
||||
extern void wsfs_session_onwritable(
|
||||
struct wsfs_session * session);
|
||||
|
||||
extern void wsfs_session_cleanup(
|
||||
struct wsfs_session * session);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
54
lib/wsfs/adapter/session_manager.c
Normal file
54
lib/wsfs/adapter/session_manager.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#include "wsfs/adapter/session_manager.h"
|
||||
#include <stddef.h>
|
||||
|
||||
void wsfs_session_manager_init(
|
||||
struct wsfs_session_manager * manager)
|
||||
{
|
||||
wsfs_session_init(&manager->session, NULL, NULL, NULL);
|
||||
}
|
||||
|
||||
void wsfs_session_manager_cleanup(
|
||||
struct wsfs_session_manager * manager)
|
||||
{
|
||||
wsfs_session_cleanup(&manager->session);
|
||||
}
|
||||
|
||||
struct wsfs_session * wsfs_session_manager_add(
|
||||
struct wsfs_session_manager * manager,
|
||||
struct lws * wsi,
|
||||
struct wsfs_authenticators * authenticators,
|
||||
struct wsfs_jsonrpc_server * rpc)
|
||||
{
|
||||
struct wsfs_session * session = NULL;
|
||||
if (NULL == manager->session.wsi)
|
||||
{
|
||||
session = &manager->session;
|
||||
wsfs_session_init(&manager->session, wsi, authenticators, rpc);
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
struct wsfs_session * wsfs_session_manager_get(
|
||||
struct wsfs_session_manager * manager,
|
||||
struct lws * wsi)
|
||||
{
|
||||
struct wsfs_session * session = NULL;
|
||||
if (wsi == manager->session.wsi)
|
||||
{
|
||||
session = &manager->session;
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
void wsfs_session_manager_remove(
|
||||
struct wsfs_session_manager * manager,
|
||||
struct lws * wsi)
|
||||
{
|
||||
if (wsi == manager->session.wsi)
|
||||
{
|
||||
wsfs_session_cleanup(&manager->session);
|
||||
manager->session.wsi = NULL;
|
||||
}
|
||||
}
|
||||
46
lib/wsfs/adapter/session_manager.h
Normal file
46
lib/wsfs/adapter/session_manager.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef WSFS_ADAPTER_SESSION_MANAGER_H
|
||||
#define WSFS_ADAPTER_SESSION_MANAGER_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
#include "wsfs/adapter/session.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct lws;
|
||||
|
||||
struct wsfs_session_manager
|
||||
{
|
||||
struct wsfs_session session;
|
||||
};
|
||||
|
||||
extern void wsfs_session_manager_init(
|
||||
struct wsfs_session_manager * manager);
|
||||
|
||||
extern void wsfs_session_manager_cleanup(
|
||||
struct wsfs_session_manager * manager);
|
||||
|
||||
extern struct wsfs_session * wsfs_session_manager_add(
|
||||
struct wsfs_session_manager * manager,
|
||||
struct lws * wsi,
|
||||
struct wsfs_authenticators * authenticators,
|
||||
struct wsfs_jsonrpc_server * rpc);
|
||||
|
||||
extern struct wsfs_session * wsfs_session_manager_get(
|
||||
struct wsfs_session_manager * manager,
|
||||
struct lws * wsi);
|
||||
|
||||
extern void wsfs_session_manager_remove(
|
||||
struct wsfs_session_manager * manager,
|
||||
struct lws * wsi);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user