Add computation of credentials
[src/app-framework-binder.git] / src / afb-api-ws.c
index 5c3dcd4..8be74c0 100644 (file)
@@ -38,6 +38,7 @@
 #include "afb-common.h"
 
 #include "afb-session.h"
+#include "afb-cred.h"
 #include "afb-ws.h"
 #include "afb-msg-json.h"
 #include "afb-apis.h"
@@ -125,6 +126,9 @@ struct api_ws_client
 
        /* websocket */
        struct afb_ws *ws;
+
+       /* credentials */
+       struct afb_cred *cred;
 };
 
 /******************* websocket interface for client part **********************************/
@@ -856,6 +860,11 @@ static int api_ws_client_connect(struct api_ws *api)
        return -1;
 }
 
+static struct afb_api_itf ws_api_itf = {
+       .call = api_ws_client_call_cb,
+       .service_start = api_ws_service_start_cb
+};
+
 /* adds a afb-ws-service client api */
 int afb_api_ws_add_client(const char *path)
 {
@@ -877,8 +886,7 @@ int afb_api_ws_add_client(const char *path)
 
        /* record it as an API */
        afb_api.closure = api;
-       afb_api.call = api_ws_client_call_cb;
-       afb_api.service_start = api_ws_service_start_cb;
+       afb_api.itf = &ws_api_itf;
        if (afb_apis_add(api->api, afb_api) < 0)
                goto error3;
 
@@ -899,6 +907,7 @@ static void api_ws_server_client_unref(struct api_ws_client *client)
        if (!--client->refcount) {
                afb_evt_listener_unref(client->listener);
                afb_ws_destroy(client->ws);
+               afb_cred_unref(client->cred);
                free(client);
        }
 }
@@ -990,6 +999,7 @@ static void api_ws_server_accept(struct api_ws *api)
                        lenaddr = (socklen_t)sizeof addr;
                        client->fd = accept(api->fd, &addr, &lenaddr);
                        if (client->fd >= 0) {
+                               client->cred = afb_cred_create_for_socket(client->fd);
                                fcntl(client->fd, F_SETFD, FD_CLOEXEC);
                                fcntl(client->fd, F_SETFL, O_NONBLOCK);
                                client->ws = afb_ws_create(afb_common_get_event_loop(), client->fd, &api_ws_server_ws_itf, client);
@@ -998,6 +1008,7 @@ static void api_ws_server_accept(struct api_ws *api)
                                        client->refcount = 1;
                                        return;
                                }
+                               afb_cred_unref(client->cred);
                                close(client->fd);
                        }
                        afb_evt_listener_unref(client->listener);