Add credential data to xreq
[src/app-framework-binder.git] / src / afb-api-ws.c
index 6995b2f..3e0b281 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 **********************************/
@@ -155,11 +159,11 @@ struct api_ws_server_req {
        uint32_t msgid;                 /* the incoming request msgid */
 };
 
-static void api_ws_server_req_success_cb(void *closure, struct json_object *obj, const char *info);
-static void api_ws_server_req_fail_cb(void *closure, const char *status, const char *info);
-static void api_ws_server_req_destroy_cb(void *closure);
-static int api_ws_server_req_subscribe_cb(void *closure, struct afb_event event);
-static int api_ws_server_req_unsubscribe_cb(void *closure, struct afb_event event);
+static void api_ws_server_req_success_cb(struct afb_xreq *xreq, struct json_object *obj, const char *info);
+static void api_ws_server_req_fail_cb(struct afb_xreq *xreq, const char *status, const char *info);
+static void api_ws_server_req_destroy_cb(struct afb_xreq *xreq);
+static int api_ws_server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event event);
+static int api_ws_server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event);
 
 const struct afb_xreq_query_itf afb_api_ws_xreq_itf = {
        .success = api_ws_server_req_success_cb,
@@ -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,9 +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.update_hooks = NULL;
+       afb_api.itf = &ws_api_itf;
        if (afb_apis_add(api->api, afb_api) < 0)
                goto error3;
 
@@ -900,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);
        }
 }
@@ -929,6 +937,7 @@ static void api_ws_server_called(struct api_ws_client *client, struct readbuf *r
         || !api_ws_read_string(rb, &wreq->request, &wreq->lenreq))
                goto overflow;
 
+       afb_xreq_init(&wreq->xreq, &afb_api_ws_xreq_itf);
        wreq->xreq.json = json_tokener_parse(wreq->request);
        if (wreq->xreq.json == NULL && strcmp(wreq->request, "null")) {
                wreq->xreq.json = json_object_new_string(wreq->request);
@@ -940,11 +949,9 @@ static void api_ws_server_called(struct api_ws_client *client, struct readbuf *r
        wreq->xreq.context.flags = flags;
 
        /* makes the call */
-       wreq->xreq.refcount = 1;
+       wreq->xreq.cred = afb_cred_addref(client->cred);
        wreq->xreq.api = client->api;
        wreq->xreq.verb = verb;
-       wreq->xreq.query = wreq;
-       wreq->xreq.queryitf = &afb_api_ws_xreq_itf;
        afb_apis_call(&wreq->xreq);
        afb_xreq_unref(&wreq->xreq);
        return;
@@ -991,6 +998,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);
@@ -999,6 +1007,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);
@@ -1061,22 +1070,23 @@ static void api_ws_server_event_broadcast(void *closure, const char *event, int
 /******************* ws request part for server *****************/
 
 /* decrement the reference count of the request and free/release it on falling to null */
-static void api_ws_server_req_destroy_cb(void *closure)
+static void api_ws_server_req_destroy_cb(struct afb_xreq *xreq)
 {
-       struct api_ws_server_req *wreq = closure;
+       struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
 
        afb_context_disconnect(&wreq->xreq.context);
+       afb_cred_unref(wreq->xreq.cred);
        json_object_put(wreq->xreq.json);
        free(wreq->rcvdata);
        api_ws_server_client_unref(wreq->client);
        free(wreq);
 }
 
-static void api_ws_server_req_success_cb(void *closure, struct json_object *obj, const char *info)
+static void api_ws_server_req_success_cb(struct afb_xreq *xreq, struct json_object *obj, const char *info)
 {
        int rc;
        struct writebuf wb = { .count = 0 };
-       struct api_ws_server_req *wreq = closure;
+       struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
 
        if (api_ws_write_char(&wb, 'T')
         && api_ws_write_uint32(&wb, wreq->msgid)
@@ -1092,11 +1102,11 @@ success:
        json_object_put(obj);
 }
 
-static void api_ws_server_req_fail_cb(void *closure, const char *status, const char *info)
+static void api_ws_server_req_fail_cb(struct afb_xreq *xreq, const char *status, const char *info)
 {
        int rc;
        struct writebuf wb = { .count = 0 };
-       struct api_ws_server_req *wreq = closure;
+       struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
 
        if (api_ws_write_char(&wb, 'F')
         && api_ws_write_uint32(&wb, wreq->msgid)
@@ -1110,11 +1120,11 @@ static void api_ws_server_req_fail_cb(void *closure, const char *status, const c
        ERROR("error while sending fail");
 }
 
-static int api_ws_server_req_subscribe_cb(void *closure, struct afb_event event)
+static int api_ws_server_req_subscribe_cb(struct afb_xreq *xreq, struct afb_event event)
 {
        int rc, rc2;
        struct writebuf wb = { .count = 0 };
-       struct api_ws_server_req *wreq = closure;
+       struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
 
        rc = afb_evt_add_watch(wreq->client->listener, event);
        if (rc < 0)
@@ -1133,11 +1143,11 @@ success:
        return rc;
 }
 
-static int api_ws_server_req_unsubscribe_cb(void *closure, struct afb_event event)
+static int api_ws_server_req_unsubscribe_cb(struct afb_xreq *xreq, struct afb_event event)
 {
        int rc, rc2;
        struct writebuf wb = { .count = 0 };
-       struct api_ws_server_req *wreq = closure;
+       struct api_ws_server_req *wreq = CONTAINER_OF_XREQ(struct api_ws_server_req, xreq);
 
        if (api_ws_write_char(&wb, 'U')
         && api_ws_write_uint32(&wb, wreq->msgid)