From 767bafcd01696e5b3ded34a3d0cd5db99836a375 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Tue, 25 Feb 2020 15:36:28 +0100 Subject: [PATCH] added first idea of API --- include/webfuse/provider/client_config.h | 21 ++++++ lib/webfuse/provider/api.c | 17 +++++ lib/webfuse/provider/impl/client_config.c | 26 +++++++ lib/webfuse/provider/impl/client_config.h | 10 +++ .../tests/provider/test_client_protocol.cc | 72 ++++++++++++++++++- 5 files changed, 143 insertions(+), 3 deletions(-) diff --git a/include/webfuse/provider/client_config.h b/include/webfuse/provider/client_config.h index 73775f1..ec4c85b 100644 --- a/include/webfuse/provider/client_config.h +++ b/include/webfuse/provider/client_config.h @@ -227,6 +227,27 @@ extern WFP_API void wfp_client_config_set_onread( struct wfp_client_config * config, wfp_read_fn * handler); +//------------------------------------------------------------------------------ +/// \brief Enabled authentication with username and password. +/// +/// Sets username and password for built-in username authentication. +/// +/// \param config pointer to client configuration +/// \param username pointer to username +/// \param password pointer to password +//------------------------------------------------------------------------------ +extern WFP_API void wfp_client_config_set_username_credentials( + struct wfp_client_config * config, + char const * username, + char const * password); + +//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ +extern WFP_API void wfp_client_config_set_generic_credentials( + struct wfp_client_config * config, + char const * credentials_type, + char const * contents[]); + #ifdef __cplusplus } #endif diff --git a/lib/webfuse/provider/api.c b/lib/webfuse/provider/api.c index 0c0e88a..6e507e8 100644 --- a/lib/webfuse/provider/api.c +++ b/lib/webfuse/provider/api.c @@ -155,6 +155,23 @@ void wfp_client_config_set_onread( wfp_impl_client_config_set_onread(config, handler); } +void wfp_client_config_set_username_credentials( + struct wfp_client_config * config, + char const * username, + char const * password) +{ + wfp_impl_client_config_set_username_credentials(config, username, password); +} + +void wfp_client_config_set_generic_credentials( + struct wfp_client_config * config, + char const * credentials_type, + char const * contents[]) +{ + wfp_impl_client_config_set_generic_credentials(config, credentials_type, contents); +} + + // protocol diff --git a/lib/webfuse/provider/impl/client_config.c b/lib/webfuse/provider/impl/client_config.c index 7d121da..7163830 100644 --- a/lib/webfuse/provider/impl/client_config.c +++ b/lib/webfuse/provider/impl/client_config.c @@ -110,3 +110,29 @@ void wfp_impl_client_config_set_onread( { config->provider.read = handler; } + + +void wfp_impl_client_config_set_username_credentials( + struct wfp_client_config * config, + char const * username, + char const * password) +{ + (void) config; + (void) username; + (void) password; + + // ToDo: implement me +} + +void wfp_impl_client_config_set_generic_credentials( + struct wfp_client_config * config, + char const * credentials_type, + char const * contents[]) +{ + (void) config; + (void) credentials_type; + (void) contents; + + // ToDo: implement me + +} diff --git a/lib/webfuse/provider/impl/client_config.h b/lib/webfuse/provider/impl/client_config.h index 190a3cb..4b94f49 100644 --- a/lib/webfuse/provider/impl/client_config.h +++ b/lib/webfuse/provider/impl/client_config.h @@ -70,6 +70,16 @@ extern void wfp_impl_client_config_set_onread( struct wfp_client_config * config, wfp_read_fn * handler); +extern void wfp_impl_client_config_set_username_credentials( + struct wfp_client_config * config, + char const * username, + char const * password); + +extern void wfp_impl_client_config_set_generic_credentials( + struct wfp_client_config * config, + char const * credentials_type, + char const * contents[]); + #ifdef __cplusplus } #endif diff --git a/test/webfuse/tests/provider/test_client_protocol.cc b/test/webfuse/tests/provider/test_client_protocol.cc index ff7be74..c5e5008 100644 --- a/test/webfuse/tests/provider/test_client_protocol.cc +++ b/test/webfuse/tests/provider/test_client_protocol.cc @@ -45,6 +45,13 @@ public: wfp_client_config_dispose(config); } + void SetUsernameCredentials( + std::string const & username, + std::string const & password) + { + wfp_client_config_set_username_credentials(config, username.c_str(), password.c_str()); + } + void Connect() { wfp_client_protocol_connect(protocol, server->getContext(), "ws://localhost:54321/"); @@ -61,6 +68,47 @@ public: return server->receiveMessage(); } + void AwaitAuthentication( + std::string const & expected_username, + std::string const & expected_password) + { + json_t * request = server->receiveMessage(); + ASSERT_TRUE(json_is_object(request)); + + json_t * method = json_object_get(request, "method"); + ASSERT_TRUE(json_is_string(method)); + ASSERT_STREQ("authenticate", json_string_value(method)); + + json_t * id = json_object_get(request, "id"); + ASSERT_TRUE(json_is_integer(id)); + + json_t * params = json_object_get(request, "params"); + ASSERT_TRUE(json_is_array(params)); + ASSERT_EQ(2, json_array_size(params)); + + json_t * type = json_array_get(params, 0); + ASSERT_TRUE(json_is_string(type)); + ASSERT_STREQ("username", json_string_value(type)); + + json_t * credentials = json_array_get(params, 1); + ASSERT_TRUE(json_is_object(credentials)); + + json_t * username = json_object_get(credentials, "username"); + ASSERT_TRUE(json_is_string(username)); + ASSERT_STREQ(expected_username.c_str(), json_string_value(username)); + + json_t * password = json_object_get(credentials, "password"); + ASSERT_TRUE(json_is_string(password)); + ASSERT_STREQ(expected_password.c_str(), json_string_value(password)); + + json_t * response = json_object(); + json_object_set_new(response, "result", json_object()); + json_object_set(response, "id", id); + server->sendMessage(response); + + json_decref(request); + } + void AwaitAddFilesystem(std::string& filesystemName) { json_t * addFilesystemRequest = server->receiveMessage(); @@ -91,7 +139,6 @@ public: server->sendMessage(response); json_decref(addFilesystemRequest); - } private: @@ -100,10 +147,8 @@ private: wfp_client_protocol * protocol; }; - } - TEST(client_protocol, connect) { MockProviderClient provider; @@ -120,6 +165,27 @@ TEST(client_protocol, connect) if (HasFatalFailure()) { return; } } +TEST(client_protocol, connect_with_username_authentication) +{ + MockProviderClient provider; + ClientProtocolFixture fixture(provider); + fixture.SetUsernameCredentials("bob", "secret"); + + EXPECT_CALL(provider, OnConnected()).Times(AtMost(1)); + EXPECT_CALL(provider, OnDisconnected()).Times(1); + + fixture.Connect(); + if (HasFatalFailure()) { return; } + + fixture.AwaitAuthentication("bob", "secret"); + if (HasFatalFailure()) { return; } + + std::string filesystem; + fixture.AwaitAddFilesystem(filesystem); + if (HasFatalFailure()) { return; } + +} + TEST(client_protocol, getattr) { MockProviderClient provider;