1
0
mirror of https://github.com/falk-werner/webfuse synced 2025-06-13 12:54:15 +00:00

propagates authenticators to server protocol

This commit is contained in:
Falk Werner 2019-03-17 19:05:51 +01:00
parent b664058583
commit 676978fdbc
7 changed files with 61 additions and 5 deletions

View File

@ -1,7 +1,8 @@
#ifndef WSFS_ADAPTER_SERVER_PROTOCOL_H #ifndef WSFS_ADAPTER_SERVER_PROTOCOL_H
#define WSFS_ADAPTER_SERVER_PROTOCOL_H #define WSFS_ADAPTER_SERVER_PROTOCOL_H
#include "wsfs/adapter/api.h" #include <wsfs/adapter/api.h>
#include <wsfs/adapter/authenticate.h>
struct wsfs_server_protocol; struct wsfs_server_protocol;
struct lws_protocols; struct lws_protocols;
@ -21,6 +22,12 @@ extern WSFSA_API void wsfs_server_protocol_init_lws(
struct wsfs_server_protocol * protocol, struct wsfs_server_protocol * protocol,
struct lws_protocols * lws_protocol); struct lws_protocols * lws_protocol);
extern WSFSA_API void wsfs_server_protocol_add_authenticator(
struct wsfs_server_protocol * protocol,
char const * type,
wsfs_authenticate_fn * authenticate,
void * user_data);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -63,6 +63,14 @@ void wsfs_authenticators_clone(
} }
extern void wsfs_authenticators_move(
struct wsfs_authenticators * authenticators,
struct wsfs_authenticators * other)
{
other->first = authenticators->first;
authenticators->first = NULL;
}
void wsfs_authenticators_add( void wsfs_authenticators_add(
struct wsfs_authenticators * authenticators, struct wsfs_authenticators * authenticators,
char const * type, char const * type,

View File

@ -30,6 +30,10 @@ extern void wsfs_authenticators_clone(
struct wsfs_authenticators * authenticators, struct wsfs_authenticators * authenticators,
struct wsfs_authenticators * other); struct wsfs_authenticators * other);
extern void wsfs_authenticators_move(
struct wsfs_authenticators * authenticators,
struct wsfs_authenticators * other);
extern void wsfs_authenticators_add( extern void wsfs_authenticators_add(
struct wsfs_authenticators * authenticators, struct wsfs_authenticators * authenticators,
char const * type, char const * type,

View File

@ -112,6 +112,7 @@ struct wsfs_server * wsfs_server_create(
{ {
server->shutdown_requested = false; server->shutdown_requested = false;
wsfs_server_config_clone(config, &server->config); wsfs_server_config_clone(config, &server->config);
wsfs_authenticators_move(&server->config.authenticators, &server->protocol.authenticators);
server->context = wsfs_server_context_create(server); server->context = wsfs_server_context_create(server);
} }
else else

View File

@ -36,12 +36,14 @@ static int wsfs_server_protocol_callback(
if (NULL == protocol->wsi) if (NULL == protocol->wsi)
{ {
protocol->wsi = wsi; protocol->wsi = wsi;
protocol->is_authenticated = wsfs_authenticators_authenticate(&protocol->authenticators, NULL);
} }
break; break;
case LWS_CALLBACK_CLOSED: case LWS_CALLBACK_CLOSED:
if (wsi == protocol->wsi) if (wsi == protocol->wsi)
{ {
protocol->wsi = NULL; protocol->wsi = NULL;
protocol->is_authenticated = false;
wsfs_message_queue_cleanup(&protocol->queue); wsfs_message_queue_cleanup(&protocol->queue);
} }
break; break;
@ -78,7 +80,7 @@ static bool wsfs_server_protocol_invoke(
bool result = false; bool result = false;
struct wsfs_server_protocol * protocol = user_data; struct wsfs_server_protocol * protocol = user_data;
if (NULL != protocol->wsi) if ((protocol->is_authenticated) && (NULL != protocol->wsi))
{ {
struct wsfs_message * message = wsfs_message_create(request); struct wsfs_message * message = wsfs_message_create(request);
if (NULL != message) if (NULL != message)
@ -131,9 +133,10 @@ bool wsfs_server_protocol_init(
char * mount_point) char * mount_point)
{ {
protocol->wsi = NULL; protocol->wsi = NULL;
protocol->is_authenticated = false;
wsfs_message_queue_init(&protocol->queue); wsfs_message_queue_init(&protocol->queue);
wsfs_timeout_manager_init(&protocol->timeout_manager); wsfs_timeout_manager_init(&protocol->timeout_manager);
wsfs_authenticators_init(&protocol->authenticators);
wsfs_jsonrpc_server_init(&protocol->rpc, &protocol->timeout_manager); wsfs_jsonrpc_server_init(&protocol->rpc, &protocol->timeout_manager);
wsfs_jsonrpc_server_add(&protocol->rpc, "lookup", &wsfs_server_protocol_invoke, protocol); wsfs_jsonrpc_server_add(&protocol->rpc, "lookup", &wsfs_server_protocol_invoke, protocol);
@ -149,6 +152,7 @@ bool wsfs_server_protocol_init(
if (!success) if (!success)
{ {
wsfs_jsonrpc_server_cleanup(&protocol->rpc); wsfs_jsonrpc_server_cleanup(&protocol->rpc);
wsfs_authenticators_cleanup(&protocol->authenticators);
wsfs_timeout_manager_cleanup(&protocol->timeout_manager); wsfs_timeout_manager_cleanup(&protocol->timeout_manager);
wsfs_message_queue_cleanup(&protocol->queue); wsfs_message_queue_cleanup(&protocol->queue);
} }
@ -163,5 +167,15 @@ void wsfs_server_protocol_cleanup(
wsfs_jsonrpc_server_cleanup(&protocol->rpc); wsfs_jsonrpc_server_cleanup(&protocol->rpc);
wsfs_timeout_manager_cleanup(&protocol->timeout_manager); wsfs_timeout_manager_cleanup(&protocol->timeout_manager);
wsfs_message_queue_cleanup(&protocol->queue); wsfs_message_queue_cleanup(&protocol->queue);
wsfs_authenticators_cleanup(&protocol->authenticators);
protocol->wsi = NULL; protocol->wsi = NULL;
} }
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);
}

View File

@ -7,6 +7,7 @@
#include "wsfs/adapter/filesystem.h" #include "wsfs/adapter/filesystem.h"
#include "wsfs/adapter/jsonrpc/server.h" #include "wsfs/adapter/jsonrpc/server.h"
#include "wsfs/adapter/time/timeout_manager.h" #include "wsfs/adapter/time/timeout_manager.h"
#include "wsfs/adapter/authenticators.h"
struct wsfs_server_protocol struct wsfs_server_protocol
{ {
@ -14,7 +15,9 @@ struct wsfs_server_protocol
struct wsfs_filesystem filesystem; struct wsfs_filesystem filesystem;
struct wsfs_jsonrpc_server rpc; struct wsfs_jsonrpc_server rpc;
struct wsfs_message_queue queue; struct wsfs_message_queue queue;
struct wsfs_authenticators authenticators;
struct lws * wsi; struct lws * wsi;
bool is_authenticated;
}; };
extern bool wsfs_server_protocol_init( extern bool wsfs_server_protocol_init(

View File

@ -40,6 +40,25 @@ TEST(Authenticators, Clone)
wsfs_authenticators_clone(&authenticators, &clone); wsfs_authenticators_clone(&authenticators, &clone);
ASSERT_NE(nullptr, clone.first); ASSERT_NE(nullptr, clone.first);
ASSERT_NE(nullptr, authenticators.first);
ASSERT_NE(authenticators.first, clone.first);
wsfs_authenticators_cleanup(&authenticators);
wsfs_authenticators_cleanup(&clone);
}
TEST(Authenticators, Move)
{
struct wsfs_authenticators authenticators;
struct wsfs_authenticators clone;
wsfs_authenticators_init(&authenticators);
wsfs_authenticators_add(&authenticators, "username", &authenticate, nullptr);
ASSERT_NE(nullptr, authenticators.first);
wsfs_authenticators_move(&authenticators, &clone);
ASSERT_NE(nullptr, clone.first);
ASSERT_EQ(nullptr, authenticators.first);
ASSERT_NE(authenticators.first, clone.first); ASSERT_NE(authenticators.first, clone.first);
wsfs_authenticators_cleanup(&authenticators); wsfs_authenticators_cleanup(&authenticators);