2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Fulup Ar Foll"
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "local-def.h"
22 // Dummy sample of Client Application Context
25 void *whateveryouwant;
26 } MyClientApplicationHandle;
29 // Request Creation of new context if it does not exist
30 STATIC json_object* clientContextCreate (AFB_request *request) {
34 AFB_clientCtx *client=request->client; // get client context from request
36 // check we do not already have a session
37 if ((client != NULL) && (client->ctx != NULL)) {
38 request->errcode=MHD_HTTP_FORBIDDEN;
39 return (jsonNewMessage(AFB_FAIL, "Token exist use refresh"));
42 // request a new client context token and check result
43 if (AFB_UNAUTH == ctxTokenCreate (request)) {
44 request->errcode=MHD_HTTP_UNAUTHORIZED;
45 jresp= jsonNewMessage(AFB_FAIL, "No/Invalid initial token provided [should match --token=xxxx]");
49 // request a new client context token and check result
50 if (AFB_SUCCESS != ctxTokenCreate (request)) {
51 request->errcode=MHD_HTTP_UNAUTHORIZED;
52 jresp= jsonNewMessage(AFB_FAIL, "Token Session Not Activated [restart with --token=xxxx]");
56 // add a client context to session
57 client->ctx = malloc (sizeof (MyClientApplicationHandle));
59 // Send response to UI
60 jresp = json_object_new_object();
61 json_object_object_add(jresp, "token", json_object_new_string (client->token));
66 // Renew an existing context
67 STATIC json_object* clientContextRefresh (AFB_request *request) {
70 // note: we do not need to parse the old token as clientContextRefresh doit for us
71 if (AFB_SUCCESS != ctxTokenRefresh (request)) {
72 request->errcode=MHD_HTTP_UNAUTHORIZED;
73 jresp= jsonNewMessage(AFB_FAIL, "Token Exchange Broken Refresh Refused");
75 jresp = json_object_new_object();
76 json_object_object_add(jresp, "token", json_object_new_string (request->client->token));
83 // Verify a context is still valid
84 STATIC json_object* clientContextCheck (AFB_request *request) {
86 json_object *jresp = json_object_new_object();
88 // add an error code to respond
89 if (AFB_SUCCESS != ctxTokenCheck (request)) {
90 request->errcode=MHD_HTTP_UNAUTHORIZED;
91 json_object_object_add(jresp, "isvalid", json_object_new_boolean (FALSE));
93 json_object_object_add(jresp, "isvalid", json_object_new_boolean (TRUE));
101 // Close and Free context
102 STATIC json_object* clientContextReset (AFB_request *request) {
105 // note: we do not need to parse the old token as clientContextRefresh doit for us
106 if (AFB_SUCCESS != ctxTokenReset (request)) {
107 request->errcode=MHD_HTTP_UNAUTHORIZED;
108 jresp= jsonNewMessage(AFB_FAIL, "No Token Client Context [use --token=xxx]");
110 jresp = json_object_new_object();
111 json_object_object_add(jresp, "uuid", json_object_new_string (request->client->uuid));
117 // Some file upload sample
118 STATIC json_object* clientFileUpload (AFB_request *request) {
124 getQueryValue(request, "filename");
125 if (filename == NULL) return (jsonNewMessage(AFB_FAIL, "No Filename provided"));
127 // add an error code to respond
128 if (request->post == NULL) {
129 request->errcode=MHD_HTTP_UNAUTHORIZED;
130 return (jsonNewMessage(AFB_FAIL, "Post No Data"));
133 // This is simple test let's write file in config->session->filename
134 strncpy (filepath, request->config->configfile, sizeof(filepath));
135 strncat (filepath, "/", sizeof(filepath));
136 strncat (filepath, "/", sizeof(filepath));
139 if((fd = open(request->config->configfile, O_RDONLY)) < 0) {
140 return (jsonNewMessage(AFB_FAIL,"Fail to Upload file [%s] at [%s] error=\n", filename, filepath, strerror(errno)));
143 // write file on disk and free fd
144 write (fd, request->post, request->len);
150 // This function is call when Client Session Context is removed
151 // Note: when freeCtxCB==NULL standard free/malloc is called
152 STATIC void clientContextFree(AFB_clientCtx *client) {
153 fprintf (stderr,"Plugin[%s] Closing Session uuid=[%s]\n", client->plugin->prefix, client->uuid);
157 STATIC AFB_restapi pluginApis[]= {
158 {"ping" , (AFB_apiCB)apiPingTest ,"Ping Rest Test Service"},
159 {"token-create" , (AFB_apiCB)clientContextCreate ,"Request Client Context Creation"},
160 {"token-refresh" , (AFB_apiCB)clientContextRefresh,"Refresh Client Context Token"},
161 {"token-check" , (AFB_apiCB)clientContextCheck ,"Check Client Context Token"},
162 {"token-reset" , (AFB_apiCB)clientContextReset ,"Close Client Context and Free resources"},
163 {"file-upload" , (AFB_apiCB)clientFileUpload ,"Demo for file upload"},
167 PUBLIC AFB_plugin *afsvRegister () {
168 AFB_plugin *plugin = malloc (sizeof (AFB_plugin));
169 plugin->type = AFB_PLUGIN_JSON;
170 plugin->info = "Application Framework Binder Service";
171 plugin->prefix= "afbs"; // url base
172 plugin->apis = pluginApis;
173 plugin->handle= (void*) "What ever you want";
174 plugin->freeCtxCB= (void*) clientContextFree;