42ea75979b653533d535b4415c474a8eac7ac5dd
[src/app-framework-binder.git] / src / afbs-api.c
1 /*
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19
20 #include "local-def.h"
21
22 // Dummy sample of Client Application Context
23 typedef struct {
24   int  something;       
25   void *whateveryouwant;
26 } MyClientApplicationHandle;
27
28
29 // Request Creation of new context if it does not exist
30 STATIC json_object* clientContextCreate (AFB_request *request) {
31     json_object *jresp;
32     int   res;
33     char *token;
34     AFB_clientCtx *client=request->client; // get client context from request
35    
36     // check we do not already have a session
37     if ((client != NULL) && (client->handle != NULL)) {
38         request->errcode=MHD_HTTP_FORBIDDEN;
39         return (jsonNewMessage(AFB_FAIL, "Token exist use refresh"));
40     }
41         
42     // request a new client context token and check result 
43     if (AFB_SUCCESS != ctxTokenCreate (request)) {
44         request->errcode=MHD_HTTP_UNAUTHORIZED;
45         jresp= jsonNewMessage(AFB_FAIL, "Token Session Not Activated [restart with --token=xxxx]");
46         return (jresp);
47     }
48    
49     // add a client handle to session
50     client->handle = malloc (sizeof (MyClientApplicationHandle));
51     
52     // Send response to UI
53     jresp = json_object_new_object();               
54     json_object_object_add(jresp, "token", json_object_new_string (client->token));
55
56     return (jresp);
57 }
58
59 // Renew an existing context
60 STATIC json_object* clientContextRefresh (AFB_request *request) {
61     json_object *jresp;
62
63     // note: we do not need to parse the old token as clientContextRefresh doit for us
64     if (AFB_SUCCESS != ctxTokenRefresh (request)) {
65         request->errcode=MHD_HTTP_UNAUTHORIZED;
66         jresp= jsonNewMessage(AFB_FAIL, "Token Exchange Broken Refresh Refused");
67     } else {
68         jresp = json_object_new_object();
69         json_object_object_add(jresp, "token", json_object_new_string (request->client->token));              
70     }
71             
72     return (jresp);
73 }
74
75
76 // Verify a context is still valid 
77 STATIC json_object* clientContextCheck (AFB_request *request) {
78     
79     json_object *jresp = json_object_new_object();
80     
81     // add an error code to respond
82     if (AFB_SUCCESS != ctxTokenCheck (request)) {
83         request->errcode=MHD_HTTP_UNAUTHORIZED;
84         json_object_object_add(jresp, "isvalid", json_object_new_boolean (FALSE));
85     } else {
86         json_object_object_add(jresp, "isvalid", json_object_new_boolean (TRUE));       
87     }
88         
89     return (jresp); 
90 }
91
92 // Close and Free context
93 STATIC json_object* clientContextReset (AFB_request *request) {
94     json_object *jresp;
95    
96     // note: we do not need to parse the old token as clientContextRefresh doit for us
97     if (AFB_SUCCESS != ctxTokenReset (request)) {
98         request->errcode=MHD_HTTP_UNAUTHORIZED;
99         jresp= jsonNewMessage(AFB_FAIL, "No Token Client Context [use --token=xxx]");
100     } else {
101         jresp = json_object_new_object();
102         json_object_object_add(jresp, "uuid", json_object_new_string (request->client->uuid));              
103     }
104     
105     return (jresp); 
106 }
107
108
109 STATIC  AFB_restapi pluginApis[]= {
110   {"ping"          , (AFB_apiCB)apiPingTest         ,"Ping Rest Test Service"},
111   {"token-create"  , (AFB_apiCB)clientContextCreate ,"Request Client Context Creation"},
112   {"token-refresh" , (AFB_apiCB)clientContextRefresh,"Refresh Client Context Token"},
113   {"token-check"   , (AFB_apiCB)clientContextCheck  ,"Check Client Context Token"},
114   {"token-reset"   , (AFB_apiCB)clientContextReset  ,"Close Client Context and Free resources"},
115   {NULL}
116 };
117
118 PUBLIC AFB_plugin *afsvRegister () {
119     AFB_plugin *plugin = malloc (sizeof (AFB_plugin));
120     plugin->type  = AFB_PLUGIN_JSON; 
121     plugin->info  = "Application Framework Binder Service";
122     plugin->prefix= "afbs";  // url base
123     plugin->apis  = pluginApis;
124     plugin->handle= (void*) "What ever you want";
125     
126     return (plugin);
127 };