Add computation of credentials
[src/app-framework-binder.git] / src / afb-ws-json1.c
1 /*
2  * Copyright (C) 2016, 2017 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <string.h>
25
26 #include <json-c/json.h>
27
28 #include <afb/afb-req-itf.h>
29
30 #include "afb-wsj1.h"
31 #include "afb-ws-json1.h"
32 #include "afb-common.h"
33 #include "afb-msg-json.h"
34 #include "afb-session.h"
35 #include "afb-cred.h"
36 #include "afb-apis.h"
37 #include "afb-xreq.h"
38 #include "afb-context.h"
39 #include "afb-evt.h"
40 #include "afb-subcall.h"
41 #include "verbose.h"
42
43 /* predeclaration of structures */
44 struct afb_ws_json1;
45 struct afb_wsreq;
46
47 /* predeclaration of websocket callbacks */
48 static void aws_on_hangup(struct afb_ws_json1 *ws, struct afb_wsj1 *wsj1);
49 static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *verb, struct afb_wsj1_msg *msg);
50 static void aws_on_event(struct afb_ws_json1 *ws, const char *event, int eventid, struct json_object *object);
51
52 /* predeclaration of wsreq callbacks */
53 static void wsreq_destroy(struct afb_wsreq *wsreq);
54 static struct json_object *wsreq_json(struct afb_wsreq *wsreq);
55 static void wsreq_reply(struct afb_wsreq *wsreq, int iserror, json_object *obj);
56
57 /* declaration of websocket structure */
58 struct afb_ws_json1
59 {
60         int refcount;
61         void (*cleanup)(void*);
62         void *cleanup_closure;
63         struct afb_session *session;
64         struct afb_evt_listener *listener;
65         struct afb_wsj1 *wsj1;
66         struct afb_cred *cred;
67         int new_session;
68 };
69
70 /* declaration of wsreq structure */
71 struct afb_wsreq
72 {
73         struct afb_xreq xreq;
74         struct afb_ws_json1 *aws;
75         struct afb_wsreq *next;
76         struct afb_wsj1_msg *msgj1;
77 };
78
79 /* interface for afb_ws_json1 / afb_wsj1 */
80 static struct afb_wsj1_itf wsj1_itf = {
81         .on_hangup = (void*)aws_on_hangup,
82         .on_call = (void*)aws_on_call
83 };
84
85 /* interface for xreq */
86 const struct afb_xreq_query_itf afb_ws_json1_xreq_itf = {
87         .json = (void*)wsreq_json,
88         .reply = (void*)wsreq_reply,
89         .unref = (void*)wsreq_destroy
90 };
91
92 /* the interface for events */
93 static const struct afb_evt_itf evt_itf = {
94         .broadcast = (void*)aws_on_event,
95         .push = (void*)aws_on_event
96 };
97
98 /***************************************************************
99 ****************************************************************
100 **
101 **  functions of afb_ws_json1 / afb_wsj1
102 **
103 ****************************************************************
104 ***************************************************************/
105
106 struct afb_ws_json1 *afb_ws_json1_create(int fd, struct afb_context *context, void (*cleanup)(void*), void *cleanup_closure)
107 {
108         struct afb_ws_json1 *result;
109
110         assert(fd >= 0);
111         assert(context != NULL);
112
113         result = malloc(sizeof * result);
114         if (result == NULL)
115                 goto error;
116
117         result->refcount = 1;
118         result->cleanup = cleanup;
119         result->cleanup_closure = cleanup_closure;
120         result->session = afb_session_addref(context->session);
121         result->new_session = context->created != 0;
122         if (result->session == NULL)
123                 goto error2;
124
125         result->wsj1 = afb_wsj1_create(afb_common_get_event_loop(), fd, &wsj1_itf, result);
126         if (result->wsj1 == NULL)
127                 goto error3;
128
129         result->listener = afb_evt_listener_create(&evt_itf, result);
130         if (result->listener == NULL)
131                 goto error4;
132
133         result->cred = afb_cred_create_for_socket(fd);
134         return result;
135
136 error4:
137         afb_wsj1_unref(result->wsj1);
138 error3:
139         afb_session_unref(result->session);
140 error2:
141         free(result);
142 error:
143         close(fd);
144         return NULL;
145 }
146
147 static struct afb_ws_json1 *aws_addref(struct afb_ws_json1 *ws)
148 {
149         ws->refcount++;
150         return ws;
151 }
152
153 static void aws_unref(struct afb_ws_json1 *ws)
154 {
155         if (--ws->refcount == 0) {
156                 afb_evt_listener_unref(ws->listener);
157                 afb_wsj1_unref(ws->wsj1);
158                 if (ws->cleanup != NULL)
159                         ws->cleanup(ws->cleanup_closure);
160                 afb_session_unref(ws->session);
161                 afb_cred_unref(ws->cred);
162                 free(ws);
163         }
164 }
165
166 static void aws_on_hangup(struct afb_ws_json1 *ws, struct afb_wsj1 *wsj1)
167 {
168         aws_unref(ws);
169 }
170
171 static void aws_on_call(struct afb_ws_json1 *ws, const char *api, const char *verb, struct afb_wsj1_msg *msg)
172 {
173         struct afb_wsreq *wsreq;
174
175         DEBUG("received websocket request for %s/%s: %s", api, verb, afb_wsj1_msg_object_s(msg));
176
177         /* allocate */
178         wsreq = calloc(1, sizeof *wsreq);
179         if (wsreq == NULL) {
180                 afb_wsj1_close(ws->wsj1, 1008, NULL);
181                 return;
182         }
183
184         /* init the context */
185         afb_context_init(&wsreq->xreq.context, ws->session, afb_wsj1_msg_token(msg));
186         if (!wsreq->xreq.context.invalidated)
187                 wsreq->xreq.context.validated = 1;
188         if (ws->new_session != 0) {
189                 wsreq->xreq.context.created = 1;
190                 ws->new_session = 0;
191         }
192
193         /* fill and record the request */
194         afb_wsj1_msg_addref(msg);
195         wsreq->msgj1 = msg;
196         wsreq->xreq.refcount = 1;
197         wsreq->xreq.query = wsreq;
198         wsreq->xreq.queryitf = &afb_ws_json1_xreq_itf;
199         wsreq->xreq.api = api;
200         wsreq->xreq.verb = verb;
201         wsreq->aws = aws_addref(ws);
202         wsreq->xreq.listener = wsreq->aws->listener;
203
204         /* emits the call */
205         afb_apis_call(&wsreq->xreq);
206         afb_xreq_unref(&wsreq->xreq);
207 }
208
209 static void aws_on_event(struct afb_ws_json1 *aws, const char *event, int eventid, struct json_object *object)
210 {
211         afb_wsj1_send_event_j(aws->wsj1, event, afb_msg_json_event(event, object));
212 }
213
214 /***************************************************************
215 ****************************************************************
216 **
217 **  functions of wsreq / afb_req
218 **
219 ****************************************************************
220 ***************************************************************/
221
222 static void wsreq_destroy(struct afb_wsreq *wsreq)
223 {
224         afb_context_disconnect(&wsreq->xreq.context);
225         afb_wsj1_msg_unref(wsreq->msgj1);
226         aws_unref(wsreq->aws);
227         free(wsreq);
228 }
229
230 static struct json_object *wsreq_json(struct afb_wsreq *wsreq)
231 {
232         return afb_wsj1_msg_object_j(wsreq->msgj1);
233 }
234
235 static void wsreq_reply(struct afb_wsreq *wsreq, int iserror, json_object *obj)
236 {
237         int rc;
238
239         rc = (iserror ? afb_wsj1_reply_error_j : afb_wsj1_reply_ok_j)(
240                         wsreq->msgj1, obj, afb_context_sent_token(&wsreq->xreq.context));
241         if (rc)
242                 ERROR("Can't send reply: %m");
243 }
244