1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-09-29 09:20:45 +00:00

feature: enabled authentication

This commit is contained in:
Falk Werner 2020-03-01 13:42:46 +01:00
parent f79b9c998a
commit 7856b5a99d
8 changed files with 115 additions and 7 deletions

View File

@ -33,7 +33,20 @@ jsonrpc_proxy_create(
extern JSONRPC_API void jsonrpc_proxy_dispose( extern JSONRPC_API void jsonrpc_proxy_dispose(
struct jsonrpc_proxy * proxy); struct jsonrpc_proxy * proxy);
//------------------------------------------------------------------------------
/// \brief Invokes a method.
///
/// Creates a method an sends it using the send function.
/// Proxy keeps track of method invokation. If no response is returned within
/// timeout, an error is propagated.
///
/// \param proxy pointer to proxy instance
/// \param finished function which is called exactly once, either on success or
/// on failure.
/// \param method_name name of the method to invoke
/// \param param_info types of the param (s = string, i = integer, j = json)
/// \param ... params
//------------------------------------------------------------------------------
extern JSONRPC_API void jsonrpc_proxy_invoke( extern JSONRPC_API void jsonrpc_proxy_invoke(
struct jsonrpc_proxy * proxy, struct jsonrpc_proxy * proxy,
jsonrpc_proxy_finished_fn * finished, jsonrpc_proxy_finished_fn * finished,

View File

@ -76,6 +76,12 @@ static json_t * jsonrpc_impl_request_create(
int const value = va_arg(args, int); int const value = va_arg(args, int);
json_array_append_new(params, json_integer(value)); json_array_append_new(params, json_integer(value));
} }
break;
case 'j':
{
json_t * const value = va_arg(args, json_t *);
json_array_append_new(params, value);
}
break; break;
default: default:
fprintf(stderr, "fatal: unknown param_type '%c'\n", *param_type); fprintf(stderr, "fatal: unknown param_type '%c'\n", *param_type);

View File

@ -8,6 +8,7 @@
#include "webfuse/provider/impl/client_config.h" #include "webfuse/provider/impl/client_config.h"
#include "webfuse/provider/impl/provider.h" #include "webfuse/provider/impl/provider.h"
#include "webfuse/provider/impl/credentials.h"
#include "webfuse/core/util.h" #include "webfuse/core/util.h"
#include "webfuse/core/message.h" #include "webfuse/core/message.h"
#include "webfuse/core/message_queue.h" #include "webfuse/core/message_queue.h"
@ -67,7 +68,7 @@ static void wfp_impl_client_protocol_process(
} }
static void static void
wfp_impl_client_protocol_on_authenticate_finished( wfp_impl_client_protocol_on_add_filesystem_finished(
void * user_data, void * user_data,
json_t const * result, json_t const * result,
json_t const * WF_UNUSED_PARAM(error)) json_t const * WF_UNUSED_PARAM(error))
@ -92,13 +93,69 @@ static void wfp_impl_client_protocol_add_filesystem(
{ {
jsonrpc_proxy_invoke( jsonrpc_proxy_invoke(
protocol->proxy, protocol->proxy,
&wfp_impl_client_protocol_on_authenticate_finished, &wfp_impl_client_protocol_on_add_filesystem_finished,
protocol, protocol,
"add_filesystem", "add_filesystem",
"s", "s",
"cprovider"); "cprovider");
} }
static void
wfp_impl_client_protocol_on_authenticate_finished(
void * user_data,
json_t const * result,
json_t const * WF_UNUSED_PARAM(error))
{
struct wfp_client_protocol * protocol = user_data;
if (NULL == protocol->wsi) { return; }
if (NULL != result)
{
wfp_impl_client_protocol_add_filesystem(protocol);
}
else
{
protocol->is_shutdown_requested = true;
lws_callback_on_writable(protocol->wsi);
}
}
static void wfp_impl_client_protocol_authenticate(
struct wfp_client_protocol * protocol)
{
struct wfp_credentials credentials;
wfp_impl_credentials_init(&credentials);
protocol->provider.get_credentials(&credentials, protocol->user_data);
char const * cred_type = wfp_impl_credentials_get_type(&credentials);
json_t * creds = wfp_impl_credentials_get(&credentials);
json_incref(creds);
jsonrpc_proxy_invoke(
protocol->proxy,
&wfp_impl_client_protocol_on_authenticate_finished,
protocol,
"authenticate",
"sj",
cred_type, creds);
wfp_impl_credentials_cleanup(&credentials);
}
static void wfp_impl_client_protocol_handshake(
struct wfp_client_protocol * protocol)
{
if (wfp_impl_provider_is_authentication_enabled(&protocol->provider))
{
wfp_impl_client_protocol_authenticate(protocol);
}
else
{
wfp_impl_client_protocol_add_filesystem(protocol);
}
}
static int wfp_impl_client_protocol_callback( static int wfp_impl_client_protocol_callback(
struct lws * wsi, struct lws * wsi,
enum lws_callback_reasons reason, enum lws_callback_reasons reason,
@ -117,7 +174,7 @@ static int wfp_impl_client_protocol_callback(
switch (reason) switch (reason)
{ {
case LWS_CALLBACK_CLIENT_ESTABLISHED: case LWS_CALLBACK_CLIENT_ESTABLISHED:
wfp_impl_client_protocol_add_filesystem(protocol); wfp_impl_client_protocol_handshake(protocol);
break; break;
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
protocol->is_connected = false; protocol->is_connected = false;

View File

@ -32,3 +32,15 @@ void wfp_impl_credentials_add(
{ {
json_object_set_new(credentials->contents, key, json_string(value)); json_object_set_new(credentials->contents, key, json_string(value));
} }
char const * wfp_impl_credentials_get_type(
struct wfp_credentials * credentials)
{
return credentials->type;
}
json_t * wfp_impl_credentials_get(
struct wfp_credentials * credentials)
{
return credentials->contents;
}

View File

@ -30,6 +30,11 @@ extern void wfp_impl_credentials_add(
char const * key, char const * key,
char const * value); char const * value);
extern char const * wfp_impl_credentials_get_type(
struct wfp_credentials * credentials);
extern json_t * wfp_impl_credentials_get(
struct wfp_credentials * credentials);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -127,3 +127,10 @@ void wfp_impl_ontimer_default(
// empty // empty
} }
bool wfp_impl_provider_is_authentication_enabled(
struct wfp_provider * provider)
{
return (NULL != provider->get_credentials);
}

View File

@ -1,9 +1,14 @@
#ifndef WF_PROVIDER_IMPL_PROVIDER_H #ifndef WF_PROVIDER_IMPL_PROVIDER_H
#define WF_PROVIDER_IMPL_PROVIDER_H #define WF_PROVIDER_IMPL_PROVIDER_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#include <jansson.h> #include <jansson.h>
#include "webfuse/provider/client_config.h" #include "webfuse/provider/client_config.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
{ {
@ -42,6 +47,9 @@ extern void wfp_impl_provider_invoke(
struct wfp_impl_invokation_context * context, struct wfp_impl_invokation_context * context,
json_t * request); json_t * request);
extern bool wfp_impl_provider_is_authentication_enabled(
struct wfp_provider * provider);
extern void wfp_impl_connected_default( extern void wfp_impl_connected_default(
void * user_data); void * user_data);

View File

@ -173,7 +173,7 @@ TEST(client_protocol, connect_with_username_authentication)
EXPECT_CALL(provider, OnConnected()).Times(AtMost(1)); EXPECT_CALL(provider, OnConnected()).Times(AtMost(1));
EXPECT_CALL(provider, OnDisconnected()).Times(1); EXPECT_CALL(provider, OnDisconnected()).Times(1);
EXPECT_CALL(provider, GetCredentials(_)).WillOnce(Invoke(GetCredentials)).Times(1); EXPECT_CALL(provider, GetCredentials(_)).Times(1).WillOnce(Invoke(GetCredentials));
fixture.Connect(); fixture.Connect();
if (HasFatalFailure()) { return; } if (HasFatalFailure()) { return; }