From c6ca2e14bdb330ba41e42a8ec76f8d7f5c83627c Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Tue, 25 Feb 2020 22:05:48 +0100 Subject: [PATCH] changed credentials API --- cmake/webfuse_provider.cmake | 1 + include/webfuse/provider/client_config.h | 21 +++------- include/webfuse/provider/credentials.h | 30 +++++++++++++++ include/webfuse_provider.h | 1 + lib/webfuse/provider/api.c | 34 ++++++++++------- lib/webfuse/provider/impl/client_config.c | 25 ++---------- lib/webfuse/provider/impl/client_config.h | 10 +---- lib/webfuse/provider/impl/credentials.c | 34 +++++++++++++++++ lib/webfuse/provider/impl/credentials.h | 38 +++++++++++++++++++ lib/webfuse/provider/impl/provider.c | 2 + lib/webfuse/provider/impl/provider.h | 1 + .../tests/provider/test_client_protocol.cc | 9 +---- 12 files changed, 140 insertions(+), 66 deletions(-) create mode 100644 include/webfuse/provider/credentials.h create mode 100644 lib/webfuse/provider/impl/credentials.c create mode 100644 lib/webfuse/provider/impl/credentials.h diff --git a/cmake/webfuse_provider.cmake b/cmake/webfuse_provider.cmake index 5079cb1..0d80d2d 100644 --- a/cmake/webfuse_provider.cmake +++ b/cmake/webfuse_provider.cmake @@ -9,6 +9,7 @@ add_library(webfuse-provider-static STATIC lib/webfuse/provider/impl/provider.c lib/webfuse/provider/impl/request.c lib/webfuse/provider/impl/dirbuffer.c + lib/webfuse/provider/impl/credentials.c lib/webfuse/provider/impl/operation/lookup.c lib/webfuse/provider/impl/operation/getattr.c lib/webfuse/provider/impl/operation/readdir.c diff --git a/include/webfuse/provider/client_config.h b/include/webfuse/provider/client_config.h index ec4c85b..0c79dd7 100644 --- a/include/webfuse/provider/client_config.h +++ b/include/webfuse/provider/client_config.h @@ -14,6 +14,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" @@ -228,25 +229,15 @@ extern WFP_API void wfp_client_config_set_onread( wfp_read_fn * handler); //------------------------------------------------------------------------------ -/// \brief Enabled authentication with username and password. -/// -/// Sets username and password for built-in username authentication. +/// \brief Enabled authentication. /// /// \param config pointer to client configuration -/// \param username pointer to username -/// \param password pointer to password +/// \param get_credentials pointer to function providing credentials when +// needed. //------------------------------------------------------------------------------ -extern WFP_API void wfp_client_config_set_username_credentials( +extern WFP_API void wfp_client_config_enable_authentication( 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[]); + wfp_get_credentials_fn * get_credentials); #ifdef __cplusplus } diff --git a/include/webfuse/provider/credentials.h b/include/webfuse/provider/credentials.h new file mode 100644 index 0000000..a6b0806 --- /dev/null +++ b/include/webfuse/provider/credentials.h @@ -0,0 +1,30 @@ +#ifndef WF_PROVIDER_CREDENTIALS_H +#define WF_PROVIDER_CREDENTIALS_H + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wfp_credentials; + +typedef void wfp_get_credentials_fn( + struct wfp_credentials * credentials, + void * user_data); + +extern WFP_API void wfp_credentials_set_type( + struct wfp_credentials * credentials, + char const * type); + +extern WFP_API void wfp_credentials_add( + struct wfp_credentials * credentials, + char const * key, + char const * value); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/webfuse_provider.h b/include/webfuse_provider.h index 7af3934..19afad2 100644 --- a/include/webfuse_provider.h +++ b/include/webfuse_provider.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include diff --git a/lib/webfuse/provider/api.c b/lib/webfuse/provider/api.c index 6e507e8..7c3220b 100644 --- a/lib/webfuse/provider/api.c +++ b/lib/webfuse/provider/api.c @@ -11,6 +11,7 @@ #include "webfuse/provider/impl/client_config.h" #include "webfuse/provider/impl/client.h" #include "webfuse/provider/impl/dirbuffer.h" +#include "webfuse/provider/impl/credentials.h" // respond @@ -155,23 +156,13 @@ void wfp_client_config_set_onread( wfp_impl_client_config_set_onread(config, handler); } -void wfp_client_config_set_username_credentials( +void wfp_client_config_enable_authentication( struct wfp_client_config * config, - char const * username, - char const * password) + wfp_get_credentials_fn * get_credentials) { - wfp_impl_client_config_set_username_credentials(config, username, password); + wfp_impl_client_config_enable_authentication(config, get_credentials); } -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 @@ -257,3 +248,20 @@ void wfp_dirbuffer_add( { wfp_impl_dirbuffer_add(buffer, name, inode); } + +// credentials + +void wfp_credentials_set_type( + struct wfp_credentials * credentials, + char const * type) +{ + wfp_impl_credentials_set_type(credentials, type); +} + +void wfp_credentials_add( + struct wfp_credentials * credentials, + char const * key, + char const * value) +{ + wfp_impl_credentials_add(credentials, key, value); +} diff --git a/lib/webfuse/provider/impl/client_config.c b/lib/webfuse/provider/impl/client_config.c index 7163830..0e8afc6 100644 --- a/lib/webfuse/provider/impl/client_config.c +++ b/lib/webfuse/provider/impl/client_config.c @@ -111,28 +111,9 @@ void wfp_impl_client_config_set_onread( config->provider.read = handler; } - -void wfp_impl_client_config_set_username_credentials( +void wfp_impl_client_config_enable_authentication( struct wfp_client_config * config, - char const * username, - char const * password) + wfp_get_credentials_fn * get_credentials) { - (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 - + config->provider.get_credentials = get_credentials; } diff --git a/lib/webfuse/provider/impl/client_config.h b/lib/webfuse/provider/impl/client_config.h index 4b94f49..f8d5b9b 100644 --- a/lib/webfuse/provider/impl/client_config.h +++ b/lib/webfuse/provider/impl/client_config.h @@ -70,15 +70,9 @@ 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( +extern void wfp_impl_client_config_enable_authentication( 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[]); + wfp_get_credentials_fn * get_credentials); #ifdef __cplusplus } diff --git a/lib/webfuse/provider/impl/credentials.c b/lib/webfuse/provider/impl/credentials.c new file mode 100644 index 0000000..8e788ca --- /dev/null +++ b/lib/webfuse/provider/impl/credentials.c @@ -0,0 +1,34 @@ +#include "webfuse/provider/impl/credentials.h" + +#include +#include + +void wfp_impl_credentials_init( + struct wfp_credentials * credentials) +{ + credentials->type = NULL; + credentials->contents = json_object(); +} + +void wfp_impl_credentials_cleanup( + struct wfp_credentials * credentials) +{ + free(credentials->type); + json_decref(credentials->contents); +} + +void wfp_impl_credentials_set_type( + struct wfp_credentials * credentials, + char const * type) +{ + free(credentials->type); + credentials->type = strdup(type); +} + +void wfp_impl_credentials_add( + struct wfp_credentials * credentials, + char const * key, + char const * value) +{ + json_object_set_new(credentials->contents, key, json_string(value)); +} diff --git a/lib/webfuse/provider/impl/credentials.h b/lib/webfuse/provider/impl/credentials.h new file mode 100644 index 0000000..6da40b9 --- /dev/null +++ b/lib/webfuse/provider/impl/credentials.h @@ -0,0 +1,38 @@ +#ifndef WF_PROVIDER_IMPL_CREDENTIALS_H +#define WF_PROVIDER_IMPL_CREDENTIALS_H + +#include "webfuse/provider/credentials.h" +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct wfp_credentials +{ + char * type; + json_t * contents; +}; + +extern void wfp_impl_credentials_init( + struct wfp_credentials * credentials); + +extern void wfp_impl_credentials_cleanup( + struct wfp_credentials * credentials); + +extern void wfp_impl_credentials_set_type( + struct wfp_credentials * credentials, + char const * type); + +extern void wfp_impl_credentials_add( + struct wfp_credentials * credentials, + char const * key, + char const * value); + + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/lib/webfuse/provider/impl/provider.c b/lib/webfuse/provider/impl/provider.c index def9e4a..d3ba96c 100644 --- a/lib/webfuse/provider/impl/provider.c +++ b/lib/webfuse/provider/impl/provider.c @@ -67,6 +67,7 @@ void wfp_impl_provider_init( provider->connected = &wfp_impl_connected_default; provider->disconnected = &wfp_impl_disconnected_default; provider->ontimer = &wfp_impl_ontimer_default; + provider->get_credentials = NULL; } void wfp_impl_provider_init_from_prototype( @@ -82,6 +83,7 @@ void wfp_impl_provider_init_from_prototype( provider->connected = prototype->connected; provider->disconnected = prototype->disconnected; provider->ontimer = prototype->ontimer; + provider->get_credentials = prototype->get_credentials; } void wfp_impl_provider_invoke( diff --git a/lib/webfuse/provider/impl/provider.h b/lib/webfuse/provider/impl/provider.h index ddee453..8f6d115 100644 --- a/lib/webfuse/provider/impl/provider.h +++ b/lib/webfuse/provider/impl/provider.h @@ -20,6 +20,7 @@ struct wfp_provider wfp_open_fn * open; wfp_close_fn * close; wfp_read_fn * read; + wfp_get_credentials_fn * get_credentials; }; struct wfp_impl_invokation_context diff --git a/test/webfuse/tests/provider/test_client_protocol.cc b/test/webfuse/tests/provider/test_client_protocol.cc index c5e5008..b9d6252 100644 --- a/test/webfuse/tests/provider/test_client_protocol.cc +++ b/test/webfuse/tests/provider/test_client_protocol.cc @@ -45,13 +45,6 @@ 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/"); @@ -169,7 +162,7 @@ TEST(client_protocol, connect_with_username_authentication) { MockProviderClient provider; ClientProtocolFixture fixture(provider); - fixture.SetUsernameCredentials("bob", "secret"); + // ToDo: enable authentication EXPECT_CALL(provider, OnConnected()).Times(AtMost(1)); EXPECT_CALL(provider, OnDisconnected()).Times(1);