diff --git a/include/wsfs/adapter/server_protocol.h b/include/wsfs/adapter/server_protocol.h index 1af4cc1..0579677 100644 --- a/include/wsfs/adapter/server_protocol.h +++ b/include/wsfs/adapter/server_protocol.h @@ -1,7 +1,8 @@ #ifndef WSFS_ADAPTER_SERVER_PROTOCOL_H #define WSFS_ADAPTER_SERVER_PROTOCOL_H -#include "wsfs/adapter/api.h" +#include +#include struct wsfs_server_protocol; struct lws_protocols; @@ -21,6 +22,12 @@ extern WSFSA_API void wsfs_server_protocol_init_lws( struct wsfs_server_protocol * 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 } #endif diff --git a/lib/wsfs/adapter/authenticators.c b/lib/wsfs/adapter/authenticators.c index 58176bb..856354d 100644 --- a/lib/wsfs/adapter/authenticators.c +++ b/lib/wsfs/adapter/authenticators.c @@ -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( struct wsfs_authenticators * authenticators, char const * type, diff --git a/lib/wsfs/adapter/authenticators.h b/lib/wsfs/adapter/authenticators.h index bbabf68..48b8056 100644 --- a/lib/wsfs/adapter/authenticators.h +++ b/lib/wsfs/adapter/authenticators.h @@ -30,6 +30,10 @@ 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, diff --git a/lib/wsfs/adapter/server.c b/lib/wsfs/adapter/server.c index 5f1631b..0ca0c74 100644 --- a/lib/wsfs/adapter/server.c +++ b/lib/wsfs/adapter/server.c @@ -112,6 +112,7 @@ struct wsfs_server * wsfs_server_create( { 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 diff --git a/lib/wsfs/adapter/server_protocol.c b/lib/wsfs/adapter/server_protocol.c index fa53e0d..843c2f1 100644 --- a/lib/wsfs/adapter/server_protocol.c +++ b/lib/wsfs/adapter/server_protocol.c @@ -36,12 +36,14 @@ static int wsfs_server_protocol_callback( if (NULL == protocol->wsi) { protocol->wsi = wsi; + protocol->is_authenticated = wsfs_authenticators_authenticate(&protocol->authenticators, NULL); } break; case LWS_CALLBACK_CLOSED: if (wsi == protocol->wsi) { protocol->wsi = NULL; + protocol->is_authenticated = false; wsfs_message_queue_cleanup(&protocol->queue); } break; @@ -78,7 +80,7 @@ static bool wsfs_server_protocol_invoke( bool result = false; 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); if (NULL != message) @@ -131,9 +133,10 @@ bool wsfs_server_protocol_init( char * mount_point) { protocol->wsi = NULL; + protocol->is_authenticated = false; wsfs_message_queue_init(&protocol->queue); - wsfs_timeout_manager_init(&protocol->timeout_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,6 +152,7 @@ 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); } @@ -163,5 +167,15 @@ void wsfs_server_protocol_cleanup( wsfs_jsonrpc_server_cleanup(&protocol->rpc); wsfs_timeout_manager_cleanup(&protocol->timeout_manager); wsfs_message_queue_cleanup(&protocol->queue); + wsfs_authenticators_cleanup(&protocol->authenticators); 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); +} diff --git a/lib/wsfs/adapter/server_protocol_intern.h b/lib/wsfs/adapter/server_protocol_intern.h index ecdb0e9..2add201 100644 --- a/lib/wsfs/adapter/server_protocol_intern.h +++ b/lib/wsfs/adapter/server_protocol_intern.h @@ -7,6 +7,7 @@ #include "wsfs/adapter/filesystem.h" #include "wsfs/adapter/jsonrpc/server.h" #include "wsfs/adapter/time/timeout_manager.h" +#include "wsfs/adapter/authenticators.h" struct wsfs_server_protocol { @@ -14,7 +15,9 @@ struct wsfs_server_protocol struct wsfs_filesystem filesystem; struct wsfs_jsonrpc_server rpc; struct wsfs_message_queue queue; - struct lws * wsi; + struct wsfs_authenticators authenticators; + struct lws * wsi; + bool is_authenticated; }; extern bool wsfs_server_protocol_init( diff --git a/test/test_authenticators.cc b/test/test_authenticators.cc index bcd2c5e..6c7c021 100644 --- a/test/test_authenticators.cc +++ b/test/test_authenticators.cc @@ -40,6 +40,25 @@ TEST(Authenticators, Clone) wsfs_authenticators_clone(&authenticators, &clone); 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); wsfs_authenticators_cleanup(&authenticators); @@ -132,4 +151,4 @@ TEST(Authenticators, FailedAuthenticateWithWrongType) wsfs_authenticators_cleanup(&authenticators); wsfs_credentials_cleanup(&creds); -} \ No newline at end of file +}